Class: SqsPoller::Common::RingBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/sqspoller/common/ring_buffer.rb

Instance Method Summary collapse

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

#clearObject



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

#countObject



17
18
19
# File 'lib/sqspoller/common/ring_buffer.rb', line 17

def count
  @count
end

#empty?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/sqspoller/common/ring_buffer.rb', line 21

def empty?
  @count == 0
end

#flushObject



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

Returns:

  • (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

#shiftObject



40
41
42
43
44
# File 'lib/sqspoller/common/ring_buffer.rb', line 40

def shift
  @mutex.synchronize do
    remove_element
  end
end