forked from couchbase/couchbase-ruby-client
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcouchbase_set.rb
More file actions
145 lines (132 loc) · 5.14 KB
/
couchbase_set.rb
File metadata and controls
145 lines (132 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# frozen_string_literal: true
# Copyright 2020-2021 Couchbase, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require "couchbase/collection"
require "couchbase/options"
require "couchbase/errors"
module Couchbase
module Datastructures
# A {CouchbaseSet} is implements +Enumerable+ interface and backed by {Collection} document (more specifically
# a JSON array).
#
# Note: sets are restricted to containing primitive types only due to server-side comparison restrictions.
class CouchbaseSet
include Enumerable
# Create a new List, backed by the document identified by +id+ in +collection+.
#
# @param [String] id the id of the document to back the set.
# @param [Collection] collection the collection through which to interact with the document.
# @param [Options::CouchbaseSet] options customization of the datastructure
def initialize(id, collection, options = Options::CouchbaseSet.new)
@id = id
@collection = collection
@options = options
@cas = 0
@observability = @collection.instance_variable_get(:@observability)
@observability = Observability::Wrapper.new if @observability.nil?
end
# Calls the given block once for each element in the set, passing that element as a parameter.
#
# @yieldparam [Object] item
#
# @return [CouchbaseSet, Enumerable]
def each(parent_span: nil, &)
if block_given?
current =
@observability.record_operation(Observability::OP_SET_EACH, parent_span, self) do |obs_handler|
options = @options.get_options.clone
options.parent_span = obs_handler.op_span
result = @collection.get(@id, options)
@cas = result.cas
result.content
rescue Error::DocumentNotFound
@cas = 0
[]
end
current.each(&)
self
else
enum_for(:each, parent_span: parent_span)
end
end
# @return [Integer] returns the number of elements in the set.
def length(parent_span: nil)
@observability.record_operation(Observability::OP_SET_LENGTH, parent_span, self) do |obs_handler|
options = @options.lookup_in_options.clone
options.parent_span = obs_handler.op_span
result = @collection.lookup_in(@id, [
LookupInSpec.count(""),
], options)
result.content(0)
rescue Error::DocumentNotFound
0
end
end
alias size length
# @return [Boolean] returns true if set is empty
def empty?(parent_span: nil)
size(parent_span: parent_span).zero?
end
# Adds the given value to the set
#
# @param [Object] obj
# @return [CouchbaseSet]
def add(obj, parent_span: nil)
@observability.record_operation(Observability::OP_SET_ADD, parent_span, self) do |obs_handler|
options = @options.mutate_in_options.clone
options.parent_span = obs_handler.op_span
@collection.mutate_in(@id, [
MutateInSpec.array_add_unique("", obj),
], options)
rescue Error::PathExists
# ignore
end
self
end
# Removes all elements from the set
def clear(parent_span: nil)
@observability.record_operation(Observability::OP_SET_CLEAR, parent_span, self) do |obs_handler|
options = @options.remove_options.clone
options.parent_span = obs_handler.op_span
@collection.remove(@id, options)
nil
rescue Error::DocumentNotFound
nil
end
end
# Deletes the given object from the set.
#
# @return [Boolean] true if the value has been removed
def delete(obj, parent_span: nil)
@observability.record_operation(Observability::OP_SET_DELETE, parent_span, self) do |obs_handler|
result = @collection.get(@id, Options::Get.new(parent_span: obs_handler.op_span))
idx = result.content.index(obj)
return false unless idx
options = Options::MutateIn.new(parent_span: obs_handler.op_span)
options.cas = result.cas
@collection.mutate_in(@id, [
MutateInSpec.remove("[#{idx}]"),
], options)
true
rescue Error::CasMismatch
retry
rescue Error::DocumentNotFound
false
end
end
end
# @api private
CouchbaseSetOptions = ::Couchbase::Options::CouchbaseSet
end
end