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 = Options::CouchbaseQueue.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 = Options::CouchbaseQueue.new) ⇒ CouchbaseQueue
Create a new List, backed by the document identified by id
in collection
.
35 36 37 38 39 40 |
# File 'lib/couchbase/datastructures/couchbase_queue.rb', line 35 def initialize(id, collection, = Options::CouchbaseQueue.new) @id = id @collection = collection @options = @cas = 0 end |
Instance Method Details
#clear ⇒ Object
Removes all elements from the queue
82 83 84 85 86 87 |
# File 'lib/couchbase/datastructures/couchbase_queue.rb', line 82 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.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/couchbase/datastructures/couchbase_queue.rb', line 47 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(&) self else enum_for(:each) end end |
#empty? ⇒ Boolean
Returns true if queue is empty
77 78 79 |
# File 'lib/couchbase/datastructures/couchbase_queue.rb', line 77 def empty? size.zero? end |
#length ⇒ Integer Also known as: size
Returns the number of elements in the queue.
65 66 67 68 69 70 71 72 |
# File 'lib/couchbase/datastructures/couchbase_queue.rb', line 65 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
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/couchbase/datastructures/couchbase_queue.rb', line 110 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
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/couchbase/datastructures/couchbase_queue.rb', line 93 def push(obj) begin @collection.mutate_in(@id, [ MutateInSpec.array_prepend("", [obj]), ], @options.) rescue Error::PathExists # ignore end self end |