Class: MobME::Infrastructure::Queue::Backends::Memory
- Inherits:
-
MobME::Infrastructure::Queue::Backend
- Object
- MobME::Infrastructure::Queue::Backend
- MobME::Infrastructure::Queue::Backends::Memory
- Defined in:
- lib/mobme/infrastructure/queue/backends/memory.rb
Instance Attribute Summary collapse
-
#scores ⇒ Object
Returns the value of attribute scores.
Instance Method Summary collapse
- #add(queue, item, metadata = {}) ⇒ Object
-
#add_bulk(queue, items = []) ⇒ Object
Adds many items together.
- #empty(queue) ⇒ Object
-
#initialize(options = {}) ⇒ Memory
constructor
Initialises the Queue.
- #list(queue) ⇒ Object
- #list_queues ⇒ Object
- #peek(queue) ⇒ Object
- #queues ⇒ Object
- #remove(queue, &block) ⇒ Object
- #remove_queues(*queues_to_delete) ⇒ Object (also: #remove_queue)
- #size(queue) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Memory
Initialises the Queue
9 10 11 |
# File 'lib/mobme/infrastructure/queue/backends/memory.rb', line 9 def initialize( = {}) @@queues ||= {} end |
Instance Attribute Details
#scores ⇒ Object
Returns the value of attribute scores.
5 6 7 |
# File 'lib/mobme/infrastructure/queue/backends/memory.rb', line 5 def scores @scores end |
Instance Method Details
#add(queue, item, metadata = {}) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/mobme/infrastructure/queue/backends/memory.rb', line 17 def add(queue, item, = {}) = () score = (['dequeue-timestamp'], ['priority']) queues[queue] ||= Containers::CRBTreeMap.new queues[queue][score] = serialize_item(item, ) end |
#add_bulk(queue, items = []) ⇒ Object
Adds many items together
26 27 28 29 30 |
# File 'lib/mobme/infrastructure/queue/backends/memory.rb', line 26 def add_bulk(queue, items = []) items.each do |item| add(queue, item) end end |
#empty(queue) ⇒ Object
61 62 63 64 65 66 |
# File 'lib/mobme/infrastructure/queue/backends/memory.rb', line 61 def empty(queue) queues[queue] = nil queues[queue] = Containers::CRBTreeMap.new true end |
#list(queue) ⇒ Object
57 58 59 |
# File 'lib/mobme/infrastructure/queue/backends/memory.rb', line 57 def list(queue) queues[queue].inject([]) { |collect, step| collect << item_with_score(queue, step[0]) } end |
#list_queues ⇒ Object
80 81 82 |
# File 'lib/mobme/infrastructure/queue/backends/memory.rb', line 80 def list_queues queues.keys end |
#peek(queue) ⇒ Object
51 52 53 54 55 |
# File 'lib/mobme/infrastructure/queue/backends/memory.rb', line 51 def peek(queue) score = queues[queue].min_key item_with_score(queue, score) end |
#queues ⇒ Object
13 14 15 |
# File 'lib/mobme/infrastructure/queue/backends/memory.rb', line 13 def queues @@queues end |
#remove(queue, &block) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/mobme/infrastructure/queue/backends/memory.rb', line 32 def remove(queue, &block) score = queues[queue].min_key item = item_with_score(queue, score) #If a block is given if block_given? begin block.call(item) rescue MobME::Infrastructure::Queue::RemoveAbort return end queues[queue].delete(score) if item else queues[queue].delete(score) if item return item end end |
#remove_queues(*queues_to_delete) ⇒ Object Also known as: remove_queue
72 73 74 75 76 77 |
# File 'lib/mobme/infrastructure/queue/backends/memory.rb', line 72 def remove_queues(*queues_to_delete) queues_to_delete = list_queues if queues_to_delete.empty? queues_to_delete.each do |queue| queues.delete(queue) end end |
#size(queue) ⇒ Object
68 69 70 |
# File 'lib/mobme/infrastructure/queue/backends/memory.rb', line 68 def size(queue) queues[queue].size end |