Example #1
0
/*
 * cursor_insert --
 *	Insert some data into a table.
 */
static void
cursor_insert(const char *uri, uint64_t i)
{
	WT_CURSOR *cursor;
	WT_ITEM vu;
	char keybuf[100], valuebuf[100];
	bool recno;

	memset(&vu, 0, sizeof(vu));

	/* Open a cursor. */
	testutil_check(wt_session->open_cursor(
	    wt_session, uri, NULL, NULL, &cursor));
	/* Operations change based on the key/value formats. */
	recno = strcmp(cursor->key_format, "r") == 0;
	if (recno)
		cursor->set_key(cursor, i);
	else {
		testutil_check(__wt_snprintf(keybuf, sizeof(keybuf),
		    "%s-%" PRIu64, KEY, i));
		cursor->set_key(cursor, keybuf);
	}
	strcpy(valuebuf, VALUE);
	cursor->set_value(cursor, valuebuf);
	testutil_check(cursor->insert(cursor));
	testutil_check(cursor->close(cursor));
}
StatusWith<RecordId> WiredTigerRecordStore::updateRecord(OperationContext* txn,
                                                         const RecordId& loc,
                                                         const char* data,
                                                         int len,
                                                         bool enforceQuota,
                                                         UpdateNotifier* notifier) {
    WiredTigerCursor curwrap(_uri, _tableId, true, txn);
    curwrap.assertInActiveTxn();
    WT_CURSOR* c = curwrap.get();
    invariant(c);
    c->set_key(c, _makeKey(loc));
    int ret = WT_OP_CHECK(c->search(c));
    invariantWTOK(ret);

    WT_ITEM old_value;
    ret = c->get_value(c, &old_value);
    invariantWTOK(ret);

    int old_length = old_value.size;

    c->set_key(c, _makeKey(loc));
    WiredTigerItem value(data, len);
    c->set_value(c, value.Get());
    ret = WT_OP_CHECK(c->insert(c));
    invariantWTOK(ret);

    _increaseDataSize(txn, len - old_length);

    cappedDeleteAsNeeded(txn, loc);

    return StatusWith<RecordId>(loc);
}
Example #3
0
File: main.c Project: jbreams/mongo
/*
 * Append to a table in a "racy" fashion - that is attempt to insert the
 * same record another thread is likely to also be inserting.
 */
