Class: Couchbase::Datastructures::CouchbaseQueue
- Inherits:
-
Object
- Object
- Couchbase::Datastructures::CouchbaseQueue
- Includes:
- Enumerable
- Defined in:
- 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 ⇒ Object
Removes all elements from the queue.
-
#each {|item| ... } ⇒ CouchbaseQueue, Enumerable
Calls the given block once for each element in the queue, passing that element as a parameter.
-
#empty? ⇒ Boolean
Returns true if queue is empty.
-
#initialize(id, collection, options = CouchbaseQueueOptions.new) ⇒ CouchbaseQueue
constructor
Create a new List, backed by the document identified by
id
incollection
. -
#length ⇒ Integer
(also: #size)
Returns the number of elements in the queue.
-
#pop ⇒ Object?
(also: #deq, #shift)
Retrieves object from the queue.
-
#push(obj) ⇒ CouchbaseQueue
(also: #enq, #<<)
Adds the given value to the queue.
Constructor Details
#initialize(id, collection, options = CouchbaseQueueOptions.new) ⇒ CouchbaseQueue
Create a new List, backed by the document identified by id
in collection
.
32 33 34 35 36 37 |
# File 'lib/couchbase/datastructures/couchbase_queue.rb', line 32 def initialize(id, collection, = CouchbaseQueueOptions.new) @id = id @collection = collection @options = @cas = 0 end |
Instance Method Details
#clear ⇒ Object
Removes all elements from the queue
81 82 83 84 85 86 |
# File 'lib/couchbase/datastructures/couchbase_queue.rb', line 81 def clear @collection.remove(@id, @options.) nil rescue Error::DocumentNotFound nil end |
#each {|item| ... } ⇒ CouchbaseQueue, Enumerable
Calls the given block once for each element in the queue, passing that element as a parameter.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/couchbase/datastructures/couchbase_queue.rb', line 44 def each if block_given? begin result = @collection.get(@id, @options.) current = result.content @cas = result.cas rescue Error::DocumentNotFound current = [] @cas = 0 end current.each do |entry| yield entry end self else enum_for(:each) end end |
#empty? ⇒ Boolean
Returns true if queue is empty
76 77 78 |
# File 'lib/couchbase/datastructures/couchbase_queue.rb', line 76 def empty? size.zero? end |
#length ⇒ Integer Also known as: size
Returns the number of elements in the queue.
64 65 66 67 68 69 70 71 |
# File 'lib/couchbase/datastructures/couchbase_queue.rb', line 64 def length result = @collection.lookup_in(@id, [ LookupInSpec.count("") ], @options.) result.content(0) rescue Error::DocumentNotFound 0 end |
#pop ⇒ Object? Also known as: deq, shift
Retrieves object from the queue
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/couchbase/datastructures/couchbase_queue.rb', line 109 def pop result = @collection.lookup_in(@id, [ LookupInSpec.get("[-1]") ], @options.) obj = result.exists?(0) ? result.content(0) : nil = Collection::MutateInOptions.new .cas = result.cas @collection.mutate_in(@id, [ MutateInSpec.remove("[-1]") ], ) obj rescue Error::CasMismatch retry rescue Error::DocumentNotFound, Error::PathNotFound nil end |
#push(obj) ⇒ CouchbaseQueue Also known as: enq, <<
Adds the given value to the queue
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/couchbase/datastructures/couchbase_queue.rb', line 92 def push(obj) begin @collection.mutate_in(@id, [ MutateInSpec.array_prepend("", [obj]) ], @options.) rescue Error::PathExists # ignore end self end |