Class: Kafka::MessageBuffer
- Inherits:
-
Object
- Object
- Kafka::MessageBuffer
- Includes:
- Enumerable
- Defined in:
- lib/kafka/message_buffer.rb
Overview
Buffers messages for specific topics/partitions.
Instance Attribute Summary collapse
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
-
#clear ⇒ nil
Clears messages across all topics and partitions.
-
#clear_messages(topic:, partition:) ⇒ nil
Clears buffered messages for the given topic and partition.
- #concat(messages, topic:, partition:) ⇒ Object
- #each ⇒ Object
- #empty? ⇒ Boolean
-
#initialize ⇒ MessageBuffer
constructor
A new instance of MessageBuffer.
- #to_h ⇒ Object
- #write(message, topic:, partition:) ⇒ Object
Constructor Details
#initialize ⇒ MessageBuffer
Returns a new instance of MessageBuffer.
9 10 11 12 |
# File 'lib/kafka/message_buffer.rb', line 9 def initialize @buffer = {} @size = 0 end |
Instance Attribute Details
#size ⇒ Object (readonly)
Returns the value of attribute size.
7 8 9 |
# File 'lib/kafka/message_buffer.rb', line 7 def size @size end |
Instance Method Details
#clear ⇒ nil
Clears messages across all topics and partitions.
56 57 58 59 |
# File 'lib/kafka/message_buffer.rb', line 56 def clear @buffer = {} @size = 0 end |
#clear_messages(topic:, partition:) ⇒ nil
Clears buffered messages for the given topic and partition.
46 47 48 49 50 51 |
# File 'lib/kafka/message_buffer.rb', line 46 def (topic:, partition:) @size -= @buffer[topic][partition].count @buffer[topic].delete(partition) @buffer.delete(topic) if @buffer[topic].empty? end |
#concat(messages, topic:, partition:) ⇒ Object
19 20 21 22 |
# File 'lib/kafka/message_buffer.rb', line 19 def concat(, topic:, partition:) @size += .count buffer_for(topic, partition).concat() end |
#each ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/kafka/message_buffer.rb', line 32 def each @buffer.each do |topic, | .each do |partition, | yield topic, partition, end end end |
#empty? ⇒ Boolean
28 29 30 |
# File 'lib/kafka/message_buffer.rb', line 28 def empty? @buffer.empty? end |
#to_h ⇒ Object
24 25 26 |
# File 'lib/kafka/message_buffer.rb', line 24 def to_h @buffer end |
#write(message, topic:, partition:) ⇒ Object
14 15 16 17 |
# File 'lib/kafka/message_buffer.rb', line 14 def write(, topic:, partition:) @size += 1 buffer_for(topic, partition) << end |