Sha256: 023a11881007641b3237d6f3036b72cb99b25a8605ef672b12e20ed58dcb3dee

Contents?: true

Size: 1.02 KB

Versions: 32

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

module Datadog
  module Core
    # Chunks list of elements into batches
    module Chunker
      module_function

      # Chunks a list into batches of at most +max_chunk_size+ elements each.
      #
      # An exception can occur if a single element is too large. That single
      # element will be returned in its own chunk. You have to verify by yourself
      # when such elements are returned.
      #
      # @param list [Enumerable] list of elements
      # @param max_chunk_size [Numeric] maximum acceptable chunk size
      # @return [Enumerable] lazy list of chunks
      def chunk_by_size(list, max_chunk_size)
        chunk_agg = 0
        list.slice_before do |elem|
          size = elem.size
          chunk_agg += size
          if chunk_agg > max_chunk_size
            # Can't fit element in current chunk, start a new one.
            chunk_agg = size
            true
          else
            # Add to current chunk
            false
          end
        end
      end
    end
  end
end

Version data entries

32 entries across 32 versions & 2 rubygems

Version Path
datadog-2.7.0 lib/datadog/core/chunker.rb
datadog-2.6.0 lib/datadog/core/chunker.rb
datadog-2.5.0 lib/datadog/core/chunker.rb
datadog-2.4.0 lib/datadog/core/chunker.rb
datadog-2.3.0 lib/datadog/core/chunker.rb
datadog-2.2.0 lib/datadog/core/chunker.rb
ddtrace-1.23.3 lib/datadog/core/chunker.rb
ddtrace-1.23.2 lib/datadog/core/chunker.rb
datadog-2.1.0 lib/datadog/core/chunker.rb
datadog-2.0.0 lib/datadog/core/chunker.rb
ddtrace-1.23.1 lib/datadog/core/chunker.rb
datadog-2.0.0.beta2 lib/datadog/core/chunker.rb
ddtrace-1.22.0 lib/datadog/core/chunker.rb
datadog-2.0.0.beta1 lib/datadog/core/chunker.rb
ddtrace-1.21.1 lib/datadog/core/chunker.rb
ddtrace-1.21.0 lib/datadog/core/chunker.rb
ddtrace-1.20.0 lib/datadog/core/chunker.rb
ddtrace-1.19.0 lib/datadog/core/chunker.rb
ddtrace-1.18.0 lib/datadog/core/chunker.rb
ddtrace-1.17.0 lib/datadog/core/chunker.rb