Permalink
Browse files

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...
1 parent 569b5ca commit 538aba7534e2255c295565242d1072a0a45482ee @avsej avsej committed with jchris Apr 11, 2012
Showing with 28 additions and 6 deletions.
  1. +10 −6 ext/couchbase_ext/couchbase_ext.c
  2. +18 −0 test/test_get.rb
View
16 ext/couchbase_ext/couchbase_ext.c
@@ -102,6 +102,7 @@ struct key_traits
int explicit_ttl;
int quiet;
int mgat;
+ int is_array;
VALUE force_format;
};
@@ -622,9 +623,12 @@ cb_args_scan_keys(long argc, VALUE argv, bucket_t *bucket, struct key_traits *tr
traits->explicit_ttl = 1;
exp = NUM2ULONG(ttl);
}
+ }
+ nn = RARRAY_LEN(argv);
+ if (nn == 1 && TYPE(RARRAY_PTR(argv)[0]) == T_ARRAY) {
+ argv = RARRAY_PTR(argv)[0];
nn = RARRAY_LEN(argv);
- } else {
- nn = argc;
+ traits->is_array = 1;
}
if (nn < 1) {
rb_raise(rb_eArgError, "must be at least one key");
@@ -2472,7 +2476,7 @@ cb_bucket_get(int argc, VALUE *argv, VALUE self)
long nn;
libcouchbase_error_t err;
struct key_traits *traits;
- int extended, mgat;
+ int extended, mgat, is_array;
long seqno;
if (bucket->handle == NULL) {
@@ -2482,7 +2486,6 @@ cb_bucket_get(int argc, VALUE *argv, VALUE self)
if (!bucket->async && proc != Qnil) {
rb_raise(rb_eArgError, "synchronous mode doesn't support callbacks");
}
- rb_funcall(args, id_flatten_bang, 0);
traits = calloc(1, sizeof(struct key_traits));
nn = cb_args_scan_keys(RARRAY_LEN(args), args, bucket, traits);
ctx = calloc(1, sizeof(context_t));
@@ -2491,6 +2494,7 @@ cb_bucket_get(int argc, VALUE *argv, VALUE self)
}
mgat = traits->mgat;
keys = traits->keys_ary;
+ is_array = traits->is_array;
ctx->proc = proc;
rb_hash_aset(object_space, ctx->proc|1, ctx->proc);
ctx->bucket = bucket;
@@ -2530,10 +2534,10 @@ cb_bucket_get(int argc, VALUE *argv, VALUE self)
if (bucket->exception != Qnil) {
rb_exc_raise(bucket->exception);
}
- if (mgat || (extended && nn > 1)) {
+ if (mgat || (extended && (nn > 1 || is_array))) {
return rv; /* return as a hash {key => [value, flags, cas], ...} */
}
- if (nn > 1) {
+ if (nn > 1 || is_array) {
long ii;
VALUE *keys_ptr, ret;
ret = rb_ary_new();
View
18 test/test_get.rb
@@ -331,4 +331,22 @@ def test_format_forcing
end
end
+ # http://www.couchbase.com/issues/browse/RCBC-31
+ def test_consistent_behaviour_for_arrays
+ connection = Couchbase.new(:hostname => @mock.host, :port => @mock.port)
+
+ cas = connection.set(uniq_id("foo"), "foo")
+ connection.set(uniq_id("bar"), "bar")
+
+ assert_equal "foo", connection.get(uniq_id("foo"))
+ assert_equal ["foo"], connection.get([uniq_id("foo")])
+ assert_equal ["foo", "bar"], connection.get([uniq_id("foo"), uniq_id("bar")])
+ assert_equal ["foo", "bar"], connection.get(uniq_id("foo"), uniq_id("bar"))
+ expected = {uniq_id("foo") => ["foo", 0x00, cas]}
+ assert_equal expected, connection.get([uniq_id("foo")], :extended => true)
+ assert_raises TypeError do
+ connection.get([uniq_id("foo"), uniq_id("bar")], [uniq_id("foo")])
+ end
+ end
+
end

0 comments on commit 538aba7

Please sign in to comment.