Class: Datadog::Core::Utils::ObjectSet

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/core/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.



12
13
14
15
16
# File 'lib/datadog/core/utils/object_set.rb', line 12

def initialize(seed = 0, &block)
  @sequence = 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.



22
23
24
25
26
# File 'lib/datadog/core/utils/object_set.rb', line 22

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



36
37
38
39
# File 'lib/datadog/core/utils/object_set.rb', line 36

def freeze
  super
  @items.freeze
end

#lengthObject



28
29
30
# File 'lib/datadog/core/utils/object_set.rb', line 28

def length
  @items.length
end

#objectsObject



32
33
34
# File 'lib/datadog/core/utils/object_set.rb', line 32

def objects
  @items.values
end