Class: Elastic::Buffer

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic/buffer.rb

Constant Summary collapse

DEFAULT_SIZE =
1_000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size: DEFAULT_SIZE, &blk) ⇒ Buffer

Returns a new instance of Buffer.



7
8
9
10
11
12
# File 'lib/elastic/buffer.rb', line 7

def initialize(size: DEFAULT_SIZE, &blk)
  @size = size
  @callback = blk
  @queue = []
  @lock = Mutex.new
end

Instance Attribute Details

#queueObject (readonly)

Returns the value of attribute queue.



5
6
7
# File 'lib/elastic/buffer.rb', line 5

def queue
  @queue
end

#sizeObject (readonly)

Returns the value of attribute size.



5
6
7
# File 'lib/elastic/buffer.rb', line 5

def size
  @size
end

Instance Method Details

#<<(object) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/elastic/buffer.rb', line 14

def <<(object)
  @lock.synchronize do
    @queue << object
    flush! if @queue.size >= size
  end
  self
end

#any?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/elastic/buffer.rb', line 29

def any?
  @queue.any?
end

#flush!Object



22
23
24
25
26
27
# File 'lib/elastic/buffer.rb', line 22

def flush!
  if @queue.any?
    @callback.call(@queue) if @callback.is_a? Proc
    @queue = []
  end
end