Example #1
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 #2
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);
	}
}
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 #4
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 #5
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 #6
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 #7
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 #8
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);
}
Example #10
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;
}
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 #12
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_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)) {
		/*
		 * The returned value should only be set if ret is non-zero, but
		 * Coverity is convinced otherwise. The code path is used enough
		 * that Coverity complains a lot, add an error check to get some
		 * peace and quiet.
		 */
		if ((ret = __wt_turtle_read(session, key, valuep)) != 0)
			__wt_free(session, *valuep);
		return (ret);
	}

	/*
	 * All metadata reads are at read-uncommitted isolation.  That's
	 * because once a schema-level operation completes, subsequent
	 * operations must see the current version of checkpoint metadata, or
	 * they may try to read blocks that may have been freed from a file.
	 * Metadata updates use non-transactional techniques (such as the
	 * schema and metadata locks) to protect access to in-flight updates.
	 */
	WT_RET(__wt_metadata_cursor(session, &cursor));
	cursor->set_key(cursor, key);
	WT_WITH_TXN_ISOLATION(session, WT_ISO_READ_UNCOMMITTED,
	    ret = cursor->search(cursor));
	WT_ERR(ret);

	WT_ERR(cursor->get_value(cursor, &value));
	WT_ERR(__wt_strdup(session, value, valuep));

err:	WT_TRET(__wt_metadata_cursor_release(session, &cursor));

	if (ret != 0)
		__wt_free(session, *valuep);
	return (ret);
}
bool WiredTigerRecordStore::findRecord(OperationContext* txn,
                                       const RecordId& loc,
                                       RecordData* out) const {
    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));
    if (ret == WT_NOTFOUND) {
        return false;
    }
    invariantWTOK(ret);
    *out = _getData(curwrap);
    return true;
}
Example #14
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 #15
0
/* perform read command */
int doread(char *name, int rnum, int rnd){
  WT_CURSOR *c;
  WT_ITEM key, value;
  int i, err;
  char buf[RECBUFSIZ];
  if(showprgr)
    printf("<Read Test of Row Store>\n  name=%s  rnum=%d\n\n", name, rnum);
  /* open a database */
  if(setup(name, NULL, NULL, NULL, &c) != 0){
    fprintf(stderr, "open failed\n");
    return 1;
  }
  err = FALSE;
  key.data = value.data = buf;
  key.size = value.size = 8;
  /* loop for each record */
  for(i = 1; i <= rnum; i++){
    /* store a record */
    sprintf(buf, "%08d", rnd ? myrand() % rnum + 1 : i);
    c->set_key(c, &key);
    if(c->search(c) != 0){
      fprintf(stderr, "search failed\n");
      err = TRUE;
      break;
    }
    /* Include the cost of getting the value. */
    c->get_value(c, &value);
    /* print progression */
    if(showprgr && rnum > 250 && i % (rnum / 250) == 0){
      putchar('.');
      fflush(stdout);
      if(i == rnum || i % (rnum / 10) == 0){
        printf(" (%08d)\n", i);
        fflush(stdout);
      }
    }
  }
  /* close the database */
  if(teardown() != 0) {
    fprintf(stderr, "close failed\n");
    return 1;
  }
  if(showprgr && !err)
    printf("ok\n\n");
  return err ? 1 : 0;
}
Example #16
0
/*
 * dump_json_table_config --
 *	Dump the config for the uri.
 */
