Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

RCBC-31 Make Bucket#get more consistent

Now it take into account the type of argument(s) it received:

  get(["foo"])  #=> ["bar"]
  get("foo")    #=> "bar"
  get(["x"], :extended => true) #=> {"x"=>["xval", 0, 18336939621176836096]}

Change-Id: Ic16463793dfa805e0af278be6e6eab95e6465834
Reviewed-on: http://review.couchbase.org/14759
Tested-by: Sergey Avseyev <sergey.avseyev@gmail.com>
Reviewed-by: Chris Anderson <jchris@couchbase.com>
  • Loading branch information...
commit 538aba7534e2255c295565242d1072a0a45482ee 1 parent 569b5ca
Sergey Avseyev avsej authored jchris committed

Showing 2 changed files with 28 additions and 6 deletions. Show diff stats Hide diff stats

  1. +10 6 ext/couchbase_ext/couchbase_ext.c
  2. +18 0 test/test_get.rb
16 ext/couchbase_ext/couchbase_ext.c
@@ -102,6 +102,7 @@ struct key_traits
102 102 int explicit_ttl;
103 103 int quiet;
104 104 int mgat;
  105 + int is_array;
105 106 VALUE force_format;
106 107 };
107 108
@@ -622,9 +623,12 @@ cb_args_scan_keys(long argc, VALUE argv, bucket_t *bucket, struct key_traits *tr
622 623 traits->explicit_ttl = 1;
623 624 exp = NUM2ULONG(ttl);
624 625 }
  626 + }
  627 + nn = RARRAY_LEN(argv);
  628 + if (nn == 1 && TYPE(RARRAY_PTR(argv)[0]) == T_ARRAY) {
  629 + argv = RARRAY_PTR(argv)[0];
625 630 nn = RARRAY_LEN(argv);
626   - } else {
627   - nn = argc;
  631 + traits->is_array = 1;
628 632 }
629 633 if (nn < 1) {
630 634 rb_raise(rb_eArgError, "must be at least one key");
@@ -2472,7 +2476,7 @@ cb_bucket_get(int argc, VALUE *argv, VALUE self)
2472 2476 long nn;
2473 2477 libcouchbase_error_t err;
2474 2478 struct key_traits *traits;
2475   - int extended, mgat;
  2479 + int extended, mgat, is_array;
2476 2480 long seqno;
2477 2481
2478 2482 if (bucket->handle == NULL) {
@@ -2482,7 +2486,6 @@ cb_bucket_get(int argc, VALUE *argv, VALUE self)
2482 2486 if (!bucket->async && proc != Qnil) {
2483 2487 rb_raise(rb_eArgError, "synchronous mode doesn't support callbacks");
2484 2488 }
2485   - rb_funcall(args, id_flatten_bang, 0);
2486 2489 traits = calloc(1, sizeof(struct key_traits));
2487 2490 nn = cb_args_scan_keys(RARRAY_LEN(args), args, bucket, traits);
2488 2491 ctx = calloc(1, sizeof(context_t));
@@ -2491,6 +2494,7 @@ cb_bucket_get(int argc, VALUE *argv, VALUE self)
2491 2494 }
2492 2495 mgat = traits->mgat;
2493 2496 keys = traits->keys_ary;
  2497 + is_array = traits->is_array;
2494 2498 ctx->proc = proc;
2495 2499 rb_hash_aset(object_space, ctx->proc|1, ctx->proc);
2496 2500 ctx->bucket = bucket;
@@ -2530,10 +2534,10 @@ cb_bucket_get(int argc, VALUE *argv, VALUE self)
2530 2534 if (bucket->exception != Qnil) {
2531 2535 rb_exc_raise(bucket->exception);
2532 2536 }
2533   - if (mgat || (extended && nn > 1)) {
  2537 + if (mgat || (extended && (nn > 1 || is_array))) {
2534 2538 return rv; /* return as a hash {key => [value, flags, cas], ...} */
2535 2539 }
2536   - if (nn > 1) {
  2540 + if (nn > 1 || is_array) {
2537 2541 long ii;
2538 2542 VALUE *keys_ptr, ret;
2539 2543 ret = rb_ary_new();
18 test/test_get.rb
@@ -331,4 +331,22 @@ def test_format_forcing
331 331 end
332 332 end
333 333
  334 + # http://www.couchbase.com/issues/browse/RCBC-31
  335 + def test_consistent_behaviour_for_arrays
  336 + connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
  337 +
  338 + cas = connection.set(uniq_id("foo"), "foo")
  339 + connection.set(uniq_id("bar"), "bar")
  340 +
  341 + assert_equal "foo", connection.get(uniq_id("foo"))
  342 + assert_equal ["foo"], connection.get([uniq_id("foo")])
  343 + assert_equal ["foo", "bar"], connection.get([uniq_id("foo"), uniq_id("bar")])
  344 + assert_equal ["foo", "bar"], connection.get(uniq_id("foo"), uniq_id("bar"))
  345 + expected = {uniq_id("foo") => ["foo", 0x00, cas]}
  346 + assert_equal expected, connection.get([uniq_id("foo")], :extended => true)
  347 + assert_raises TypeError do
  348 + connection.get([uniq_id("foo"), uniq_id("bar")], [uniq_id("foo")])
  349 + end
  350 + end
  351 +
334 352 end

0 comments on commit 538aba7

Please sign in to comment.
Something went wrong with that request. Please try again.