Class: Karafka::Params::ParamsBatch

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

Overview

Note:

Params internally are lazy loaded before first use. That way we can skip deserialization process if we have after_fetch that rejects some incoming messages without using params It can be also used when handling really heavy data.

Params batch represents a set of messages received from Kafka.

Instance Method Summary collapse

Constructor Details

#initialize(params_array) ⇒ Karafka::Params::ParamsBatch

Returns lazy evaluated params batch object.

Parameters:



14
15
16
# File 'lib/karafka/params/params_batch.rb', line 14

def initialize(params_array)
  @params_array = params_array
end

Instance Method Details

#deserialize!Array<Karafka::Params::Params>

Returns all the params in a loaded state, so they can be used for batch insert, etc. Without invoking all, up until first use, they won’t be deserialized

Returns:

  • (Array<Karafka::Params::Params>)

    returns all the params in a loaded state, so they can be used for batch insert, etc. Without invoking all, up until first use, they won’t be deserialized



28
29
30
# File 'lib/karafka/params/params_batch.rb', line 28

def deserialize!
  each(&:payload)
end

#each {|each| ... } ⇒ Object

Note:

Invocation of this method will not cause loading and deserializing each param after another.

Yield Parameters:



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

def each
  @params_array.each { |param| yield(param) }
end

#firstKarafka::Params::Params

Returns first element.

Returns:



40
41
42
# File 'lib/karafka/params/params_batch.rb', line 40

def first
  @params_array.first
end

#lastKarafka::Params::Params

Returns last element.

Returns:



45
46
47
# File 'lib/karafka/params/params_batch.rb', line 45

def last
  @params_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



35
36
37
# File 'lib/karafka/params/params_batch.rb', line 35

def payloads
  map(&:payload)
end

#sizeInteger

Returns number of messages in the batch.

Returns:

  • (Integer)

    number of messages in the batch



50
51
52
# File 'lib/karafka/params/params_batch.rb', line 50

def size
  @params_array.size
end

#to_aArray<Karafka::Params::Params>

Returns pure array with params.

Returns:



55
56
57
# File 'lib/karafka/params/params_batch.rb', line 55

def to_a
  @params_array
end