couchbase-jruby-client 0.1.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.ruby-version +1 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE.txt +203 -0
  6. data/README.md +347 -0
  7. data/Rakefile +10 -0
  8. data/couchbase-jruby-client.gemspec +30 -0
  9. data/lib/couchbase/async/callback.rb +19 -0
  10. data/lib/couchbase/async/queue.rb +26 -0
  11. data/lib/couchbase/async.rb +139 -0
  12. data/lib/couchbase/bucket.rb +663 -0
  13. data/lib/couchbase/cluster.rb +105 -0
  14. data/lib/couchbase/constants.rb +12 -0
  15. data/lib/couchbase/error.rb +28 -0
  16. data/lib/couchbase/jruby/couchbase_client.rb +22 -0
  17. data/lib/couchbase/jruby/future.rb +8 -0
  18. data/lib/couchbase/operations/arithmetic.rb +301 -0
  19. data/lib/couchbase/operations/delete.rb +104 -0
  20. data/lib/couchbase/operations/get.rb +298 -0
  21. data/lib/couchbase/operations/stats.rb +16 -0
  22. data/lib/couchbase/operations/store.rb +468 -0
  23. data/lib/couchbase/operations/touch.rb +123 -0
  24. data/lib/couchbase/operations/utils.rb +49 -0
  25. data/lib/couchbase/operations.rb +23 -0
  26. data/lib/couchbase/result.rb +43 -0
  27. data/lib/couchbase/transcoder.rb +83 -0
  28. data/lib/couchbase/utils.rb +62 -0
  29. data/lib/couchbase/version.rb +3 -0
  30. data/lib/couchbase/view.rb +506 -0
  31. data/lib/couchbase/view_row.rb +272 -0
  32. data/lib/couchbase.rb +177 -0
  33. data/lib/jars/commons-codec-1.5.jar +0 -0
  34. data/lib/jars/couchbase-client-1.2.0-javadoc.jar +0 -0
  35. data/lib/jars/couchbase-client-1.2.0-sources.jar +0 -0
  36. data/lib/jars/couchbase-client-1.2.0.jar +0 -0
  37. data/lib/jars/httpcore-4.1.1.jar +0 -0
  38. data/lib/jars/httpcore-nio-4.1.1.jar +0 -0
  39. data/lib/jars/jettison-1.1.jar +0 -0
  40. data/lib/jars/netty-3.5.5.Final.jar +0 -0
  41. data/lib/jars/spymemcached-2.10.0-javadoc.jar +0 -0
  42. data/lib/jars/spymemcached-2.10.0-sources.jar +0 -0
  43. data/lib/jars/spymemcached-2.10.0.jar +0 -0
  44. data/test/profile/.gitignore +1 -0
  45. data/test/profile/Gemfile +6 -0
  46. data/test/profile/benchmark.rb +195 -0
  47. data/test/setup.rb +201 -0
  48. data/test/test_arithmetic.rb +177 -0
  49. data/test/test_async.rb +324 -0
  50. data/test/test_bucket.rb +213 -0
  51. data/test/test_cas.rb +78 -0
  52. data/test/test_couchbase.rb +29 -0
  53. data/test/test_couchbase_rails_cache_store.rb +341 -0
  54. data/test/test_delete.rb +125 -0
  55. data/test/test_errors.rb +82 -0
  56. data/test/test_format.rb +161 -0
  57. data/test/test_get.rb +417 -0
  58. data/test/test_stats.rb +57 -0
  59. data/test/test_store.rb +216 -0
  60. data/test/test_timer.rb +42 -0
  61. data/test/test_touch.rb +97 -0
  62. data/test/test_unlock.rb +119 -0
  63. data/test/test_utils.rb +58 -0
  64. data/test/test_version.rb +52 -0
  65. metadata +226 -0
