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



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

def deserialize!
  each(&:itself)
end

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

Note:

Invocation of this method will cause loading and deserializing each param after another. If you want to get access without deserializing, please access params_array directly

Yield Parameters:



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

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

#firstKarafka::Params::Params

Returns first element after the deserialization process.

Returns:



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

def first
  @params_array.first.deserialize!
end

#lastKarafka::Params::Params

Returns last element after the deserialization process.

Returns:



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

def last
  @params_array.last.deserialize!
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



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

def payloads
  deserialize!.map(&:payload)
end

#sizeInteger

Returns number of messages in the batch.

Returns:

  • (Integer)

    number of messages in the batch



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

def size
  @params_array.size
end

#to_aArray<Karafka::Params::Params>

Returns pure array with params (not deserialized).

Returns:



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

def to_a
  @params_array
end