Sha256: 58703704cf035a4c67a38431457394100a34e134d454776b652b8c55afd2e98d

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

require "ddsketch/proto/ddsketch_pb"

module DDSketch
  # Namespace for protobuf object generated by `google-protobuf`
  # @!visibility private
  module Proto
    INTERPOLATION_MAPPING = {
      linear: IndexMapping::Interpolation::LINEAR,
      cubic: IndexMapping::Interpolation::CUBIC
    }.freeze

    private_constant :INTERPOLATION_MAPPING

    module_function

    def serialize_sketch(sketch)
      DDSketch.new(
        mapping: serialize_key_mapping(sketch.mapping),
        positiveValues: serialize_store(sketch.store),
        negativeValues: serialize_store(sketch.negative_store),
        zeroCount: sketch.zero_count
      )
    end

    def serialize_store(store)
      Store.new(
        contiguousBinCounts: store.bins,
        contiguousBinIndexOffset: store.offset
      )
    end

    def serialize_key_mapping(mapping)
      IndexMapping.new(
        gamma: mapping.relative_accuracy,
        indexOffset: mapping.offset,
        interpolation: serialize_interpolation(mapping)
      )
    end

    def serialize_interpolation(mapping)
      INTERPOLATION_MAPPING.fetch(mapping.interpolation, IndexMapping::Interpolation::NONE)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ddsketch-0.1.0 lib/ddsketch/proto.rb