5
5
6
6
use DateTimeInterface ;
7
7
use Palicao \PhpRedisTimeSeries \Client \RedisClientInterface ;
8
+ use Palicao \PhpRedisTimeSeries \Exception \InvalidDuplicatePolicyException ;
8
9
use Palicao \PhpRedisTimeSeries \Exception \RedisClientException ;
9
10
use RedisException ;
10
11
11
12
final class TimeSeries
12
13
{
14
+ public const DUPLICATE_POLICY_BLOCK = 'BLOCK ' ;
15
+ public const DUPLICATE_POLICY_FIRST = 'FIRST ' ;
16
+ public const DUPLICATE_POLICY_LAST = 'LAST ' ;
17
+ public const DUPLICATE_POLICY_MIN = 'MIN ' ;
18
+ public const DUPLICATE_POLICY_MAX = 'MAX ' ;
19
+ public const DUPLICATE_POLICY_SUM = 'SUM ' ;
20
+
21
+ private const DUPLICATE_POLICIES = [
22
+ self ::DUPLICATE_POLICY_BLOCK ,
23
+ self ::DUPLICATE_POLICY_FIRST ,
24
+ self ::DUPLICATE_POLICY_LAST ,
25
+ self ::DUPLICATE_POLICY_MIN ,
26
+ self ::DUPLICATE_POLICY_MAX ,
27
+ self ::DUPLICATE_POLICY_SUM
28
+ ];
29
+
30
+
13
31
/** @var RedisClientInterface */
14
32
private $ redis ;
15
33
@@ -27,15 +45,44 @@ public function __construct(RedisClientInterface $redis)
27
45
* @param string $key
28
46
* @param int|null $retentionMs
29
47
* @param Label[] $labels
48
+ * @param bool $uncompressed
49
+ * @param int|null $chunkSize
50
+ * @param string|null $duplicatePolicy
30
51
* @return void
31
- * @throws RedisClientException
32
52
* @throws RedisException
33
53
*/
34
- public function create (string $ key , ?int $ retentionMs = null , array $ labels = []): void
54
+ public function create (
55
+ string $ key ,
56
+ ?int $ retentionMs = null ,
57
+ array $ labels = [],
58
+ bool $ uncompressed = false ,
59
+ ?int $ chunkSize = null ,
60
+ ?string $ duplicatePolicy = null
61
+ ): void
35
62
{
63
+ $ params = [];
64
+
65
+ if ($ uncompressed === true ) {
66
+ $ params [] = 'UNCOMPRESSED ' ;
67
+ }
68
+
69
+ if ($ chunkSize !== null ) {
70
+ $ params [] = 'CHUNK_SIZE ' ;
71
+ $ params [] = (string ) $ chunkSize ;
72
+ }
73
+
74
+ if ($ duplicatePolicy !== null ) {
75
+ if (!in_array ($ duplicatePolicy , self ::DUPLICATE_POLICIES )) {
76
+ throw new InvalidDuplicatePolicyException (sprintf ("Duplicate policy %s is invalid " , $ duplicatePolicy ));
77
+ }
78
+ $ params [] = 'DUPLICATE_POLICY ' ;
79
+ $ params [] = $ duplicatePolicy ;
80
+ }
81
+
36
82
$ this ->redis ->executeCommand (array_merge (
37
83
['TS.CREATE ' , $ key ],
38
84
$ this ->getRetentionParams ($ retentionMs ),
85
+ $ params ,
39
86
$ this ->getLabelsParams (...$ labels )
40
87
));
41
88
}
@@ -65,15 +112,44 @@ public function alter(string $key, ?int $retentionMs = null, array $labels = [])
65
112
* @param Sample $sample
66
113
* @param int|null $retentionMs
67
114
* @param Label[] $labels
115
+ * @param bool $uncompressed
116
+ * @param int|null $chunkSize
117
+ * @param string|null $duplicatePolicy
68
118
* @return Sample
69
- * @throws RedisClientException
70
119
* @throws RedisException
71
120
*/
72
- public function add (Sample $ sample , ?int $ retentionMs = null , array $ labels = []): Sample
121
+ public function add (
122
+ Sample $ sample ,
123
+ ?int $ retentionMs = null ,
124
+ array $ labels = [],
125
+ bool $ uncompressed = false ,
126
+ ?int $ chunkSize = null ,
127
+ ?string $ duplicatePolicy = null
128
+ ): Sample
73
129
{
130
+ $ params = [];
131
+
132
+ if ($ uncompressed === true ) {
133
+ $ params [] = 'UNCOMPRESSED ' ;
134
+ }
135
+
136
+ if ($ chunkSize !== null ) {
137
+ $ params [] = 'CHUNK_SIZE ' ;
138
+ $ params [] = (string ) $ chunkSize ;
139
+ }
140
+
141
+ if ($ duplicatePolicy !== null ) {
142
+ if (!in_array ($ duplicatePolicy , self ::DUPLICATE_POLICIES )) {
143
+ throw new InvalidDuplicatePolicyException (sprintf ("Duplicate policy %s is invalid " , $ duplicatePolicy ));
144
+ }
145
+ $ params [] = 'ON_DUPLICATE ' ;
146
+ $ params [] = $ duplicatePolicy ;
147
+ }
148
+
74
149
$ timestamp = (int )$ this ->redis ->executeCommand (array_merge (
75
150
['TS.ADD ' ],
76
151
$ sample ->toRedisParams (),
152
+ $ params ,
77
153
$ this ->getRetentionParams ($ retentionMs ),
78
154
$ this ->getLabelsParams (...$ labels )
79
155
));
0 commit comments