Permalink
Browse files

simplify cb_params_build by including args into cb_params_st

Change-Id: Ic9d374f00263f1b26719cd9ed177e00c0b2c7937
Reviewed-on: http://review.couchbase.org/23464
Reviewed-by: Sergey Avseyev <sergey.avseyev@gmail.com>
Tested-by: Sergey Avseyev <sergey.avseyev@gmail.com>
  • Loading branch information...
funny-falcon authored and avsej committed Dec 20, 2012
1 parent 0e09bab commit 48ee01b4af358d89081e4435946a3a4af5308cfa
@@ -744,6 +744,7 @@ cb_params_destroy(struct cb_params_st *params)
{
rb_ary_clear(params->ensurance);
params->ensurance = Qfalse;
+ params->args = Qfalse;
switch (params->type) {
case cb_cmd_get:
_release_data_for(get);
@@ -776,22 +777,14 @@ cb_params_destroy(struct cb_params_st *params)
}
}
-struct build_params_st
-{
- struct cb_params_st *params;
- int argc;
- VALUE argv;
-};
-
static VALUE
do_params_build(VALUE ptr)
{
VALUE opts;
/* unpack arguments */
- struct build_params_st *p = (struct build_params_st *)ptr;
- struct cb_params_st *params = p->params;
- int argc = p->argc;
- VALUE argv = p->argv;
+ struct cb_params_st *params = (struct cb_params_st*)ptr;
+ int argc = RARRAY_LEN(params->args);
+ VALUE argv = params->args;
/* extract options */
if (argc > 1 && TYPE(RARRAY_PTR(argv)[argc-1]) == T_HASH) {
@@ -884,16 +877,12 @@ do_params_build(VALUE ptr)
}
void
-cb_params_build(struct cb_params_st *params, int argc, VALUE argv)
+cb_params_build(struct cb_params_st *params)
{
int fail = 0;
- struct build_params_st args;
params->ensurance = rb_ary_new();
- args.params = params;
- args.argc = argc;
- args.argv = argv;
- rb_protect(do_params_build, (VALUE)&args, &fail);
+ rb_protect(do_params_build, (VALUE)params, &fail);
if (fail) {
cb_params_destroy(params);
/* raise exception from protected block */
@@ -71,22 +71,22 @@ cb_bucket_arithmetic(int sign, int argc, VALUE *argv, VALUE self)
{
struct cb_bucket_st *bucket = DATA_PTR(self);
struct cb_context_st *ctx;
- VALUE args, rv, proc, exc;
+ VALUE rv, proc, exc;
lcb_error_t err;
struct cb_params_st params;
if (bucket->handle == NULL) {
rb_raise(cb_eConnectError, "closed connection");
}
- rb_scan_args(argc, argv, "0*&", &args, &proc);
+ memset(&params, 0, sizeof(struct cb_params_st));
+ rb_scan_args(argc, argv, "0*&", &params.args, &proc);
if (!bucket->async && proc != Qnil) {
rb_raise(rb_eArgError, "synchronous mode doesn't support callbacks");
}
- memset(&params, 0, sizeof(struct cb_params_st));
params.type = cb_cmd_arith;
params.bucket = bucket;
params.cmd.arith.sign = sign;
- cb_params_build(&params, RARRAY_LEN(args), args);
+ cb_params_build(&params);
ctx = cb_context_alloc_common(bucket, proc, params.cmd.arith.num);
err = lcb_arithmetic(bucket->handle, (const void *)ctx,
params.cmd.arith.num, params.cmd.arith.ptr);
@@ -549,10 +549,11 @@ struct cb_params_st
/* the approximate size of the data to be sent */
size_t npayload;
VALUE ensurance;
+ VALUE args;
};
void cb_params_destroy(struct cb_params_st *params);
-void cb_params_build(struct cb_params_st *params, int argc, VALUE argv);
+void cb_params_build(struct cb_params_st *params);
LIBCOUCHBASE_API
lcb_error_t cb_create_ruby_mt_io_opts(int version, lcb_io_opt_t *io, void *arg);
View
@@ -103,22 +103,22 @@ cb_bucket_delete(int argc, VALUE *argv, VALUE self)
struct cb_bucket_st *bucket = DATA_PTR(self);
struct cb_context_st *ctx;
VALUE rv, exc;
- VALUE args, proc;
+ VALUE proc;
lcb_error_t err;
struct cb_params_st params;
if (bucket->handle == NULL) {
rb_raise(cb_eConnectError, "closed connection");
}
- rb_scan_args(argc, argv, "0*&", &args, &proc);
+ memset(&params, 0, sizeof(struct cb_params_st));
+ rb_scan_args(argc, argv, "0*&", &params.args, &proc);
if (!bucket->async && proc != Qnil) {
rb_raise(rb_eArgError, "synchronous mode doesn't support callbacks");
}
- rb_funcall(args, cb_id_flatten_bang, 0);
- memset(&params, 0, sizeof(struct cb_params_st));
+ rb_funcall(params.args, cb_id_flatten_bang, 0);
params.type = cb_cmd_remove;
params.bucket = bucket;
- cb_params_build(&params, RARRAY_LEN(args), args);
+ cb_params_build(&params);
ctx = cb_context_alloc_common(bucket, proc, params.cmd.remove.num);
ctx->quiet = params.cmd.remove.quiet;
View
@@ -218,23 +218,23 @@ cb_bucket_get(int argc, VALUE *argv, VALUE self)
{
struct cb_bucket_st *bucket = DATA_PTR(self);
struct cb_context_st *ctx;
- VALUE args, rv, proc, exc;
+ VALUE rv, proc, exc;
size_t ii;
lcb_error_t err = LCB_SUCCESS;
struct cb_params_st params;
if (bucket->handle == NULL) {
rb_raise(cb_eConnectError, "closed connection");
}
- rb_scan_args(argc, argv, "0*&", &args, &proc);
+ memset(&params, 0, sizeof(struct cb_params_st));
+ rb_scan_args(argc, argv, "0*&", &params.args, &proc);
if (!bucket->async && proc != Qnil) {
rb_raise(rb_eArgError, "synchronous mode doesn't support callbacks");
}
- memset(&params, 0, sizeof(struct cb_params_st));
params.type = cb_cmd_get;
params.bucket = bucket;
params.cmd.get.keys_ary = rb_ary_new();
- cb_params_build(&params, RARRAY_LEN(args), args);
+ cb_params_build(&params);
ctx = cb_context_alloc_common(bucket, proc, params.cmd.get.num);
ctx->extended = params.cmd.get.extended;
ctx->quiet = params.cmd.get.quiet;
@@ -115,21 +115,21 @@ cb_bucket_observe(int argc, VALUE *argv, VALUE self)
{
struct cb_bucket_st *bucket = DATA_PTR(self);
struct cb_context_st *ctx;
- VALUE args, rv, proc, exc;
+ VALUE rv, proc, exc;
lcb_error_t err;
struct cb_params_st params;
if (bucket->handle == NULL) {
rb_raise(cb_eConnectError, "closed connection");
}
- rb_scan_args(argc, argv, "0*&", &args, &proc);
+ memset(&params, 0, sizeof(struct cb_params_st));
+ rb_scan_args(argc, argv, "0*&", &params.args, &proc);
if (!bucket->async && proc != Qnil) {
rb_raise(rb_eArgError, "synchronous mode doesn't support callbacks");
}
- memset(&params, 0, sizeof(struct cb_params_st));
params.type = cb_cmd_observe;
params.bucket = bucket;
- cb_params_build(&params, RARRAY_LEN(args), args);
+ cb_params_build(&params);
ctx = cb_context_alloc_common(bucket, proc, params.cmd.observe.num);
err = lcb_observe(bucket->handle, (const void *)ctx,
params.cmd.observe.num, params.cmd.observe.ptr);
@@ -109,21 +109,21 @@ cb_bucket_stats(int argc, VALUE *argv, VALUE self)
{
struct cb_bucket_st *bucket = DATA_PTR(self);
struct cb_context_st *ctx;
- VALUE rv, exc, args, proc;
+ VALUE rv, exc, proc;
lcb_error_t err;
struct cb_params_st params;
if (bucket->handle == NULL) {
rb_raise(cb_eConnectError, "closed connection");
}
- rb_scan_args(argc, argv, "0*&", &args, &proc);
+ memset(&params, 0, sizeof(struct cb_params_st));
+ rb_scan_args(argc, argv, "0*&", &params.args, &proc);
if (!bucket->async && proc != Qnil) {
rb_raise(rb_eArgError, "synchronous mode doesn't support callbacks");
}
- memset(&params, 0, sizeof(struct cb_params_st));
params.type = cb_cmd_stats;
params.bucket = bucket;
- cb_params_build(&params, RARRAY_LEN(args), args);
+ cb_params_build(&params);
ctx = cb_context_alloc_common(bucket, proc, params.cmd.stats.num);
err = lcb_server_stats(bucket->handle, (const void *)ctx,
params.cmd.stats.num, params.cmd.stats.ptr);
@@ -116,22 +116,22 @@ cb_bucket_store(lcb_storage_t cmd, int argc, VALUE *argv, VALUE self)
{
struct cb_bucket_st *bucket = DATA_PTR(self);
struct cb_context_st *ctx;
- VALUE args, rv, proc, exc, obs = Qnil;
+ VALUE rv, proc, exc, obs = Qnil;
lcb_error_t err;
struct cb_params_st params;
if (bucket->handle == NULL) {
rb_raise(cb_eConnectError, "closed connection");
}
- rb_scan_args(argc, argv, "0*&", &args, &proc);
+ memset(&params, 0, sizeof(struct cb_params_st));
+ rb_scan_args(argc, argv, "0*&", &params.args, &proc);
if (!bucket->async && proc != Qnil) {
rb_raise(rb_eArgError, "synchronous mode doesn't support callbacks");
}
- memset(&params, 0, sizeof(struct cb_params_st));
params.type = cb_cmd_store;
params.bucket = bucket;
params.cmd.store.operation = cmd;
- cb_params_build(&params, RARRAY_LEN(args), args);
+ cb_params_build(&params);
obs = params.cmd.store.observe;
ctx = cb_context_alloc(bucket);
if (!bucket->async) {
View
@@ -126,22 +126,22 @@ cb_bucket_touch(int argc, VALUE *argv, VALUE self)
{
struct cb_bucket_st *bucket = DATA_PTR(self);
struct cb_context_st *ctx;
- VALUE args, rv, proc, exc;
+ VALUE rv, proc, exc;
lcb_error_t err;
struct cb_params_st params;
if (bucket->handle == NULL) {
rb_raise(cb_eConnectError, "closed connection");
}
- rb_scan_args(argc, argv, "0*&", &args, &proc);
+ memset(&params, 0, sizeof(struct cb_params_st));
+ rb_scan_args(argc, argv, "0*&", &params.args, &proc);
if (!bucket->async && proc != Qnil) {
rb_raise(rb_eArgError, "synchronous mode doesn't support callbacks");
}
- rb_funcall(args, cb_id_flatten_bang, 0);
- memset(&params, 0, sizeof(struct cb_params_st));
+ rb_funcall(params.args, cb_id_flatten_bang, 0);
params.type = cb_cmd_touch;
params.bucket = bucket;
- cb_params_build(&params, RARRAY_LEN(args), args);
+ cb_params_build(&params);
ctx = cb_context_alloc_common(bucket, proc, params.cmd.touch.num);
ctx->quiet = params.cmd.touch.quiet;
err = lcb_touch(bucket->handle, (const void *)ctx,
View
@@ -116,22 +116,22 @@ cb_bucket_unlock(int argc, VALUE *argv, VALUE self)
{
struct cb_bucket_st *bucket = DATA_PTR(self);
struct cb_context_st *ctx;
- VALUE args, rv, proc, exc;
+ VALUE rv, proc, exc;
lcb_error_t err;
struct cb_params_st params;
if (bucket->handle == NULL) {
rb_raise(cb_eConnectError, "closed connection");
}
- rb_scan_args(argc, argv, "0*&", &args, &proc);
+ memset(&params, 0, sizeof(struct cb_params_st));
+ rb_scan_args(argc, argv, "0*&", &params.args, &proc);
if (!bucket->async && proc != Qnil) {
rb_raise(rb_eArgError, "synchronous mode doesn't support callbacks");
}
- rb_funcall(args, cb_id_flatten_bang, 0);
- memset(&params, 0, sizeof(struct cb_params_st));
+ rb_funcall(params.args, cb_id_flatten_bang, 0);
params.type = cb_cmd_unlock;
params.bucket = bucket;
- cb_params_build(&params, RARRAY_LEN(args), args);
+ cb_params_build(&params);
ctx = cb_context_alloc_common(bucket, proc, params.cmd.unlock.num);
ctx->quiet = params.cmd.unlock.quiet;
err = lcb_unlock(bucket->handle, (const void *)ctx,
@@ -90,21 +90,21 @@ cb_bucket_version(int argc, VALUE *argv, VALUE self)
{
struct cb_bucket_st *bucket = DATA_PTR(self);
struct cb_context_st *ctx;
- VALUE rv, exc, args, proc;
+ VALUE rv, exc, proc;
lcb_error_t err;
struct cb_params_st params;
if (bucket->handle == NULL) {
rb_raise(cb_eConnectError, "closed connection");
}
- rb_scan_args(argc, argv, "0*&", &args, &proc);
+ memset(&params, 0, sizeof(struct cb_params_st));
+ rb_scan_args(argc, argv, "0*&", &params.args, &proc);
if (!bucket->async && proc != Qnil) {
rb_raise(rb_eArgError, "synchronous mode doesn't support callbacks");
}
- memset(&params, 0, sizeof(struct cb_params_st));
params.type = cb_cmd_version;
params.bucket = bucket;
- cb_params_build(&params, RARRAY_LEN(args), args);
+ cb_params_build(&params);
ctx = cb_context_alloc_common(bucket, proc, params.cmd.version.num);
err = lcb_server_versions(bucket->handle, (const void *)ctx,
params.cmd.version.num, params.cmd.version.ptr);

0 comments on commit 48ee01b

Please sign in to comment.