Class: Datadog::DI::SnapshotPruner::Node Private

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/di/snapshot_pruner/node.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

A JSON object of an encoded snapshot: its byte range, its nesting level, its parent and child objects, and the flags that rank it for pruning.

Constant Summary collapse

PRUNE_FIRST_BIT =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Bit of the packed priority set for an object truncated for depth or promoted to a pruning candidate, ranking it ahead of every object without the bit.

1 << 61
LEVEL_SHIFT =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Shift of the level within the packed priority.

44
NOT_CAPTURED_BIT =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Bit of the packed priority set for an object carrying a notCapturedReason.

1 << 43

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_offset:, level:, parent:) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes an open object whose closing brace has not been read.

Parameters:

  • start_offset (Integer) —

    byte offset of the object's opening brace

  • level (Integer) —

    nesting level, 0 for the root object

  • parent (Node, nil) —

    the enclosing object, nil for the root object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/datadog/di/snapshot_pruner/node.rb', line 30

def initialize(start_offset:, level:, parent:)
  @start_offset = start_offset
  @end_offset = start_offset
  @level = level
  @parent = parent
  @children = []
  @pruned_child_count = 0
  @not_captured = false
  @not_captured_depth = false
  @promoted = false
end

Instance Attribute Details

#children ⇒ Array<Node> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The objects nested directly in this object.

Returns:



60
61
62
# File 'lib/datadog/di/snapshot_pruner/node.rb', line 60

def children
  @children
end

#end_offset ⇒ Integer (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The byte offset just after the object's closing brace.

Returns:

  • (Integer)


48
49
50
# File 'lib/datadog/di/snapshot_pruner/node.rb', line 48

def end_offset
  @end_offset
end

#level ⇒ Integer (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The nesting level of the object, 0 for the root object.

Returns:

  • (Integer)


52
53
54
# File 'lib/datadog/di/snapshot_pruner/node.rb', line 52

def level
  @level
end

#not_captured ⇒ Boolean (readonly) Also known as: not_captured?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Whether the object carries a notCapturedReason member.

Returns:

  • (Boolean)


69
70
71
# File 'lib/datadog/di/snapshot_pruner/node.rb', line 69

def not_captured
  @not_captured
end

#not_captured_depth ⇒ Boolean (readonly) Also known as: not_captured_depth?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Whether the object's notCapturedReason is depth.

Returns:

  • (Boolean)


74
75
76
# File 'lib/datadog/di/snapshot_pruner/node.rb', line 74

def not_captured_depth
  @not_captured_depth
end

#parent ⇒ Node? (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The enclosing object, nil for the root object.

Returns:



56
57
58
# File 'lib/datadog/di/snapshot_pruner/node.rb', line 56

def parent
  @parent
end

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Whether the object was promoted to a pruning candidate because all of its children were resolved.

Returns:

  • (Boolean)


80
81
82
# File 'lib/datadog/di/snapshot_pruner/node.rb', line 80

def promoted
  @promoted
end

#pruned_child_count ⇒ Integer (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The number of children already resolved from the pruning queue, whether selected for pruning or subsumed by promotion.

Returns:

  • (Integer)


65
66
67
# File 'lib/datadog/di/snapshot_pruner/node.rb', line 65

def pruned_child_count
  @pruned_child_count
end

#start_offset ⇒ Integer (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The byte offset of the object's opening brace.

Returns:

  • (Integer)


44
45
46
# File 'lib/datadog/di/snapshot_pruner/node.rb', line 44

def start_offset
  @start_offset
end

Instance Method Details

#add_child(child) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Appends a directly nested object.

Parameters:

  • child (Node) —

    the nested object



116
117
118
119
# File 'lib/datadog/di/snapshot_pruner/node.rb', line 116

def add_child(child)
  children << child
  nil
end

#all_children_pruned? ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns whether every child has been resolved from the pruning queue.

Returns:

  • (Boolean)


158
159
160
# File 'lib/datadog/di/snapshot_pruner/node.rb', line 158

def all_children_pruned?
  pruned_child_count >= children.size
end

#close(end_offset) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Records the position of the object's closing brace.

Parameters:

  • end_offset (Integer) —

    byte offset just after the closing brace



125
126
127
128
# File 'lib/datadog/di/snapshot_pruner/node.rb', line 125

def close(end_offset)
  @end_offset = end_offset
  nil
end

#each_leaf {|leaf| ... } ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Yields every leaf of the subtree.

Yield Parameters:

  • leaf (Node) —

    a leaf object



176
177
178
179
180
181
182
183
184
185
186
# File 'lib/datadog/di/snapshot_pruner/node.rb', line 176

def each_leaf
  pending = [self] #: Array[Node]
  while (node = pending.pop)
    if node.leaf?
      yield node
    else
      pending.concat(node.children)
    end
  end
  nil
end

#leaf? ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns whether the object is a leaf.

Returns:

  • (Boolean)


93
94
95
# File 'lib/datadog/di/snapshot_pruner/node.rb', line 93

def leaf?
  children.empty?
end

#mark_not_captured ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Flags the object as carrying a notCapturedReason member.



133
134
135
136
# File 'lib/datadog/di/snapshot_pruner/node.rb', line 133

def mark_not_captured
  @not_captured = true
  nil
end

#mark_not_captured_depth ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Flags the object as truncated for depth.



141
142
143
144
# File 'lib/datadog/di/snapshot_pruner/node.rb', line 141

def mark_not_captured_depth
  @not_captured_depth = true
  nil
end

#priority ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Packs the pruning sort key (prune-first bit, level, not-captured flag, size) into one Integer, so that ranking two objects is one Integer comparison. The prune-first bit is set for an object truncated for depth and for an object promoted to a pruning candidate. The fields fit below 262 for levels below 217 and sizes below 2**43 bytes.

Returns:

  • (Integer) —

    a greater value for an object to prune earlier



105
106
107
108
109
110
# File 'lib/datadog/di/snapshot_pruner/node.rb', line 105

def priority
  @priority ||= ((not_captured_depth? || promoted?) ? PRUNE_FIRST_BIT : 0) |
    (level << LEVEL_SHIFT) |
    (not_captured? ? NOT_CAPTURED_BIT : 0) |
    size
end

#promote ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Turns the object, whose children are all selected, into a pruning candidate that ranks ahead of every object without the prune-first bit.



167
168
169
170
# File 'lib/datadog/di/snapshot_pruner/node.rb', line 167

def promote
  @promoted = true
  @priority = nil
end

#record_pruned_child ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Records one child resolved from the pruning queue.



149
150
151
152
# File 'lib/datadog/di/snapshot_pruner/node.rb', line 149

def record_pruned_child
  @pruned_child_count = pruned_child_count + 1
  nil
end

#size ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the encoded size of the object.

Returns:

  • (Integer) —

    bytes from the opening brace through the closing brace



86
87
88
# File 'lib/datadog/di/snapshot_pruner/node.rb', line 86

def size
  end_offset - start_offset
end