Пример #1
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);
}
Пример #2
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);
}
Пример #3
0
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);
}
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;
}
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;
}
Пример #6
0
Файл: cb.c Проект: muut/cberl
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");
}
Пример #7
0
PHP_COUCHBASE_LOCAL
void php_couchbase_callbacks_remove_init(lcb_t handle)
{
    lcb_set_remove_callback(handle, php_couchbase_remove_callback);
}
// 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;
}