class Datadog::Utils::StringTable

Tracks strings and returns IDs

Public Class Methods

new() click to toggle source
# File lib/ddtrace/utils/string_table.rb, line 8
def initialize
  @sequence = Sequence.new
  @ids = { ''.freeze => @sequence.next }
end

Public Instance Methods

[](id) click to toggle source
# File lib/ddtrace/utils/string_table.rb, line 37
def [](id)
  @ids.key(id)
end
fetch(string) click to toggle source

Returns an ID for the string

# File lib/ddtrace/utils/string_table.rb, line 14
def fetch(string)
  @ids[string.to_s] ||= @sequence.next
end
fetch_string(string) click to toggle source

Returns the canonical copy of this string Typically used for psuedo interning; reduce identical copies of a string to one object.

# File lib/ddtrace/utils/string_table.rb, line 21
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
strings() click to toggle source
# File lib/ddtrace/utils/string_table.rb, line 41
def strings
  @ids.keys
end