void *
thread_insert_race(void *arg)
{
	TEST_OPTS *opts;
	WT_CONNECTION *conn;
	WT_CURSOR *cursor;
	WT_SESSION *session;
	uint64_t i, value;
	int ret;

	opts = (TEST_OPTS *)arg;
	conn = opts->conn;

	testutil_check(conn->open_session(conn, NULL, NULL, &session));
	testutil_check(session->open_cursor(
	    session, opts->uri, NULL, NULL, &cursor));

	printf("Running insert thread\n");
	for (i = 0; i < opts->nrecords; ++i) {
		testutil_check(
		    session->begin_transaction(session, "isolation=snapshot"));
		cursor->set_key(cursor, 1);
		testutil_check(cursor->search(cursor));
		testutil_check(cursor->get_value(cursor, &value));
		cursor->set_key(cursor, 1);
		cursor->set_value(cursor, value + 1);
		if ((ret = cursor->update(cursor)) != 0) {
			if (ret == WT_ROLLBACK) {
				testutil_check(session->rollback_transaction(
				    session, NULL));
				i--;
				continue;
			}
			printf("Error in update: %d\n", ret);
		}
		testutil_check(session->commit_transaction(session, NULL));
		if (i % 10000 == 0) {
			printf("insert: %" PRIu64 "\r", i);
			fflush(stdout);
		}
	}
	if (i > 10000)
		printf("\n");

	opts->running = false;

	return (NULL);
}
Example #4
0
void
load(SHARED_CONFIG *cfg, const char *name)
{
	WT_CONNECTION *conn;
	WT_CURSOR *cursor;
	WT_ITEM *value, _value;
	WT_SESSION *session;
	size_t len;
	uint64_t keyno;
	char keybuf[64], valuebuf[64];

	conn = cfg->conn;

	file_create(cfg, name);

	testutil_check(conn->open_session(conn, NULL, NULL, &session));

	testutil_check(
	    session->open_cursor(session, name, NULL, "bulk", &cursor));

	value = &_value;
	for (keyno = 1; keyno <= cfg->nkeys; ++keyno) {
		if (cfg->ftype == ROW) {
			testutil_check(__wt_snprintf(
			    keybuf, sizeof(keybuf), "%016" PRIu64, keyno));
			cursor->set_key(cursor, keybuf);
		} else
			cursor->set_key(cursor, (uint32_t)keyno);
		value->data = valuebuf;
		if (cfg->ftype == FIX)
			cursor->set_value(cursor, 0x01);
		else {
			testutil_check(__wt_snprintf_len_set(
			    valuebuf, sizeof(valuebuf),
			    &len, "%37" PRIu64, keyno));
			value->size = (uint32_t)len;
			cursor->set_value(cursor, value);
		}
		testutil_check(cursor->insert(cursor));
	}

	/* Setup the starting key range for the workload phase. */
	cfg->key_range = cfg->nkeys;
	testutil_check(cursor->close(cursor));
	testutil_check(session->checkpoint(session, NULL));

	testutil_check(session->close(session, NULL));
}
Example #5
0
/*! [thread main] */
int
main(void)
{
	WT_SESSION *session;
	WT_CURSOR *cursor;
	pthread_t threads[NUM_THREADS];
	int i, ret;

	if ((ret = wiredtiger_open(home, NULL,
	    "create", &conn)) != 0)
		fprintf(stderr, "Error connecting to %s: %s\n",
		    home, wiredtiger_strerror(ret));
	/* Note: further error checking omitted for clarity. */

	ret = conn->open_session(conn, NULL, NULL, &session);
	ret = session->create(session, "table:access",
	    "key_format=S,value_format=S");
	ret = session->open_cursor(session, "table:access", NULL,
	    "overwrite", &cursor);
	cursor->set_key(cursor, "key1");
	cursor->set_value(cursor, "value1");
	ret = cursor->insert(cursor);
	ret = session->close(session, NULL);

	for (i = 0; i < NUM_THREADS; i++)
		ret = pthread_create(&threads[i], NULL, scan_thread, NULL);

	for (i = 0; i < NUM_THREADS; i++)
		ret = pthread_join(threads[i], NULL);

	ret = conn->close(conn, NULL);

	return (ret);
}
Example #6
0
    // static
    StatusWith<uint64_t> WiredTigerUtil::getStatisticsValue(WT_SESSION* session,
                                                            const std::string& uri,
                                                            const std::string& config,
                                                            int statisticsKey) {
        invariant(session);
        WT_CURSOR* cursor = NULL;
        const char* cursorConfig = config.empty() ? NULL : config.c_str();
        int ret = session->open_cursor(session, uri.c_str(), NULL, cursorConfig, &cursor);
        if (ret != 0) {
            return StatusWith<uint64_t>(ErrorCodes::CursorNotFound, str::stream()
                << "unable to open cursor at URI " << uri
                << ". reason: " << wiredtiger_strerror(ret));
        }
        invariant(cursor);
        ON_BLOCK_EXIT(cursor->close, cursor);

        cursor->set_key(cursor, statisticsKey);
        ret = cursor->search(cursor);
        if (ret != 0) {
            return StatusWith<uint64_t>(ErrorCodes::NoSuchKey, str::stream()
                << "unable to find key " << statisticsKey << " at URI " << uri
                << ". reason: " << wiredtiger_strerror(ret));
        }

        uint64_t value;
        ret = cursor->get_value(cursor, NULL, NULL, &value);
        if (ret != 0) {
            return StatusWith<uint64_t>(ErrorCodes::BadValue, str::stream()
                << "unable to get value for key " << statisticsKey << " at URI " << uri
                << ". reason: " << wiredtiger_strerror(ret));
        }

        return StatusWith<uint64_t>(value);
    }
