/*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 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); }
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++); } }
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; }
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); }
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; }
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[]) { 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; }
/** * 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; }
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_error_callback(handle->instance, error_callback); (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); } return enif_make_atom(env, "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; }