-
-
Notifications
You must be signed in to change notification settings - Fork 471
Expand file tree
/
Copy pathTelemetryStorage.php
More file actions
109 lines (95 loc) · 2.51 KB
/
Copy pathTelemetryStorage.php
File metadata and controls
109 lines (95 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
declare(strict_types=1);
namespace Sentry\Util;
/**
* Creates a new data container for Telemetry data such as Logs or Metrics.
* If a size parameter is passed, it will create a RingBuffer under the hood to restrict the number of
* items, if no size is used then it will be backed by a regular array.
*
* The TelemetryStorage operates under the same constraints as the RingBuffer, meaning that it's possible to
* add/remove from the front and the back, but it's not possible to remove from the middle or based on an offset.
* To do that, one has to either drain or convert it into an array (which will be basically free if unbounded)
*
* @template T
*
* @internal
*/
class TelemetryStorage implements \Countable
{
/**
* @var T[]|RingBuffer<T>
*/
private $data;
private function __construct(?int $size = null)
{
if ($size !== null) {
$this->data = new RingBuffer($size);
} else {
$this->data = [];
}
}
public function count(): int
{
return \count($this->data);
}
/**
* @param T $value
*/
public function push($value): void
{
if ($this->data instanceof RingBuffer) {
$this->data->push($value);
} else {
$this->data[] = $value;
}
}
/**
* @return T[]
*/
public function drain(): array
{
if ($this->data instanceof RingBuffer) {
return $this->data->drain();
}
$data = $this->data;
$this->data = [];
return $data;
}
/**
* @return T[]
*/
public function toArray(): array
{
if ($this->data instanceof RingBuffer) {
return $this->data->toArray();
}
return $this->data;
}
public function isEmpty(): bool
{
if ($this->data instanceof RingBuffer) {
return $this->data->isEmpty();
}
return empty($this->data);
}
/**
* Creates a new TelemetryStorage that is not bounded in size. This version should only be used if there
* is another flushing signal available.
*
* @return self<T>
*/
public static function unbounded(): self
{
return new self();
}
/**
* Creates a TelemetryStorage that has an upper bound of $size. It will drop the oldest items when new items
* are added while being at capacity.
*
* @return self<T>
*/
public static function bounded(int $size): self
{
return new self($size);
}
}