Class: CouchbaseStructures::Queue
- Inherits:
-
Object
- Object
- CouchbaseStructures::Queue
- Includes:
- CouchbaseDocStore
- Defined in:
- lib/couchbase_structures/queue.rb
Instance Method Summary collapse
- #delete ⇒ Object
-
#enqueue(value) ⇒ Object
Add an item to the end of the queue (tail).
-
#initialize(key) ⇒ Queue
constructor
A new instance of Queue.
- #inspect(html = false) ⇒ Object
-
#peek(queue_index) ⇒ Object
peek at item at index (zero based).
-
#pop ⇒ Object
Pop an item off the front of queue (head).
- #size ⇒ Object
- #to_a(html = false) ⇒ Object
Constructor Details
#initialize(key) ⇒ Queue
Returns a new instance of Queue.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/couchbase_structures/queue.rb', line 7 def initialize(key) @user_key = key @key = "#{key}::queue" @head_index_key = "#{@key}::head" @tail_index_key = "#{@key}::tail" initialize_document(@head_index_key, 0) initialize_document(@tail_index_key, 0) initialize_document(@key, { :type => "queue", :class => "CouchbaseStructures::Queue", :user_key => key } ) self end |
Instance Method Details
#delete ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/couchbase_structures/queue.rb', line 54 def delete #head_index = get_document(@head_index_key) # (not needed) tail_index = get_document(@tail_index_key) # delete all queued documents, including those that were before the current head (those aren't deleted as of now) 1.upto(tail_index) do |i| delete_document("#{@key}::#{i}") end delete_document(@head_index_key) delete_document(@tail_index_key) delete_document(@key) @user_key = nil @key = nil @head_index_key = nil @tail_index_key = nil nil end |
#enqueue(value) ⇒ Object
Add an item to the end of the queue (tail)
32 33 34 35 36 |
# File 'lib/couchbase_structures/queue.rb', line 32 def enqueue(value) new_tail_index = increase_atomic_count(@tail_index_key) create_document("#{@key}::#{new_tail_index}", value) self end |
#inspect(html = false) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/couchbase_structures/queue.rb', line 18 def inspect(html = false) if html "<strong>key</strong> = #{@key} <br /><strong>head_index</strong> = #{get_document(@head_index_key).to_s} <br /><strong>tail_index</strong> = #{get_document(@tail_index_key).to_s} <br /><strong>items</strong> = #{self.to_a(true)}" else "key = #{@key} head_index = #{get_document(@head_index_key).to_s} tail_index = #{get_document(@tail_index_key).to_s} items = #{self.to_a}" end end |
#peek(queue_index) ⇒ Object
peek at item at index (zero based)
27 28 29 |
# File 'lib/couchbase_structures/queue.rb', line 27 def peek(queue_index) self end |
#pop ⇒ Object
Pop an item off the front of queue (head)
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/couchbase_structures/queue.rb', line 39 def pop() head_index = get_document(@head_index_key) tail_index = get_document(@tail_index_key) if tail_index > head_index # if there is an item in the queue increase_atomic_count(@head_index_key) #incremented new_head_index is ignored, but we are incrementing and popping value return get_document("#{@key}::#{head_index + 1}") else # nil end end |
#size ⇒ Object
50 51 52 |
# File 'lib/couchbase_structures/queue.rb', line 50 def size() get_document(@tail_index_key) - get_document(@head_index_key) end |
#to_a(html = false) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/couchbase_structures/queue.rb', line 72 def to_a(html=false) a = [] head_index = get_document(@head_index_key) + 1 tail_index = get_document(@tail_index_key) # delete all queued documents, including those that were before the current head (those aren't deleted as of now) head_index.upto(tail_index) do |i| a << get_document("#{@key}::#{i}") end if html str = "[" a.each do |item| str += "<br /> " + item.inspect end str += "<br />]" return str else return a end end |