Sha256: 6c7721838a3b11a39795cd0111951eca32af023426e2fbb36f98f03f9e0b1a53

Contents?: true

Size: 969 Bytes

Versions: 21

Compression:

Stored size: 969 Bytes

Contents

require 'json'
require 'msgpack'

module Datadog
  # 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

Version data entries

21 entries across 21 versions & 2 rubygems

Version Path
ddtrace-0.51.1 lib/ddtrace/chunker.rb
ddtrace-0.51.0 lib/ddtrace/chunker.rb
ddtrace-0.50.0 lib/ddtrace/chunker.rb
ddtrace-0.49.0 lib/ddtrace/chunker.rb
ddtrace-0.48.0 lib/ddtrace/chunker.rb
ddtrace-0.47.0 lib/ddtrace/chunker.rb
ddtrace-0.46.0 lib/ddtrace/chunker.rb
ddtrace-0.45.0 lib/ddtrace/chunker.rb
ddtrace-0.44.0 lib/ddtrace/chunker.rb
ddtrace-0.43.0 lib/ddtrace/chunker.rb
ddtrace-0.42.0 lib/ddtrace/chunker.rb
ddtrace-0.41.0 lib/ddtrace/chunker.rb
ls-trace-0.2.0 lib/ddtrace/chunker.rb
ddtrace-0.40.0 lib/ddtrace/chunker.rb
ddtrace-0.39.0 lib/ddtrace/chunker.rb
ddtrace-0.38.0 lib/ddtrace/chunker.rb
ddtrace-0.37.0 lib/ddtrace/chunker.rb
ddtrace-0.36.0 lib/ddtrace/chunker.rb
ddtrace-0.35.2 lib/ddtrace/chunker.rb
ddtrace-0.35.1 lib/ddtrace/chunker.rb