Example #7
0
File: main.c Project: ajdavis/mongo
static void
sweep_stats(void)
{
	static const int list[] = {
		WT_STAT_CONN_CURSOR_SWEEP_BUCKETS,
		WT_STAT_CONN_CURSOR_SWEEP_CLOSED,
		WT_STAT_CONN_CURSOR_SWEEP_EXAMINED,
		WT_STAT_CONN_CURSOR_SWEEP,
		WT_STAT_CONN_DH_SWEEP_REF,
		WT_STAT_CONN_DH_SWEEP_CLOSE,
		WT_STAT_CONN_DH_SWEEP_REMOVE,
		WT_STAT_CONN_DH_SWEEP_TOD,
		WT_STAT_CONN_DH_SWEEPS,
		WT_STAT_CONN_DH_SESSION_SWEEPS,
		-1
	};
	WT_SESSION *session;
	WT_CURSOR *cursor;
	uint64_t value;
	int i;
	const char *desc, *pvalue;

	testutil_check(conn->open_session(conn, NULL, NULL, &session));
	testutil_check(session->open_cursor(
	    session, "statistics:", NULL, NULL, &cursor));
	for (i = 0;; ++i) {
		if (list[i] == -1)
			break;
		cursor->set_key(cursor, list[i]);
		testutil_check(cursor->search(cursor));
		testutil_check(
		    cursor->get_value(cursor, &desc, &pvalue, &value));
		printf("\t" "%s=%s\n", desc, pvalue);
	}
}
Example #8
0
StatusWith<std::string> WiredTigerUtil::getMetadata(OperationContext* opCtx, StringData uri) {
    invariant(opCtx);

    auto session = WiredTigerRecoveryUnit::get(opCtx)->getSessionNoTxn();
    WT_CURSOR* cursor =
        session->getCursor("metadata:create", WiredTigerSession::kMetadataTableId, false);
    invariant(cursor);
    auto releaser =
        makeGuard([&] { session->releaseCursor(WiredTigerSession::kMetadataTableId, cursor); });

    std::string strUri = uri.toString();
    cursor->set_key(cursor, strUri.c_str());
    int ret = cursor->search(cursor);
    if (ret == WT_NOTFOUND) {
        return StatusWith<std::string>(ErrorCodes::NoSuchKey,
                                       str::stream() << "Unable to find metadata for " << uri);
    } else if (ret != 0) {
        return StatusWith<std::string>(wtRCToStatus(ret));
    }
    const char* metadata = NULL;
    ret = cursor->get_value(cursor, &metadata);
    if (ret != 0) {
        return StatusWith<std::string>(wtRCToStatus(ret));
    }
    invariant(metadata);
    return StatusWith<std::string>(metadata);
}
Example #9
0
/*
 * Set up the table and index of the data.
 */
static void
setup_table(WT_SESSION *session)
{
	WT_CURSOR *cursor;
	struct president_data p;
	int i;

	/* Create the primary table. It has a key of the unique ID. */
	error_check(session->create(session, "table:presidents",
	    "key_format=I,value_format=SSHH,"
	    "columns=(ID,last_name,first_name,term_begin,term_end)"));

	/*
	 * Create the index that is generated with an extractor. The index
	 * will generate an entry in the index for each year a president
	 * was in office.
	 */
	error_check(session->create(session, "index:presidents:term",
	    "key_format=H,columns=(term),extractor=my_extractor"));

	error_check(session->open_cursor(
	    session, "table:presidents", NULL, NULL, &cursor));
	for (i = 0; example_data[i].last_name != NULL; i++) {
		p = example_data[i];
		cursor->set_key(cursor, p.id);
		cursor->set_value(cursor,
		    p.last_name, p.first_name, p.term_start, p.term_end);
		fprintf(stderr,
		    "SETUP: table insert %" PRIu16 "-%" PRIu16 ": %s %s\n",
		    p.term_start, p.term_end,
		    p.first_name, p.last_name);
		error_check(cursor->insert(cursor));
	}
}
Example #10
0
/*
 * __wt_metadata_search --
 *	Return a copied row from the metadata.
 *	The caller is responsible for freeing the allocated memory.
 */
int
__wt_metadata_search(
    WT_SESSION_IMPL *session, const char *key, char **valuep)
{
	WT_CURSOR *cursor;
	WT_DECL_RET;
	const char *value;

	*valuep = NULL;

	WT_RET(__wt_verbose(session, WT_VERB_METADATA,
	    "Search: key: %s, tracking: %s, %s" "turtle",
	    key, WT_META_TRACKING(session) ? "true" : "false",
	    __metadata_turtle(key) ? "" : "not "));

	if (__metadata_turtle(key))
		return (__wt_turtle_read(session, key, valuep));

	WT_RET(__wt_metadata_cursor(session, NULL, &cursor));
	cursor->set_key(cursor, key);
	WT_ERR(cursor->search(cursor));
	WT_ERR(cursor->get_value(cursor, &value));
	WT_ERR(__wt_strdup(session, value, valuep));

err:	WT_TRET(cursor->close(cursor));
	return (ret);
}
Example #11
0
int
main(void)
{
	WT_CONNECTION *conn;
	WT_CURSOR *cursor;
	WT_SESSION *session;
	int ret;

	ret = wiredtiger_open(home, NULL, "create,statistics=(all)", &conn);
	ret = conn->open_session(conn, NULL, NULL, &session);
	ret = session->create(
	    session, "table:access", "key_format=S,value_format=S");

	ret = session->open_cursor(
	    session, "table:access", NULL, NULL, &cursor);
	cursor->set_key(cursor, "key");
	cursor->set_value(cursor, "value");
	ret = cursor->insert(cursor);
	cursor->close(cursor);

	ret = session->checkpoint(session, NULL);

	ret = print_database_stats(session);

	ret = print_file_stats(session);

	ret = print_overflow_pages(session);

	ret = print_derived_stats(session);

	return (conn->close(conn, NULL) == 0 ? ret : EXIT_FAILURE);
}
boost::optional<RecordId> WiredTigerRecordStore::oplogStartHack(
    OperationContext* txn, const RecordId& startingPosition) const {
    if (!_useOplogHack)
        return boost::none;

    {
        WiredTigerRecoveryUnit* wru = WiredTigerRecoveryUnit::get(txn);
        _oplogSetStartHack(wru);
    }

    WiredTigerCursor cursor(_uri, _tableId, true, txn);
    WT_CURSOR* c = cursor.get();

    int cmp;
    c->set_key(c, _makeKey(startingPosition));
    int ret = WT_OP_CHECK(c->search_near(c, &cmp));
    if (ret == 0 && cmp > 0)
        ret = c->prev(c);  // landed one higher than startingPosition
    if (ret == WT_NOTFOUND)
        return RecordId();  // nothing <= startingPosition
    invariantWTOK(ret);

    int64_t key;
    ret = c->get_key(c, &key);
    invariantWTOK(ret);
    return _fromKey(key);
}
Example #13
0
/*
 * __wt_metadata_update --
 *	Update a row in the metadata.
 */