static int
dump_json_table_config(WT_SESSION *session, const char *uri)
{
	WT_CURSOR *cursor;
	WT_DECL_RET;
	int tret;
	char *value;

	/* Dump the config. */
	/* Open a metadata cursor. */
	if ((ret = session->open_cursor(
	    session, "metadata:create", NULL, NULL, &cursor)) != 0) {
		fprintf(stderr, "%s: %s: session.open_cursor: %s\n",
		    progname, "metadata:create",
		    session->strerror(session, ret));
		return (1);
	}

	/*
	 * Search for the object itself, to make sure it
	 * exists, and get its config string. This where we
	 * find out a table object doesn't exist, use a simple
	 * error message.
	 */
	cursor->set_key(cursor, uri);
	if ((ret = cursor->search(cursor)) == 0) {
		if ((ret = cursor->get_value(cursor, &value)) != 0)
			ret = util_cerr(cursor, "get_value", ret);
		else if (dump_json_table_begin(
		    session, cursor, uri, value) != 0)
			ret = 1;
	} else if (ret == WT_NOTFOUND)
		ret = util_err(
		    session, 0, "%s: No such object exists", uri);
	else
		ret = util_err(session, ret, "%s", uri);

	if ((tret = cursor->close(cursor)) != 0) {
		tret = util_cerr(cursor, "close", tret);
		if (ret == 0)
			ret = tret;
	}

	return (ret);
}
    boost::optional<Record> seekExact(const RecordId& id) final {
        WT_CURSOR* c = _cursor->get();
        c->set_key(c, _makeKey(id));
        // Nothing after the next line can throw WCEs.
        int seekRet = WT_OP_CHECK(c->search(c));
        if (seekRet == WT_NOTFOUND) {
            _eof = true;
            return {};
        }
        invariantWTOK(seekRet);

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

        _lastReturnedId = id;
        _eof = false;
        return {{id, {static_cast<const char*>(value.data), static_cast<int>(value.size)}}};
    }
Example #18
0
/*
 * __wt_bloom_hash_get --
 *	Tests whether the key (as given by its hash signature) is in the Bloom
 *	filter.  Returns zero if found, WT_NOTFOUND if not.
 */
int
__wt_bloom_hash_get(WT_BLOOM *bloom, WT_BLOOM_HASH *bhash)
{
	WT_CURSOR *c;
	WT_DECL_RET;
	int result;
	uint32_t i;
	uint64_t h1, h2;
	uint8_t bit;

	/* Get operations are only supported by finalized bloom filters. */
	WT_ASSERT(bloom->session, bloom->bitstring == NULL);

	/* Create a cursor on the first time through. */
	WT_ERR(__bloom_open_cursor(bloom, NULL));
	c = bloom->c;

	h1 = bhash->h1;
	h2 = bhash->h2;

	result = 0;
	for (i = 0; i < bloom->k; i++, h1 += h2) {
		/*
		 * Add 1 to the hash because WiredTiger tables are 1 based and
		 * the original bitstring array was 0 based.
		 */
		c->set_key(c, (h1 % bloom->m) + 1);
		WT_ERR(c->search(c));
		WT_ERR(c->get_value(c, &bit));

		if (bit == 0) {
			result = WT_NOTFOUND;
			break;
		}
	}
	WT_ERR(c->reset(c));
	return (result);

err:	/* Don't return WT_NOTFOUND from a failed search. */
	if (ret == WT_NOTFOUND)
		ret = WT_ERROR;
	__wt_err(bloom->session, ret, "Failed lookup in bloom filter.");
	return (ret);
}
Example #19
0
void
print_overflow_pages(WT_SESSION *session)
{
	/*! [statistics retrieve by key] */
	WT_CURSOR *cursor;
	const char *desc, *pvalue;
	uint64_t value;

	error_check(session->open_cursor(session,
	    "statistics:table:access", NULL, NULL, &cursor));

	cursor->set_key(cursor, WT_STAT_DSRC_BTREE_OVERFLOW);
	error_check(cursor->search(cursor));
	error_check(cursor->get_value(cursor, &desc, &pvalue, &value));
	printf("%s=%s\n", desc, pvalue);

	error_check(cursor->close(cursor));
	/*! [statistics retrieve by key] */
}
void WiredTigerRecordStore::deleteRecord(OperationContext* txn, const RecordId& loc) {
    WiredTigerCursor cursor(_uri, _tableId, true, txn);
    cursor.assertInActiveTxn();
    WT_CURSOR* c = cursor.get();
    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;

    ret = WT_OP_CHECK(c->remove(c));
    invariantWTOK(ret);

    _changeNumRecords(txn, -1);
    _increaseDataSize(txn, -old_length);
}
Example #21
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;

	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 #22
