Exemplo n.º 1
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);
}
Exemplo n.º 2
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);
}
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;
}
Exemplo n.º 4
0
Arquivo: cb.c Projeto: 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");
}
Exemplo n.º 5
0
PHP_COUCHBASE_LOCAL
void php_couchbase_callbacks_arithmetic_init(lcb_t handle)
{
	php_ignore_value(
			lcb_set_arithmetic_callback(handle, php_couchbase_arithmetic_callback));
}
Exemplo n.º 6
0
PHP_COUCHBASE_LOCAL
void php_couchbase_callbacks_arithmetic_init(lcb_t handle)
{
	(void)lcb_set_arithmetic_callback(handle, php_couchbase_arithmetic_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;
}