int
__wt_metadata_update(
    WT_SESSION_IMPL *session, const char *key, const char *value)
{
	WT_CURSOR *cursor;
	WT_DECL_RET;

	WT_RET(__wt_verbose(session, WT_VERB_METADATA,
	    "Update: key: %s, value: %s, tracking: %s, %s" "turtle",
	    key, value, WT_META_TRACKING(session) ? "true" : "false",
	    __metadata_turtle(key) ? "" : "not "));

	if (__metadata_turtle(key)) {
		WT_WITH_TURTLE_LOCK(session, ret,
		    ret = __wt_turtle_update(session, key, value));
		return (ret);
	}

	if (WT_META_TRACKING(session))
		WT_RET(__wt_meta_track_update(session, key));

	WT_RET(__wt_metadata_cursor(session, &cursor));
	/* This cursor needs to have overwrite semantics. */
	WT_ASSERT(session, F_ISSET(cursor, WT_CURSTD_OVERWRITE));

	cursor->set_key(cursor, key);
	cursor->set_value(cursor, value);
	WT_ERR(cursor->insert(cursor));
err:	WT_TRET(__wt_metadata_cursor_release(session, &cursor));
	return (ret);
}
Example #14
0
 int WiredTigerEngine::Get(const Slice& key, std::string* value, const Options& options)
 {
     WT_SESSION* session = GetContextHolder().session;
     if (NULL == session)
     {
         return -1;
     }
     WT_CURSOR *cursor = create_wiredtiger_cursor(session);
     if (NULL == cursor)
     {
         return -1;
     }
     WT_ITEM key_item, value_item;
     key_item.data = key.data();
     key_item.size = key.size();
     cursor->set_key(cursor, &key_item);
     int ret = 0;
     if ((ret = cursor->search(cursor)) == 0)
     {
         ret = cursor->get_value(cursor, &value_item);
         if (0 == ret)
         {
             if (NULL != value)
             {
                 value->assign((const char*) value_item.data, value_item.size);
             }
         }
         else
         {
             CHECK_WT_RETURN(ret);
         }
     }
     cursor->close(cursor);
     return ret;
 }
Example #15
0
/*
 * __recovery_file_scan --
 *	Scan the files referenced from the metadata and gather information
 *	about them for recovery.
 */
static int
__recovery_file_scan(WT_RECOVERY *r)
{
	WT_CURSOR *c;
	WT_DECL_RET;
	int cmp;
	const char *uri, *config;

	/* Scan through all files in the metadata. */
	c = r->files[0].c;
	c->set_key(c, "file:");
	if ((ret = c->search_near(c, &cmp)) != 0) {
		/* Is the metadata empty? */
		WT_RET_NOTFOUND_OK(ret);
		return (0);
	}
	if (cmp < 0)
		WT_RET_NOTFOUND_OK(c->next(c));
	for (; ret == 0; ret = c->next(c)) {
		WT_RET(c->get_key(c, &uri));
		if (!WT_PREFIX_MATCH(uri, "file:"))
			break;
		WT_RET(c->get_value(c, &config));
		WT_RET(__recovery_setup_file(r, uri, config));
	}
	WT_RET_NOTFOUND_OK(ret);
	return (0);
}
Example #16
0
/*
 * Open and close cursor on a table.
 */