0
/* perform read command */
int doflcsread(char *name, int rnum, int rnd){
  WT_CURSOR *c;
  uint8_t value;
  int i, err;
  if(showprgr)
    printf("<Read Test of var-length Column Store>\n  name=%s  rnum=%d\n\n", name, rnum);
  /* open a database */
  if(setup(name, NULL, NULL, NULL, &c) != 0){
    fprintf(stderr, "open failed\n");
    return 1;
  }
  err = FALSE;
  /* loop for each record */
  for(i = 1; i <= rnum; i++){
    c->set_key(c, (uint64_t)(rnd ? myrand() % rnum + 1 : i));
    if(c->search(c) != 0){
      fprintf(stderr, "search failed\n");
      err = TRUE;
      break;
    }
    /* Include the cost of getting the value. */
    c->get_value(c, &value);
    /* print progression */
    if(showprgr && rnum > 250 && i % (rnum / 250) == 0){
      putchar('.');
      fflush(stdout);
      if(i == rnum || i % (rnum / 10) == 0){
        printf(" (%08d)\n", i);
        fflush(stdout);
      }
    }
  }
  /* close the database */
  if(teardown() != 0) {
    fprintf(stderr, "close failed\n");
    return 1;
  }
  if(showprgr && !err)
    printf("ok\n\n");
  return err ? 1 : 0;
}
Example #23
0
/*
 * Read the index by year and print out who was in office that year.
 */
static void
read_index(WT_SESSION *session)
{
	WT_CURSOR *cursor;
	int i, ret;
	char *first_name, *last_name;
	uint16_t rec_year, term_end, term_start, year;

	year = 0;
	srand((unsigned int)getpid());
	error_check(session->open_cursor(
	    session, "index:presidents:term", NULL, NULL, &cursor));

	/*
	 * Pick 10 random years and read the data.
	 */
	for (i = 0; i < 10; i++) {
		year = (uint16_t)((rand() % YEAR_SPAN) + YEAR_BASE);
		printf("Year %" PRIu16 ":\n", year);
		cursor->set_key(cursor, year);
		error_check(cursor->search(cursor));
		error_check(cursor->get_key(cursor, &rec_year));
		error_check(cursor->get_value(cursor,
		    &last_name, &first_name, &term_start, &term_end));

		/* Report all presidents that served during the chosen year */
		ret = 0;
		while (term_start <= year &&
		    year <= term_end && year == rec_year) {
			printf("\t%s %s\n", first_name, last_name);
			if ((ret = cursor->next(cursor)) != 0)
				break;
			error_check(cursor->get_key(cursor, &rec_year));
			error_check(cursor->get_value(cursor,
			    &last_name, &first_name, &term_start, &term_end));
		}
		scan_end_check(ret == 0 || ret == WT_NOTFOUND);
	}

	error_check(cursor->close(cursor));
}
Example #24
0
int 
print_overflow_pages(WT_SESSION *session)
{
	/*! [statistics retrieve by key] */
	WT_CURSOR *cursor;
	const char *desc, *pvalue;
	uint64_t value;
	int ret;

	if ((ret = session->open_cursor(session,
	    "statistics:file:access.wt", NULL, NULL, &cursor)) != 0)
		return (ret);

	cursor->set_key(cursor, WT_STAT_file_overflow);
	ret = cursor->search(cursor);
	ret = cursor->get_value(cursor, &desc, &pvalue, &value);
	printf("%s=%s\n", desc, pvalue);
	/*! [statistics retrieve by key] */

	return (ret);
}
Example #25
0
/*
 * dump_config --
 *	Dump the config for the uri.
 */
