couchbase 3.0.0.alpha.5 → 3.0.0.beta.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +12 -3
- data/README.md +4 -2
- data/Rakefile +1 -1
- data/couchbase.gemspec +17 -12
- data/ext/.idea/misc.xml +12 -0
- data/ext/CMakeLists.txt +10 -1
- data/ext/build_config.hxx.in +20 -0
- data/ext/build_version.hxx.in +1 -1
- data/ext/couchbase/bucket.hxx +90 -24
- data/ext/couchbase/cluster.hxx +125 -84
- data/ext/couchbase/cluster_options.hxx +53 -0
- data/ext/couchbase/configuration.hxx +220 -2
- data/ext/couchbase/couchbase.cxx +134 -127
- data/ext/couchbase/io/dns_client.hxx +3 -1
- data/ext/couchbase/io/http_command.hxx +91 -0
- data/ext/couchbase/io/http_session.hxx +58 -19
- data/ext/couchbase/io/http_session_manager.hxx +26 -31
- data/ext/couchbase/io/mcbp_command.hxx +180 -0
- data/ext/couchbase/io/mcbp_message.hxx +5 -0
- data/ext/couchbase/io/mcbp_session.hxx +213 -98
- data/ext/couchbase/io/streams.hxx +165 -0
- data/ext/couchbase/operations.hxx +1 -1
- data/ext/couchbase/operations/analytics_dataset_create.hxx +1 -1
- data/ext/couchbase/operations/bucket_create.hxx +4 -2
- data/ext/couchbase/operations/bucket_drop.hxx +4 -2
- data/ext/couchbase/operations/bucket_flush.hxx +4 -2
- data/ext/couchbase/operations/bucket_get.hxx +4 -2
- data/ext/couchbase/operations/bucket_get_all.hxx +4 -2
- data/ext/couchbase/operations/bucket_update.hxx +4 -2
- data/ext/couchbase/operations/cluster_developer_preview_enable.hxx +4 -2
- data/ext/couchbase/operations/collection_create.hxx +4 -2
- data/ext/couchbase/operations/collection_drop.hxx +4 -2
- data/ext/couchbase/operations/document_analytics.hxx +0 -4
- data/ext/couchbase/operations/document_decrement.hxx +6 -3
- data/ext/couchbase/operations/document_get.hxx +3 -0
- data/ext/couchbase/operations/document_get_and_lock.hxx +3 -0
- data/ext/couchbase/operations/document_get_and_touch.hxx +5 -2
- data/ext/couchbase/operations/document_get_projected.hxx +12 -9
- data/ext/couchbase/operations/document_increment.hxx +6 -3
- data/ext/couchbase/operations/document_insert.hxx +5 -2
- data/ext/couchbase/operations/document_lookup_in.hxx +3 -0
- data/ext/couchbase/operations/document_mutate_in.hxx +6 -3
- data/ext/couchbase/operations/document_remove.hxx +3 -0
- data/ext/couchbase/operations/document_replace.hxx +5 -2
- data/ext/couchbase/operations/document_search.hxx +6 -7
- data/ext/couchbase/operations/document_touch.hxx +5 -2
- data/ext/couchbase/operations/document_unlock.hxx +3 -0
- data/ext/couchbase/operations/document_upsert.hxx +5 -2
- data/ext/couchbase/operations/query_index_build_deferred.hxx +3 -3
- data/ext/couchbase/operations/query_index_create.hxx +3 -3
- data/ext/couchbase/operations/query_index_drop.hxx +3 -3
- data/ext/couchbase/operations/query_index_get_all.hxx +3 -3
- data/ext/couchbase/operations/scope_create.hxx +4 -2
- data/ext/couchbase/operations/scope_drop.hxx +4 -2
- data/ext/couchbase/operations/scope_get_all.hxx +4 -2
- data/ext/couchbase/operations/search_index_analyze_document.hxx +2 -2
- data/ext/couchbase/operations/search_index_control_ingest.hxx +2 -2
- data/ext/couchbase/operations/search_index_control_plan_freeze.hxx +2 -2
- data/ext/couchbase/operations/search_index_control_query.hxx +2 -2
- data/ext/couchbase/operations/search_index_drop.hxx +2 -2
- data/ext/couchbase/operations/search_index_get.hxx +2 -2
- data/ext/couchbase/operations/search_index_get_all.hxx +2 -2
- data/ext/couchbase/operations/search_index_get_documents_count.hxx +2 -2
- data/ext/couchbase/operations/search_index_upsert.hxx +2 -2
- data/ext/couchbase/origin.hxx +148 -0
- data/ext/couchbase/protocol/cmd_cluster_map_change_notification.hxx +1 -6
- data/ext/couchbase/protocol/cmd_decrement.hxx +5 -5
- data/ext/couchbase/protocol/cmd_get_and_touch.hxx +5 -5
- data/ext/couchbase/protocol/cmd_get_cluster_config.hxx +1 -6
- data/ext/couchbase/protocol/cmd_increment.hxx +5 -5
- data/ext/couchbase/protocol/cmd_info.hxx +0 -11
- data/ext/couchbase/protocol/cmd_insert.hxx +5 -5
- data/ext/couchbase/protocol/cmd_mutate_in.hxx +6 -6
- data/ext/couchbase/protocol/cmd_replace.hxx +5 -5
- data/ext/couchbase/protocol/cmd_touch.hxx +1 -1
- data/ext/couchbase/protocol/cmd_upsert.hxx +5 -5
- data/ext/couchbase/timeout_defaults.hxx +7 -0
- data/ext/couchbase/utils/connection_string.hxx +139 -0
- data/ext/extconf.rb +44 -11
- data/ext/test/main.cxx +93 -15
- data/ext/third_party/http_parser/Makefile +160 -0
- data/ext/third_party/json/Makefile +77 -0
- data/lib/couchbase/analytics_options.rb +18 -4
- data/lib/couchbase/binary_collection.rb +2 -2
- data/lib/couchbase/binary_collection_options.rb +2 -2
- data/lib/couchbase/bucket.rb +4 -4
- data/lib/couchbase/cluster.rb +60 -46
- data/lib/couchbase/collection.rb +13 -13
- data/lib/couchbase/collection_options.rb +15 -9
- data/{bin/console → lib/couchbase/datastructures.rb} +4 -7
- data/lib/couchbase/datastructures/couchbase_list.rb +171 -0
- data/lib/couchbase/datastructures/couchbase_map.rb +205 -0
- data/lib/couchbase/datastructures/couchbase_queue.rb +145 -0
- data/lib/couchbase/datastructures/couchbase_set.rb +138 -0
- data/lib/couchbase/errors.rb +66 -63
- data/lib/couchbase/management/user_manager.rb +1 -1
- data/lib/couchbase/mutation_state.rb +1 -0
- data/lib/couchbase/query_options.rb +25 -2
- data/lib/couchbase/scope.rb +0 -7
- data/lib/couchbase/search_options.rb +7 -0
- data/lib/couchbase/version.rb +1 -1
- data/lib/couchbase/view_options.rb +4 -3
- metadata +20 -82
- data/.github/workflows/tests-6.0.3.yml +0 -52
- data/.github/workflows/tests-dev-preview.yml +0 -55
- data/.github/workflows/tests.yml +0 -50
- data/.gitignore +0 -20
- data/.gitmodules +0 -21
- data/.idea/.gitignore +0 -5
- data/.idea/dictionaries/gem_terms.xml +0 -18
- data/.idea/inspectionProfiles/Project_Default.xml +0 -8
- data/.idea/vcs.xml +0 -13
- data/bin/check-cluster +0 -31
- data/bin/fetch-stats +0 -19
- data/bin/init-cluster +0 -82
- data/bin/jenkins/build-extension +0 -35
- data/bin/jenkins/install-dependencies +0 -47
- data/bin/jenkins/test-with-cbdyncluster +0 -58
- data/bin/setup +0 -24
- data/ext/couchbase/configuration_monitor.hxx +0 -93
- data/ext/couchbase/operations/command.hxx +0 -163
- data/rbi/couchbase.rbi +0 -79
@@ -0,0 +1,205 @@
|
|
1
|
+
# Copyright 2020 Couchbase, Inc.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
require "couchbase/collection"
|
16
|
+
require "couchbase/errors"
|
17
|
+
|
18
|
+
module Couchbase
|
19
|
+
module Datastructures
|
20
|
+
# A {CouchbaseMap} is implements +Enumerable+ interface and backed by {Collection} document (more specifically
|
21
|
+
# a JSON array).
|
22
|
+
#
|
23
|
+
# Note that as such, a {CouchbaseMap} is restricted to the types that JSON array can contain.
|
24
|
+
class CouchbaseMap
|
25
|
+
include Enumerable
|
26
|
+
|
27
|
+
# Create a new Map, backed by the document identified by +id+ in +collection+.
|
28
|
+
#
|
29
|
+
# @param [String] id the id of the document to back the map.
|
30
|
+
# @param [Collection] collection the Couchbase collection through which to interact with the document.
|
31
|
+
# @param [CouchbaseMapOptions] options customization of the datastructure
|
32
|
+
def initialize(id, collection, options = CouchbaseMapOptions.new)
|
33
|
+
@id = id
|
34
|
+
@collection = collection
|
35
|
+
@options = options
|
36
|
+
@cas = 0
|
37
|
+
end
|
38
|
+
|
39
|
+
# Calls the given block once for each element in the map, passing that element as a parameter.
|
40
|
+
#
|
41
|
+
# @yieldparam [Object] item
|
42
|
+
#
|
43
|
+
# @return [CouchbaseMap, Enumerable]
|
44
|
+
def each
|
45
|
+
if block_given?
|
46
|
+
begin
|
47
|
+
result = @collection.get(@id, @options.get_options)
|
48
|
+
current = result.content
|
49
|
+
@cas = result.cas
|
50
|
+
rescue Error::DocumentNotFound
|
51
|
+
current = []
|
52
|
+
@cas = 0
|
53
|
+
end
|
54
|
+
current.each do |key, value|
|
55
|
+
yield key, value
|
56
|
+
end
|
57
|
+
self
|
58
|
+
else
|
59
|
+
enum_for(:each)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# @return [Integer] returns the number of elements in the map.
|
64
|
+
def length
|
65
|
+
result = @collection.lookup_in(@id, [
|
66
|
+
LookupInSpec.count("")
|
67
|
+
], @options.lookup_in_options)
|
68
|
+
result.content(0)
|
69
|
+
rescue Error::DocumentNotFound
|
70
|
+
0
|
71
|
+
end
|
72
|
+
|
73
|
+
alias_method :size, :length
|
74
|
+
|
75
|
+
# @return [Boolean] returns true if map is empty
|
76
|
+
def empty?
|
77
|
+
size.zero?
|
78
|
+
end
|
79
|
+
|
80
|
+
# Removes all elements from the map
|
81
|
+
def clear
|
82
|
+
@collection.remove(@id, @options.remove_options)
|
83
|
+
nil
|
84
|
+
rescue Error::DocumentNotFound
|
85
|
+
nil
|
86
|
+
end
|
87
|
+
|
88
|
+
# Returns a value from the map for the given key.
|
89
|
+
#
|
90
|
+
# If the key cannot be found, there are several options:
|
91
|
+
#
|
92
|
+
# * with no other arguments, it will raise a KeyError exception
|
93
|
+
# * if +default+ is given, then that will be returned
|
94
|
+
# * if the optional code +block+ is specified, then that will be run and its result returned
|
95
|
+
#
|
96
|
+
# @overload fetch(key)
|
97
|
+
# Gets the value, associated with the key, or raise KeyError if key could not be found
|
98
|
+
# @param key [String] key
|
99
|
+
#
|
100
|
+
# @overload fetch(key, default)
|
101
|
+
# Gets the value, associated with the key, or return +default+ if key could not be found
|
102
|
+
# @param key [String] key
|
103
|
+
#
|
104
|
+
# @overload fetch(key, &block)
|
105
|
+
# Gets the value, associated with the key, or invoke specified +block+ and propagate its return value
|
106
|
+
# if key could not be found
|
107
|
+
# @param key [String] key
|
108
|
+
# @yieldreturn [Object] the default value to return in case the key could not be found
|
109
|
+
#
|
110
|
+
# @return [Object]
|
111
|
+
def fetch(key, *rest)
|
112
|
+
result = @collection.lookup_in(@id, [
|
113
|
+
LookupInSpec.get(key)
|
114
|
+
], @options.lookup_in_options)
|
115
|
+
result.content(0)
|
116
|
+
rescue Error::DocumentNotFound, Error::PathNotFound
|
117
|
+
return yield if block_given?
|
118
|
+
return rest.first if rest.size > 0
|
119
|
+
raise KeyError, "key not found: #{key}"
|
120
|
+
end
|
121
|
+
|
122
|
+
# Returns a value from the map for the given key.
|
123
|
+
#
|
124
|
+
# If the key cannot be found, +nil+ will be returned.
|
125
|
+
#
|
126
|
+
# @param [String] key
|
127
|
+
#
|
128
|
+
# @return [Object, nil] value, associated with the key or +nil+
|
129
|
+
def [](key)
|
130
|
+
fetch(key, nil)
|
131
|
+
end
|
132
|
+
|
133
|
+
# Associate the value given by +value+ with the key given by +key+.
|
134
|
+
#
|
135
|
+
# @param [String] key
|
136
|
+
# @param [Object] value
|
137
|
+
#
|
138
|
+
# @return Object
|
139
|
+
def []=(key, value)
|
140
|
+
@collection.mutate_in(@id, [
|
141
|
+
MutateInSpec.upsert(key, value)
|
142
|
+
], @options.mutate_in_options)
|
143
|
+
value
|
144
|
+
end
|
145
|
+
|
146
|
+
# Deletes the key-value pair from the map.
|
147
|
+
#
|
148
|
+
# @param [String] key
|
149
|
+
#
|
150
|
+
# @return void
|
151
|
+
def delete(key)
|
152
|
+
@collection.mutate_in(@id, [
|
153
|
+
MutateInSpec.remove(key)
|
154
|
+
])
|
155
|
+
rescue Error::DocumentNotFound, Error::PathNotFound
|
156
|
+
nil
|
157
|
+
end
|
158
|
+
|
159
|
+
# Returns +true+ if the given key is present
|
160
|
+
#
|
161
|
+
# @param [String] key
|
162
|
+
# @return [Boolean]
|
163
|
+
def key?(key)
|
164
|
+
result = @collection.lookup_in(@id, [
|
165
|
+
LookupInSpec.exists(key)
|
166
|
+
], @options.lookup_in_options)
|
167
|
+
result.exists?(0)
|
168
|
+
rescue Error::DocumentNotFound, Error::PathNotFound
|
169
|
+
false
|
170
|
+
end
|
171
|
+
|
172
|
+
alias_method :member?, :key?
|
173
|
+
alias_method :include?, :key?
|
174
|
+
|
175
|
+
# Returns a new array populated with the keys from the map.
|
176
|
+
#
|
177
|
+
# @return [Array]
|
178
|
+
def keys
|
179
|
+
map {|key, _value| key}
|
180
|
+
end
|
181
|
+
|
182
|
+
# Returns a new array populated with the values from the map.
|
183
|
+
#
|
184
|
+
# @return [Array]
|
185
|
+
def values
|
186
|
+
map {|_key, value| value}
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
class CouchbaseMapOptions
|
191
|
+
attr_accessor :get_options
|
192
|
+
attr_accessor :lookup_in_options
|
193
|
+
attr_accessor :mutate_in_options
|
194
|
+
attr_accessor :remove_options
|
195
|
+
|
196
|
+
def initialize
|
197
|
+
@get_options = Collection::GetOptions.new
|
198
|
+
@remove_options = Collection::RemoveOptions.new
|
199
|
+
@lookup_in_options = Collection::LookupInOptions.new
|
200
|
+
@mutate_in_options = Collection::MutateInOptions.new
|
201
|
+
@mutate_in_options.store_semantics = :upsert
|
202
|
+
end
|
203
|
+
end
|
204
|
+
end
|
205
|
+
end
|
@@ -0,0 +1,145 @@
|
|
1
|
+
# Copyright 2020 Couchbase, Inc.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
require "couchbase/collection"
|
16
|
+
require "couchbase/errors"
|
17
|
+
|
18
|
+
module Couchbase
|
19
|
+
module Datastructures
|
20
|
+
# A {CouchbaseQueue} is implements FIFO queue with +Enumerable+ interface and backed by {Collection} document
|
21
|
+
# (more specifically a JSON array).
|
22
|
+
#
|
23
|
+
# Note: sets are restricted to containing primitive types only due to server-side comparison restrictions.
|
24
|
+
class CouchbaseQueue
|
25
|
+
include Enumerable
|
26
|
+
|
27
|
+
# Create a new List, backed by the document identified by +id+ in +collection+.
|
28
|
+
#
|
29
|
+
# @param [String] id the id of the document to back the queue.
|
30
|
+
# @param [Collection] collection the collection through which to interact with the document.
|
31
|
+
# @param [CouchbaseListOptions] options customization of the datastructure
|
32
|
+
def initialize(id, collection, options = CouchbaseQueueOptions.new)
|
33
|
+
@id = id
|
34
|
+
@collection = collection
|
35
|
+
@options = options
|
36
|
+
@cas = 0
|
37
|
+
end
|
38
|
+
|
39
|
+
# Calls the given block once for each element in the queue, passing that element as a parameter.
|
40
|
+
#
|
41
|
+
# @yieldparam [Object] item
|
42
|
+
#
|
43
|
+
# @return [CouchbaseQueue, Enumerable]
|
44
|
+
def each
|
45
|
+
if block_given?
|
46
|
+
begin
|
47
|
+
result = @collection.get(@id, @options.get_options)
|
48
|
+
current = result.content
|
49
|
+
@cas = result.cas
|
50
|
+
rescue Error::DocumentNotFound
|
51
|
+
current = []
|
52
|
+
@cas = 0
|
53
|
+
end
|
54
|
+
current.each do |entry|
|
55
|
+
yield entry
|
56
|
+
end
|
57
|
+
self
|
58
|
+
else
|
59
|
+
enum_for(:each)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# @return [Integer] returns the number of elements in the queue.
|
64
|
+
def length
|
65
|
+
result = @collection.lookup_in(@id, [
|
66
|
+
LookupInSpec.count("")
|
67
|
+
], @options.lookup_in_options)
|
68
|
+
result.content(0)
|
69
|
+
rescue Error::DocumentNotFound
|
70
|
+
0
|
71
|
+
end
|
72
|
+
|
73
|
+
alias_method :size, :length
|
74
|
+
|
75
|
+
# @return [Boolean] returns true if queue is empty
|
76
|
+
def empty?
|
77
|
+
size.zero?
|
78
|
+
end
|
79
|
+
|
80
|
+
# Removes all elements from the queue
|
81
|
+
def clear
|
82
|
+
@collection.remove(@id, @options.remove_options)
|
83
|
+
nil
|
84
|
+
rescue Error::DocumentNotFound
|
85
|
+
nil
|
86
|
+
end
|
87
|
+
|
88
|
+
# Adds the given value to the queue
|
89
|
+
#
|
90
|
+
# @param [Object] obj
|
91
|
+
# @return [CouchbaseQueue]
|
92
|
+
def push(obj)
|
93
|
+
begin
|
94
|
+
@collection.mutate_in(@id, [
|
95
|
+
MutateInSpec.array_prepend("", [obj])
|
96
|
+
], @options.mutate_in_options)
|
97
|
+
rescue Error::PathExists
|
98
|
+
# ignore
|
99
|
+
end
|
100
|
+
self
|
101
|
+
end
|
102
|
+
|
103
|
+
alias_method :enq, :push
|
104
|
+
alias_method :<<, :push
|
105
|
+
|
106
|
+
# Retrieves object from the queue
|
107
|
+
#
|
108
|
+
# @return [Object, nil] queue entry or nil
|
109
|
+
def pop
|
110
|
+
result = @collection.lookup_in(@id, [
|
111
|
+
LookupInSpec.get("[-1]")
|
112
|
+
], @options.lookup_in_options)
|
113
|
+
obj = result.exists?(0) ? result.content(0) : nil
|
114
|
+
options = Collection::MutateInOptions.new
|
115
|
+
options.cas = result.cas
|
116
|
+
@collection.mutate_in(@id, [
|
117
|
+
MutateInSpec.remove("[-1]")
|
118
|
+
], options)
|
119
|
+
obj
|
120
|
+
rescue Error::CasMismatch
|
121
|
+
retry
|
122
|
+
rescue Error::DocumentNotFound, Error::PathNotFound
|
123
|
+
nil
|
124
|
+
end
|
125
|
+
|
126
|
+
alias_method :deq, :pop
|
127
|
+
alias_method :shift, :pop
|
128
|
+
end
|
129
|
+
|
130
|
+
class CouchbaseQueueOptions
|
131
|
+
attr_accessor :get_options
|
132
|
+
attr_accessor :lookup_in_options
|
133
|
+
attr_accessor :mutate_in_options
|
134
|
+
attr_accessor :remove_options
|
135
|
+
|
136
|
+
def initialize
|
137
|
+
@get_options = Collection::GetOptions.new
|
138
|
+
@remove_options = Collection::RemoveOptions.new
|
139
|
+
@lookup_in_options = Collection::LookupInOptions.new
|
140
|
+
@mutate_in_options = Collection::MutateInOptions.new
|
141
|
+
@mutate_in_options.store_semantics = :upsert
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
@@ -0,0 +1,138 @@
|
|
1
|
+
# Copyright 2020 Couchbase, Inc.
|
2
|
+
#
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
# you may not use this file except in compliance with the License.
|
5
|
+
# You may obtain a copy of the License at
|
6
|
+
#
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
#
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
# See the License for the specific language governing permissions and
|
13
|
+
# limitations under the License.
|
14
|
+
|
15
|
+
require "couchbase/collection"
|
16
|
+
require "couchbase/errors"
|
17
|
+
|
18
|
+
module Couchbase
|
19
|
+
module Datastructures
|
20
|
+
# A {CouchbaseSet} is implements +Enumerable+ interface and backed by {Collection} document (more specifically
|
21
|
+
# a JSON array).
|
22
|
+
#
|
23
|
+
# Note: sets are restricted to containing primitive types only due to server-side comparison restrictions.
|
24
|
+
class CouchbaseSet
|
25
|
+
include Enumerable
|
26
|
+
|
27
|
+
# Create a new List, backed by the document identified by +id+ in +collection+.
|
28
|
+
#
|
29
|
+
# @param [String] id the id of the document to back the set.
|
30
|
+
# @param [Collection] collection the collection through which to interact with the document.
|
31
|
+
# @param [CouchbaseListOptions] options customization of the datastructure
|
32
|
+
def initialize(id, collection, options = CouchbaseSetOptions.new)
|
33
|
+
@id = id
|
34
|
+
@collection = collection
|
35
|
+
@options = options
|
36
|
+
@cas = 0
|
37
|
+
end
|
38
|
+
|
39
|
+
# Calls the given block once for each element in the set, passing that element as a parameter.
|
40
|
+
#
|
41
|
+
# @yieldparam [Object] item
|
42
|
+
#
|
43
|
+
# @return [CouchbaseSet, Enumerable]
|
44
|
+
def each
|
45
|
+
if block_given?
|
46
|
+
begin
|
47
|
+
result = @collection.get(@id, @options.get_options)
|
48
|
+
current = result.content
|
49
|
+
@cas = result.cas
|
50
|
+
rescue Error::DocumentNotFound
|
51
|
+
current = []
|
52
|
+
@cas = 0
|
53
|
+
end
|
54
|
+
current.each do |entry|
|
55
|
+
yield entry
|
56
|
+
end
|
57
|
+
self
|
58
|
+
else
|
59
|
+
enum_for(:each)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# @return [Integer] returns the number of elements in the set.
|
64
|
+
def length
|
65
|
+
result = @collection.lookup_in(@id, [
|
66
|
+
LookupInSpec.count("")
|
67
|
+
], @options.lookup_in_options)
|
68
|
+
result.content(0)
|
69
|
+
rescue Error::DocumentNotFound
|
70
|
+
0
|
71
|
+
end
|
72
|
+
|
73
|
+
alias_method :size, :length
|
74
|
+
|
75
|
+
# @return [Boolean] returns true if set is empty
|
76
|
+
def empty?
|
77
|
+
size.zero?
|
78
|
+
end
|
79
|
+
|
80
|
+
# Adds the given value to the set
|
81
|
+
#
|
82
|
+
# @param [Object] obj
|
83
|
+
# @return [CouchbaseSet]
|
84
|
+
def add(obj)
|
85
|
+
begin
|
86
|
+
@collection.mutate_in(@id, [
|
87
|
+
MutateInSpec.array_add_unique("", obj)
|
88
|
+
], @options.mutate_in_options)
|
89
|
+
rescue Error::PathExists
|
90
|
+
# ignore
|
91
|
+
end
|
92
|
+
self
|
93
|
+
end
|
94
|
+
|
95
|
+
# Removes all elements from the set
|
96
|
+
def clear
|
97
|
+
@collection.remove(@id, @options.remove_options)
|
98
|
+
nil
|
99
|
+
rescue Error::DocumentNotFound
|
100
|
+
nil
|
101
|
+
end
|
102
|
+
|
103
|
+
# Deletes the given object from the set.
|
104
|
+
#
|
105
|
+
# @return [Boolean] true if the value has been removed
|
106
|
+
def delete(obj)
|
107
|
+
result = @collection.get(@id)
|
108
|
+
idx = result.content.index(obj)
|
109
|
+
return false unless idx
|
110
|
+
options = Collection::MutateInOptions.new
|
111
|
+
options.cas = result.cas
|
112
|
+
@collection.mutate_in(@id, [
|
113
|
+
MutateInSpec.remove("[#{idx}]")
|
114
|
+
], options)
|
115
|
+
true
|
116
|
+
rescue Error::CasMismatch
|
117
|
+
retry
|
118
|
+
rescue Error::DocumentNotFound
|
119
|
+
false
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
class CouchbaseSetOptions
|
124
|
+
attr_accessor :get_options
|
125
|
+
attr_accessor :lookup_in_options
|
126
|
+
attr_accessor :mutate_in_options
|
127
|
+
attr_accessor :remove_options
|
128
|
+
|
129
|
+
def initialize
|
130
|
+
@get_options = Collection::GetOptions.new
|
131
|
+
@remove_options = Collection::RemoveOptions.new
|
132
|
+
@lookup_in_options = Collection::LookupInOptions.new
|
133
|
+
@mutate_in_options = Collection::MutateInOptions.new
|
134
|
+
@mutate_in_options.store_semantics = :upsert
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|