void
op_cursor(void *arg)
{
	TEST_OPTS *opts;
	TEST_PER_THREAD_OPTS *args;
	WT_CURSOR *cursor;
	WT_SESSION *session;
	int i, ret;

	args = (TEST_PER_THREAD_OPTS *)arg;
	opts = args->testopts;

	testutil_check(
	    opts->conn->open_session(opts->conn, NULL, NULL, &session));

	if ((ret = session->open_cursor(
	    session, opts->uri, NULL, NULL, &cursor)) != 0) {
		if (ret != ENOENT && ret != EBUSY)
			testutil_die(ret, "session.open_cursor");
	} else {
		/* Put some data in if asked to. */
		if (args->testopts->do_data_ops) {
			for (i = 0; i < 1000; i++) {
				cursor->set_key(cursor, i);
				cursor->set_value(cursor, "abcdef");
				cursor->insert(cursor);
			}
		}
		testutil_check(cursor->close(cursor));
	}

	testutil_check(session->close(session, NULL));
	args->thread_counter++;
}
Example #17
0
/*
 * __wt_metadata_insert --
 *	Insert a row into the metadata.
 */
int
__wt_metadata_insert(
    WT_SESSION_IMPL *session, const char *key, const char *value)
{
	WT_CURSOR *cursor;
	WT_DECL_RET;

	WT_RET(__wt_verbose(session, WT_VERB_METADATA,
	    "Insert: key: %s, value: %s, tracking: %s, %s" "turtle",
	    key, value, WT_META_TRACKING(session) ? "true" : "false",
	    __metadata_turtle(key) ? "" : "not "));

	if (__metadata_turtle(key))
		WT_RET_MSG(session, EINVAL,
		    "%s: insert not supported on the turtle file", key);

	WT_RET(__wt_metadata_cursor(session, NULL, &cursor));
	cursor->set_key(cursor, key);
	cursor->set_value(cursor, value);
	WT_ERR(cursor->insert(cursor));
	if (WT_META_TRACKING(session))
		WT_ERR(__wt_meta_track_insert(session, key));

err:	WT_TRET(cursor->close(cursor));
	return (ret);
}
Example #18
0
/*
 * __wt_metadata_update --
 *	Update a row in the metadata.
 */
int
__wt_metadata_update(
    WT_SESSION_IMPL *session, const char *key, const char *value)
{
	WT_CURSOR *cursor;
	WT_DECL_RET;

	WT_RET(__wt_verbose(session, WT_VERB_METADATA,
	    "Update: key: %s, value: %s, tracking: %s, %s" "turtle",
	    key, value, WT_META_TRACKING(session) ? "true" : "false",
	    __metadata_turtle(key) ? "" : "not "));

	if (__metadata_turtle(key))
		return (__wt_turtle_update(session, key, value));

	if (WT_META_TRACKING(session))
		WT_RET(__wt_meta_track_update(session, key));

	WT_RET(__wt_metadata_cursor(session, "overwrite", &cursor));
	cursor->set_key(cursor, key);
	cursor->set_value(cursor, value);
	WT_ERR(cursor->insert(cursor));

err:	WT_TRET(cursor->close(cursor));
	return (ret);
}
Example #19
0
/*
 * __wt_metadata_remove --
 *	Remove a row from the metadata.
 */