static int
dump_config(WT_SESSION *session, const char *uri, WT_CURSOR *cursor, bool hex,
    bool json)
{
	WT_CURSOR *mcursor;
	WT_DECL_RET;
	int tret;

	/* Open a metadata cursor. */
	if ((ret = session->open_cursor(
	    session, "metadata:create", NULL, NULL, &mcursor)) != 0) {
		fprintf(stderr, "%s: %s: session.open_cursor: %s\n", progname,
		    "metadata:create", session->strerror(session, ret));
		return (1);
	}
	/*
	 * Search for the object itself, just to make sure it exists, we don't
	 * want to output a header if the user entered the wrong name. This is
	 * where we find out a table doesn't exist, use a simple error message.
	 */
	mcursor->set_key(mcursor, uri);
	if ((ret = mcursor->search(mcursor)) == 0) {
		if ((!json && dump_prefix(session, hex, json) != 0) ||
		    dump_table_config(session, mcursor, cursor,
		    uri, json) != 0 ||
		    dump_suffix(session, json) != 0)
			ret = 1;
	} else if (ret == WT_NOTFOUND)
		ret = util_err(session, 0, "%s: No such object exists", uri);
	else
		ret = util_err(session, ret, "%s", uri);

	if ((tret = mcursor->close(mcursor)) != 0) {
		tret = util_cerr(mcursor, "close", tret);
		if (ret == 0)
			ret = tret;
	}

	return (ret);
}
Example #26
0
StatusWith<std::string> WiredTigerUtil::getMetadata(OperationContext* opCtx, StringData uri) {
    invariant(opCtx);
    WiredTigerCursor curwrap("metadata:create", WiredTigerSession::kMetadataTableId, false, opCtx);
    WT_CURSOR* cursor = curwrap.get();
    invariant(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 #27
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, const char **valuep)
{
	WT_CURSOR *cursor;
	WT_DECL_RET;
	const char *value;

	*valuep = NULL;

	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);
}
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 #29
0
/*
 * dump_config --
 *	Dump the config for the uri.
 */
static int
dump_config(WT_SESSION *session, const char *uri, int hex)
{
	WT_CURSOR *cursor;
	WT_DECL_RET;
	int tret;

	/* Open a metadata cursor. */
	if ((ret = session->open_cursor(
	    session, WT_METADATA_URI, NULL, NULL, &cursor)) != 0) {
		fprintf(stderr, "%s: %s: session.open_cursor: %s\n",
		    progname, WT_METADATA_URI, wiredtiger_strerror(ret));
		return (1);
	}
	/*
	 * Search for the object itself, just to make sure it exists, we don't
	 * want to output a header if the user entered the wrong name. This is
	 * where we find out a table doesn't exist, use a simple error message.
	 */
	cursor->set_key(cursor, uri);
	if ((ret = cursor->search(cursor)) == 0) {
		if (dump_prefix(hex) != 0 ||
		    dump_table_config(session, cursor, uri) != 0 ||
		    dump_suffix() != 0)
			ret = 1;
	} else if (ret == WT_NOTFOUND)
		ret = util_err(0, "%s: No such object exists", uri);
	else
		ret = util_err(ret, "%s", uri);

	if ((tret = cursor->close(cursor)) != 0) {
		tret = util_cerr(uri, "close", tret);
		if (ret == 0)
			ret = tret;
	}

	return (ret);
}
Example #30
0
void *
read_thread(void *arg)
{
	CONFIG *cfg;
	WT_CONNECTION *conn;
	WT_SESSION *session;
	WT_CURSOR *cursor;
	char *key_buf;
	int ret;

	cfg = (CONFIG *)arg;
	conn = cfg->conn;
	key_buf = calloc(cfg->key_sz, 1);
	if (key_buf == NULL)
		return (arg);

	if ((ret = conn->open_session(conn, NULL, NULL, &session)) != 0) {
		fprintf(stderr,
		    "open_session failed in read thread: %d\n", ret);
		return (NULL);
	}
	if ((ret = session->open_cursor(session, cfg->uri,
	    NULL, NULL, &cursor)) != 0) {
		fprintf(stderr, "open_cursor failed in read thread: %d\n", ret);
		return (NULL);
	}

	while (running) {
		++nops;
		sprintf(key_buf, "%d", rand() % cfg->icount);
		cursor->set_key(cursor, key_buf);
		cursor->search(cursor);
	}

	session->close(session, NULL);
	free(key_buf);
	return (arg);
}