Shows how to integrate the library with an external event loop
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libcouchbase/api3.h>
#include <event2/event.h>
static void
{
fprintf(stderr,
"ERROR: %s\n",
lcb_strerror(instance, err));
exit(EXIT_FAILURE);
}
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);
}
fprintf(stdout,
"I stored and retrieved the key 'foo'. Value: %.*s. Terminate program\n", (
int)rg->
nvalue, rg->
value);
(void)cbtype;
}
{
fprintf(stderr,
"Failed to store key: %s\n",
lcb_strerror(instance, rb->
rc));
exit(EXIT_FAILURE);
}
fprintf(stderr,
"Failed to schedule get request: %s\n",
lcb_strerror(NULL, rc));
exit(EXIT_FAILURE);
}
(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;
}
create_libcouchbase_handle(lcb_io_opt_t ioops)
{
memset(&copts, 0, sizeof(copts));
copts.v.v0.host = getenv("LCB_EVENT_SERVER");
copts.v.v0.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(void)
{
struct event_base *evbase = event_base_new();
lcb_io_opt_t ioops = create_libevent_io_ops(evbase);
lcb_t instance = create_libcouchbase_handle(ioops);
event_base_loop(evbase, 0);
event_base_free(evbase);
exit(EXIT_SUCCESS);
}