我正在尝试实现一个C应用程序,它将监视来自 different application 的couchbase远程集群上发生的 writes / modifications / new documents 事件 . 我现在熟悉couchbase C SDK和同步实例,但我无法将它与libevent结合使用进行异步I / O.
我读了couchbase libevent plugin documentation和external event loop integration example,但我无法理解如何告诉我的event_base,例如:
在此存储桶上监控此文件,并在修改后向我发送回调
这是我到目前为止所做的事情:
首先,我创建了libevent IO选项
struct event_base *mEvbase = event_base_new();
lcb_t instance;
lcb_error_t err;
struct lcb_create_io_ops_st ciops;
lcb_io_opt_t ioops;
memset(&ciops, 0, sizeof(ciops));
ciops.v.v0.type = LCB_IO_OPS_LIBEVENT;
ciops.v.v0.cookie = mEvbase;
err = lcb_create_libevent_io_opts(0, &ioops, mEvbase);
if (err != LCB_SUCCESS) {
ERRORMSG0("Failed to create an IOOPS structure for libevent: %s\n", lcb_strerror(NULL, error));
}
然后我创建我的实例:
struct lcb_create_st create_options;
std::string host = std::string("couchbase://192.168.130.10/");
host.append(bucket);
const char password[] = "password";
create_options.version = 3;
create_options.v.v3.connstr = host.c_str();
create_options.v.v3.passwd = password;
create_options.v.v3.io = ioops;
//Creating a lcb instance
err = lcb_create(&instance, &create_options);
if (err != LCB_SUCCESS) {
die(NULL, "Couldn't create couchbase handler\n", err);
return;
}
/* Assign the handlers to be called for the operation types */
lcb_set_bootstrap_callback(instance, bootstrap_callback);
lcb_set_get_callback(instance, generic_get_callback);
lcb_set_store_callback(instance, generic_store_callback);
然后我安排了一个连接 .
//We now schedule a connection to the server
err = lcb_connect(instance);
if (err != LCB_SUCCESS) {
die(instance, "Couldn't schedule connection\n", err);
lcb_destroy(instance);
}
lcb_set_cookie(instance, mEvbase);
我使用 libcouchbase version 2.0.17 , libevent core version 2.0.so.5.1.9 和 libevent extra version 2.0.so.5.1.9 . 使用上面的代码,我的实例无法连接到couchbase . 我收到以下警告:
event_pending: event has no event_base set.
event_add: event has no event_base set.
这里有两个问题:我无法使用上面的代码连接,我不知道去哪个方向开始接收事件 . 如果有人指向我这个简单案例的链接或编码示例,我将解除阻止 .
1 回答
确保您为库和应用程序使用相同的libevent版本 . 从存储库安装软件包,您需要与其中使用的libevent版本一致(例如
ldd /usr/lib64/libcouchbase_libevent.so
) . 请记住,这必须是相同的ABI(因此,例如,使用libevent的2.0 - > 1.4 compat层将不起作用,因为这两个版本包含不同的ABI,并且使用与1.4链接的libcouchbase_libevent.so将在2.0下中断) .有关完整的交换,请参阅有关问题的评论:)