Class: FreeMessageQueue::LinkedQueue

Inherits:
BaseQueue
  • Object
show all
Defined in:
lib/fmq/queues/linked.rb

Overview

DO NOT USE THIS QUEUE DIRECTLY IN THE QUEUE MANAGER it is not thread safe. This Queue implements a FIFO based store in system memory.

Direct Known Subclasses

SyncronizedQueue

Constant Summary

Constants inherited from BaseQueue

BaseQueue::INFINITE

Instance Attribute Summary

Attributes inherited from BaseQueue

#bytes, #manager, #max_messages, #max_size, #size

Instance Method Summary collapse

Methods inherited from BaseQueue

#empty?

Constructor Details

#initialize(manager) ⇒ LinkedQueue

Returns a new instance of LinkedQueue.



26
27
28
29
# File 'lib/fmq/queues/linked.rb', line 26

def initialize(manager)
  super(manager)
  @last_message = @first_message = nil
end

Instance Method Details

#clearObject

Remove all items from the queue



32
33
34
# File 'lib/fmq/queues/linked.rb', line 32

def clear 
  while self.poll; end
end

#pollObject

Return an message from the queue or nil if the queue is empty



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/fmq/queues/linked.rb', line 55

def poll()
  unless empty?
    # remove allways the first item
    message = @first_message
    
    # took it off
    @first_message = message.next
    @last_message = nil if @first_message.nil?
    message.next = nil # unlink the message
    remove_message(message) # update stats
  else
    return nil
  end
end

#put(message) ⇒ Object

Put an item to the queue



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fmq/queues/linked.rb', line 37

def put(message)
  return false if message.nil?
  
  add_message(message) # update stats and check constraints
  
  # insert at end of list
  if @first_message == nil
    # first and last item are same if there is no item to the queue
    @first_message = @last_message = message
  else
    # append the message to the end of the queue
    @last_message = @last_message.next = message
  end
  
  return true
end