示例#1
0
int main(void)
{
	/*! [example connection] */
	WT_ASYNC_OP *op;
	WT_CONNECTION *wt_conn;
	WT_SESSION *session;
	int i, ret;
	char k[MAX_KEYS][16], v[MAX_KEYS][16];

	if ((ret = wiredtiger_open(home, NULL,
	    "create,cache_size=100MB,async=(enabled=true,ops_max=10,threads=2)",
	    &wt_conn)) != 0) {
		fprintf(stderr, "Error connecting to %s: %s\n",
		    home, wiredtiger_strerror(ret));
		return (ret);
	}
	/*! [example connection] */

	/*! [example table create] */
	ret = wt_conn->open_session(wt_conn, NULL, NULL, &session);
	ret = session->create(session, uri,
	    "key_format=S,value_format=S");
	/*! [example table create] */

	for (i = 0; i < MAX_KEYS; i++) {
		/*! [Allocate a handle] */
		op = NULL;
retry:
		ret = wt_conn->async_new_op(wt_conn, uri, NULL, &cb, &op);
		if (ret != 0) {
			/*
			 * If we used up all the ops, pause and retry to
			 * give the workers a chance to process them.
			 */
			fprintf(stderr,
			    "Iteration %d: async_new_op ret %d\n",i,ret);
			sleep(1);
			goto retry;
		}
		/*! [Allocate a handle] */
		snprintf(k[i], sizeof(k), "key%d", i);
		snprintf(v[i], sizeof(v), "value%d", i);
		/*! [Set the operation's string key] */
		op->set_key(op, k[i]);
		/*! [Set the operation's string key] */
		/*! [Set the operation's string value] */
		op->set_value(op, v[i]);
		/*! [Set the operation's string value] */
		/*! [example insert] */
		ret = op->insert(op);
		/*! [example insert] */
	}

	/*! [flush] */
	wt_conn->async_flush(wt_conn);
	/*! [flush] */
	/*! [Compact a table] */
	ret = wt_conn->async_new_op(wt_conn, uri, "timeout=10", &cb, &op);
	op->compact(op);
	/*! [Compact a table] */

	for (i = 0; i < MAX_KEYS; i++) {
		op = NULL;
retry2:
		ret = wt_conn->async_new_op(wt_conn, uri, NULL, &cb, &op);
		if (ret != 0) {
			/*
			 * If we used up all the ops, pause and retry to
			 * give the workers a chance to process them.
			 */
			fprintf(stderr,
			    "Iteration %d: async_new_op ret %d\n",i,ret);
			sleep(1);
			goto retry2;
		}
		snprintf(k[i], sizeof(k), "key%d", i);
		op->set_key(op, k[i]);
		/*! [search] */
		op->search(op);
		/*! [search] */
	}

	/*! [example close] */
	/*
	 * Connection close automatically does an async_flush so it will
	 * allow all queued search operations to complete.
	 */
	ret = wt_conn->close(wt_conn, NULL);
	/*! [example close] */

	return (ret);
}
示例#2
0
int
main(void)
{
	WT_ASYNC_OP *op;
	WT_CONNECTION *conn;
	WT_SESSION *session;
	int i, ret;
	char k[MAX_KEYS][16], v[MAX_KEYS][16];

	/*! [async example connection] */
	ret = wiredtiger_open(home, NULL,
	    "create,cache_size=100MB,"
	    "async=(enabled=true,ops_max=20,threads=2)", &conn);
	/*! [async example connection] */

	/*! [async example table create] */
	ret = conn->open_session(conn, NULL, NULL, &session);
	ret = session->create(
	    session, "table:async", "key_format=S,value_format=S");
	/*! [async example table create] */

	/* Insert a set of keys asynchronously. */
	for (i = 0; i < MAX_KEYS; i++) {
		/*! [async handle allocation] */
		while ((ret = conn->async_new_op(conn,
		    "table:async", NULL, &ex_asynckeys.iface, &op)) != 0) {
			/*
			 * If we used up all the handles, pause and retry to
			 * give the workers a chance to catch up.
			 */
			fprintf(stderr,
			    "asynchronous operation handle not available\n");
			if (ret == EBUSY)
				sleep(1);
			else
				return (ret);
		}
		/*! [async handle allocation] */

		/*! [async insert] */
		/*
		 * Set the operation's string key and value, and then do
		 * an asynchronous insert.
		 */
		/*! [async set the operation's string key] */
		snprintf(k[i], sizeof(k), "key%d", i);
		op->set_key(op, k[i]);
		/*! [async set the operation's string key] */

		/*! [async set the operation's string value] */
		snprintf(v[i], sizeof(v), "value%d", i);
		op->set_value(op, v[i]);
		/*! [async set the operation's string value] */

		ret = op->insert(op);
		/*! [async insert] */
	}

	/*! [async flush] */
	/* Wait for all outstanding operations to complete. */
	ret = conn->async_flush(conn);
	/*! [async flush] */

	/*! [async compaction] */
	/*
	 * Compact a table asynchronously, limiting the run-time to 5 minutes.
	 */
	ret = conn->async_new_op(
	    conn, "table:async", "timeout=300", &ex_asynckeys.iface, &op);
	ret = op->compact(op);
	/*! [async compaction] */

	/* Search for the keys we just inserted, asynchronously. */
	for (i = 0; i < MAX_KEYS; i++) {
		while ((ret = conn->async_new_op(conn,
		    "table:async", NULL, &ex_asynckeys.iface, &op)) != 0) {
			/*
			 * If we used up all the handles, pause and retry to
			 * give the workers a chance to catch up.
			 */
			fprintf(stderr,
			    "asynchronous operation handle not available\n");
			if (ret == EBUSY)
				sleep(1);
			else
				return (ret);
		}

		/*! [async search] */
		/*
		 * Set the operation's string key and value, and then do
		 * an asynchronous search.
		 */
		snprintf(k[i], sizeof(k), "key%d", i);
		op->set_key(op, k[i]);
		op->search(op);
		/*! [async search] */
	}

	/*
	 * Connection close automatically does an async_flush so it will wait
	 * for all queued search operations to complete.
	 */
	ret = conn->close(conn, NULL);

	printf("Searched for %d keys\n", ex_asynckeys.num_keys);

	return (ret);
}