/*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); }
static PyObject * Bucket__close(pycbc_Bucket *self) { lcb_error_t err; if (self->flags & PYCBC_CONN_F_CLOSED) { Py_RETURN_NONE; } self->flags |= PYCBC_CONN_F_CLOSED; lcb_destroy(self->instance); if (self->iopswrap) { Py_XDECREF(self->iopswrap); self->iopswrap = NULL; } err = lcb_create(&self->instance, NULL); pycbc_assert(err == LCB_SUCCESS); if (err != LCB_SUCCESS) { PYCBC_EXC_WRAP(PYCBC_EXC_LCBERR, err, "Internal error while closing object"); return NULL; } Py_RETURN_NONE; }
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; }
int main(void) { struct lcb_create_st create_options; lcb_t instance; lcb_error_t err; memset(&create_options, 0, sizeof(create_options)); create_options.v.v0.host = "10.128.34.251:8091"; create_options.v.v0.user = "******"; create_options.v.v0.passwd = "jiubugaosuni"; create_options.v.v0.bucket = "beer-sample"; 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; } /* Set up the handler to catch all errors! */ 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)); return 1; } /* Run the event loop and wait until we've connected */ lcb_wait(instance); /* * * Set up a callback for our get requests * */ lcb_set_get_callback(instance, get_callback); lcb_get_cmd_t cmd; const lcb_get_cmd_t * const commands[1] = { &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); exit(EXIT_SUCCESS); }
int main(void) { struct lcb_create_st create_options; lcb_t instance; lcb_error_t err; memset(&create_options, 0, sizeof(create_options)); create_options.v.v0.host = "ec2-204-236-172-232.us-west-1.compute.amazonaws.com:8091"; create_options.v.v0.user = "******"; create_options.v.v0.passwd = "password"; create_options.v.v0.bucket = "default"; 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; } else { fprintf(stdout, "Successful in creating libCouchbase instance: %s\n"); } if ((err = lcb_connect(instance)) != LCB_SUCCESS) { fprintf(stderr, "Failed to initiate connect: %s\n", lcb_strerror(NULL, err)); return 1; } else { fprintf(stdout, "Successful in initializing libCouchbase instance: %s\n"); } /* Run the event loop and wait until we've connected */ lcb_wait(instance); lcb_arithmetic_cmd_t arithmetic; memset(&arithmetic, 0, sizeof(arithmetic)); arithmetic.version = 0; arithmetic.v.v0.key = "036"; arithmetic.v.v0.nkey = sizeof(arithmetic.v.v0.key); arithmetic.v.v0.create = 1; arithmetic.v.v0.delta = -10; arithmetic.v.v0.initial = 036; const lcb_arithmetic_cmd_t* const commands[1] = { &arithmetic }; err = lcb_arithmetic(instance, NULL, 1, commands); if (err != LCB_SUCCESS) { fprintf(stderr, "Failed to incr %s\n", lcb_strerror(NULL, err)); return 1; } else { fprintf(stdout, "Successful in incrementingthe keys and Values: %s\n"); } lcb_wait(instance); lcb_destroy(instance); exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { lcb_error_t err; lcb_t instance; { struct lcb_create_st create_options = {}; create_options.version = 3; if (argc < 2) { fprintf(stderr, "Usage: %s couchbase://host/bucket [ password [ username ] ]\n", argv[0]); exit(EXIT_FAILURE); } create_options.v.v3.connstr = argv[1]; if (argc > 2) { create_options.v.v3.passwd = argv[2]; } if (argc > 3) { create_options.v.v3.username = argv[3]; } err = lcb_create(&instance, &create_options); if (err != LCB_SUCCESS) { die(NULL, "Couldn't create couchbase handle", err); } err = lcb_connect(instance); if (err != LCB_SUCCESS) { die(instance, "Couldn't schedule connection", err); } lcb_wait(instance); err = lcb_get_bootstrap_status(instance); if (err != LCB_SUCCESS) { die(instance, "Couldn't bootstrap from cluster", err); } lcb_install_callback3(instance, LCB_CALLBACK_GET, op_callback); } lcbcrypto_register(instance, "AES-256-HMAC-SHA256", osp_create()); get_encrypted(instance, "secret-1"); printf("\n"); get_encrypted(instance, "secret-2"); printf("\n"); get_encrypted(instance, "secret-3"); printf("\n"); get_encrypted(instance, "secret-4"); printf("\n"); get_encrypted(instance, "secret-5"); lcb_destroy(instance); return 0; }
static lcb_error_t test_connect(char **argv, const char *username, const char *password, const char *bucket) { const char *endpoint; lcb_error_t rc; struct lcb_create_st options; lcb_assert(session == NULL); lcb_assert(mock == NULL); lcb_assert(io == NULL); if (lcb_create_io_ops(&io, NULL) != LCB_SUCCESS) { fprintf(stderr, "Failed to create IO instance\n"); exit(1); } mock = start_test_server(argv); if (mock == NULL) { err_exit("Failed to start mock server"); } endpoint = get_mock_http_server(mock); memset(&options, 0, sizeof(options)); options.version = 2; options.v.v2.host = endpoint; options.v.v2.user = username; options.v.v2.passwd = password; options.v.v2.bucket = bucket; options.v.v2.io = io; options.v.v2.transports = enabled_transports; if (lcb_create(&session, &options) != LCB_SUCCESS) { err_exit("Failed to create libcouchbase session"); } (void)lcb_set_error_callback(session, error_callback2); if (lcb_connect(session) != LCB_SUCCESS) { err_exit("Failed to connect to server"); } lcb_wait(session); rc = global_error; lcb_destroy(session); lcb_destroy_io_ops(io); session = NULL; io = NULL; shutdown_mock_server(mock); mock = NULL; return rc; }
static void setup(char **argv, const char *username, const char *password, const char *bucket) { const char *endpoint; struct lcb_create_st options; lcb_assert(session == NULL); lcb_assert(mock == NULL); lcb_assert(io == NULL); if (lcb_create_io_ops(&io, NULL) != LCB_SUCCESS) { fprintf(stderr, "Failed to create IO instance\n"); exit(1); } mock = start_test_server(argv); if (mock == NULL) { err_exit("Failed to start mock server"); } endpoint = get_mock_http_server(mock); memset(&options, 0, sizeof(options)); if (!mock->is_mock) { username = mock->username; password = mock->password; bucket = mock->bucket; } options.version = 2; options.v.v2.host = endpoint; options.v.v2.user = username; options.v.v2.passwd = password; options.v.v2.bucket = bucket; options.v.v2.io = io; options.v.v2.transports = enabled_transports; if (lcb_create(&session, &options) != LCB_SUCCESS) { err_exit("Failed to create libcouchbase session"); } (void)lcb_set_error_callback(session, error_callback); if (lcb_connect(session) != LCB_SUCCESS) { err_exit("Failed to connect to server"); } lcb_wait(session); if (!mock->is_mock) { const char *const *servers; total_node_count = 0; servers = lcb_get_server_list(session); for (; *servers; servers++, total_node_count++); } }
int main(int argc, char *argv[]) { lcb_STATUS err; lcb_INSTANCE *instance; char *bucket = NULL; size_t ii; if (argc < 2) { printf("Usage: %s couchbase://host/beer-sample [ password [ username ] ]\n", argv[0]); exit(EXIT_FAILURE); } { struct lcb_create_st create_options = {0}; create_options.version = 3; create_options.v.v3.connstr = argv[1]; if (argc > 2) { create_options.v.v3.passwd = argv[2]; } if (argc > 3) { create_options.v.v3.username = argv[3]; } check(lcb_create(&instance, &create_options), "create couchbase handle"); check(lcb_connect(instance), "schedule connection"); lcb_wait(instance); check(lcb_get_bootstrap_status(instance), "bootstrap from cluster"); check(lcb_cntl(instance, LCB_CNTL_GET, LCB_CNTL_BUCKETNAME, &bucket), "get bucket name"); if (strcmp(bucket, "beer-sample") != 0) { fail("expected bucket to be \"beer-sample\""); } } { const char *stmt = "SELECT * FROM breweries LIMIT 2"; lcb_CMDANALYTICS *cmd; int idx = 0; lcb_cmdanalytics_create(&cmd); lcb_cmdanalytics_callback(cmd, row_callback); lcb_cmdanalytics_statement(cmd, stmt, strlen(stmt)); lcb_cmdanalytics_deferred(cmd, 1); check(lcb_analytics(instance, &idx, cmd), "schedule analytics query"); printf("----> \x1b[36m%s\x1b[0m\n", stmt); lcb_cmdanalytics_destroy(cmd); lcb_wait(instance); } /* Now that we're all done, close down the connection handle */ lcb_destroy(instance); return 0; }
/** 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; }
lcb_t AdFeedbackMgr::get_conn_from_pool() { lcb_t ret; { boost::unique_lock<boost::mutex> dmp_pool_lock_; if (!dmp_conn_pool_.empty()) { ret = dmp_conn_pool_.front(); dmp_conn_pool_.pop_front(); return ret; } } lcb_error_t err; struct lcb_create_st options; memset(&options, 0, sizeof(options)); options.v.v0.host = dmp_server_ip_.c_str(); options.v.v0.user = "******"; options.v.v0.passwd = ""; options.v.v0.bucket = "user_profile"; err = lcb_create(&ret, &options); if (err != LCB_SUCCESS) { LOG(ERROR) << "failed to create couchbase connection." << lcb_strerror(NULL, err); return NULL; } lcb_set_error_callback(ret, err_callback); err = lcb_connect(ret); if (err != LCB_SUCCESS) { LOG(ERROR) << "failed to connect to couchbase." << lcb_strerror(NULL, err); lcb_destroy(ret); ret = NULL; return ret; } lcb_wait(ret); // usec for timeout. lcb_set_timeout(ret, 1000*100); lcb_set_get_callback(ret, get_callback); LOG(INFO) << "a new connection to DMP established"; return ret; }
/** 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; }
int main(int argc, char **argv) { lcb_t instance; struct lcb_create_st cropts; lcb_error_t rc; memset(&cropts, 0, sizeof cropts); cropts.version = 3; cropts.v.v3.connstr = "couchbases://10.0.0.31/default?certpath=/tmp/couchbase-ssl-certificate.pem"; rc = lcb_create(&instance, &cropts); if (rc != LCB_SUCCESS) { die(rc, "Creating instance"); } rc = lcb_connect(instance); if (rc != LCB_SUCCESS) { die(rc, "Connection scheduling"); } /* This function required to actually schedule the operations on the network */ lcb_wait(instance); /* Determines if the bootstrap/connection succeeded */ rc = lcb_get_bootstrap_status(instance); if (rc != LCB_SUCCESS) { die(rc, "Connection bootstraping"); } else { printf("Connection succeeded. Cluster has %d nodes\n", lcb_get_num_nodes(instance)); } /* SSL connections use different ports. For example, the REST API * connection will use port 18091 rather than 8091 when using SSL */ const char *node = lcb_get_node(instance, LCB_NODE_HTCONFIG, 0); printf("First node address for REST API: %s\n", node); lcb_destroy(instance); return 0; }
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; }
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; }
// 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; }
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; }
static int Connection__init__(pycbc_Connection *self, PyObject *args, PyObject *kwargs) { int rv; int conntype = LCB_TYPE_BUCKET; lcb_error_t err; char *conncache = NULL; PyObject *unlock_gil_O = NULL; PyObject *iops_O = NULL; PyObject *timeout = NULL; PyObject *dfl_fmt = NULL; PyObject *tc = NULL; struct lcb_create_st create_opts = { 0 }; struct lcb_cached_config_st cached_config = { { 0 } }; /** * This xmacro enumerates the constructor keywords, targets, and types. * This was converted into an xmacro to ease the process of adding or * removing various parameters. */ #define XCTOR_ARGS(X) \ X("_errors", &self->errors, "O") \ X("_flags", &self->flags, "I") \ X("bucket", &create_opts.v.v1.bucket, "z") \ X("username", &create_opts.v.v1.user, "z") \ X("password", &create_opts.v.v1.passwd, "z") \ X("host", &create_opts.v.v1.host, "z") \ X("conncache", &conncache, "z") \ X("quiet", &self->quiet, "I") \ X("unlock_gil", &unlock_gil_O, "O") \ X("transcoder", &tc, "O") \ X("timeout", &timeout, "O") \ X("default_format", &dfl_fmt, "O") \ X("lockmode", &self->lockmode, "i") \ X("_conntype", &conntype, "i") \ X("_iops", &iops_O, "O") static char *kwlist[] = { #define X(s, target, type) s, XCTOR_ARGS(X) #undef X NULL }; #define X(s, target, type) type static char *argspec = "|" XCTOR_ARGS(X); #undef X if (self->init_called) { PyErr_SetString(PyExc_RuntimeError, "__init__ was already called"); return -1; } self->init_called = 1; self->flags = 0; self->unlock_gil = 1; self->lockmode = PYCBC_LOCKMODE_EXC; #define X(s, target, type) target, rv = PyArg_ParseTupleAndKeywords(args, kwargs, argspec, kwlist, XCTOR_ARGS(X) NULL); #undef X if (!rv) { PYCBC_EXCTHROW_ARGS(); return -1; } if (unlock_gil_O && PyObject_IsTrue(unlock_gil_O) == 0) { self->unlock_gil = 0; } if (create_opts.v.v1.bucket) { self->bucket = pycbc_SimpleStringZ(create_opts.v.v1.bucket); } create_opts.version = 1; create_opts.v.v1.type = conntype; if (iops_O && iops_O != Py_None) { self->iops = pycbc_iops_new(self, iops_O); create_opts.v.v1.io = self->iops; self->unlock_gil = 0; } Py_INCREF(self->errors); if (dfl_fmt == Py_None || dfl_fmt == NULL) { /** Set to 0 if None or NULL */ dfl_fmt = pycbc_IntFromL(0); } else { Py_INCREF(dfl_fmt); /* later decref */ } rv = Connection_set_format(self, dfl_fmt, NULL); Py_XDECREF(dfl_fmt); if (rv == -1) { return rv; } /** Set the transcoder */ if (tc && Connection_set_transcoder(self, tc, NULL) == -1) { return -1; } #ifdef WITH_THREAD if (!self->unlock_gil) { self->lockmode = PYCBC_LOCKMODE_NONE; } if (self->lockmode != PYCBC_LOCKMODE_NONE) { self->lock = PyThread_allocate_lock(); } #endif if (conncache) { if (conntype != LCB_TYPE_BUCKET) { PYCBC_EXC_WRAP(PYCBC_EXC_ARGUMENTS, 0, "Cannot use connection cache with " "management connection"); return -1; } cached_config.cachefile = conncache; memcpy(&cached_config.createopt, &create_opts, sizeof(create_opts)); err = lcb_create_compat(LCB_CACHED_CONFIG, &cached_config, &self->instance, NULL); } else { err = lcb_create(&self->instance, &create_opts); } if (err != LCB_SUCCESS) { self->instance = NULL; PYCBC_EXC_WRAP(PYCBC_EXC_LCBERR, err, "Couldn't create instance. Either bad " "credentials/hosts/bucket names were " "passed, or there was an internal error in creating the " "object"); return -1; } pycbc_callbacks_init(self->instance); lcb_set_cookie(self->instance, self); if (timeout && timeout != Py_None) { if (Connection_set_timeout(self, timeout, NULL) == -1) { return -1; } } PYCBC_CONN_THR_BEGIN(self); err = lcb_connect(self->instance); PYCBC_CONN_THR_END(self); if (err != LCB_SUCCESS) { PYCBC_EXC_WRAP(PYCBC_EXC_LCBERR, err, "Couldn't schedule connection. This might be a result of " "an invalid hostname."); return -1; } err = pycbc_oputil_wait_common(self); if (err != LCB_SUCCESS) { PYCBC_EXCTHROW_WAIT(err); return -1; } return 0; }
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) { // 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; }
static int Bucket__init__(pycbc_Bucket *self, PyObject *args, PyObject *kwargs) { int rv; int conntype = LCB_TYPE_BUCKET; lcb_error_t err; PyObject *unlock_gil_O = NULL; PyObject *iops_O = NULL; PyObject *dfl_fmt = NULL; PyObject *tc = NULL; struct lcb_create_st create_opts = { 0 }; /** * This xmacro enumerates the constructor keywords, targets, and types. * This was converted into an xmacro to ease the process of adding or * removing various parameters. */ #define XCTOR_ARGS(X) \ X("connection_string", &create_opts.v.v3.connstr, "z") \ X("connstr", &create_opts.v.v3.connstr, "z") \ X("username", &create_opts.v.v3.username, "z") \ X("password", &create_opts.v.v3.passwd, "z") \ X("quiet", &self->quiet, "I") \ X("unlock_gil", &unlock_gil_O, "O") \ X("transcoder", &tc, "O") \ X("default_format", &dfl_fmt, "O") \ X("lockmode", &self->lockmode, "i") \ X("_flags", &self->flags, "I") \ X("_conntype", &conntype, "i") \ X("_iops", &iops_O, "O") static char *kwlist[] = { #define X(s, target, type) s, XCTOR_ARGS(X) #undef X NULL }; #define X(s, target, type) type static char *argspec = "|" XCTOR_ARGS(X); #undef X if (self->init_called) { PyErr_SetString(PyExc_RuntimeError, "__init__ was already called"); return -1; } self->init_called = 1; self->flags = 0; self->unlock_gil = 1; self->lockmode = PYCBC_LOCKMODE_EXC; #define X(s, target, type) target, rv = PyArg_ParseTupleAndKeywords(args, kwargs, argspec, kwlist, XCTOR_ARGS(X) NULL); #undef X if (!rv) { PYCBC_EXCTHROW_ARGS(); return -1; } if (unlock_gil_O && PyObject_IsTrue(unlock_gil_O) == 0) { self->unlock_gil = 0; } create_opts.version = 3; create_opts.v.v3.type = conntype; if (iops_O && iops_O != Py_None) { self->iopswrap = pycbc_iowrap_new(self, iops_O); create_opts.v.v3.io = pycbc_iowrap_getiops(self->iopswrap); self->unlock_gil = 0; } if (dfl_fmt == Py_None || dfl_fmt == NULL) { /** Set to 0 if None or NULL */ dfl_fmt = pycbc_IntFromL(PYCBC_FMT_JSON); } else { Py_INCREF(dfl_fmt); /* later decref */ } rv = Bucket_set_format(self, dfl_fmt, NULL); Py_XDECREF(dfl_fmt); if (rv == -1) { return rv; } /** Set the transcoder */ if (tc && Bucket_set_transcoder(self, tc, NULL) == -1) { return -1; } #if defined(WITH_THREAD) if (!self->unlock_gil) { self->lockmode = PYCBC_LOCKMODE_NONE; } if (self->lockmode != PYCBC_LOCKMODE_NONE) { self->lock = PyThread_allocate_lock(); } #else self->unlock_gil = 0; self->lockmode = PYCBC_LOCKMODE_NONE; #endif err = lcb_create(&self->instance, &create_opts); if (err != LCB_SUCCESS) { self->instance = NULL; PYCBC_EXC_WRAP(PYCBC_EXC_LCBERR, err, "Couldn't create instance. Either bad " "credentials/hosts/bucket names were " "passed, or there was an internal error in creating the " "object"); return -1; } if (pycbc_log_handler) { err = lcb_cntl(self->instance, LCB_CNTL_SET, LCB_CNTL_LOGGER, &pycbc_lcb_logprocs); if (err != LCB_SUCCESS) { self->instance = NULL; PYCBC_EXC_WRAP(PYCBC_EXC_LCBERR, err, "Couldn't create log handler"); return -1; } } pycbc_callbacks_init(self->instance); lcb_set_cookie(self->instance, self); { const char *bucketstr; err = lcb_cntl(self->instance, LCB_CNTL_GET, LCB_CNTL_BUCKETNAME, &bucketstr); if (err == LCB_SUCCESS && bucketstr != NULL) { self->bucket = pycbc_SimpleStringZ(bucketstr); } } 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; }
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; }
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); }
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; }