Sha256: 509350c40a7e012d4cee692834fc7d7e84c610d5e16f861c527bc66fe06c47a0

Contents?: true

Size: 1.08 KB

Versions: 6

Compression:

Stored size: 1.08 KB

Contents

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

6 entries across 6 versions & 1 rubygems

Version Path
ddtrace-0.51.1 lib/ddtrace/utils/string_table.rb
ddtrace-0.51.0 lib/ddtrace/utils/string_table.rb
ddtrace-0.50.0 lib/ddtrace/utils/string_table.rb
ddtrace-0.49.0 lib/ddtrace/utils/string_table.rb
ddtrace-0.48.0 lib/ddtrace/utils/string_table.rb
ddtrace-0.47.0 lib/ddtrace/utils/string_table.rb