static void test_get1(void) { lcb_error_t err; struct rvbuf rv; lcb_store_cmd_t storecmd; const lcb_store_cmd_t *storecmds[] = { &storecmd }; lcb_get_cmd_t getcmd; const lcb_get_cmd_t *getcmds[] = { &getcmd }; memset(&storecmd, 0, sizeof(storecmd)); storecmd.v.v0.key = "foo"; storecmd.v.v0.nkey = strlen(storecmd.v.v0.key); storecmd.v.v0.bytes = "bar"; storecmd.v.v0.nbytes = strlen(storecmd.v.v0.bytes); storecmd.v.v0.operation = LCB_SET; (void)lcb_set_store_callback(session, store_callback); (void)lcb_set_get_callback(session, get_callback); err = lcb_store(session, &rv, 1, storecmds); lcb_assert(err == LCB_SUCCESS); io->v.v0.run_event_loop(io); lcb_assert(rv.error == LCB_SUCCESS); memset(&rv, 0, sizeof(rv)); memset(&getcmd, 0, sizeof(getcmd)); getcmd.v.v0.key = "foo"; getcmd.v.v0.nkey = strlen(getcmd.v.v0.key); err = lcb_get(session, &rv, 1, getcmds); lcb_assert(err == LCB_SUCCESS); io->v.v0.run_event_loop(io); lcb_assert(rv.error == LCB_SUCCESS); lcb_assert(rv.nbytes == strlen("bar")); lcb_assert(memcmp(rv.bytes, "bar", 3) == 0); }
static void test_set2(void) { lcb_error_t err; struct rvbuf rv; lcb_size_t ii; lcb_store_cmd_t cmd; const lcb_store_cmd_t *cmds[] = { &cmd }; memset(&cmd, 0, sizeof(cmd)); cmd.v.v0.key = "foo"; cmd.v.v0.nkey = strlen(cmd.v.v0.key); cmd.v.v0.bytes = "bar"; cmd.v.v0.nbytes = strlen(cmd.v.v0.bytes); cmd.v.v0.operation = LCB_SET; (void)lcb_set_store_callback(session, mstore_callback); rv.errors = 0; rv.counter = 0; for (ii = 0; ii < 10; ++ii, ++rv.counter) { err = lcb_store(session, &rv, 1, cmds); lcb_assert(err == LCB_SUCCESS); } io->v.v0.run_event_loop(io); lcb_assert(rv.errors == 0); }
/*opaque_t*/ int couchdb_create(void) { struct lcb_create_st create_opt; struct lcb_create_io_ops_st io_opts; lcb_error_t ret; io_opts.version = 0; io_opts.v.v0.type = LCB_IO_OPS_DEFAULT; io_opts.v.v0.cookie = NULL; ret = lcb_create_io_ops(&create_opt.v.v0.io, &io_opts); if (ret != LCB_SUCCESS) { syslog(LOG_ERR, "%s:Failed to create IO instance: %s\n", __FUNCTION__, lcb_strerror(NULL, ret)); return -1; } memset(&create_opt, 0, sizeof(struct lcb_create_st)); create_opt.v.v0.host = "127.0.0.1:8091"; create_opt.v.v0.user = "******"; create_opt.v.v0.passwd = "123456"; create_opt.v.v0.bucket = "default"; ret = lcb_create(&couchdb_handle, &create_opt); if (ret != LCB_SUCCESS) { COUCHDB_LOG("Error creating couchbase DB instance\n"); return (-1); } /* Setup the Global variables */ value = (void *) malloc(DATA_SIZE * sizeof(char)); if (NULL == value) { syslog(LOG_ERR, "%s: Failed to allocate memory for value\n", __FUNCTION__); return -ENOMEM; } pthread_rwlock_init(&db_rwlock, NULL); /* Once a valid handle is obtained, set up all the callbacks */ lcb_set_error_callback(couchdb_handle, error_cb); lcb_set_get_callback(couchdb_handle, get_cb); lcb_set_store_callback(couchdb_handle, set_cb); lcb_set_flush_callback(couchdb_handle, flush_cb); lcb_set_remove_callback(couchdb_handle, remove_cb); /* Now connect to the cluster having the server node on the "host" ip */ ret = lcb_connect(couchdb_handle); if (ret != LCB_SUCCESS) { COUCHDB_LOG("Error connecting to the cluster\n"); return (-1); } /* Since the couchbase APIs are all asynchronous calls, we need to block the thread until the connect succeeds */ lcb_wait(couchdb_handle); return (0); }
int _tmain(int argc, _TCHAR* argv[]) { // initializing struct lcb_create_st cropts = { 0 }; cropts.version = 3; cropts.v.v3.connstr = "couchbase://localhost/default"; lcb_error_t err; lcb_t instance; err = lcb_create(&instance, &cropts); if (err != LCB_SUCCESS) { printf("Couldn't create instance!\n"); exit(1); } // connecting lcb_connect(instance); lcb_wait(instance); if ((err = lcb_get_bootstrap_status(instance)) != LCB_SUCCESS) { printf("Couldn't bootstrap!\n"); exit(1); } // installing callbacks lcb_set_store_callback(instance, storage_callback); lcb_set_get_callback(instance, get_callback); // scheduling operations lcb_store_cmd_t scmd; const lcb_store_cmd_t *scmdlist = &scmd; scmd.v.v0.key = "Hello"; scmd.v.v0.nkey = 5; scmd.v.v0.bytes = "Couchbase"; scmd.v.v0.nbytes = 9; scmd.v.v0.operation = LCB_SET; err = lcb_store(instance, NULL, 1, &scmdlist); if (err != LCB_SUCCESS) { printf("Couldn't schedule storage operation!\n"); exit(1); } lcb_wait(instance); //storage_callback is invoked here lcb_get_cmd_t gcmd = { 0 }; const lcb_get_cmd_t *gcmdlist = &gcmd; gcmd.v.v0.key = "Hello"; gcmd.v.v0.nkey = 5; err = lcb_get(instance, NULL, 1, &gcmdlist); if (err != LCB_SUCCESS) { printf("Couldn't schedule get operation!\n"); exit(1); } lcb_wait(instance); // get_callback is invoked here lcb_destroy(instance); return 0; }
void pycbc_callbacks_init(lcb_t instance) { lcb_set_store_callback(instance, store_callback); lcb_set_unlock_callback(instance, unlock_callback); lcb_set_get_callback(instance, get_callback); lcb_set_touch_callback(instance, touch_callback); lcb_set_arithmetic_callback(instance, arithmetic_callback); lcb_set_remove_callback(instance, delete_callback); lcb_set_stat_callback(instance, stat_callback); lcb_set_error_callback(instance, error_callback); lcb_set_http_complete_callback(instance, http_complete_callback); }
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); }
/** Initialize a Couchbase connection instance * * Initialize all information relating to a Couchbase instance and configure available method callbacks. * This function forces synchronous operation and will wait for a connection or timeout. * * @param instance Empty (un-allocated) Couchbase instance object. * @param host The Couchbase server or list of servers. * @param bucket The Couchbase bucket to associate with the instance. * @param pass The Couchbase bucket password (NULL if none). * @param timeout Maximum time to wait for obtaining the initial configuration. * @return Couchbase error object. */ lcb_error_t couchbase_init_connection(lcb_t *instance, const char *host, const char *bucket, const char *pass, struct timeval const *timeout) { lcb_error_t error; /* couchbase command return */ struct lcb_create_st options; /* init create struct */ lcb_uint32_t timeout_ms = FR_TIMEVAL_TO_MS(timeout); /* init options */ memset(&options, 0, sizeof(options)); /* assign couchbase create options */ options.v.v0.host = host; options.v.v0.bucket = bucket; /* assign user and password if they were both passed */ if (bucket != NULL && pass != NULL) { options.v.v0.user = bucket; options.v.v0.passwd = pass; } /* create couchbase connection instance */ error = lcb_create(instance, &options); if (error != LCB_SUCCESS) return error; error = lcb_cntl(*instance, LCB_CNTL_SET, LCB_CNTL_CONFIGURATION_TIMEOUT, &timeout_ms); if (error != LCB_SUCCESS) return error; /* initiate connection */ error = lcb_connect(*instance); if (error != LCB_SUCCESS) return error; /* set general method callbacks */ lcb_set_stat_callback(*instance, couchbase_stat_callback); lcb_set_store_callback(*instance, couchbase_store_callback); lcb_set_get_callback(*instance, couchbase_get_callback); lcb_set_http_data_callback(*instance, couchbase_http_data_callback); /* wait on connection */ lcb_wait(*instance); return LCB_SUCCESS; }
/** Initialize a Couchbase connection instance * * Initialize all information relating to a Couchbase instance and configure available method callbacks. * This function forces synchronous operation and will wait for a connection or timeout. * * @param instance Empty (un-allocated) Couchbase instance object. * @param host The Couchbase server or list of servers. * @param bucket The Couchbase bucket to associate with the instance. * @param pass The Couchbase bucket password (NULL if none). * @return Couchbase error object. */ lcb_error_t couchbase_init_connection(lcb_t *instance, const char *host, const char *bucket, const char *pass) { lcb_error_t error; /* couchbase command return */ struct lcb_create_st options; /* init create struct */ /* init options */ memset(&options, 0, sizeof(options)); /* assign couchbase create options */ options.v.v0.host = host; options.v.v0.bucket = bucket; /* assign user and password if they were both passed */ if (bucket != NULL && pass != NULL) { options.v.v0.user = bucket; options.v.v0.passwd = pass; } /* create couchbase connection instance */ if ((error = lcb_create(instance, &options)) != LCB_SUCCESS) { /* return error */ return error; } /* initiate connection */ if ((error = lcb_connect(*instance)) == LCB_SUCCESS) { /* set general method callbacks */ lcb_set_stat_callback(*instance, couchbase_stat_callback); lcb_set_store_callback(*instance, couchbase_store_callback); lcb_set_get_callback(*instance, couchbase_get_callback); lcb_set_http_data_callback(*instance, couchbase_http_data_callback); /* wait on connection */ lcb_wait(*instance); } else { /* return error */ return error; } /* return instance */ return error; }
static void test_spurious_saslerr(void) { int iterations = 50; struct rvbuf rvs[50]; int i; lcb_error_t err; lcb_store_cmd_t storecmd; const lcb_store_cmd_t *storecmds[] = { &storecmd }; lcb_set_store_callback(session, mstore_callback); memset(rvs, 0, sizeof(rvs)); for (i = 0; i < iterations; i++) { rvs[i].counter = 999; /*don't trigger a stop_event_loop*/ memset(&storecmd, 0, sizeof(storecmd)); storecmd.v.v0.key = "KEY"; storecmd.v.v0.nkey = strlen(storecmd.v.v0.key); storecmd.v.v0.bytes = "KEY"; storecmd.v.v0.nbytes = strlen(storecmd.v.v0.bytes); storecmd.v.v0.operation = LCB_SET; err = lcb_store(session, rvs + i, 1, storecmds); if (err != LCB_SUCCESS) { err_exit("Store operation failed"); } } lcb_wait(session); for (i = 0; i < iterations; i++) { char *errinfo = NULL; if (rvs[i].errors != LCB_SUCCESS) { errinfo = "Did not get success response"; } else if (rvs[i].nkey != 3) { errinfo = "Did not get expected key length"; } else if (memcmp(rvs[i].key, "KEY", 3) != 0) { errinfo = "Weird key size"; } if (errinfo) { err_exit("%s", errinfo); } } }
static void test_set1(void) { lcb_error_t err; struct rvbuf rv; lcb_store_cmd_t cmd; const lcb_store_cmd_t *cmds[] = { &cmd }; memset(&cmd, 0, sizeof(cmd)); cmd.v.v0.key = "foo"; cmd.v.v0.nkey = strlen(cmd.v.v0.key); cmd.v.v0.bytes = "bar"; cmd.v.v0.nbytes = strlen(cmd.v.v0.bytes); cmd.v.v0.operation = LCB_SET; (void)lcb_set_store_callback(session, store_callback); err = lcb_store(session, &rv, 1, cmds); lcb_assert(err == LCB_SUCCESS); io->v.v0.run_event_loop(io); lcb_assert(rv.error == LCB_SUCCESS); lcb_assert(rv.operation == LCB_SET); lcb_assert(memcmp(rv.key, "foo", 3) == 0); }
PHP_COUCHBASE_LOCAL void php_couchbase_callbacks_store_init(lcb_t handle) { lcb_set_store_callback(handle, php_couchbase_store_callback); }
int main(int argc, char *argv[]) { char *method = NULL; char *key = NULL; lcb_error_t err; lcb_t instance; struct lcb_create_st create_options; struct lcb_create_io_ops_st io_opts; io_opts.version = 0; io_opts.v.v0.type = LCB_IO_OPS_DEFAULT; io_opts.v.v0.cookie = NULL; memset(&create_options, 0, sizeof(create_options)); err = lcb_create_io_ops(&create_options.v.v0.io, &io_opts); if (err != LCB_SUCCESS) { fprintf(stderr, "Failed to create IO instance: %s\n", lcb_strerror(NULL, err)); return 1; } if (argc > 1) { create_options.v.v0.host = argv[1]; } if (argc > 2) { create_options.v.v0.user = argv[2]; create_options.v.v0.bucket = argv[2]; } if (argc > 3) { create_options.v.v0.passwd = argv[3]; } if (argc > 4) { method = argv[4]; } if (argc > 5) { key = argv[5]; } int count = 1; if (argc > 6) { count = atoi(argv[6]); } err = lcb_create(&instance, &create_options); if (err != LCB_SUCCESS) { fprintf(stderr, "Failed to create libcouchbase instance: %s\n", lcb_strerror(NULL, err)); return 1; } (void)lcb_set_error_callback(instance, error_callback); /* Initiate the connect sequence in libcouchbase */ if ((err = lcb_connect(instance)) != LCB_SUCCESS) { fprintf(stderr, "Failed to initiate connect: %s\n", lcb_strerror(NULL, err)); lcb_destroy(instance); return 1; } (void)lcb_set_get_callback(instance, get_callback); (void)lcb_set_store_callback(instance, store_callback); /* Run the event loop and wait until we've connected */ lcb_wait(instance); if (method == NULL || strcmp(method, "set") == 0) { err = form_command(err, instance, key, count); if (err != LCB_SUCCESS) { fprintf(stderr, "Failed to store: %s\n", lcb_strerror(NULL, err)); return 1; } form_command(err, instance, "fra", 1); } lcb_wait(instance); lcb_wait(instance); if (method == NULL || strcmp(method, "fset") == 0) { err = form_command_fset(err, instance, "fra"); if (err != LCB_SUCCESS) { fprintf(stderr, "Failed to fragment store: %s\n", lcb_strerror(NULL, err)); return 1; } } if (method == NULL || strcmp(method, "get") == 0) { err = form_command_get(count, err, instance, key); if (err != LCB_SUCCESS) { fprintf(stderr, "Failed to get: %s\n", lcb_strerror(NULL, err)); return 1; } } lcb_wait(instance); lcb_destroy(instance); return 0; }
int main(void) { // initializing 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.110.101,10.141.110.102,10.141.110.103/testbucket"; 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/java-2.2/env-config.html //Enable details error codes: //http://developer.couchbase.com/documentation/server/current/sdks/c-2.4/options.html 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); } sleep(20); // installing callbacks lcb_set_store_callback(instance, storage_callback); lcb_set_get_callback(instance, get_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=0; 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, &scmdlist); if (err != LCB_SUCCESS) { printf("Couldn't schedule storage operation!\n"); exit(1); } lcb_wait(instance); //storage_callback is invoked here } /* GET section */ lcb_get_cmd_t gcmd = { 0 }; const lcb_get_cmd_t *gcmdlist = &gcmd; for( int counter=0; counter <NUMBER_OF_DOCUMENTS; counter ++){ char key[12]; // sleep(3); sprintf(key,"%s%d", "test", counter); gcmd.v.v0.key = key; gcmd.v.v0.nkey = strlen(key); fprintf(stderr, "GET : %s\n", key); // printf("GET %s\n", key); err = lcb_get(instance, NULL, 1, &gcmdlist); lcb_wait(instance); // get_callback is invoked here } //closing the connection lcb_destroy(instance); return 0; }
// Main int main(int argc, char* argv[]) { printf("Welcome.\n"); // Do this once srand(time(NULL)); // initializing struct lcb_create_st cropts = { 0 }; cropts.version = 3; cropts.v.v3.connstr = "couchbase://localhost/default"; // cropts.v.v3.connstr = "couchbase://192.168.38.101/default"; lcb_error_t create_err; lcb_t instance; create_err = lcb_create(&instance, &cropts); if (create_err != LCB_SUCCESS) { printf("Couldn't create instance!\n"); exit(1); } // connecting lcb_connect(instance); lcb_wait(instance); lcb_error_t bootstrap_status_err; if ( (bootstrap_status_err = lcb_get_bootstrap_status(instance)) != LCB_SUCCESS ) { printf("Couldn't bootstrap!\n"); exit(1); } // installing callbacks lcb_set_store_callback(instance, storage_callback); lcb_set_get_callback(instance, get_callback); lcb_set_arithmetic_callback(instance, arithmetic_callback); lcb_set_touch_callback(instance, touch_callback); lcb_set_remove_callback(instance, remove_callback); // Main part of the program int numTimesToRun = 10; // specify how many times to iterate int useARandomKey = 1; // specify whether to use a random or sequential key char keyNameBuffer[100]; int i = 0; for (i = 0; i < numTimesToRun; i++) { if (useARandomKey == 0) { // Do NOT use a random key sprintf(keyNameBuffer, "NonRandomKey%d", i); } else { // Use a random key, generate a new one for each iteration of the loop int randomNumber = rand(); sprintf(keyNameBuffer, "randomKey%d", randomNumber); } printf("#### Iteration: %5d Key: %10s\n", i, keyNameBuffer); // Create-Increment blockingArithmeticIncrement2(instance, keyNameBuffer); // Make sure its there blockingGet(instance, keyNameBuffer); // Delete blockingRemove(instance, keyNameBuffer); // Sleep 60 printf("About to sleep...\n"); sleep(1); printf("Done with sleep...\n"); // Touch, should fail blockingTouch2(instance, 60, keyNameBuffer); // Increment blockingArithmeticIncrement2(instance, keyNameBuffer); // Touch blockingTouch2(instance, 60, keyNameBuffer); printf("##### touches that worked: %d touches that failed %d\n", touchesWorked, touchesFailed); } // loop for numTimesToRun printf("Almost done. Calling lcb_destroy()\n"); lcb_destroy(instance); printf("Goodbye\n"); return 0; }
static void test_touch1(void) { lcb_error_t err; struct rvbuf rv; char *key = "fooX", *val = "bar"; lcb_size_t nkey = strlen(key), nval = strlen(val); char **keys; lcb_size_t *nkeys, ii; lcb_time_t *ttls; lcb_store_cmd_t storecmd; const lcb_store_cmd_t *storecmds[] = { &storecmd }; lcb_touch_cmd_t *touchcmds[26]; (void)lcb_set_store_callback(session, store_callback); (void)lcb_set_touch_callback(session, touch_callback); keys = malloc(26 * sizeof(char *)); nkeys = malloc(26 * sizeof(lcb_size_t)); ttls = malloc(26 * sizeof(lcb_time_t)); if (keys == NULL || nkeys == NULL || ttls == NULL) { err_exit("Failed to allocate memory for keys"); } for (ii = 0; ii < 26; ii++) { nkeys[ii] = nkey; keys[ii] = strdup(key); ttls[ii] = 1; if (keys[ii] == NULL) { err_exit("Failed to allocate memory for key"); } keys[ii][3] = (char)(ii + 'a'); memset(&storecmd, 0, sizeof(storecmd)); storecmd.v.v0.key = keys[ii]; storecmd.v.v0.nkey = nkey; storecmd.v.v0.bytes = val; storecmd.v.v0.nbytes = nval; storecmd.v.v0.operation = LCB_SET; err = lcb_store(session, &rv, 1, storecmds); lcb_assert(err == LCB_SUCCESS); io->v.v0.run_event_loop(io); lcb_assert(rv.error == LCB_SUCCESS); memset(&rv, 0, sizeof(rv)); touchcmds[ii] = calloc(1, sizeof(lcb_touch_cmd_t)); if (touchcmds[ii] == NULL) { err_exit("Failed to allocate memory for touch command"); } touchcmds[ii]->v.v0.key = keys[ii]; touchcmds[ii]->v.v0.nkey = nkey; } rv.counter = 26; err = lcb_touch(session, &rv, 26, (const lcb_touch_cmd_t * const *)touchcmds); lcb_assert(err == LCB_SUCCESS); io->v.v0.run_event_loop(io); lcb_assert(rv.error == LCB_SUCCESS); for (ii = 0; ii < 26; ii++) { free(keys[ii]); free(touchcmds[ii]); } free(keys); free(ttls); free(nkeys); }
ERL_NIF_TERM cb_connect(ErlNifEnv* env, handle_t* handle, void* obj) { connect_args_t* args = (connect_args_t*)obj; lcb_error_t err; struct lcb_create_st create_options; struct lcb_create_io_ops_st io_opts; io_opts.version = 0; io_opts.v.v0.type = LCB_IO_OPS_DEFAULT; io_opts.v.v0.cookie = NULL; memset(&create_options, 0, sizeof(create_options)); err = lcb_create_io_ops(&create_options.v.v0.io, &io_opts); if (err != LCB_SUCCESS) { printf("failed create io ops\n"); fprintf(stderr, "Failed to create IO instance: %s\n", lcb_strerror(NULL, err)); return return_lcb_error(env, err); } create_options.v.v0.host = args->host; create_options.v.v0.user = args->user; create_options.v.v0.bucket = args->bucket; create_options.v.v0.passwd = args->pass; err = lcb_create(&(handle->instance), &create_options); free(args->host); free(args->user); free(args->pass); free(args->bucket); if (err != LCB_SUCCESS) { return enif_make_tuple2(env, enif_make_atom(env, "error"), enif_make_string(env, lcb_strerror(NULL, err), ERL_NIF_LATIN1)); } (void)lcb_set_get_callback(handle->instance, get_callback); (void)lcb_set_store_callback(handle->instance, store_callback); (void)lcb_set_unlock_callback(handle->instance, unlock_callback); (void)lcb_set_touch_callback(handle->instance, touch_callback); (void)lcb_set_arithmetic_callback(handle->instance, arithmetic_callback); (void)lcb_set_remove_callback(handle->instance, remove_callback); (void)lcb_set_http_complete_callback(handle->instance, http_callback); err = lcb_connect(handle->instance); if (err != LCB_SUCCESS) { return return_lcb_error(env, err); } err = lcb_wait(handle->instance); if(err != LCB_SUCCESS) { return return_lcb_error(env, err); } #ifdef LCB_CNTL_DETAILED_ERRCODES int val = 1; err = lcb_cntl(handle->instance, LCB_CNTL_SET, LCB_CNTL_DETAILED_ERRCODES, &val); if(err != LCB_SUCCESS) { return return_lcb_error(env, err); } #endif return enif_make_atom(env, "ok"); }
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; }
/** * Program entry point. * * "monitor" a directory and upload all of the JSON files in that * directory to a couchbase server. * * @param argc argument count * @param argv argument vector */ int main(int argc, char **argv) { const char *spool = default_spool; const char *host = NULL; const char *user = NULL; const char *passwd = NULL; const char *bucket = NULL; int sleep_time = 5; lcb_error_t ret; int cmd; struct option opts[7]; struct lcb_create_st create_options; memset(opts, 0, sizeof(opts)); setup_options(opts); /* Parse command line arguments */ while ((cmd = getopt_long(argc, argv, "s:" /* spool directory */ "h:" /* host */ "u:" /* user */ "p:" /* password */ "b:" /* bucket */ "t:", /* Sleep time */ opts, NULL)) != -1) { switch (cmd) { case 's' : spool = optarg; break; case 'h' : host = optarg; break; case 'u' : user = optarg; break; case 'p' : passwd = optarg; break; case 'b' : bucket = optarg; break; case 't' : sleep_time = atoi(optarg); break; default: fprintf(stderr, "Usage: vacuum [options]\n" "\t-h host\tHostname to connect to (default: localhost:8091)\n" "\t-u user\tUsername to log in with (default: none)\n" "\t-p passwd\tPassword to log in with (default: none)\n" "\t-b name\tName of bucket to connect to (default: default)\n" "\t-v\tEnable verbosity\n" "\t-t sec\tNumber of seconds between each scan (default: 5)\n" "\t-s dir\tLocation of spool directory (default: %s)\n", default_spool); exit(EXIT_FAILURE); } } /* Change working directory to the spool directory */ if (chdir(spool) != 0) { fprintf(stderr, "Failed to enter directory %s: %s\n", spool, strerror(errno)); exit(EXIT_FAILURE); } memset(&create_options, 0, sizeof(create_options)); create_options.v.v0.host = host; create_options.v.v0.user = user; create_options.v.v0.passwd = passwd; create_options.v.v0.bucket = bucket; /* Create the instance to lcb */ ret = lcb_create(&instance, &create_options); if (ret != LCB_SUCCESS) { fprintf(stderr, "Failed to create couchbase instance\n"); exit(EXIT_FAILURE); } /* Set up the callbacks we want */ (void)lcb_set_store_callback(instance, store_callback); (void)lcb_set_error_callback(instance, error_callback); /* Tell libcouchback to connect to the server */ ret = lcb_connect(instance); if (ret != LCB_SUCCESS) { fprintf(stderr, "Failed to connect: %s\n", lcb_strerror(instance, ret)); exit(EXIT_FAILURE); } /* Wait for the server to complete */ lcb_wait(instance); if ((ret = lcb_enable_timings(instance) != LCB_SUCCESS)) { fprintf(stderr, "Failed to enable timings: %s\n", lcb_strerror(instance, ret)); } /* Loop forever and process the spool directory */ while (1) { int ii; int nsec = sleep_time; process_directory(); do { fprintf(stdout, "\rsleeping %d secs before retry..", nsec); fflush(stdout); sleep(1); --nsec; } while (nsec > 0); fprintf(stdout, "\r"); for (ii = 0; ii < 70; ++ii) { fprintf(stdout, " "); } fprintf(stdout, "\r"); fflush(stdout); } return 0; }
couchbase_con* couchbase_new_connection(struct cachedb_id* id) { couchbase_con *con; struct lcb_create_st options; lcb_t instance; lcb_error_t rc; last_error = LCB_SUCCESS; if (id == NULL) { LM_ERR("null cachedb_id\n"); return 0; } con = pkg_malloc(sizeof(couchbase_con)); if (con == NULL) { LM_ERR("no more pkg \n"); return 0; } memset(con,0,sizeof(couchbase_con)); con->id = id; con->ref = 1; /* TODO - support custom ports - couchbase expects host:port in id->host */ memset(&options,0,sizeof(struct lcb_create_st)); options.version = 0; options.v.v0.host = id->host; options.v.v0.user = id->username; options.v.v0.passwd = id->password; options.v.v0.bucket = id->database; rc=lcb_create(&instance, &options); if (rc!=LCB_SUCCESS) { LM_ERR("Failed to create libcouchbase instance: 0x%02x, %s\n", rc, lcb_strerror(NULL, rc)); return 0; } (void)lcb_set_error_callback(instance, couchbase_error_cb); (void)lcb_set_get_callback(instance, couchbase_get_cb); (void)lcb_set_store_callback(instance, couchbase_store_cb); (void)lcb_set_remove_callback(instance, couchbase_remove_cb); (void)lcb_set_arithmetic_callback(instance,couchbase_arithmetic_cb); (void)lcb_set_timeout(instance,couch_timeout_usec); rc=lcb_connect(instance); if (rc != LCB_SUCCESS) { LM_ERR("Failed to connect to the Couchbase. Host: %s Bucket: %s Error: %s\n", id->host, id->database, lcb_strerror(instance, rc)); lcb_destroy(instance); return 0; } /* Wait for the connect to complete */ lcb_wait(instance); /*Check connection*/ if (last_error != LCB_SUCCESS) { /*Consider these connect failurs as fatal*/ if(last_error == LCB_AUTH_ERROR || last_error == LCB_INVALID_HOST_FORMAT || last_error == LCB_INVALID_CHAR) { LM_ERR("Fatal connection error to Couchbase. Host: %s Bucket: %s Error: %s", id->host, id->database, lcb_strerror(instance, last_error)); lcb_destroy(instance); return 0; } else { /* Non-fatal errors, we may be able to connect later */ LM_ERR("Non-Fatal connection error to Couchbase. Host: %s Bucket: %s Error: %s", id->host, id->database, lcb_strerror(instance, last_error)); } } else { LM_DBG("Succesfully connected to Couchbase Server. Host: %s Bucket: %s\n", id->host, id->database); } con->couchcon = instance; return con; }
int main(int argc, char*argv[]) { //couchbase error type lcb_error_t err; //couchbase type lcb_t instance; // couchbase create struct? its a struct with options to create what? a cb cluster, bucket or something else? struct lcb_create_st create_options = {0}; memset(&create_options,0,sizeof(create_options)); // set command type, and creating a list of store commands? lcb_store_cmd_t scmd = {0}; const lcb_store_cmd_t *scmdlist[1]; // get command type, and list of get commands! lcb_get_cmd_t gcmd = {0}; const lcb_get_cmd_t *gcmdlist[1]; create_options.version = 3; // cb version 3? if (argc < 2) { fprintf(stderr, "Usage: %s couchbase://host/bucket [password] \n",argv[0] ); exit(EXIT_FAILURE); } create_options.v.v3.connstr = argv[1]; if (argc >=3) { create_options.v.v3.passwd = argv[2]; } //fprintf(stderr, "\n %s,%s,%s\n",create_options.v.v3.connstr,create_options.v.v3.passwd,"test" ); /* create_options.version = 3; create_options.v.v3.connstr = "couchbase://192.168.56.101/pramod"; create_options.v.v3.passwd = "couchbase"; */ err = lcb_create(&instance, &create_options); if(err != LCB_SUCCESS) { die(instance, "Couldn't create couchbase handle", err); } err = lcb_connect(instance); if(err != LCB_SUCCESS) { die(instance, "Couldn't schedule connection", err); } lcb_wait(instance); // It says this will be blocking wait and won't execute in a async fashion err = lcb_get_bootstrap_status(instance); if (err != LCB_SUCCESS) { die(instance, "Couldn't bootstrap from cluster", err); } // Assign the handlers to be called for the operations types lcb_set_get_callback(instance, get_callback); lcb_set_store_callback(instance, store_callback); //what are these callbacks for ? I thought callbacks are for getting results of asynchronous exectution of jobs? // what is happening in here? setting values? to the source command ? //let my try to wrap this up in a loop to set a 1000 values? scmd.v.v0.operation = LCB_SET; scmd.v.v0.key = "foo"; scmd.v.v0.nkey = 3 ; scmd.v.v0.bytes = "barbeque"; scmd.v.v0.nbytes = 8; scmdlist[0] = &scmd; err = lcb_store(instance, NULL, 1, scmdlist); // so here is where the data is getting written!! if (err != LCB_SUCCESS) { die (instance, "Coudn't schedule storage operations", err); } // the store_callback is involed from lcb_wait() fprintf(stderr, "Will wait for storage operation to complete\n"); lcb_wait(instance); //now fetch the items back gcmd.v.v0.key = "foo"; gcmd.v.v0.nkey = 3; gcmd.v.v0.exptime = 60; gcmdlist[0] = &gcmd; err = lcb_get(instance, NULL, 1, gcmdlist); if (err != LCB_SUCCESS) { die(instance, "Couldn't schedule retrieval operation", err); } // get callback invoked from here. fprintf(stderr, "Will wait to retrieve the set item!\n"); lcb_wait(instance); // close the connection handle after everything is done lcb_destroy(instance); return 0; }
static ngx_int_t ngx_lcb_init_process(ngx_cycle_t *cycle) { ngx_lcb_main_conf_t *cmcf; struct lcb_create_io_ops_st options; lcb_error_t err; ngx_int_t rc; ngx_uint_t i; ngx_lcb_connection_t *conn; ngx_lcb_loc_conf_t **ccfp; /* initialize libcouchbase IO plugin */ memset(&options, 0, sizeof(options)); options.version = 2; options.v.v2.create = ngx_lcb_create_io_opts; options.v.v2.cookie = &lcb_cookie; err = lcb_create_io_ops(&lcb_cookie.io, &options); if (err != LCB_SUCCESS) { ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, "couchbase: failed to create IO object for libcouchbase: 0x%02xd \"%s\"", err, lcb_strerror(NULL, err)); return NGX_ERROR; } lcb_cookie.log = cycle->log; lcb_cookie.pool = cycle->pool; /* materialize upstream connections */ rc = ngx_array_init(&lcb_connections, cycle->pool, 4, sizeof(ngx_lcb_connection_t)); if (rc != NGX_OK) { return NGX_ERROR; } cmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_couchbase_module); ccfp = cmcf->connection_confs.elts; for (i = 0; i < cmcf->connection_confs.nelts; i++) { struct lcb_create_st opts = ccfp[i]->options; conn = ngx_array_push(&lcb_connections); if (conn == NULL) { return NGX_ERROR; } conn->log = cycle->log; conn->name = ccfp[i]->name; rc = ngx_array_init(&conn->backlog, cycle->pool, 4, sizeof(ngx_http_request_t *)); if (rc != NGX_OK) { return NGX_ERROR; } opts.v.v0.io = lcb_cookie.io; err = lcb_create(&conn->lcb, &opts); if (err != LCB_SUCCESS) { ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, "couchbase: failed to create libcouchbase instance: 0x%02xd \"%s\"", err, lcb_strerror(NULL, err)); return NGX_ERROR; } (void)lcb_set_error_callback(conn->lcb, ngx_lcb_error_callback); (void)lcb_set_timeout(conn->lcb, ccfp[i]->connect_timeout * 1000); /* in usec */ (void)lcb_set_get_callback(conn->lcb, ngx_lcb_get_callback); (void)lcb_set_store_callback(conn->lcb, ngx_lcb_store_callback); (void)lcb_set_remove_callback(conn->lcb, ngx_lcb_remove_callback); (void)lcb_set_configuration_callback(conn->lcb, ngx_lcb_configuration_callback); (void)lcb_set_cookie(conn->lcb, conn); ngx_log_debug7(NGX_LOG_DEBUG_HTTP, cycle->log, 0, "couchbase(%p): configured connection \"%V\": connect_timeout:%Mms " "address:%s bucket:%s user:%s password:%s", conn->lcb, &conn->name, ccfp[i]->connect_timeout, opts.v.v0.host ? opts.v.v0.host : "(null)", opts.v.v0.bucket ? opts.v.v0.bucket : "(null)", opts.v.v0.user ? opts.v.v0.user : "******", opts.v.v0.passwd ? opts.v.v0.passwd : "(null)"); } return NGX_OK; }
int main(int argc, char *argv[]) { lcb_error_t err; lcb_t instance; struct lcb_create_st create_options; struct lcb_create_io_ops_st io_opts; io_opts.version = 0; io_opts.v.v0.type = LCB_IO_OPS_DEFAULT; io_opts.v.v0.cookie = NULL; memset(&create_options, 0, sizeof(create_options)); err = lcb_create_io_ops(&create_options.v.v0.io, &io_opts); if (err != LCB_SUCCESS) { fprintf(stderr, "Failed to create IO instance: %s\n", lcb_strerror(NULL, err)); return 1; } if (argc > 1) { create_options.v.v0.host = argv[1]; } if (argc > 2) { create_options.v.v0.user = argv[2]; create_options.v.v0.bucket = argv[2]; } if (argc > 3) { create_options.v.v0.passwd = argv[3]; } err = lcb_create(&instance, &create_options); if (err != LCB_SUCCESS) { fprintf(stderr, "Failed to create libcouchbase instance: %s\n", lcb_strerror(NULL, err)); return 1; } (void)lcb_set_error_callback(instance, error_callback); /* Initiate the connect sequence in libcouchbase */ if ((err = lcb_connect(instance)) != LCB_SUCCESS) { fprintf(stderr, "Failed to initiate connect: %s\n", lcb_strerror(NULL, err)); lcb_destroy(instance); return 1; } (void)lcb_set_get_callback(instance, get_callback); (void)lcb_set_store_callback(instance, store_callback); /* Run the event loop and wait until we've connected */ lcb_wait(instance); { lcb_store_cmd_t cmd; const lcb_store_cmd_t *commands[1]; commands[0] = &cmd; memset(&cmd, 0, sizeof(cmd)); cmd.v.v0.operation = LCB_SET; cmd.v.v0.key = "foo"; cmd.v.v0.nkey = 3; cmd.v.v0.bytes = "bar"; cmd.v.v0.nbytes = 3; err = lcb_store(instance, NULL, 1, commands); if (err != LCB_SUCCESS) { fprintf(stderr, "Failed to store: %s\n", lcb_strerror(NULL, err)); return 1; } } lcb_wait(instance); { lcb_get_cmd_t cmd; const lcb_get_cmd_t *commands[1]; commands[0] = &cmd; memset(&cmd, 0, sizeof(cmd)); cmd.v.v0.key = "foo"; cmd.v.v0.nkey = 3; err = lcb_get(instance, NULL, 1, commands); if (err != LCB_SUCCESS) { fprintf(stderr, "Failed to get: %s\n", lcb_strerror(NULL, err)); return 1; } } lcb_wait(instance); lcb_destroy(instance); return 0; }