Sha256: ea20b96078b0a2e156762338e5ac349a5a3081c06a6597a3a848ad47cfc2dca8

Contents?: true

Size: 1.17 KB

Versions: 12

Compression:

Stored size: 1.17 KB

Contents

# typed: true

require_relative 'sequence'

module Datadog
  module Core
    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
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
ddtrace-1.9.0 lib/datadog/core/utils/string_table.rb
ddtrace-1.8.0 lib/datadog/core/utils/string_table.rb
ddtrace-1.7.0 lib/datadog/core/utils/string_table.rb
ddtrace-1.6.1 lib/datadog/core/utils/string_table.rb
ddtrace-1.6.0 lib/datadog/core/utils/string_table.rb
ddtrace-1.5.2 lib/datadog/core/utils/string_table.rb
ddtrace-1.5.1 lib/datadog/core/utils/string_table.rb
ddtrace-1.5.0 lib/datadog/core/utils/string_table.rb
ddtrace-1.4.2 lib/datadog/core/utils/string_table.rb
ddtrace-1.4.1 lib/datadog/core/utils/string_table.rb
ddtrace-1.4.0 lib/datadog/core/utils/string_table.rb
ddtrace-1.3.0 lib/datadog/core/utils/string_table.rb