Class: Datadog::Core::Buffer::Random

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/core/buffer/random.rb

Overview

Buffer that stores objects. The buffer has a maximum size and when the buffer is full, a random object is discarded.

Direct Known Subclasses

CRuby, ThreadSafe

Instance Method Summary collapse

Constructor Details

#initialize(max_size) ⇒ Random

Returns a new instance of Random.



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

def initialize(max_size)
  @max_size = max_size
  @items = []
  @closed = false
end

Instance Method Details

#closeObject

Closes this buffer, preventing further pushing. Draining is still allowed.



79
80
81
# File 'lib/datadog/core/buffer/random.rb', line 79

def close
  @closed = true
end

#closed?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/datadog/core/buffer/random.rb', line 83

def closed?
  @closed
end

#concat(items) ⇒ Object

A bulk push alternative to #push. Use this method if pushing more than one item for efficiency.



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/datadog/core/buffer/random.rb', line 33

def concat(items)
  return if closed?

  # Segment items into underflow and overflow
  underflow, overflow = overflow_segments(items)

  # Concatenate items do not exceed capacity.
  add_all!(underflow) unless underflow.nil?

  # Iteratively replace items, to ensure pseudo-random replacement.
  overflow&.each { |item| replace!(item) }
end

#empty?Boolean

Return if the buffer is empty.

Returns:

  • (Boolean)


73
74
75
# File 'lib/datadog/core/buffer/random.rb', line 73

def empty?
  @items.empty?
end

#lengthObject

Return the current number of stored items.



68
69
70
# File 'lib/datadog/core/buffer/random.rb', line 68

def length
  @items.length
end

#popObject

Stored items are returned and the local buffer is reset.



63
64
65
# File 'lib/datadog/core/buffer/random.rb', line 63

def pop
  drain!
end

#push(item) ⇒ Object

Add a new “item“ in the local queue. This method doesn’t block the execution even if the buffer is full.

When the buffer is full, we try to ensure that we are fairly choosing newly pushed items by randomly inserting them into the buffer slots. This discards old items randomly while trying to ensure that recent items are still captured.



24
25
26
27
28
29
# File 'lib/datadog/core/buffer/random.rb', line 24

def push(item)
  return if closed?

  full? ? replace!(item) : add!(item)
  item
end

#unshift(*items) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/datadog/core/buffer/random.rb', line 46

def unshift(*items)
  # TODO The existing concat implementation does not always append
  # to the end of the buffer - if the buffer is full, a random
  # item is deleted and the new item is added in the position of
  # removed item.
  # Therefore, if we want to preserve the item order, concat
  # would also need to be changed to maintain order.
  # With the existing implementation, the idea is to not move
  # existing items around, which is what sets unshift apart from
  # concat to begin with.
  #
  # Since this method currently delegates to +concat+, it does not
  # have a matching definition in the thread-safe worker.
  concat(items)
end