Show how to configure and use asynchronous operations.
#include <test_util.h>
static const char *home;
#if defined(_lint)
#define ATOMIC_ADD(v, val) ((v) += (val), (v))
#elif defined(_WIN32)
#define ATOMIC_ADD(v, val) (_InterlockedExchangeAdd(&(v), val) + val)
#else
#define ATOMIC_ADD(v, val) __atomic_add_fetch(&(v), val, __ATOMIC_SEQ_CST)
#endif
static int global_error = 0;
typedef struct {
uint32_t num_keys;
} ASYNC_KEYS;
static int
{
ASYNC_KEYS *asynckey = (ASYNC_KEYS *)cb;
const char *key, *value;
uint64_t id;
(void)flags;
if (wiredtiger_error != 0) {
fprintf(stderr, "ID %" PRIu64 " error %d: %s\n", id, wiredtiger_error,
global_error = wiredtiger_error;
return (1);
}
ATOMIC_ADD(asynckey->num_keys, 1);
printf("Id %" PRIu64 " got record: %s : %s\n", id, key, value);
}
return (0);
}
static ASYNC_KEYS ex_asynckeys = {{async_callback}, 0};
#define MAX_KEYS 15
int
main(int argc, char *argv[])
{
int i, ret;
char k[MAX_KEYS][16], v[MAX_KEYS][16];
home = example_setup(argc, argv);
"create,cache_size=100MB,"
"async=(enabled=true,ops_max=20,threads=2)",
&conn));
error_check(conn->
open_session(conn, NULL, NULL, &session));
error_check(session->
create(session,
"table:async",
"key_format=S,value_format=S"));
for (i = 0; i < MAX_KEYS; i++) {
while (
(ret = conn->
async_new_op(conn,
"table:async", NULL, &ex_asynckeys.iface, &op)) != 0) {
fprintf(stderr, "asynchronous operation handle not available\n");
if (ret == EBUSY)
sleep(1);
else
return (EXIT_FAILURE);
}
(void)snprintf(k[i], sizeof(k), "key%d", i);
(void)snprintf(v[i], sizeof(v), "value%d", i);
}
error_check(conn->
async_new_op(conn,
"table:async",
"timeout=300", &ex_asynckeys.iface, &op));
for (i = 0; i < MAX_KEYS; i++) {
while (
(ret = conn->
async_new_op(conn,
"table:async", NULL, &ex_asynckeys.iface, &op)) != 0) {
fprintf(stderr, "asynchronous operation handle not available\n");
if (ret == EBUSY)
sleep(1);
else
return (EXIT_FAILURE);
}
(void)snprintf(k[i], sizeof(k), "key%d", i);
}
error_check(conn->
close(conn, NULL));
printf("Searched for %" PRIu32 " keys\n", ex_asynckeys.num_keys);
return (EXIT_SUCCESS);
}