/*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 lcb_io_opt_t plcba_default_io_opts(void) { struct lcb_create_io_ops_st options = { 0 }; lcb_io_opt_t iops = NULL; options.v.v0.cookie = NULL; options.v.v0.type = LCB_IO_OPS_DEFAULT; lcb_create_io_ops(&iops, &options); return iops; }
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; }
lcb_io_opt_t pycbc_iops_new(pycbc_Connection *conn, PyObject *pyio) { lcb_io_opt_t ret = NULL; lcb_io_opt_t dfl = NULL; lcb_error_t err; struct lcb_create_io_ops_st options = { 0 }; pycbc_iops_t *pio; pio = calloc(1, sizeof(*pio)); ret = &pio->iops; pio->conn = conn; pio->pyio = pyio; Py_INCREF(pyio); /** * We create the select 'iops' handle and copy over its functionality * from there. Now that libcouchbase has the 'select' iops build in, we use * that instead. * * We discard the default iops loop data at the expense of leaking a * dlhandle. */ options.v.v0.type = LCB_IO_OPS_SELECT; err = lcb_create_io_ops(&dfl, &options); if (err != LCB_SUCCESS) { PYCBC_EXC_WRAP(PYCBC_EXC_LCBERR, err, "Couldn't create IOPS"); return NULL; } memcpy(&pio->iops, dfl, sizeof(*dfl)); /* hide the dlsym */ dfl->dlhandle = NULL; lcb_destroy_io_ops(dfl); dfl = NULL; ret->v.v0.create_event = create_event; ret->v.v0.create_timer = create_timer; ret->v.v0.destroy_event = destroy_event_common; ret->v.v0.destroy_timer = destroy_event_common; ret->v.v0.update_event = update_event; ret->v.v0.delete_event = delete_event; ret->v.v0.delete_timer = delete_timer; ret->v.v0.update_timer = update_timer; ret->v.v0.run_event_loop = run_event_loop; ret->v.v0.stop_event_loop = stop_event_loop; ret->destructor = iops_destructor; 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; }
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"); }
LIBCOUCHBASE_API lcb_error_t lcb_create(lcb_t *instance, const struct lcb_create_st *options) { const char *host = NULL; const char *user = NULL; const char *passwd = NULL; const char *bucket = NULL; struct lcb_io_opt_st *io = NULL; struct lcb_create_st options_container; struct lcb_create_st2 *e_options = &options_container.v.v2; lcb_type_t type = LCB_TYPE_BUCKET; lcb_t obj; lcb_error_t err; lcb_settings *settings; err = normalize_options(&options_container, options); if (err != LCB_SUCCESS) { return err; } host = get_nonempty_string(e_options->host); user = get_nonempty_string(e_options->user); passwd = get_nonempty_string(e_options->passwd); bucket = get_nonempty_string(e_options->bucket); io = e_options->io; type = e_options->type; if (type == LCB_TYPE_CLUSTER && user == NULL && passwd == NULL) { return LCB_EINVAL; } if (host == NULL) { host = "localhost"; } if (bucket == NULL) { bucket = "default"; } /* Do not allow people use Administrator account for data access */ if (type == LCB_TYPE_BUCKET && user && strcmp(user, bucket) != 0) { return LCB_INVALID_USERNAME; } if ((obj = calloc(1, sizeof(*obj))) == NULL) { return LCB_CLIENT_ENOMEM; } obj->type = type; obj->compat.type = (lcb_compat_t)0xdead; if (io == NULL) { lcb_io_opt_t ops; if ((err = lcb_create_io_ops(&ops, NULL)) != LCB_SUCCESS) { /* You can't initialize the library without a io-handler! */ free(obj); return err; } io = ops; io->v.v0.need_cleanup = 1; } settings = &obj->settings; settings->randomize_bootstrap_nodes = 1; settings->bummer = 0; settings->io = io; obj->syncmode = LCB_ASYNCHRONOUS; settings->ipv6 = LCB_IPV6_DISABLED; settings->operation_timeout = LCB_DEFAULT_TIMEOUT; settings->config_timeout = LCB_DEFAULT_CONFIGURATION_TIMEOUT; settings->config_node_timeout = LCB_DEFAULT_NODECONFIG_TIMEOUT; settings->views_timeout = LCB_DEFAULT_VIEW_TIMEOUT; settings->rbufsize = LCB_DEFAULT_RBUFSIZE; settings->wbufsize = LCB_DEFAULT_WBUFSIZE; settings->durability_timeout = LCB_DEFAULT_DURABILITY_TIMEOUT; settings->durability_interval = LCB_DEFAULT_DURABILITY_INTERVAL; settings->http_timeout = LCB_DEFAULT_HTTP_TIMEOUT; settings->weird_things_threshold = LCB_DEFAULT_CONFIG_ERRORS_THRESHOLD; settings->weird_things_delay = LCB_DEFAULT_CONFIG_ERRORS_DELAY; settings->max_redir = LCB_DEFAULT_CONFIG_MAXIMUM_REDIRECTS; settings->grace_next_cycle = LCB_DEFAULT_CLCONFIG_GRACE_CYCLE; settings->grace_next_provider = LCB_DEFAULT_CLCONFIG_GRACE_NEXT; settings->bc_http_stream_time = LCB_DEFAULT_BC_HTTP_DISCONNTMO; settings->bucket = strdup(bucket); settings->logger = lcb_init_console_logger(); settings->iid = lcb_instance_index++; if (user) { settings->username = strdup(user); } else { settings->username = strdup(settings->bucket); } if (passwd) { settings->password = strdup(passwd); } lcb_initialize_packet_handlers(obj); obj->memd_sockpool = connmgr_create(settings, io); obj->memd_sockpool->max_idle = 1; obj->memd_sockpool->idle_timeout = 10000000; obj->confmon = lcb_confmon_create(settings); obj->usernodes = hostlist_create(); /** We might want to sanitize this a bit more later on.. */ if (strstr(host, "://") != NULL && strstr(host, "http://") == NULL) { lcb_destroy(obj); return LCB_INVALID_HOST_FORMAT; } err = hostlist_add_string(obj->usernodes, host, -1, LCB_CONFIG_HTTP_PORT); if (err != LCB_SUCCESS) { lcb_destroy(obj); return err; } err = lcb_init_providers(obj, e_options); if (err != LCB_SUCCESS) { lcb_destroy(obj); return err; } lcb_initialize_packet_handlers(obj); obj->timers = hashset_create(); obj->http_requests = hashset_create(); obj->durability_polls = hashset_create(); /* No error has occurred yet. */ obj->last_error = LCB_SUCCESS; if ((obj->cmdht = lcb_hashtable_szt_new(32)) == NULL) { lcb_destroy(obj); return LCB_CLIENT_ENOMEM; } if (!ringbuffer_initialize(&obj->purged_buf, 4096)) { lcb_destroy(obj); return LCB_CLIENT_ENOMEM; } if (!ringbuffer_initialize(&obj->purged_cookies, 4096)) { lcb_destroy(obj); return LCB_CLIENT_ENOMEM; } *instance = obj; return LCB_SUCCESS; }
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; }