Class: Karafka::Params::ParamsBatch
- Inherits:
-
Object
- Object
- Karafka::Params::ParamsBatch
- Includes:
- Enumerable
- Defined in:
- lib/karafka/params/params_batch.rb
Overview
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
-
#deserialize! ⇒ Array<Karafka::Params::Params>
Returns all the params in a loaded state, so they can be used for batch insert, etc.
- #each {|each| ... } ⇒ Object
-
#first ⇒ Karafka::Params::Params
First element after the deserialization process.
-
#initialize(params_array) ⇒ Karafka::Params::ParamsBatch
constructor
Lazy evaluated params batch object.
-
#last ⇒ Karafka::Params::Params
Last element after the deserialization process.
-
#payloads ⇒ Array<Object>
Array with deserialized payloads.
-
#size ⇒ Integer
Number of messages in the batch.
-
#to_a ⇒ Array<Karafka::Params::Params>
Pure array with params (not deserialized).
Constructor Details
#initialize(params_array) ⇒ Karafka::Params::ParamsBatch
Returns lazy evaluated params batch object.
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
29 30 31 |
# File 'lib/karafka/params/params_batch.rb', line 29 def deserialize! each(&:itself) end |
#each {|each| ... } ⇒ Object
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
22 23 24 |
# File 'lib/karafka/params/params_batch.rb', line 22 def each @params_array.each { |param| yield(param.deserialize!) } end |
#first ⇒ Karafka::Params::Params
Returns first element after the deserialization process.
41 42 43 |
# File 'lib/karafka/params/params_batch.rb', line 41 def first @params_array.first.deserialize! end |
#last ⇒ Karafka::Params::Params
Returns last element after the deserialization process.
46 47 48 |
# File 'lib/karafka/params/params_batch.rb', line 46 def last @params_array.last.deserialize! end |
#payloads ⇒ Array<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.
36 37 38 |
# File 'lib/karafka/params/params_batch.rb', line 36 def payloads deserialize!.map(&:payload) end |
#size ⇒ Integer
Returns 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_a ⇒ Array<Karafka::Params::Params>
Returns pure array with params (not deserialized).
51 52 53 |
# File 'lib/karafka/params/params_batch.rb', line 51 def to_a @params_array end |