Class: Datadog::Utils::StringTable

Inherits:
Object
  • Object
show all
Defined in:
lib/ddtrace/utils/string_table.rb

Overview

Tracks strings and returns IDs

Direct Known Subclasses

Profiling::Pprof::StringTable

Instance Method Summary collapse

Constructor Details

#initializeStringTable

Returns a new instance of StringTable.



8
9
10
11
# File 'lib/ddtrace/utils/string_table.rb', line 8

def initialize
  @sequence = Sequence.new
  @ids = { ''.freeze => @sequence.next }
end

Instance Method Details

#[](id) ⇒ Object



37
38
39
# File 'lib/ddtrace/utils/string_table.rb', line 37

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

#fetch(string) ⇒ Object

Returns an ID for the string



14
15
16
# File 'lib/ddtrace/utils/string_table.rb', line 14

def fetch(string)
  @ids[string.to_s] ||= @sequence.next
end

#fetch_string(string) ⇒ Object

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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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

#stringsObject



41
42
43
# File 'lib/ddtrace/utils/string_table.rb', line 41

def strings
  @ids.keys
end