Class: Couchbase::Datastructures::CouchbaseQueue
- Inherits:
-
Object
- Object
- Couchbase::Datastructures::CouchbaseQueue
- Includes:
- Enumerable
- Defined in:
- lib/couchbase/datastructures/couchbase_queue.rb,
/home/runner/work/couchbase-ruby-client/couchbase-ruby-client/lib/couchbase/datastructures/couchbase_queue.rb
Overview
A CouchbaseQueue is implements FIFO queue with 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
-
#clear(parent_span: nil) ⇒ Object
Removes all elements from the queue.
-
#each(parent_span: nil) {|item| ... } ⇒ CouchbaseQueue, Enumerable
Calls the given block once for each element in the queue, passing that element as a parameter.
-
#empty?(parent_span: nil) ⇒ Boolean
Returns true if queue is empty.
-
#initialize(id, collection, options = Options::CouchbaseQueue.new) ⇒ CouchbaseQueue
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 queue.
-
#pop(parent_span: nil) ⇒ Object?
(also: #deq, #shift)
Retrieves object from the queue.
-
#push(obj, parent_span: nil) ⇒ CouchbaseQueue
(also: #enq, #<<)
Adds the given value to the queue.
Constructor Details
#initialize(id, collection, options = Options::CouchbaseQueue.new) ⇒ CouchbaseQueue
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_queue.rb', line 35 def initialize(id, collection, = Options::CouchbaseQueue.new) @id = id @collection = collection @options = @cas = 0 @observability = @collection.instance_variable_get(:@observability) end |
Instance Method Details
#clear(parent_span: nil) ⇒ Object
Removes all elements from the queue
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/couchbase/datastructures/couchbase_queue.rb', line 89 def clear(parent_span: nil) @observability.record_operation(Observability::OP_QUEUE_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 |
#each(parent_span: nil) {|item| ... } ⇒ CouchbaseQueue, Enumerable
Calls the given block once for each element in the queue, passing that element as a parameter.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/couchbase/datastructures/couchbase_queue.rb', line 48 def each(parent_span: nil, &) if block_given? current = @observability.record_operation(Observability::OP_QUEUE_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 queue is empty
84 85 86 |
# File 'lib/couchbase/datastructures/couchbase_queue.rb', line 84 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 queue.
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/couchbase/datastructures/couchbase_queue.rb', line 68 def length(parent_span: nil) @observability.record_operation(Observability::OP_QUEUE_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 |
#pop(parent_span: nil) ⇒ Object? Also known as: deq, shift
Retrieves object from the queue
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/couchbase/datastructures/couchbase_queue.rb', line 123 def pop(parent_span: nil) @observability.record_operation(Observability::OP_QUEUE_POP, parent_span, self) do |obs_handler| obj, cas = begin = @options..clone .parent_span = obs_handler.op_span result = @collection.lookup_in(@id, [ LookupInSpec.get("[-1]"), ], @options.) [ result.exists?(0) ? result.content(0) : nil, result.cas, ] end begin = Options::MutateIn.new( parent_span: obs_handler.op_span, cas: cas, ) @collection.mutate_in(@id, [ MutateInSpec.remove("[-1]"), ], ) end obj rescue Error::CasMismatch retry rescue Error::DocumentNotFound, Error::PathNotFound nil end end |
#push(obj, parent_span: nil) ⇒ CouchbaseQueue Also known as: enq, <<
Adds the given value to the queue
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/couchbase/datastructures/couchbase_queue.rb', line 104 def push(obj, parent_span: nil) @observability.record_operation(Observability::OP_QUEUE_PUSH, parent_span, self) do |obs_handler| = @options..clone .parent_span = obs_handler.op_span @collection.mutate_in(@id, [ MutateInSpec.array_prepend("", [obj]), ], ) rescue Error::PathExists # ignore end self end |