Sha256: 5640c31844225f14e4cc2ccfd4b475e911066952da60c6b561e489ea5aaaf4d9

Contents?: true

Size: 980 Bytes

Versions: 40

Compression:

Stored size: 980 Bytes

Contents

# frozen_string_literal: true

module Deimos
  module ActiveRecordConsume
    # Helper class for breaking down batches into independent groups for
    # processing
    class BatchSlicer
      # Split the batch into a series of independent slices. Each slice contains
      # messages that can be processed in any order (i.e. they have distinct
      # keys). Messages with the same key will be separated into different
      # slices that maintain the correct order.
      # E.g. Given messages A1, A2, B1, C1, C2, C3, they will be sliced as:
      # [[A1, B1, C1], [A2, C2], [C3]]
      # @param messages [Array<Message>]
      # @return [Array<Array<Message>>]
      def self.slice(messages)
        ops = messages.group_by(&:key)

        # Find maximum depth
        depth = ops.values.map(&:length).max || 0

        # Generate slices for each depth
        depth.times.map do |i|
          ops.values.map { |arr| arr.dig(i) }.compact
        end
      end
    end
  end
end

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
deimos-ruby-1.20.1 lib/deimos/active_record_consume/batch_slicer.rb
deimos-ruby-1.20.0 lib/deimos/active_record_consume/batch_slicer.rb
deimos-ruby-1.19.7 lib/deimos/active_record_consume/batch_slicer.rb
deimos-ruby-1.19.6 lib/deimos/active_record_consume/batch_slicer.rb
deimos-ruby-1.19.5 lib/deimos/active_record_consume/batch_slicer.rb
deimos-ruby-1.19.4 lib/deimos/active_record_consume/batch_slicer.rb
deimos-ruby-1.19.3 lib/deimos/active_record_consume/batch_slicer.rb
deimos-ruby-1.19.2 lib/deimos/active_record_consume/batch_slicer.rb
deimos-ruby-1.19.1 lib/deimos/active_record_consume/batch_slicer.rb
deimos-ruby-1.19.1.pre.beta1 lib/deimos/active_record_consume/batch_slicer.rb
deimos-ruby-1.19.0 lib/deimos/active_record_consume/batch_slicer.rb
deimos-ruby-1.19.beta2 lib/deimos/active_record_consume/batch_slicer.rb
deimos-ruby-1.19.beta1 lib/deimos/active_record_consume/batch_slicer.rb
deimos-ruby-1.18.2 lib/deimos/active_record_consume/batch_slicer.rb
deimos-ruby-1.18.1 lib/deimos/active_record_consume/batch_slicer.rb
deimos-ruby-1.18.0 lib/deimos/active_record_consume/batch_slicer.rb
deimos-ruby-1.17.1 lib/deimos/active_record_consume/batch_slicer.rb
deimos-ruby-1.17.0 lib/deimos/active_record_consume/batch_slicer.rb
deimos-ruby-1.16.5 lib/deimos/active_record_consume/batch_slicer.rb
deimos-ruby-1.16.4 lib/deimos/active_record_consume/batch_slicer.rb