LCBMT_INTERNAL void lcbmt_wrap_callbacks(lcbmt_ctx_t *mt, lcb_t instance) { lcb_set_store_callback(instance, store_callback); lcb_set_get_callback(instance, get_callback); lcb_set_remove_callback(instance, remove_callback); lcb_set_arithmetic_callback(instance, arithmetic_callback); lcb_set_touch_callback(instance, touch_callback); lcb_set_unlock_callback(instance, unlock_callback); lcb_set_stat_callback(instance, stats_callback); lcb_set_durability_callback(instance, endure_callback); lcb_set_http_data_callback(instance, http_data_callback); lcb_set_http_complete_callback(instance, http_complete_callback); }
PHP_METHOD(Bucket, __construct) { bucket_object *data = PCBC_PHP_THISOBJ(); zval *zdsn = NULL; zval *zname = NULL; zval *zpassword = NULL; char *dsn = NULL; char *name = NULL; char *password = NULL; lcb_error_t err; lcb_t instance; struct lcb_create_st create_options; char *connkey = NULL; pcbc_lcb *conn_iter, *conn; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|zzz", &zdsn, &zname, &zpassword) == FAILURE) { RETURN_NULL(); } if (zap_zval_is_undef(zdsn)) { zdsn = NULL; } if (zap_zval_is_undef(zname)) { zname = NULL; } if (zap_zval_is_undef(zpassword)) { zpassword = NULL; } if (zdsn) { if (Z_TYPE_P(zdsn) == IS_STRING) { dsn = estrndup(Z_STRVAL_P(zdsn), Z_STRLEN_P(zdsn)); } else { throw_pcbc_exception("Expected dsn as string", LCB_EINVAL); RETURN_NULL(); } } if (zname) { if (Z_TYPE_P(zname) == IS_STRING) { name = estrndup(Z_STRVAL_P(zname), Z_STRLEN_P(zname)); } else { throw_pcbc_exception("Expected bucket name as string", LCB_EINVAL); if (dsn) efree(dsn); RETURN_NULL(); } } if (zpassword) { if (Z_TYPE_P(zpassword) == IS_STRING) { password = estrndup(Z_STRVAL_P(zpassword), Z_STRLEN_P(zpassword)); } else { throw_pcbc_exception("Expected bucket password as string", LCB_EINVAL); if (dsn) efree(dsn); if (name) efree(name); RETURN_NULL(); } } spprintf(&connkey, 512, "%s|%s|%s", dsn ? dsn : "", name ? name : "", password ? password : ""); conn_iter = PCBCG(first_bconn); while (conn_iter) { if (strcmp(conn_iter->key, connkey) == 0) { break; } conn_iter = conn_iter->next; } if (!conn_iter) { memset(&create_options, 0, sizeof(create_options)); create_options.version = 3; create_options.v.v3.connstr = dsn; create_options.v.v3.username = name; create_options.v.v3.passwd = password; create_options.v.v3.type = LCB_TYPE_BUCKET; err = lcb_create(&instance, &create_options); if (dsn) efree(dsn); if (password) efree(password); if (err != LCB_SUCCESS) { efree(connkey); efree(name); throw_lcb_exception(err); RETURN_NULL(); } lcb_cntl(instance, LCB_CNTL_SET, LCB_CNTL_CLIENT_STRING, pcbc_client_string); lcb_install_callback3(instance, LCB_CALLBACK_GET, get_callback); lcb_install_callback3(instance, LCB_CALLBACK_UNLOCK, unlock_callback); lcb_install_callback3(instance, LCB_CALLBACK_STORE, store_callback); lcb_install_callback3(instance, LCB_CALLBACK_STOREDUR, store_callback); lcb_install_callback3(instance, LCB_CALLBACK_REMOVE, remove_callback); lcb_install_callback3(instance, LCB_CALLBACK_TOUCH, touch_callback); lcb_install_callback3(instance, LCB_CALLBACK_COUNTER, counter_callback); lcb_install_callback3(instance, LCB_CALLBACK_SDLOOKUP, subdoc_callback); lcb_install_callback3(instance, LCB_CALLBACK_SDMUTATE, subdoc_callback); lcb_install_callback3(instance, LCB_CALLBACK_HTTP, http_callback); lcb_set_durability_callback(instance, durability_callback); err = lcb_connect(instance); if (err != LCB_SUCCESS) { efree(connkey); efree(name); lcb_destroy(instance); throw_lcb_exception(err); RETURN_NULL(); } // We use lcb_wait here as no callbacks are invoked by connect. lcb_wait(instance); err = lcb_get_bootstrap_status(instance); if (err != LCB_SUCCESS) { efree(connkey); efree(name); lcb_destroy(instance); throw_lcb_exception(err); RETURN_NULL(); } conn = pemalloc(sizeof(pcbc_lcb), 1); conn->bucket = pestrdup(name, 1); efree(name); conn->key = pestrdup(connkey, 1); conn->lcb = instance; conn->next = NULL; data->conn = conn; if (PCBCG(last_bconn)) { PCBCG(last_bconn)->next = conn; PCBCG(last_bconn) = conn; } else { PCBCG(first_bconn) = conn; PCBCG(last_bconn) = conn; } } else { if (dsn) efree(dsn); if (name) efree(name); if (password) efree(password); data->conn = conn_iter; } efree(connkey); }
int main(void) { struct lcb_create_st create_options; memset(&create_options, 0, sizeof create_options); create_options.version = 3; create_options.v.v3.connstr = "couchbase://10.141.95.101/default"; // create_options.v.v3.passwd = "testbucket"; lcb_error_t err; lcb_t instance = NULL; err = lcb_create(&instance, &create_options); if (err != LCB_SUCCESS) { fprintf(stderr, "Failed to create a couchbase instance: %s\n", lcb_strerror(instance,err)); return 1; } // Default timeout values and settings: //http://developer.couchbase.com/documentation/server/current/sdks/c-2.4/options.html //Enable details error codes: lcb_cntl_string(instance,"detailed_errcodes","1"); //Connecting lcb_connect(instance); lcb_wait(instance); if ( (err = lcb_get_bootstrap_status(instance)) != LCB_SUCCESS ) { printf("Couldn't bootstrap. Exiting!\n"); exit(1); } // installing callbacks lcb_set_store_callback(instance, storage_callback); lcb_set_durability_callback(instance, durability_callback); // scheduling set operations with elements that expire lcb_store_cmd_t scmd = { 0 }; const lcb_store_cmd_t *scmdlist = &scmd; /* INSERT, Loop through x number of keys and insert them with expiry */ for( int counter=1; counter <= NUMBER_OF_DOCUMENTS; counter++){ // Setting keys and values const char *doc = "{\"json\" : \"data\" }"; char key [12]; sprintf(key, "%s%d", "test", counter); scmd.v.v0.key = key; scmd.v.v0.nkey = strlen(key); scmd.v.v0.bytes = doc; scmd.v.v0.nbytes = strlen(doc); scmd.v.v0.exptime = 300; //expiry of 5 minutes scmd.v.v0.operation = LCB_SET; err = lcb_store(instance, NULL, 1, (const lcb_store_cmd_t * const *)&scmdlist); if (err != LCB_SUCCESS) { printf("Couldn't schedule storage operation!\n"); exit(1); } lcb_wait(instance); //Durability is invoked here lcb_durability_opts_t options; lcb_durability_cmd_t cmd = { 0 }; const lcb_durability_cmd_t *cmdp = &cmd; cmd.v.v0.key = key; cmd.v.v0.nkey = strlen(key); options.v.v0.persist_to = 1; options.v.v0.replicate_to = 1; err = lcb_durability_poll(instance,NULL,&options,1,&cmdp); if (err != LCB_SUCCESS) { printf("couldn't schedule durability operation %s \n", lcb_strerror(instance, err)); } lcb_wait(instance); } //closing the connection lcb_destroy(instance); return 0; }