Class: Datadog::Utils::ObjectSet

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

Overview

Acts as a unique dictionary of objects

Direct Known Subclasses

Profiling::Pprof::MessageSet

Instance Method Summary collapse

Constructor Details

#initialize(seed = 0, &block) ⇒ ObjectSet

You can provide a block that defines how the key for this message type is resolved.



10
11
12
13
14
# File 'lib/ddtrace/utils/object_set.rb', line 10

def initialize(seed = 0, &block)
  @sequence = Utils::Sequence.new(seed)
  @items = {}
  @key_block = block
end

Instance Method Details

#fetch(*args) ⇒ Object

Submit an array of arguments that define the message. If they match an existing message, it will return the matching object. If it doesn't match, it will yield to the block with the next ID & args given.



20
21
22
23
24
# File 'lib/ddtrace/utils/object_set.rb', line 20

def fetch(*args)
  # TODO: Array hashing is **really** expensive, we probably want to get rid of it in the future
  key = @key_block ? @key_block.call(*args) : args.hash
  @items[key] ||= yield(@sequence.next, *args)
end

#freezeObject



34
35
36
37
# File 'lib/ddtrace/utils/object_set.rb', line 34

def freeze
  super
  @items.freeze
end

#lengthObject



26
27
28
# File 'lib/ddtrace/utils/object_set.rb', line 26

def length
  @items.length
end

#objectsObject



30
31
32
# File 'lib/ddtrace/utils/object_set.rb', line 30

def objects
  @items.values
end