Sha256: 5640c31844225f14e4cc2ccfd4b475e911066952da60c6b561e489ea5aaaf4d9

Contents?: true

Size: 980 Bytes

Versions: 38

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

38 entries across 38 versions & 1 rubygems

Version Path
deimos-ruby-1.24.2 lib/deimos/active_record_consume/batch_slicer.rb
deimos-ruby-1.24.1 lib/deimos/active_record_consume/batch_slicer.rb
deimos-ruby-1.24.0 lib/deimos/active_record_consume/batch_slicer.rb
deimos-ruby-1.23.3 lib/deimos/active_record_consume/batch_slicer.rb
deimos-ruby-1.23.2 lib/deimos/active_record_consume/batch_slicer.rb
deimos-ruby-1.23.1.pre.beta6 lib/deimos/active_record_consume/batch_slicer.rb
deimos-ruby-1.23.1.pre.beta5 lib/deimos/active_record_consume/batch_slicer.rb
deimos-ruby-1.23.1.pre.beta4 lib/deimos/active_record_consume/batch_slicer.rb
deimos-ruby-1.23.1.pre.beta3 lib/deimos/active_record_consume/batch_slicer.rb
deimos-ruby-1.23.1.pre.beta2 lib/deimos/active_record_consume/batch_slicer.rb
deimos-ruby-1.23.1.pre.beta1 lib/deimos/active_record_consume/batch_slicer.rb
deimos-ruby-1.23.0 lib/deimos/active_record_consume/batch_slicer.rb
deimos-ruby-1.22.5 lib/deimos/active_record_consume/batch_slicer.rb
deimos-ruby-1.22.4 lib/deimos/active_record_consume/batch_slicer.rb
deimos-ruby-1.22.3 lib/deimos/active_record_consume/batch_slicer.rb
deimos-ruby-1.22.2 lib/deimos/active_record_consume/batch_slicer.rb
deimos-ruby-1.22.1 lib/deimos/active_record_consume/batch_slicer.rb
deimos-ruby-1.22 lib/deimos/active_record_consume/batch_slicer.rb
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