Class: Karafka::Messages::Messages

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/karafka/messages/messages.rb

Overview

Messages batch represents a set of messages received from Kafka of a single topic partition.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(messages_array, metadata) ⇒ Karafka::Messages::Messages

Returns lazy evaluated messages batch object.

Parameters:



14
15
16
17
# File 'lib/karafka/messages/messages.rb', line 14

def initialize(messages_array, )
  @messages_array = messages_array
  @metadata = 
end

Instance Attribute Details

#metadataObject (readonly)

Returns the value of attribute metadata.



9
10
11
# File 'lib/karafka/messages/messages.rb', line 9

def 
  @metadata
end

Instance Method Details

#deserialize!Array<Karafka::Messages::Message>

Runs deserialization of all the messages and returns them

Returns:



27
28
29
# File 'lib/karafka/messages/messages.rb', line 27

def deserialize!
  each(&:payload)
end

#each(&block) ⇒ Object

Note:

Invocation of this method will not cause loading and deserializing of messages.

Parameters:

  • block (Proc)

    block we want to execute per each message



21
22
23
# File 'lib/karafka/messages/messages.rb', line 21

def each(&block)
  @messages_array.each(&block)
end

#empty?Boolean

Returns is the messages batch empty.

Returns:

  • (Boolean)

    is the messages batch empty



44
45
46
# File 'lib/karafka/messages/messages.rb', line 44

def empty?
  @messages_array.empty?
end

#firstKarafka::Messages::Message

Returns first message.

Returns:



49
50
51
# File 'lib/karafka/messages/messages.rb', line 49

def first
  @messages_array.first
end

#lastKarafka::Messages::Message

Returns last message.

Returns:



54
55
56
# File 'lib/karafka/messages/messages.rb', line 54

def last
  @messages_array.last
end

#payloadsArray<Object>

Returns array with deserialized payloads. This method can be useful when we don’t care about metadata and just want to extract all the data payloads from the batch.

Returns:

  • (Array<Object>)

    array with deserialized payloads. This method can be useful when we don’t care about metadata and just want to extract all the data payloads from the batch



34
35
36
# File 'lib/karafka/messages/messages.rb', line 34

def payloads
  map(&:payload)
end

#raw_payloadsArray<String>

Returns array with raw, not deserialized payloads.

Returns:

  • (Array<String>)

    array with raw, not deserialized payloads



39
40
41
# File 'lib/karafka/messages/messages.rb', line 39

def raw_payloads
  map(&:raw_payload)
end

#sizeInteger Also known as: count

Returns number of messages in the batch.

Returns:

  • (Integer)

    number of messages in the batch



59
60
61
# File 'lib/karafka/messages/messages.rb', line 59

def size
  @messages_array.size
end

#to_aArray<Karafka::Messages::Message>

Returns copy of the pure array with messages.

Returns:



64
65
66
# File 'lib/karafka/messages/messages.rb', line 64

def to_a
  @messages_array.dup
end