Class: Elastic::Buffer
- Inherits:
-
Object
- Object
- Elastic::Buffer
- Defined in:
- lib/elastic/buffer.rb
Constant Summary collapse
- DEFAULT_SIZE =
1_000
Instance Attribute Summary collapse
-
#queue ⇒ Object
readonly
Returns the value of attribute queue.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
- #<<(object) ⇒ Object
- #any? ⇒ Boolean
- #flush! ⇒ Object
-
#initialize(size: DEFAULT_SIZE, &blk) ⇒ Buffer
constructor
A new instance of Buffer.
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
#queue ⇒ Object (readonly)
Returns the value of attribute queue.
5 6 7 |
# File 'lib/elastic/buffer.rb', line 5 def queue @queue end |
#size ⇒ Object (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
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 |