@@ -0,0 +1,298 @@
1
+ module Couchbase::Operations
2
+ module Get
3
+
4
+ # Obtain an object stored in Couchbase by given key.
5
+ #
6
+ # @since 1.0.0
7
+ #
8
+ # @see http://couchbase.com/docs/couchbase-manual-2.0/couchbase-architecture-apis-memcached-protocol-additions.html#couchbase-architecture-apis-memcached-protocol-additions-getl
9
+ #
10
+ # @overload get(*keys, options = {})
11
+ # @param keys [String, Symbol, Array] One or several keys to fetch
12
+ # @param options [Hash] Options for operation.
13
+ # @option options [true, false] :extended (false) If set to +true+, the
14
+ # operation will return a tuple +[value, flags, cas]+, otherwise (by
15
+ # default) it returns just the value.
16
+ # @option options [Fixnum] :ttl (self.default_ttl) Expiry time for key.
17
+ # Values larger than 30*24*60*60 seconds (30 days) are interpreted as
18
+ # absolute times (from the epoch).
19
+ # @option options [true, false] :quiet (self.quiet) If set to +true+, the
20
+ # operation won't raise error for missing key, it will return +nil+.
21
+ # Otherwise it will raise error in synchronous mode. In asynchronous
22
+ # mode this option ignored.
23
+ # @option options [Symbol] :format (nil) Explicitly choose the decoder
24
+ # for this key (+:plain+, +:document+, +:marshal+). See
25
+ # {Bucket#default_format}.
26
+ # @option options [Fixnum, Boolean] :lock Lock the keys for time span.
27
+ # If this parameter is +true+ the key(s) will be locked for default
28
+ # timeout. Also you can use number to setup your own timeout in
29
+ # seconds. If it will be lower that zero or exceed the maximum, the
30
+ # server will use default value. You can determine actual default and
31
+ # maximum values calling {Bucket#stats} without arguments and
32
+ # inspecting keys "ep_getl_default_timeout" and "ep_getl_max_timeout"
33
+ # correspondingly. See overloaded hash syntax to specify custom timeout
34
+ # per each key.
35
+ # @option options [true, false] :assemble_hash (false) Assemble Hash for
36
+ # results. Hash assembled automatically if +:extended+ option is true
37
+ # or in case of "get and touch" multimple keys.
38
+ # @option options [true, false] :replica (false) Read key from replica
39
+ # node. Options +:ttl+ and +:lock+ are not compatible with +:replica+.
40
+ #
41
+ # @yieldparam ret [Result] the result of operation in asynchronous mode
42
+ # (valid attributes: +error+, +operation+, +key+, +value+, +flags+,
43
+ # +cas+).
44
+ #
45
+ # @return [Object, Array, Hash] the value(s) (or tuples in extended mode)
46
+ # associated with the key.
47
+ #
48
+ # @raise [Couchbase::Error::NotFound] if the key is missing in the
49
+ # bucket.
50
+ #
51
+ # @raise [Couchbase::Error::Connect] if connection closed (see {Bucket#reconnect})
52
+ #
53
+ # @raise [ArgumentError] when passing the block in synchronous mode
54
+ #
55
+ # @example Get single value in quiet mode (the default)
56
+ # c.get("foo") #=> the associated value or nil
57
+ #
58
+ # @example Use alternative hash-like syntax
59
+ # c["foo"] #=> the associated value or nil
60
+ #
61
+ # @example Get single value in verbose mode
62
+ # c.get("missing-foo", :quiet => false) #=> raises Couchbase::NotFound
63
+ # c.get("missing-foo", :quiet => true) #=> returns nil
64
+ #
65
+ # @example Get and touch single value. The key won't be accessible after 10 seconds
66
+ # c.get("foo", :ttl => 10)
67
+ #
68
+ # @example Extended get
69
+ # val, flags, cas = c.get("foo", :extended => true)
70
+ #
71
+ # @example Get multiple keys
72
+ # c.get("foo", "bar", "baz") #=> [val1, val2, val3]
73
+ #
74
+ # @example Get multiple keys with assembing result into the Hash
75
+ # c.get("foo", "bar", "baz", :assemble_hash => true)
76
+ # #=> {"foo" => val1, "bar" => val2, "baz" => val3}
77
+ #
78
+ # @example Extended get multiple keys
79
+ # c.get("foo", "bar", :extended => true)
80
+ # #=> {"foo" => [val1, flags1, cas1], "bar" => [val2, flags2, cas2]}
81
+ #
82
+ # @example Asynchronous get
83
+ # c.run do
84
+ # c.get("foo", "bar", "baz") do |res|
85
+ # ret.operation #=> :get
86
+ # ret.success? #=> true
87
+ # ret.key #=> "foo", "bar" or "baz" in separate calls
88
+ # ret.value
89
+ # ret.flags
90
+ # ret.cas
91
+ # end
92
+ # end
93
+ #
94
+ # @example Get and lock key using default timeout
95
+ # c.get("foo", :lock => true)
96
+ #
97
+ # @example Determine lock timeout parameters
98
+ # c.stats.values_at("ep_getl_default_timeout", "ep_getl_max_timeout")
99
+ # #=> [{"127.0.0.1:11210"=>"15"}, {"127.0.0.1:11210"=>"30"}]
100
+ #
101
+ # @example Get and lock key using custom timeout
102
+ # c.get("foo", :lock => 3)
103
+ #
104
+ # @example Get and lock multiple keys using custom timeout
105
+ # c.get("foo", "bar", :lock => 3)
106
+ #
107
+ # @overload get(keys, options = {})
108
+ # When the method receive hash map, it will behave like it receive list
109
+ # of keys (+keys.keys+), but also touch each key setting expiry time to
110
+ # the corresponding value. But unlike usual get this command always
111
+ # return hash map +{key => value}+ or +{key => [value, flags, cas]}+.
112
+ #
113
+ # @param keys [Hash] Map key-ttl
114
+ # @param options [Hash] Options for operation. (see options definition
115
+ # above)
116
+ #
117
+ # @return [Hash] the values (or tuples in extended mode) associated with
118
+ # the keys.
119
+ #
120
+ # @example Get and touch multiple keys
121
+ # c.get("foo" => 10, "bar" => 20) #=> {"foo" => val1, "bar" => val2}
122
+ #
123
+ # @example Extended get and touch multiple keys
124
+ # c.get({"foo" => 10, "bar" => 20}, :extended => true)
125
+ # #=> {"foo" => [val1, flags1, cas1], "bar" => [val2, flags2, cas2]}
126
+ #
127
+ # @example Get and lock multiple keys for chosen period in seconds
128
+ # c.get("foo" => 10, "bar" => 20, :lock => true)
129
+ # #=> {"foo" => val1, "bar" => val2}
130
+ #
131
+ def get(*args, &block)
132
+ sync_block_error if !async? && block_given?
133
+ key, options = expand_get_args(args)
134
+
135
+ if async?
136
+ if block_given?
137
+ async_get(key, options, &Proc.new)
138
+ else
139
+ async_get(key, options)
140
+ end
141
+ else
142
+ sync_get(key, options)
143
+ end
144
+ end
145
+
146
+ def [](key, options = {})
147
+ get(key, options)
148
+ end
149
+
150
+ def get_multi(keys, options)
151
+ results = if options[:extended]
152
+ get_multi_extended(keys)
153
+ else
154
+ java_get_multi(keys)
155
+ end
156
+
157
+ not_found_error(results.size != keys.size, options)
158
+ results = transcode_multi_results(results) unless options[:extended]
159
+
160
+ if options[:assemble_hash] || options[:extended]
161
+ results
162
+ else
163
+ ordered_multi_values(keys, results)
164
+ end
165
+ end
166
+
167
+ private
168
+
169
+ def sync_get(key, options)
170
+ case key
171
+ when String, Symbol
172
+ get_single(key, options)
173
+ when Array
174
+ get_multi(key, options)
175
+ when Hash
176
+ get_and_touch(key, options)
177
+ end
178
+ end
179
+
180
+ def async_get(key, options, &block)
181
+ case key
182
+ when String, Symbol
183
+ async_get_single(key, options, &block)
184
+ when Array
185
+ async_get_multi(key, options, &block)
186
+ when Hash
187
+ async_get_and_touch(key, options, &block)
188
+ end
189
+ end
190
+
191
+ def expand_get_args(args)
192
+ options = extract_options_hash(args)
193
+ key = args.size == 1 ? args.first : args
194
+
195
+ [key, options]
196
+ end
197
+
198
+ def get_single(key, options)
199
+ if options[:extended]
200
+ get_single_extended(key, options)
201
+ else
202
+ value = if options.key?(:lock)
203
+ java_get_and_lock(key, options[:lock])
204
+ elsif options.key?(:ttl)
205
+ java_get_and_touch(key, options[:ttl])
206
+ else
207
+ java_get(key)
208
+ end
209
+
210
+ not_found_error(value.nil?, options)
211
+ value.nil? ? nil : load(value)
212
+ end
213
+ end
214
+
215
+ def get_single_extended(key, options = {})
216
+ extended = java_get_extended(key)
217
+ not_found_error(extended.nil?, options)
218
+ extended
219
+ end
220
+
221
+ def async_get_single(key, options, &block)
222
+ future = client.asyncGet(key)
223
+ register_future(future, { op: :get, key: key }, &block)
224
+ end
225
+
226
+ def async_get_multi(keys, options, &block)
227
+ future = client.asyncGetBulk(keys)
228
+ register_future(future, { op: :get }, &block)
229
+ end
230
+
231
+ def get_and_touch(key, options = {})
232
+ if key.size > 1
233
+ get_multi_and_touch(key, options)
234
+ else
235
+ key, ttl = key.first
236
+ value = java_get_and_touch(key, ttl)
237
+ not_found_error(value.nil?)
238
+ { key => load(value) }
239
+ end
240
+ end
241
+
242
+ def get_multi_and_touch(keys, options = {})
243
+ options.merge!(assemble_hash: true)
244
+ results = get_multi(keys.keys, options)
245
+ touch(keys)
246
+ results.to_hash
247
+ end
248
+
249
+ def get_multi_extended(keys, options = {})
250
+ {}.tap do |results|
251
+ keys.each do |key|
252
+ results[key] = get_single_extended(key, options)
253
+ end
254
+ end
255
+ end
256
+
257
+ def ordered_multi_values(keys, results)
258
+ keys.map { |key| results[key] }
259
+ end
260
+
261
+ def java_get(key)
262
+ client.get(key)
263
+ end
264
+
265
+ def java_get_and_touch(key, ttl)
266
+ client.getAndTouch(key, ttl).getValue
267
+ end
268
+
269
+ def java_get_and_lock(key, lock)
270
+ client.getAndLock(key, lock).value
271
+ end
272
+
273
+ def java_get_extended(key)
274
+ cas_value = client.gets(key)
275
+
276
+ if cas_value.nil?
277
+ nil
278
+ else
279
+ [load(cas_value.getValue), nil, cas_value.getCas]
280
+ end
281
+ end
282
+
283
+ def java_get_multi(keys)
284
+ client.getBulk(keys)
285
+ rescue java.lang.ClassCastException
286
+ raise TypeError.new
287
+ end
288
+
289
+ def transcode_multi_results(results)
290
+ {}.tap do |new_results|
291
+ results.each do |k, v|
292
+ new_results[k] = load(v)
293
+ end
294
+ end
295
+ end
296
+
297
+ end
298
+ end
@@ -0,0 +1,16 @@
1
+ module Couchbase::Operations
2
+ module Stats
3
+
4
+ def stats(statname = nil)
5
+ sync_block_error if !async? && block_given?
6
+ stats = if statname.nil?
7
+ client.getStats
8
+ else
9
+ client.getStats(statname)
10
+ end
11
+
12
+ stats.first.last.to_hash
13
+ end
14
+
15
+ end
16
+ end