#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libcouchbase/api3.h>
#include <event2/event.h>
const char key[] = "foo";
lcb_SIZE nkey = sizeof(key);
const char val[] = "{\"answer\":42}";
lcb_SIZE nval = sizeof(val);
int nreq = 1;
int nresp = 1;
int interval = 0;
struct event *timer = NULL;
{
fprintf(stderr,
"ERROR: %s\n",
lcb_strerror(instance, err));
exit(EXIT_FAILURE);
}
printf("successfully bootstrapped\n");
fflush(stdout);
fprintf(stderr,
"Failed to set up store request: %s\n",
lcb_strerror(instance, err));
exit(EXIT_FAILURE);
}
}
{
fprintf(stderr,
"Failed to get key: %s\n",
lcb_strerror(instance, rg->
rc));
exit(EXIT_FAILURE);
}
printf(
"%d. retrieved the key 'foo', value: %.*s\n", nresp, (
int)rg->
nvalue, rg->
value);
fflush(stdout);
nresp--;
if (nresp == 0) {
printf("stopping the loop\n");
}
(void)cbtype;
}
static void schedule_timer();
static void timer_callback(int fd, short event, void *arg)
{
fprintf(stderr,
"Failed to schedule get request: %s\n",
lcb_strerror(NULL, rc));
exit(EXIT_FAILURE);
}
(void)fd;
(void)event;
schedule_timer();
}
static void schedule_timer()
{
struct timeval tv;
if (!nreq) {
return;
}
tv.tv_sec = interval;
tv.tv_usec = 0;
evtimer_add(timer, &tv);
nreq--;
}
{
fprintf(stderr,
"Failed to store key: %s\n",
lcb_strerror(instance, rb->
rc));
exit(EXIT_FAILURE);
}
printf("stored key 'foo'\n");
fflush(stdout);
{
struct event_base *evbase = (
struct event_base *)
lcb_get_cookie(instance);
printf("try to get value %d times with %dsec interval\n", nreq, interval);
timer = evtimer_new(evbase, timer_callback, instance);
schedule_timer();
}
(void)cbtype;
}
static lcb_io_opt_t create_libevent_io_ops(struct event_base *evbase)
{
lcb_io_opt_t ioops;
memset(&ciops, 0, sizeof(ciops));
ciops.v.v0.cookie = evbase;
fprintf(stderr,
"Failed to create an IOOPS structure for libevent: %s\n",
lcb_strerror(NULL, error));
exit(EXIT_FAILURE);
}
return ioops;
}
static lcb_t create_libcouchbase_handle(lcb_io_opt_t ioops,
int argc,
char **argv)
{
memset(&copts, 0, sizeof(copts));
copts.version = 3;
if (argc > 1) {
copts.v.v3.connstr = argv[1];
}
if (argc > 2) {
copts.v.v3.passwd = argv[2];
}
if (argc > 3) {
copts.v.v3.username = argv[3];
}
copts.v.v3.io = ioops;
fprintf(stderr,
"Failed to create a libcouchbase instance: %s\n",
lcb_strerror(NULL, error));
exit(EXIT_FAILURE);
}
fprintf(stderr,
"Failed to connect libcouchbase instance: %s\n",
lcb_strerror(NULL, error));
exit(EXIT_FAILURE);
}
return instance;
}
int main(int argc, char **argv)
{
struct event_base *evbase = event_base_new();
lcb_io_opt_t ioops = create_libevent_io_ops(evbase);
lcb_t instance = create_libcouchbase_handle(ioops, argc, argv);
if (argc > 4) {
nreq = nresp = atoi(argv[4]);
}
if (argc > 5) {
interval = atoi(argv[4]);
}
event_base_loop(evbase, 0);
if (timer) {
evtimer_del(timer);
}
event_base_free(evbase);
return EXIT_SUCCESS;
}