Class: Couchbase::Datastructures::CouchbaseSet
- Inherits:
-
Object
- Object
- Couchbase::Datastructures::CouchbaseSet
- Includes:
- Enumerable
- Defined in:
- lib/couchbase/datastructures/couchbase_set.rb,
/home/runner/work/couchbase-ruby-client/couchbase-ruby-client/lib/couchbase/datastructures/couchbase_set.rb
Overview
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.
Instance Method Summary collapse
-
#add(obj, parent_span: nil) ⇒ CouchbaseSet
Adds the given value to the set.
-
#clear(parent_span: nil) ⇒ Object
Removes all elements from the set.
-
#delete(obj, parent_span: nil) ⇒ Boolean
Deletes the given object from the set.
-
#each(parent_span: nil) {|item| ... } ⇒ CouchbaseSet, Enumerable
Calls the given block once for each element in the set, passing that element as a parameter.
-
#empty?(parent_span: nil) ⇒ Boolean
Returns true if set is empty.
-
#initialize(id, collection, options = Options::CouchbaseSet.new) ⇒ CouchbaseSet
constructor
Create a new List, backed by the document identified by
idincollection. -
#length(parent_span: nil) ⇒ Integer
(also: #size)
Returns the number of elements in the set.
Constructor Details
#initialize(id, collection, options = Options::CouchbaseSet.new) ⇒ CouchbaseSet
Create a new List, backed by the document identified by id in collection.
35 36 37 38 39 40 41 |
# File 'lib/couchbase/datastructures/couchbase_set.rb', line 35 def initialize(id, collection, = Options::CouchbaseSet.new) @id = id @collection = collection @options = @cas = 0 @observability = @collection.instance_variable_get(:@observability) end |
Instance Method Details
#add(obj, parent_span: nil) ⇒ CouchbaseSet
Adds the given value to the set
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/couchbase/datastructures/couchbase_set.rb', line 93 def add(obj, parent_span: nil) @observability.record_operation(Observability::OP_SET_ADD, parent_span, self) do |obs_handler| = @options..clone .parent_span = obs_handler.op_span @collection.mutate_in(@id, [ MutateInSpec.array_add_unique("", obj), ], ) rescue Error::PathExists # ignore end self end |
#clear(parent_span: nil) ⇒ Object
Removes all elements from the set
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/couchbase/datastructures/couchbase_set.rb', line 107 def clear(parent_span: nil) @observability.record_operation(Observability::OP_SET_CLEAR, parent_span, self) do |obs_handler| = @options..clone .parent_span = obs_handler.op_span @collection.remove(@id, ) nil rescue Error::DocumentNotFound nil end end |
#delete(obj, parent_span: nil) ⇒ Boolean
Deletes the given object from the set.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/couchbase/datastructures/couchbase_set.rb', line 121 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::MutateIn.new(parent_span: obs_handler.op_span) .cas = result.cas @collection.mutate_in(@id, [ MutateInSpec.remove("[#{idx}]"), ], ) true rescue Error::CasMismatch retry rescue Error::DocumentNotFound false end end |
#each(parent_span: nil) {|item| ... } ⇒ CouchbaseSet, Enumerable
Calls the given block once for each element in the set, passing that element as a parameter.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/couchbase/datastructures/couchbase_set.rb', line 48 def each(parent_span: nil, &) if block_given? current = @observability.record_operation(Observability::OP_SET_EACH, parent_span, self) do |obs_handler| = @options..clone .parent_span = obs_handler.op_span result = @collection.get(@id, ) @cas = result.cas result.content rescue Error::DocumentNotFound @cas = 0 [] end current.each(&) self else enum_for(:each, parent_span: parent_span) end end |
#empty?(parent_span: nil) ⇒ Boolean
Returns true if set is empty
85 86 87 |
# File 'lib/couchbase/datastructures/couchbase_set.rb', line 85 def empty?(parent_span: nil) size(parent_span: parent_span).zero? end |
#length(parent_span: nil) ⇒ Integer Also known as: size
Returns the number of elements in the set.
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/couchbase/datastructures/couchbase_set.rb', line 69 def length(parent_span: nil) @observability.record_operation(Observability::OP_SET_LENGTH, parent_span, self) do |obs_handler| = @options..clone .parent_span = obs_handler.op_span result = @collection.lookup_in(@id, [ LookupInSpec.count(""), ], ) result.content(0) rescue Error::DocumentNotFound 0 end end |