Sha256: 04e9d528ef4b09d3e640656617c745b3df8091d0e1fa9be093209380f2f61e4a

Contents?: true

Size: 1.09 KB

Versions: 5

Compression:

Stored size: 1.09 KB

Contents

# typed: true
require 'ddtrace/utils/sequence'

module Datadog
  module Utils
    # Tracks strings and returns IDs
    class StringTable
      def initialize
        @sequence = Sequence.new
        @ids = { ''.freeze => @sequence.next }
      end

      # Returns an ID for the string
      def fetch(string)
        @ids[string.to_s] ||= @sequence.next
      end

      # Returns the canonical copy of this string
      # Typically used for psuedo interning; reduce
      # identical copies of a string to one object.
      def fetch_string(string)
        return nil if string.nil?

        # Co-erce to string
        string = string.to_s

        # Add to string table if no match
        @ids[string] = @sequence.next unless @ids.key?(string)

        # Get and return matching string in table
        # NOTE: Have to resolve the key and retrieve from table again
        #       because "string" argument is not same object as string key.
        id = @ids[string]
        @ids.key(id)
      end

      def [](id)
        @ids.key(id)
      end

      def strings
        @ids.keys
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ddtrace-0.54.2 lib/ddtrace/utils/string_table.rb
ddtrace-0.54.1 lib/ddtrace/utils/string_table.rb
ddtrace-0.54.0 lib/ddtrace/utils/string_table.rb
ddtrace-0.53.0 lib/ddtrace/utils/string_table.rb
ddtrace-0.52.0 lib/ddtrace/utils/string_table.rb