Show hot to configure and use asynchronous operations.
#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <wiredtiger.h>
#if defined(_lint)
#define ATOMIC_ADD(v, val) ((v) += (val), (v))
#else
#define ATOMIC_ADD(v, val) __sync_add_and_fetch(&(v), val)
#endif
const char *home = NULL;
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;
int ret;
(void)flags;
ret = 0;
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 (ret);
}
static ASYNC_KEYS ex_asynckeys = { {async_callback}, 0 };
#define MAX_KEYS 15
int
main(void)
{
int i, ret;
char k[MAX_KEYS][16], v[MAX_KEYS][16];
"create,cache_size=100MB,"
"async=(enabled=true,ops_max=20,threads=2)", &conn);
session, "table:async", "key_format=S,value_format=S");
for (i = 0; i < MAX_KEYS; i++) {
"table:async", NULL, &ex_asynckeys.iface, &op)) != 0) {
fprintf(stderr,
"asynchronous operation handle not available\n");
if (ret == EBUSY)
sleep(1);
else
return (ret);
}
snprintf(k[i], sizeof(k), "key%d", i);
snprintf(v[i], sizeof(v), "value%d", i);
}
conn, "table:async", "timeout=300", &ex_asynckeys.iface, &op);
for (i = 0; i < MAX_KEYS; i++) {
"table:async", NULL, &ex_asynckeys.iface, &op)) != 0) {
fprintf(stderr,
"asynchronous operation handle not available\n");
if (ret == EBUSY)
sleep(1);
else
return (ret);
}
snprintf(k[i], sizeof(k), "key%d", i);
}
ret = conn->
close(conn, NULL);
printf("Searched for %d keys\n", ex_asynckeys.num_keys);
return (ret);
}