55
66use DateTimeInterface ;
77use Palicao \PhpRedisTimeSeries \Client \RedisClientInterface ;
8+ use Palicao \PhpRedisTimeSeries \Exception \InvalidDuplicatePolicyException ;
89use Palicao \PhpRedisTimeSeries \Exception \RedisClientException ;
910use RedisException ;
1011
1112final class TimeSeries
1213{
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+
1331 /** @var RedisClientInterface */
1432 private $ redis ;
1533
@@ -27,15 +45,44 @@ public function __construct(RedisClientInterface $redis)
2745 * @param string $key
2846 * @param int|null $retentionMs
2947 * @param Label[] $labels
48+ * @param bool $uncompressed
49+ * @param int|null $chunkSize
50+ * @param string|null $duplicatePolicy
3051 * @return void
31- * @throws RedisClientException
3252 * @throws RedisException
3353 */
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
3562 {
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+
3682 $ this ->redis ->executeCommand (array_merge (
3783 ['TS.CREATE ' , $ key ],
3884 $ this ->getRetentionParams ($ retentionMs ),
85+ $ params ,
3986 $ this ->getLabelsParams (...$ labels )
4087 ));
4188 }
@@ -65,15 +112,44 @@ public function alter(string $key, ?int $retentionMs = null, array $labels = [])
65112 * @param Sample $sample
66113 * @param int|null $retentionMs
67114 * @param Label[] $labels
115+ * @param bool $uncompressed
116+ * @param int|null $chunkSize
117+ * @param string|null $duplicatePolicy
68118 * @return Sample
69- * @throws RedisClientException
70119 * @throws RedisException
71120 */
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
73129 {
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+
74149 $ timestamp = (int )$ this ->redis ->executeCommand (array_merge (
75150 ['TS.ADD ' ],
76151 $ sample ->toRedisParams (),
152+ $ params ,
77153 $ this ->getRetentionParams ($ retentionMs ),
78154 $ this ->getLabelsParams (...$ labels )
79155 ));
0 commit comments