Show how to configure and use asynchronous operations.
#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef _WIN32
#include <unistd.h>
#else
#include "windows_shim.h"
#endif
#include <wiredtiger.h>
#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) __sync_add_and_fetch(&(v), val)
#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;
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;
const char *home;
char k[MAX_KEYS][16], v[MAX_KEYS][16];
if (getenv("WIREDTIGER_HOME") == NULL) {
home = "WT_HOME";
ret = system("rm -rf WT_HOME && mkdir WT_HOME");
} else
home = NULL;
"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 (EXIT_FAILURE);
}
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 (EXIT_FAILURE);
}
snprintf(k[i], sizeof(k), "key%d", i);
}
ret = conn->
close(conn, NULL);
printf("Searched for %" PRIu32 " keys\n", ex_asynckeys.num_keys);
return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}