Class: SqsPoller::Common::RingBuffer
- Inherits:
-
Object
- Object
- SqsPoller::Common::RingBuffer
- Defined in:
- lib/sqspoller/common/ring_buffer.rb
Instance Method Summary collapse
- #clear ⇒ Object
- #count ⇒ Object
- #empty? ⇒ Boolean
- #flush ⇒ Object
- #full? ⇒ Boolean
-
#initialize(size) ⇒ RingBuffer
constructor
A new instance of RingBuffer.
- #push(value) ⇒ Object (also: #<<)
- #shift ⇒ Object
Constructor Details
#initialize(size) ⇒ RingBuffer
Returns a new instance of RingBuffer.
5 6 7 8 9 10 11 |
# File 'lib/sqspoller/common/ring_buffer.rb', line 5 def initialize(size) @size = size @start = 0 @count = 0 @buffer = Array.new(size) @mutex = Mutex.new end |
Instance Method Details
#clear ⇒ Object
56 57 58 59 60 |
# File 'lib/sqspoller/common/ring_buffer.rb', line 56 def clear @buffer = Array.new(@size) @start = 0 @count = 0 end |
#count ⇒ Object
17 18 19 |
# File 'lib/sqspoller/common/ring_buffer.rb', line 17 def count @count end |
#empty? ⇒ Boolean
21 22 23 |
# File 'lib/sqspoller/common/ring_buffer.rb', line 21 def empty? @count == 0 end |
#flush ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/sqspoller/common/ring_buffer.rb', line 46 def flush values = [] @mutex.synchronize do until empty? values << remove_element end end values end |
#full? ⇒ Boolean
13 14 15 |
# File 'lib/sqspoller/common/ring_buffer.rb', line 13 def full? @count == @size end |
#push(value) ⇒ Object Also known as: <<
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/sqspoller/common/ring_buffer.rb', line 25 def push(value) @mutex.synchronize do stop = (@start + @count) % @size @buffer[stop] = value if full? @start = (@start + 1) % @size else @count += 1 end value end end |
#shift ⇒ Object
40 41 42 43 44 |
# File 'lib/sqspoller/common/ring_buffer.rb', line 40 def shift @mutex.synchronize do remove_element end end |