int
__wt_metadata_remove(WT_SESSION_IMPL *session, const char *key)
{
	WT_CURSOR *cursor;
	WT_DECL_RET;

	WT_RET(__wt_verbose(session, WT_VERB_METADATA,
	    "Remove: key: %s, tracking: %s, %s" "turtle",
	    key, WT_META_TRACKING(session) ? "true" : "false",
	    __metadata_turtle(key) ? "" : "not "));

	if (__metadata_turtle(key))
		WT_RET_MSG(session, EINVAL,
		    "%s: remove not supported on the turtle file", key);

	WT_RET(__wt_metadata_cursor(session, NULL, &cursor));
	cursor->set_key(cursor, key);
	WT_ERR(cursor->search(cursor));
	if (WT_META_TRACKING(session))
		WT_ERR(__wt_meta_track_update(session, key));
	WT_ERR(cursor->remove(cursor));

err:	WT_TRET(cursor->close(cursor));
	return (ret);
}
Example #20
0
int wt_id2entry_delete(
	Operation *op,
	WT_SESSION *session,
	Entry *e )
{
	int rc;
	WT_CURSOR *cursor = NULL;
	rc = session->open_cursor(session, WT_TABLE_ID2ENTRY, NULL,
							  NULL, &cursor);
	if ( rc ) {
		Debug( LDAP_DEBUG_ANY,
			   LDAP_XSTRING(wt_id2entry_delete)
			   ": open_cursor failed: %s (%d)\n",
			   wiredtiger_strerror(rc), rc, 0 );
		goto done;
	}
	cursor->set_key(cursor, e->e_id);
	rc = cursor->remove(cursor);
	if ( rc ) {
		Debug( LDAP_DEBUG_ANY,
			   LDAP_XSTRING(wt_id2entry_delete)
			   ": remove failed: %s (%d)\n",
			   wiredtiger_strerror(rc), rc, 0 );
		goto done;
	}

done:
	if(cursor){
		cursor->close(cursor);
	}
	return rc;
}
    boost::optional<Record> next() final {
        if (_eof)
            return {};

        WT_CURSOR* c = _cursor->get();

        bool mustAdvance = true;
        if (_lastReturnedId.isNull() && !_forward && _rs._isCapped) {
            // In this case we need to seek to the highest visible record.
            const RecordId reverseCappedInitialSeekPoint =
                _readUntilForOplog.isNull() ? _rs.lowestCappedHiddenRecord() : _readUntilForOplog;

            if (!reverseCappedInitialSeekPoint.isNull()) {
                c->set_key(c, _makeKey(reverseCappedInitialSeekPoint));
                int cmp;
                int seekRet = WT_OP_CHECK(c->search_near(c, &cmp));
                if (seekRet == WT_NOTFOUND) {
                    _eof = true;
                    return {};
                }
                invariantWTOK(seekRet);

                // If we landed at or past the lowest hidden record, we must advance to be in
                // the visible range.
                mustAdvance = _rs.isCappedHidden(reverseCappedInitialSeekPoint)
                    ? (cmp >= 0)
                    : (cmp > 0);  // No longer hidden.
            }
        }

        if (mustAdvance) {
            // Nothing after the next line can throw WCEs.
            // Note that an unpositioned (or eof) WT_CURSOR returns the first/last entry in the
            // table when you call next/prev.
            int advanceRet = WT_OP_CHECK(_forward ? c->next(c) : c->prev(c));
            if (advanceRet == WT_NOTFOUND) {
                _eof = true;
                return {};
            }
            invariantWTOK(advanceRet);
        }

        int64_t key;
        invariantWTOK(c->get_key(c, &key));
        const RecordId id = _fromKey(key);

        if (!isVisible(id)) {
            _eof = true;
            return {};
        }

        WT_ITEM value;
        invariantWTOK(c->get_value(c, &value));

        _lastReturnedId = id;
        return {{id, {static_cast<const char*>(value.data), static_cast<int>(value.size)}}};
    }
