couchbase-jruby-client 0.2.2-java → 1.0.4-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +103 -0
- data/.ruby-version +1 -1
- data/Rakefile +13 -2
- data/couchbase-jruby-client.gemspec +7 -6
- data/lib/couchbase/bucket.rb +11 -338
- data/lib/couchbase/cluster.rb +10 -88
- data/lib/couchbase/configuration.rb +39 -0
- data/lib/couchbase/design_doc.rb +18 -73
- data/lib/couchbase/document.rb +34 -0
- data/lib/couchbase/error.rb +0 -35
- data/lib/couchbase/operations.rb +60 -40
- data/lib/couchbase/version.rb +1 -1
- data/lib/couchbase/view.rb +55 -345
- data/lib/couchbase.rb +37 -141
- data/lib/jars/couchbase-core-io-1.1.4.jar +0 -0
- data/lib/jars/couchbase-java-client-2.1.4.jar +0 -0
- data/lib/jars/rxjava-1.0.8.jar +0 -0
- data/lib/jars/rxjruby-0.0.1.jar +0 -0
- data/test/{test_cas.rb → helper.rb} +15 -10
- data/test/test_bucket.rb +14 -228
- data/test/test_cluster.rb +1 -29
- data/test/test_configuration.rb +51 -0
- data/test/test_couchbase.rb +28 -28
- data/test/test_design_doc.rb +29 -0
- data/test/test_document.rb +51 -0
- data/test/test_operations.rb +49 -0
- data/test/test_view.rb +62 -116
- metadata +43 -125
- data/lib/couchbase/async/callback.rb +0 -38
- data/lib/couchbase/async/queue.rb +0 -26
- data/lib/couchbase/async.rb +0 -32
- data/lib/couchbase/constants.rb +0 -29
- data/lib/couchbase/operations/arithmetic.rb +0 -290
- data/lib/couchbase/operations/delete.rb +0 -115
- data/lib/couchbase/operations/design_docs.rb +0 -99
- data/lib/couchbase/operations/fetch.rb +0 -33
- data/lib/couchbase/operations/get.rb +0 -303
- data/lib/couchbase/operations/stats.rb +0 -42
- data/lib/couchbase/operations/store.rb +0 -463
- data/lib/couchbase/operations/touch.rb +0 -140
- data/lib/couchbase/operations/unlock.rb +0 -209
- data/lib/couchbase/operations/utils.rb +0 -68
- data/lib/couchbase/query.rb +0 -76
- data/lib/couchbase/result.rb +0 -60
- data/lib/couchbase/transcoder.rb +0 -81
- data/lib/couchbase/utils.rb +0 -62
- data/lib/couchbase/view_row.rb +0 -227
- data/lib/jars/commons-codec-1.5.jar +0 -0
- data/lib/jars/couchbase-client-1.3.2-javadoc.jar +0 -0
- data/lib/jars/couchbase-client-1.3.2-sources.jar +0 -0
- data/lib/jars/couchbase-client-1.3.2.jar +0 -0
- data/lib/jars/httpcore-4.3.1.jar +0 -0
- data/lib/jars/httpcore-nio-4.3.1.jar +0 -0
- data/lib/jars/jettison-1.1.jar +0 -0
- data/lib/jars/netty-3.5.5.Final.jar +0 -0
- data/lib/jars/spymemcached-2.10.5-javadoc.jar +0 -0
- data/lib/jars/spymemcached-2.10.5-sources.jar +0 -0
- data/lib/jars/spymemcached-2.10.5.jar +0 -0
- data/tasks/benchmark.rake +0 -6
- data/tasks/test.rake +0 -36
- data/tasks/util.rake +0 -21
- data/test/mock.rb +0 -85
- data/test/profile/.gitignore +0 -1
- data/test/profile/.jrubyrc +0 -722
- data/test/profile/Gemfile +0 -7
- data/test/profile/benchmark.rb +0 -177
- data/test/profile/profile.rb +0 -59
- data/test/setup.rb +0 -74
- data/test/test_arithmetic.rb +0 -155
- data/test/test_async.rb +0 -24
- data/test/test_couchbase_rails_cache_store.rb +0 -341
- data/test/test_delete.rb +0 -139
- data/test/test_design_docs.rb +0 -67
- data/test/test_errors.rb +0 -74
- data/test/test_fetch.rb +0 -71
- data/test/test_format.rb +0 -142
- data/test/test_get.rb +0 -363
- data/test/test_query.rb +0 -23
- data/test/test_result.rb +0 -15
- data/test/test_stats.rb +0 -44
- data/test/test_store.rb +0 -203
- data/test/test_touch.rb +0 -90
- data/test/test_unlock.rb +0 -89
- data/test/test_utils.rb +0 -67
- data/test/test_version.rb +0 -28
- data/test/test_view_row.rb +0 -74
@@ -1,463 +0,0 @@
|
|
1
|
-
# Author:: Mike Evans <mike@urlgonomics.com>
|
2
|
-
# Copyright:: 2013 Urlgonomics LLC.
|
3
|
-
# License:: Apache License, Version 2.0
|
4
|
-
#
|
5
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
-
# you may not use this file except in compliance with the License.
|
7
|
-
# You may obtain a copy of the License at
|
8
|
-
#
|
9
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
-
#
|
11
|
-
# Unless required by applicable law or agreed to in writing, software
|
12
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
-
# See the License for the specific language governing permissions and
|
15
|
-
# limitations under the License.
|
16
|
-
#
|
17
|
-
|
18
|
-
# java_import com.couchbase.client.CouchbaseClient
|
19
|
-
|
20
|
-
# class CouchbaseClient
|
21
|
-
# java_alias :java_set, :set, [java.lang.String, Java::int, java.lang.Object, Java::net.spy.memcached.transcoders.Transcoder.java_class]
|
22
|
-
# end
|
23
|
-
|
24
|
-
module Couchbase::Operations
|
25
|
-
module Store
|
26
|
-
|
27
|
-
STORE_OP_METHODS = {
|
28
|
-
set: -> client, key, value, ttl, transcoder { client.set(key, ttl.to_i, value, transcoder) },
|
29
|
-
add: -> client, key, value, ttl, transcoder { client.add(key, ttl.to_i, value, transcoder) },
|
30
|
-
replace: -> client, key, value, ttl, transcoder { client.replace(key, ttl.to_i, value, transcoder) },
|
31
|
-
append: -> client, key, value, ttl, transcoder { client.append(key, value, transcoder) },
|
32
|
-
prepend: -> client, key, value, ttl, transcoder { client.prepend(key, value, transcoder) }
|
33
|
-
}.freeze
|
34
|
-
|
35
|
-
# Unconditionally store the object in the Couchbase
|
36
|
-
#
|
37
|
-
# @since 1.0.0
|
38
|
-
#
|
39
|
-
# @overload set(key, value, options = {})
|
40
|
-
#
|
41
|
-
# @param key [String, Symbol] Key used to reference the value.
|
42
|
-
# @param value [Object] Value to be stored
|
43
|
-
# @param options [Hash] Options for operation.
|
44
|
-
# @option options [Fixnum] :ttl (self.default_ttl) Expiry time for key.
|
45
|
-
# Values larger than 30*24*60*60 seconds (30 days) are interpreted as
|
46
|
-
# absolute times (from the epoch).
|
47
|
-
# @option options [Fixnum] :flags (self.default_flags) Flags for storage
|
48
|
-
# options. Flags are ignored by the server but preserved for use by the
|
49
|
-
# client. For more info see {Bucket#default_flags}.
|
50
|
-
# @option options [Symbol] :format (self.default_format) The
|
51
|
-
# representation for storing the value in the bucket. For more info see
|
52
|
-
# {Bucket#default_format}.
|
53
|
-
# @option options [Fixnum] :cas The CAS value for an object. This value is
|
54
|
-
# created on the server and is guaranteed to be unique for each value of
|
55
|
-
# a given key. This value is used to provide simple optimistic
|
56
|
-
# concurrency control when multiple clients or threads try to update an
|
57
|
-
# item simultaneously.
|
58
|
-
# @option options [Hash] :observe Apply persistence condition before
|
59
|
-
# returning result. When this option specified the library will observe
|
60
|
-
# given condition. See {Bucket#observe_and_wait}.
|
61
|
-
#
|
62
|
-
# @yieldparam ret [Result] the result of operation in asynchronous mode
|
63
|
-
# (valid attributes: +error+, +operation+, +key+).
|
64
|
-
#
|
65
|
-
# @return [Fixnum] The CAS value of the object.
|
66
|
-
#
|
67
|
-
# @raise [Couchbase::Error::Connect] if connection closed (see {Bucket#reconnect}).
|
68
|
-
# @raise [Couchbase::Error::KeyExists] if the key already exists on the
|
69
|
-
# server.
|
70
|
-
# @raise [Couchbase::Error::ValueFormat] if the value cannot be serialized
|
71
|
-
# with chosen encoder, e.g. if you try to store the Hash in +:plain+
|
72
|
-
# mode.
|
73
|
-
# @raise [ArgumentError] when passing the block in synchronous mode
|
74
|
-
# @raise [Couchbase::Error::Timeout] if timeout interval for observe
|
75
|
-
# exceeds
|
76
|
-
#
|
77
|
-
# @example Store the key which will be expired in 2 seconds using relative TTL.
|
78
|
-
# c.set("foo", "bar", :ttl => 2)
|
79
|
-
#
|
80
|
-
# @example Perform multi-set operation. It takes a Hash store its keys/values into the bucket
|
81
|
-
# c.set("foo1" => "bar1", "foo2" => "bar2")
|
82
|
-
# #=> {"foo1" => cas1, "foo2" => cas2}
|
83
|
-
#
|
84
|
-
# @example More advanced multi-set using asynchronous mode
|
85
|
-
# c.run do
|
86
|
-
# # fire and forget
|
87
|
-
# c.set("foo1", "bar1", :ttl => 10)
|
88
|
-
# # receive result into the callback
|
89
|
-
# c.set("foo2", "bar2", :ttl => 10) do |ret|
|
90
|
-
# if ret.success?
|
91
|
-
# puts ret.cas
|
92
|
-
# end
|
93
|
-
# end
|
94
|
-
# end
|
95
|
-
#
|
96
|
-
# @example Store the key which will be expired in 2 seconds using absolute TTL.
|
97
|
-
# c.set("foo", "bar", :ttl => Time.now.to_i + 2)
|
98
|
-
#
|
99
|
-
# @example Force JSON document format for value
|
100
|
-
# c.set("foo", {"bar" => "baz}, :format => :document)
|
101
|
-
#
|
102
|
-
# @example Use hash-like syntax to store the value
|
103
|
-
# c["foo"] = {"bar" => "baz}
|
104
|
-
#
|
105
|
-
# @example Use extended hash-like syntax
|
106
|
-
# c["foo", {:flags => 0x1000, :format => :plain}] = "bar"
|
107
|
-
# c["foo", :flags => 0x1000] = "bar" # for ruby 1.9.x only
|
108
|
-
#
|
109
|
-
# @example Set application specific flags (note that it will be OR-ed with format flags)
|
110
|
-
# c.set("foo", "bar", :flags => 0x1000)
|
111
|
-
#
|
112
|
-
# @example Perform optimistic locking by specifying last known CAS version
|
113
|
-
# c.set("foo", "bar", :cas => 8835713818674332672)
|
114
|
-
#
|
115
|
-
# @example Perform asynchronous call
|
116
|
-
# c.run do
|
117
|
-
# c.set("foo", "bar") do |ret|
|
118
|
-
# ret.operation #=> :set
|
119
|
-
# ret.success? #=> true
|
120
|
-
# ret.key #=> "foo"
|
121
|
-
# ret.cas
|
122
|
-
# end
|
123
|
-
# end
|
124
|
-
#
|
125
|
-
# @example Ensure that the key will be persisted at least on the one node
|
126
|
-
# c.set("foo", "bar", :observe => {:persisted => 1})
|
127
|
-
#
|
128
|
-
def set(key, value = nil, options = {})
|
129
|
-
store_op(:set, key, value, options)
|
130
|
-
end
|
131
|
-
|
132
|
-
def async_set(key, value, options = {}, &block)
|
133
|
-
async_store_op(:set, key, value, options, &block)
|
134
|
-
end
|
135
|
-
|
136
|
-
def []=(key, *args)
|
137
|
-
options = args.size > 1 ? args.shift : {}
|
138
|
-
value = args.pop
|
139
|
-
|
140
|
-
set(key, value, options)
|
141
|
-
end
|
142
|
-
|
143
|
-
# Add the item to the database, but fail if the object exists already
|
144
|
-
#
|
145
|
-
# @since 1.0.0
|
146
|
-
#
|
147
|
-
# @overload add(key, value, options = {})
|
148
|
-
#
|
149
|
-
# @param key [String, Symbol] Key used to reference the value.
|
150
|
-
# @param value [Object] Value to be stored
|
151
|
-
# @param options [Hash] Options for operation.
|
152
|
-
# @option options [Fixnum] :ttl (self.default_ttl) Expiry time for key.
|
153
|
-
# Values larger than 30*24*60*60 seconds (30 days) are interpreted as
|
154
|
-
# absolute times (from the epoch).
|
155
|
-
# @option options [Fixnum] :flags (self.default_flags) Flags for storage
|
156
|
-
# options. Flags are ignored by the server but preserved for use by the
|
157
|
-
# client. For more info see {Bucket#default_flags}.
|
158
|
-
# @option options [Symbol] :format (self.default_format) The
|
159
|
-
# representation for storing the value in the bucket. For more info see
|
160
|
-
# {Bucket#default_format}.
|
161
|
-
# @option options [Fixnum] :cas The CAS value for an object. This value
|
162
|
-
# created on the server and is guaranteed to be unique for each value of
|
163
|
-
# a given key. This value is used to provide simple optimistic
|
164
|
-
# concurrency control when multiple clients or threads try to update an
|
165
|
-
# item simultaneously.
|
166
|
-
# @option options [Hash] :observe Apply persistence condition before
|
167
|
-
# returning result. When this option specified the library will observe
|
168
|
-
# given condition. See {Bucket#observe_and_wait}.
|
169
|
-
#
|
170
|
-
# @yieldparam ret [Result] the result of operation in asynchronous mode
|
171
|
-
# (valid attributes: +error+, +operation+, +key+).
|
172
|
-
#
|
173
|
-
# @return [Fixnum] The CAS value of the object.
|
174
|
-
#
|
175
|
-
# @raise [Couchbase::Error::Connect] if connection closed (see {Bucket#reconnect})
|
176
|
-
# @raise [Couchbase::Error::KeyExists] if the key already exists on the
|
177
|
-
# server
|
178
|
-
# @raise [Couchbase::Error::ValueFormat] if the value cannot be serialized
|
179
|
-
# with chosen encoder, e.g. if you try to store the Hash in +:plain+
|
180
|
-
# mode.
|
181
|
-
# @raise [ArgumentError] when passing the block in synchronous mode
|
182
|
-
# @raise [Couchbase::Error::Timeout] if timeout interval for observe
|
183
|
-
# exceeds
|
184
|
-
#
|
185
|
-
# @example Add the same key twice
|
186
|
-
# c.add("foo", "bar") #=> stored successully
|
187
|
-
# c.add("foo", "baz") #=> will raise Couchbase::Error::KeyExists: failed to store value (key="foo", error=0x0c)
|
188
|
-
#
|
189
|
-
# @example Ensure that the key will be persisted at least on the one node
|
190
|
-
# c.add("foo", "bar", :observe => {:persisted => 1})
|
191
|
-
#
|
192
|
-
def add(key, value = nil, options = {})
|
193
|
-
store_op(:add, key, value, options)
|
194
|
-
end
|
195
|
-
|
196
|
-
def async_add(key, value, options, &block)
|
197
|
-
async_store_op(:add, key, value, options, &block)
|
198
|
-
end
|
199
|
-
|
200
|
-
# Replace the existing object in the database
|
201
|
-
#
|
202
|
-
# @since 1.0.0
|
203
|
-
#
|
204
|
-
# @overload replace(key, value, options = {})
|
205
|
-
# @param key [String, Symbol] Key used to reference the value.
|
206
|
-
# @param value [Object] Value to be stored
|
207
|
-
# @param options [Hash] Options for operation.
|
208
|
-
# @option options [Fixnum] :ttl (self.default_ttl) Expiry time for key.
|
209
|
-
# Values larger than 30*24*60*60 seconds (30 days) are interpreted as
|
210
|
-
# absolute times (from the epoch).
|
211
|
-
# @option options [Fixnum] :flags (self.default_flags) Flags for storage
|
212
|
-
# options. Flags are ignored by the server but preserved for use by the
|
213
|
-
# client. For more info see {Bucket#default_flags}.
|
214
|
-
# @option options [Symbol] :format (self.default_format) The
|
215
|
-
# representation for storing the value in the bucket. For more info see
|
216
|
-
# {Bucket#default_format}.
|
217
|
-
# @option options [Fixnum] :cas The CAS value for an object. This value
|
218
|
-
# created on the server and is guaranteed to be unique for each value of
|
219
|
-
# a given key. This value is used to provide simple optimistic
|
220
|
-
# concurrency control when multiple clients or threads try to update an
|
221
|
-
# item simultaneously.
|
222
|
-
# @option options [Hash] :observe Apply persistence condition before
|
223
|
-
# returning result. When this option specified the library will observe
|
224
|
-
# given condition. See {Bucket#observe_and_wait}.
|
225
|
-
#
|
226
|
-
# @return [Fixnum] The CAS value of the object.
|
227
|
-
#
|
228
|
-
# @raise [Couchbase::Error::Connect] if connection closed (see {Bucket#reconnect})
|
229
|
-
# @raise [Couchbase::Error::NotFound] if the key doesn't exists
|
230
|
-
# @raise [Couchbase::Error::KeyExists] on CAS mismatch
|
231
|
-
# @raise [ArgumentError] when passing the block in synchronous mode
|
232
|
-
# @raise [Couchbase::Error::Timeout] if timeout interval for observe
|
233
|
-
# exceeds
|
234
|
-
#
|
235
|
-
# @example Replacing missing key
|
236
|
-
# c.replace("foo", "baz") #=> will raise Couchbase::Error::NotFound: failed to store value (key="foo", error=0x0d)
|
237
|
-
#
|
238
|
-
# @example Ensure that the key will be persisted at least on the one node
|
239
|
-
# c.replace("foo", "bar", :observe => {:persisted => 1})
|
240
|
-
#
|
241
|
-
def replace(key, value, options = {})
|
242
|
-
store_op(:replace, key, value, options)
|
243
|
-
end
|
244
|
-
|
245
|
-
def async_replace(key, value, options, &block)
|
246
|
-
async_store_op(:replace, key, value, options, &block)
|
247
|
-
end
|
248
|
-
|
249
|
-
# Append this object to the existing object
|
250
|
-
#
|
251
|
-
# @since 1.0.0
|
252
|
-
#
|
253
|
-
# @note This operation is kind of data-aware from server point of view.
|
254
|
-
# This mean that the server treats value as binary stream and just
|
255
|
-
# perform concatenation, therefore it won't work with +:marshal+ and
|
256
|
-
# +:document+ formats, because of lack of knowledge how to merge values
|
257
|
-
# in these formats. See {Bucket#cas} for workaround.
|
258
|
-
#
|
259
|
-
# @overload append(key, value, options = {})
|
260
|
-
# @param key [String, Symbol] Key used to reference the value.
|
261
|
-
# @param value [Object] Value to be stored
|
262
|
-
# @param options [Hash] Options for operation.
|
263
|
-
# @option options [Fixnum] :cas The CAS value for an object. This value
|
264
|
-
# created on the server and is guaranteed to be unique for each value of
|
265
|
-
# a given key. This value is used to provide simple optimistic
|
266
|
-
# concurrency control when multiple clients or threads try to update an
|
267
|
-
# item simultaneously.
|
268
|
-
# @option options [Symbol] :format (self.default_format) The
|
269
|
-
# representation for storing the value in the bucket. For more info see
|
270
|
-
# {Bucket#default_format}.
|
271
|
-
# @option options [Hash] :observe Apply persistence condition before
|
272
|
-
# returning result. When this option specified the library will observe
|
273
|
-
# given condition. See {Bucket#observe_and_wait}.
|
274
|
-
#
|
275
|
-
# @return [Fixnum] The CAS value of the object.
|
276
|
-
#
|
277
|
-
# @raise [Couchbase::Error::Connect] if connection closed (see {Bucket#reconnect})
|
278
|
-
# @raise [Couchbase::Error::KeyExists] on CAS mismatch
|
279
|
-
# @raise [Couchbase::Error::NotStored] if the key doesn't exist
|
280
|
-
# @raise [ArgumentError] when passing the block in synchronous mode
|
281
|
-
# @raise [Couchbase::Error::Timeout] if timeout interval for observe
|
282
|
-
# exceeds
|
283
|
-
#
|
284
|
-
# @example Simple append
|
285
|
-
# c.set("foo", "aaa")
|
286
|
-
# c.append("foo", "bbb")
|
287
|
-
# c.get("foo") #=> "aaabbb"
|
288
|
-
#
|
289
|
-
# @example Implementing sets using append
|
290
|
-
# def set_add(key, *values)
|
291
|
-
# encoded = values.flatten.map{|v| "+#{v} "}.join
|
292
|
-
# append(key, encoded)
|
293
|
-
# end
|
294
|
-
#
|
295
|
-
# def set_remove(key, *values)
|
296
|
-
# encoded = values.flatten.map{|v| "-#{v} "}.join
|
297
|
-
# append(key, encoded)
|
298
|
-
# end
|
299
|
-
#
|
300
|
-
# def set_get(key)
|
301
|
-
# encoded = get(key)
|
302
|
-
# ret = Set.new
|
303
|
-
# encoded.split(' ').each do |v|
|
304
|
-
# op, val = v[0], v[1..-1]
|
305
|
-
# case op
|
306
|
-
# when "-"
|
307
|
-
# ret.delete(val)
|
308
|
-
# when "+"
|
309
|
-
# ret.add(val)
|
310
|
-
# end
|
311
|
-
# end
|
312
|
-
# ret
|
313
|
-
# end
|
314
|
-
#
|
315
|
-
# @example Using optimistic locking. The operation will fail on CAS mismatch
|
316
|
-
# ver = c.set("foo", "aaa")
|
317
|
-
# c.append("foo", "bbb", :cas => ver)
|
318
|
-
#
|
319
|
-
# @example Ensure that the key will be persisted at least on the one node
|
320
|
-
# c.append("foo", "bar", :observe => {:persisted => 1})
|
321
|
-
#
|
322
|
-
def append(key, value)
|
323
|
-
store_op(:append, key, value)
|
324
|
-
end
|
325
|
-
|
326
|
-
# Prepend this object to the existing object
|
327
|
-
#
|
328
|
-
# @since 1.0.0
|
329
|
-
#
|
330
|
-
# @note This operation is kind of data-aware from server point of view.
|
331
|
-
# This mean that the server treats value as binary stream and just
|
332
|
-
# perform concatenation, therefore it won't work with +:marshal+ and
|
333
|
-
# +:document+ formats, because of lack of knowledge how to merge values
|
334
|
-
# in these formats. See {Bucket#cas} for workaround.
|
335
|
-
#
|
336
|
-
# @overload prepend(key, value, options = {})
|
337
|
-
# @param key [String, Symbol] Key used to reference the value.
|
338
|
-
# @param value [Object] Value to be stored
|
339
|
-
# @param options [Hash] Options for operation.
|
340
|
-
# @option options [Fixnum] :cas The CAS value for an object. This value
|
341
|
-
# created on the server and is guaranteed to be unique for each value of
|
342
|
-
# a given key. This value is used to provide simple optimistic
|
343
|
-
# concurrency control when multiple clients or threads try to update an
|
344
|
-
# item simultaneously.
|
345
|
-
# @option options [Symbol] :format (self.default_format) The
|
346
|
-
# representation for storing the value in the bucket. For more info see
|
347
|
-
# {Bucket#default_format}.
|
348
|
-
# @option options [Hash] :observe Apply persistence condition before
|
349
|
-
# returning result. When this option specified the library will observe
|
350
|
-
# given condition. See {Bucket#observe_and_wait}.
|
351
|
-
#
|
352
|
-
# @raise [Couchbase::Error::Connect] if connection closed (see {Bucket#reconnect})
|
353
|
-
# @raise [Couchbase::Error::KeyExists] on CAS mismatch
|
354
|
-
# @raise [Couchbase::Error::NotStored] if the key doesn't exist
|
355
|
-
# @raise [ArgumentError] when passing the block in synchronous mode
|
356
|
-
# @raise [Couchbase::Error::Timeout] if timeout interval for observe
|
357
|
-
# exceeds
|
358
|
-
#
|
359
|
-
# @example Simple prepend example
|
360
|
-
# c.set("foo", "aaa")
|
361
|
-
# c.prepend("foo", "bbb")
|
362
|
-
# c.get("foo") #=> "bbbaaa"
|
363
|
-
#
|
364
|
-
# @example Using explicit format option
|
365
|
-
# c.default_format #=> :document
|
366
|
-
# c.set("foo", {"y" => "z"})
|
367
|
-
# c.prepend("foo", '[', :format => :plain)
|
368
|
-
# c.append("foo", ', {"z": "y"}]', :format => :plain)
|
369
|
-
# c.get("foo") #=> [{"y"=>"z"}, {"z"=>"y"}]
|
370
|
-
#
|
371
|
-
# @example Using optimistic locking. The operation will fail on CAS mismatch
|
372
|
-
# ver = c.set("foo", "aaa")
|
373
|
-
# c.prepend("foo", "bbb", :cas => ver)
|
374
|
-
#
|
375
|
-
# @example Ensure that the key will be persisted at least on the one node
|
376
|
-
# c.prepend("foo", "bar", :observe => {:persisted => 1})
|
377
|
-
#
|
378
|
-
def prepend(key, value)
|
379
|
-
store_op(:prepend, key, value)
|
380
|
-
end
|
381
|
-
|
382
|
-
private
|
383
|
-
|
384
|
-
def store_args_parser(key, value, options)
|
385
|
-
key = key.to_str if key.respond_to?(:to_str)
|
386
|
-
ttl = options.delete(:ttl) || default_ttl
|
387
|
-
transcoder = @transcoders[options.delete(:format)] || self.transcoder
|
388
|
-
|
389
|
-
[validate_key(key), value, ttl, transcoder]
|
390
|
-
end
|
391
|
-
|
392
|
-
def store_op(op, key, value, options = {})
|
393
|
-
key, value, ttl, transcoder = store_args_parser(key, value, options)
|
394
|
-
|
395
|
-
if key.is_a?(String) || key.is_a?(Symbol)
|
396
|
-
store_by_string(op, key.to_s, value, ttl, transcoder, options)
|
397
|
-
elsif key.is_a?(Hash)
|
398
|
-
store_by_hash(op, key, value)
|
399
|
-
else
|
400
|
-
fail TypeError.new
|
401
|
-
end
|
402
|
-
end
|
403
|
-
|
404
|
-
def store_by_string(op, key, value, ttl, transcoder, options)
|
405
|
-
if options[:cas] && op == :set
|
406
|
-
client_cas(key, value, ttl, options[:cas], transcoder)
|
407
|
-
else
|
408
|
-
transcoder = op == :append || op == :prepend ? transcoders[:plain] : transcoder
|
409
|
-
future = client_store_op(op, key, value, ttl, transcoder)
|
410
|
-
if cas = future_cas(future)
|
411
|
-
cas
|
412
|
-
else
|
413
|
-
fail_store_op(op)
|
414
|
-
end
|
415
|
-
end
|
416
|
-
end
|
417
|
-
|
418
|
-
def store_by_hash(op, key, value)
|
419
|
-
fail TypeError.new if !value.nil?
|
420
|
-
multi_op(op, key)
|
421
|
-
end
|
422
|
-
|
423
|
-
def async_store_op(op, key, value, options, &block)
|
424
|
-
key, value, ttl, transcoder = store_args_parser(key, value, options)
|
425
|
-
future = client_store_op(op, key, value, ttl, transcoder)
|
426
|
-
register_future(future, { op: op }, &block)
|
427
|
-
end
|
428
|
-
|
429
|
-
def multi_op(op, keys)
|
430
|
-
{}.tap do |results|
|
431
|
-
keys.each_pair do |key, value|
|
432
|
-
results[key] = client.send(op, key, default_ttl, value)
|
433
|
-
end
|
434
|
-
end
|
435
|
-
end
|
436
|
-
|
437
|
-
def client_store_op(op, key, value, ttl, transcoder)
|
438
|
-
STORE_OP_METHODS[op].call(self.client, key, value, ttl, transcoder)
|
439
|
-
end
|
440
|
-
|
441
|
-
def client_cas(key, value, ttl, cas, transcoder)
|
442
|
-
cas_response = client.cas(key, cas, ttl, value, transcoder)
|
443
|
-
if cas_response.to_s == 'OK'
|
444
|
-
get(key, extended: true)[2]
|
445
|
-
else
|
446
|
-
raise Couchbase::Error::KeyExists.new
|
447
|
-
end
|
448
|
-
end
|
449
|
-
|
450
|
-
def fail_store_op(op)
|
451
|
-
case op
|
452
|
-
when :replace
|
453
|
-
fail Couchbase::Error::NotFound
|
454
|
-
when :append, :prepend
|
455
|
-
fail Couchbase::Error::NotStored
|
456
|
-
else
|
457
|
-
fail Couchbase::Error::KeyExists
|
458
|
-
end
|
459
|
-
end
|
460
|
-
|
461
|
-
end
|
462
|
-
|
463
|
-
end
|
@@ -1,140 +0,0 @@
|
|
1
|
-
# Author:: Mike Evans <mike@urlgonomics.com>
|
2
|
-
# Copyright:: 2013 Urlgonomics LLC.
|
3
|
-
# License:: Apache License, Version 2.0
|
4
|
-
#
|
5
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
-
# you may not use this file except in compliance with the License.
|
7
|
-
# You may obtain a copy of the License at
|
8
|
-
#
|
9
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
-
#
|
11
|
-
# Unless required by applicable law or agreed to in writing, software
|
12
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
-
# See the License for the specific language governing permissions and
|
15
|
-
# limitations under the License.
|
16
|
-
#
|
17
|
-
|
18
|
-
module Couchbase::Operations
|
19
|
-
module Touch
|
20
|
-
|
21
|
-
# Update the expiry time of an item
|
22
|
-
#
|
23
|
-
# @since 1.0.0
|
24
|
-
#
|
25
|
-
# The +touch+ method allow you to update the expiration time on a given
|
26
|
-
# key. This can be useful for situations where you want to prevent an item
|
27
|
-
# from expiring without resetting the associated value. For example, for a
|
28
|
-
# session database you might want to keep the session alive in the database
|
29
|
-
# each time the user accesses a web page without explicitly updating the
|
30
|
-
# session value, keeping the user's session active and available.
|
31
|
-
#
|
32
|
-
# @overload touch(key, options = {})
|
33
|
-
# @param key [String, Symbol] Key used to reference the value.
|
34
|
-
# @param options [Hash] Options for operation.
|
35
|
-
# @option options [Fixnum] :ttl (self.default_ttl) Expiry time for key.
|
36
|
-
# Values larger than 30*24*60*60 seconds (30 days) are interpreted as
|
37
|
-
# absolute times (from the epoch).
|
38
|
-
# @option options [true, false] :quiet (self.quiet) If set to +true+, the
|
39
|
-
# operation won't raise error for missing key, it will return +nil+.
|
40
|
-
#
|
41
|
-
# @yieldparam ret [Result] the result of operation in asynchronous mode
|
42
|
-
# (valid attributes: +error+, +operation+, +key+).
|
43
|
-
#
|
44
|
-
# @return [true, false] +true+ if the operation was successful and +false+
|
45
|
-
# otherwise.
|
46
|
-
#
|
47
|
-
# @raise [Couchbase::Error::Connect] if connection closed (see {Bucket#reconnect})
|
48
|
-
#
|
49
|
-
# @raise [ArgumentError] when passing the block in synchronous mode
|
50
|
-
#
|
51
|
-
# @example Touch value using +default_ttl+
|
52
|
-
# c.touch("foo")
|
53
|
-
#
|
54
|
-
# @example Touch value using custom TTL (10 seconds)
|
55
|
-
# c.touch("foo", :ttl => 10)
|
56
|
-
#
|
57
|
-
# @overload touch(keys)
|
58
|
-
# @param keys [Hash] The Hash where keys represent the keys in the
|
59
|
-
# database, values -- the expiry times for corresponding key. See
|
60
|
-
# description of +:ttl+ argument above for more information about TTL
|
61
|
-
# values.
|
62
|
-
#
|
63
|
-
# @yieldparam ret [Result] the result of operation for each key in
|
64
|
-
# asynchronous mode (valid attributes: +error+, +operation+, +key+).
|
65
|
-
#
|
66
|
-
# @return [Hash] Mapping keys to result of touch operation (+true+ if the
|
67
|
-
# operation was successful and +false+ otherwise)
|
68
|
-
#
|
69
|
-
# @example Touch several values
|
70
|
-
# c.touch("foo" => 10, :bar => 20) #=> {"foo" => true, "bar" => true}
|
71
|
-
#
|
72
|
-
# @example Touch several values in async mode
|
73
|
-
# c.run do
|
74
|
-
# c.touch("foo" => 10, :bar => 20) do |ret|
|
75
|
-
# ret.operation #=> :touch
|
76
|
-
# ret.success? #=> true
|
77
|
-
# ret.key #=> "foo" and "bar" in separate calls
|
78
|
-
# end
|
79
|
-
# end
|
80
|
-
#
|
81
|
-
# @example Touch single value
|
82
|
-
# c.touch("foo" => 10) #=> true
|
83
|
-
#
|
84
|
-
def touch(*args)
|
85
|
-
key, ttl, options = expand_touch_args(args)
|
86
|
-
|
87
|
-
case key
|
88
|
-
when String, Symbol
|
89
|
-
success = client_touch(key, ttl)
|
90
|
-
not_found_error(!success, options)
|
91
|
-
success
|
92
|
-
when Hash
|
93
|
-
multi_touch_hash(key, options)
|
94
|
-
when Array
|
95
|
-
multi_touch_array(key, options)
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
def async_touch(key, ttl, &block)
|
100
|
-
register_future(client.touch(key, ttl), { op: :touch }, &block)
|
101
|
-
end
|
102
|
-
|
103
|
-
private
|
104
|
-
|
105
|
-
def expand_touch_args(args)
|
106
|
-
options = extract_options_hash(args)
|
107
|
-
ttl = options[:ttl] || if args.size > 1 && args.last.respond_to?(:to_int?)
|
108
|
-
args.pop
|
109
|
-
else
|
110
|
-
default_ttl
|
111
|
-
end
|
112
|
-
key = args.size > 1 ? args : args.pop
|
113
|
-
|
114
|
-
[validate_key(key), ttl, options]
|
115
|
-
end
|
116
|
-
|
117
|
-
def multi_touch_hash(keys, options = {})
|
118
|
-
{}.tap do |results|
|
119
|
-
keys.each_pair do |key, ttl|
|
120
|
-
results[key] = client_touch(key, ttl)
|
121
|
-
end
|
122
|
-
end
|
123
|
-
end
|
124
|
-
|
125
|
-
def multi_touch_array(keys, options = {})
|
126
|
-
ttl = options[:ttl] || default_ttl
|
127
|
-
|
128
|
-
{}.tap do |results|
|
129
|
-
keys.each do |key|
|
130
|
-
results[key] = client_touch(key, ttl)
|
131
|
-
end
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
|
-
def client_touch(key, ttl, options = {})
|
136
|
-
client.touch(key, ttl).get
|
137
|
-
end
|
138
|
-
|
139
|
-
end
|
140
|
-
end
|