Sha256: effb723407348072b4125805c6ee7e0e57e5395f7209d96402a959fc14b8b8fd

Contents?: true

Size: 1.91 KB

Versions: 18

Compression:

Stored size: 1.91 KB

Contents

# frozen_string_literal: true

module Karafka
  module Params
    # Params batch represents a set of messages received from Kafka.
    # @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.
    class ParamsBatch
      include Enumerable

      # @param params_array [Array<Karafka::Params::Params>] array with karafka params
      # @return [Karafka::Params::ParamsBatch] lazy evaluated params batch object
      def initialize(params_array)
        @params_array = params_array
      end

      # @yieldparam [Karafka::Params::Params] each params instance
      # @note Invocation of this method will not cause loading and deserializing each param after
      #   another.
      def each
        @params_array.each { |param| yield(param) }
      end

      # @return [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
      def deserialize!
        each(&:payload)
      end

      # @return [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
      def payloads
        map(&:payload)
      end

      # @return [Karafka::Params::Params] first element
      def first
        @params_array.first
      end

      # @return [Karafka::Params::Params] last element
      def last
        @params_array.last
      end

      # @return [Integer] number of messages in the batch
      def size
        @params_array.size
      end

      # @return [Array<Karafka::Params::Params>] pure array with params
      def to_a
        @params_array
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
karafka-1.4.15 lib/karafka/params/params_batch.rb
karafka-1.4.14 lib/karafka/params/params_batch.rb
karafka-1.4.13 lib/karafka/params/params_batch.rb
karafka-1.4.12 lib/karafka/params/params_batch.rb
karafka-1.4.11 lib/karafka/params/params_batch.rb
karafka-1.4.10 lib/karafka/params/params_batch.rb
karafka-1.4.9 lib/karafka/params/params_batch.rb
karafka-1.4.8 lib/karafka/params/params_batch.rb
karafka-1.4.7 lib/karafka/params/params_batch.rb
karafka-1.4.6 lib/karafka/params/params_batch.rb
karafka-1.4.5 lib/karafka/params/params_batch.rb
karafka-1.4.4 lib/karafka/params/params_batch.rb
karafka-1.4.3 lib/karafka/params/params_batch.rb
karafka-1.4.2 lib/karafka/params/params_batch.rb
karafka-1.4.1 lib/karafka/params/params_batch.rb
karafka-1.4.0 lib/karafka/params/params_batch.rb
karafka-1.4.0.rc2 lib/karafka/params/params_batch.rb
karafka-1.4.0.rc1 lib/karafka/params/params_batch.rb