Example #22
0
void
load(const char *name)
{
	WT_CURSOR *cursor;
	WT_ITEM *key, _key, *value, _value;
	WT_SESSION *session;
	size_t len;
	uint64_t keyno;
	char keybuf[64], valuebuf[64];

	file_create(name);

	testutil_check(conn->open_session(conn, NULL, NULL, &session));

	testutil_check(
	    session->open_cursor(session, name, NULL, "bulk", &cursor));

	key = &_key;
	value = &_value;
	for (keyno = 1; keyno <= nkeys; ++keyno) {
		if (ftype == ROW) {
			testutil_check(__wt_snprintf_len_set(
			    keybuf, sizeof(keybuf),
			    &len, "%017" PRIu64, keyno));
			key->data = keybuf;
			key->size = (uint32_t)len;
			cursor->set_key(cursor, key);
		} else
			cursor->set_key(cursor, keyno);
		if (ftype == FIX)
			cursor->set_value(cursor, 0x01);
		else {
			testutil_check(__wt_snprintf_len_set(
			    valuebuf, sizeof(valuebuf),
			    &len, "%37" PRIu64, keyno));
			value->data = valuebuf;
			value->size = (uint32_t)len;
			cursor->set_value(cursor, value);
		}
		testutil_check(cursor->insert(cursor));
	}

	testutil_check(session->close(session, NULL));
}
Example #23
0
void
load(const char *name)
{
    WT_CURSOR *cursor;
    WT_ITEM *key, _key, *value, _value;
    WT_SESSION *session;
    char keybuf[64], valuebuf[64];
    u_int keyno;
    int ret;

    file_create(name);

    if ((ret = conn->open_session(conn, NULL, NULL, &session)) != 0)
        testutil_die(ret, "conn.session");

    if ((ret =
                session->open_cursor(session, name, NULL, "bulk", &cursor)) != 0)
        testutil_die(ret, "cursor.open");

    key = &_key;
    value = &_value;
    for (keyno = 1; keyno <= nkeys; ++keyno) {
        if (ftype == ROW) {
            key->data = keybuf;
            key->size = (uint32_t)
                        snprintf(keybuf, sizeof(keybuf), "%017u", keyno);
            cursor->set_key(cursor, key);
        } else
            cursor->set_key(cursor, (uint32_t)keyno);
        value->data = valuebuf;
        if (ftype == FIX)
            cursor->set_value(cursor, 0x01);
        else {
            value->size = (uint32_t)
                          snprintf(valuebuf, sizeof(valuebuf), "%37u", keyno);
            cursor->set_value(cursor, value);
        }
        if ((ret = cursor->insert(cursor)) != 0)
            testutil_die(ret, "cursor.insert");
    }

    if ((ret = session->close(session, NULL)) != 0)
        testutil_die(ret, "session.close");
}
Example #24
0
File: main.c Project: ajdavis/mongo
static void
op(WT_SESSION *session, WT_RAND_STATE *rnd, WT_CURSOR **cpp)
{
	WT_CURSOR *cursor;
	WT_DECL_RET;
	u_int i, key;
	char buf[128];
	bool readonly;

	/* Close any open cursor in the slot we're about to reuse. */
	if (*cpp != NULL) {
		testutil_check((*cpp)->close(*cpp));
		*cpp = NULL;
	}

	cursor = NULL;
	readonly = __wt_random(rnd) % 2 == 0;

	/* Loop to open an object handle. */
	for (i = __wt_random(rnd) % uris; !done; __wt_yield()) {
		/* Use a checkpoint handle for 50% of reads. */
		ret = session->open_cursor(session, uri_list[i], NULL,
		    readonly && (i % 2 == 0) ?
		    "checkpoint=WiredTigerCheckpoint" : NULL, &cursor);
		if (ret != EBUSY) {
			testutil_check(ret);
			break;
		}
		(void)__wt_atomic_add64(&worker_busy, 1);
	}
	if (cursor == NULL)
		return;

	/* Operate on some number of key/value pairs. */
	for (key = 1;
	    !done && key < MAXKEY; key += __wt_random(rnd) % 37, __wt_yield()) {
		testutil_check(
		    __wt_snprintf(buf, sizeof(buf), "key:%020u", key));
		cursor->set_key(cursor, buf);
		if (readonly)
			testutil_check(cursor->search(cursor));
		else {
			cursor->set_value(cursor, buf);
			testutil_check(cursor->insert(cursor));
		}
	}

	/* Close the cursor half the time, otherwise cache it. */
	if (__wt_random(rnd) % 2 == 0)
		testutil_check(cursor->close(cursor));
	else
		*cpp = cursor;

	(void)__wt_atomic_add64(&worker, 1);
}
RecordData WiredTigerRecordStore::dataFor(OperationContext* txn, const RecordId& loc) const {
    // ownership passes to the shared_array created below
    WiredTigerCursor curwrap(_uri, _tableId, true, txn);
    WT_CURSOR* c = curwrap.get();
    invariant(c);
    c->set_key(c, _makeKey(loc));
    int ret = WT_OP_CHECK(c->search(c));
    massert(28556, "Didn't find RecordId in WiredTigerRecordStore", ret != WT_NOTFOUND);
    invariantWTOK(ret);
    return _getData(curwrap);
}
bool WiredTigerKVEngine::_hasUri(WT_SESSION* session, const std::string& uri) const {
    // can't use WiredTigerCursor since this is called from constructor.
    WT_CURSOR* c = NULL;
    int ret = session->open_cursor(session, "metadata:", NULL, NULL, &c);
    if (ret == ENOENT)
        return false;
    invariantWTOK(ret);
    ON_BLOCK_EXIT(c->close, c);

    c->set_key(c, uri.c_str());
    return c->search(c) == 0;
}
Example #27
0
int wt_id2entry( BackendDB *be,
				 WT_SESSION *session,
				 ID id,
				 Entry **ep ){
	int rc;
	WT_CURSOR *cursor = NULL;
	WT_ITEM item;
	EntryHeader eh;
	int eoff;
	Entry *e = NULL;

	rc = session->open_cursor(session, WT_TABLE_ID2ENTRY"(entry)", NULL,
							  NULL, &cursor);
	if ( rc ) {
		Debug( LDAP_DEBUG_ANY,
			   LDAP_XSTRING(wt_id2entry)
			   ": open_cursor failed: %s (%d)\n",
			   wiredtiger_strerror(rc), rc, 0 );
		goto done;
	}

	cursor->set_key(cursor, id);
	rc = cursor->search(cursor);
	if ( rc ) {
		goto done;
	}

	cursor->get_value(cursor, &item);
	rc = wt_entry_header( &item,  &eh );
	eoff = eh.data - (char *)item.data;
	eh.bv.bv_len = eh.nvals * sizeof( struct berval ) + item.size;
	eh.bv.bv_val = ch_malloc( eh.bv.bv_len );
	memset(eh.bv.bv_val, 0xff, eh.bv.bv_len);
	eh.data = eh.bv.bv_val + eh.nvals * sizeof( struct berval );
	memcpy(eh.data, item.data, item.size);
	eh.data += eoff;
	rc = entry_decode( &eh, &e );
	if ( rc ) {
		Debug( LDAP_DEBUG_ANY,
			   LDAP_XSTRING(wt_id2entry)
			   ": entry decode error: %s (%d)\n",
			   rc, 0, 0 );
		goto done;
	}
	e->e_id = id;
	*ep = e;

done:
	if(cursor){
		cursor->close(cursor);
	}
	return rc;
}
TEST_F(WiredTigerRecoveryUnitTestFixture, WriteOnADocumentBeingPreparedTriggersWTRollback) {
    // Prepare but don't commit a transaction
    ru1->beginUnitOfWork(clientAndCtx1.second.get());
    WT_CURSOR* cursor;
    getCursor(ru1, &cursor);
    cursor->set_key(cursor, "key");
    cursor->set_value(cursor, "value");
    invariantWTOK(cursor->insert(cursor));
    ru1->setPrepareTimestamp({1, 1});
    ru1->prepareUnitOfWork();

    // Another transaction with write triggers WT_ROLLBACK
    ru2->beginUnitOfWork(clientAndCtx2.second.get());
    getCursor(ru2, &cursor);
    cursor->set_key(cursor, "key");
    cursor->set_value(cursor, "value2");
    int ret = cursor->insert(cursor);
    ASSERT_EQ(WT_ROLLBACK, ret);

    ru1->abortUnitOfWork();
    ru2->abortUnitOfWork();
}
TEST_F(WiredTigerRecoveryUnitTestFixture,
       LocalReadOnADocumentBeingPreparedWithoutIgnoringPreparedTriggersPrepareConflict) {
    // Prepare but don't commit a transaction
    ru1->beginUnitOfWork(clientAndCtx1.second.get());
    WT_CURSOR* cursor;
    getCursor(ru1, &cursor);
    cursor->set_key(cursor, "key");
    cursor->set_value(cursor, "value");
    invariantWTOK(cursor->insert(cursor));
    ru1->setPrepareTimestamp({1, 1});
    ru1->prepareUnitOfWork();

    // The transaction read default enforces prepare conflicts and triggers a WT_PREPARE_CONFLICT.
    ru2->beginUnitOfWork(clientAndCtx2.second.get());
    getCursor(ru2, &cursor);
    cursor->set_key(cursor, "key");
    int ret = cursor->search(cursor);
    ASSERT_EQ(WT_PREPARE_CONFLICT, ret);

    ru1->abortUnitOfWork();
    ru2->abortUnitOfWork();
}
Example #30
0
    int WiredTigerEngine::Put(const Slice& key, const Slice& value, const Options& options)
    {
        ContextHolder& holder = GetContextHolder();
        WT_SESSION* session = holder.session;
        if (NULL == session)
        {
            return -1;
        }
//        if (holder.trasc_ref > 0)
//        {
//            PutOperation* op = new PutOperation;
//            op->key.assign((const char*) key.data(), key.size());
//            op->value.assign((const char*) value.data(), value.size());
//            m_write_queue.Push(op);
//            holder.readonly_transc = false;
//            return 0;
//        }
        int ret = 0;
        WT_CURSOR *cursor = holder.batch == NULL ? create_wiredtiger_cursor(session) : holder.batch;
        if (NULL == cursor)
        {
            return -1;
        }
        WT_ITEM key_item, value_item;
        key_item.data = key.data();
        key_item.size = key.size();
        value_item.data = value.data();
        value_item.size = value.size();
        cursor->set_key(cursor, &key_item);
        cursor->set_value(cursor, &value_item);
        ret = cursor->insert(cursor);
        if (0 != ret)
        {
            ERROR_LOG("Error write data for reason: %s", wiredtiger_strerror(ret));
            ret = -1;
        }
        if (holder.batch == NULL)
        {
            ret = cursor->close(cursor);
            CHECK_WT_RETURN(ret);
        }
        else
        {
            holder.IncBatchWriteCount();
            if (holder.batch_write_count >= m_cfg.batch_commit_watermark)
            {
                holder.RestartBatchWrite();
            }
        }
        return ret;
    }