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 parsing process if we have after_fetch that rejects some incoming messages without using params It can be also used when handling really heavy data (in terms of parsing).

Params batch represents a set of messages received from Kafka.

Instance Method Summary collapse

Constructor Details

#initialize(messages_batch, topic_parser) ⇒ ParamsBatch

Builds up a params batch based on raw kafka messages

Parameters:

  • messages_batch (Array<Kafka::FetchedMessage>)

    messages batch

  • topic_parser (Class)

    topic parser for unparsing messages values



15
16
17
18
19
# File 'lib/karafka/params/params_batch.rb', line 15

def initialize(messages_batch, topic_parser)
  @params_batch = messages_batch.map! do |message|
    Karafka::Params::Params.build(message, topic_parser)
  end
end

Instance Method Details

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

Note:

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

Yield Parameters:

  • each (Karafka::Params::Params)

    parsed and loaded params instance



24
25
26
# File 'lib/karafka/params/params_batch.rb', line 24

def each
  @params_batch.each { |param| yield(param.retrieve!) }
end

#lastKarafka::Params::Params

Returns last element after the unparsing process.

Returns:

  • (Karafka::Params::Params)

    last element after the unparsing process



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

def last
  @params_batch.last.retrieve!
end

#parsedArray<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 parsed

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 parsed



31
32
33
# File 'lib/karafka/params/params_batch.rb', line 31

def parsed
  each(&:itself)
end

#to_aArray<Karafka::Params::Params>

Returns pure array with params (not parsed).

Returns:

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

    pure array with params (not parsed)



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

def to_a
  @params_batch
end