forked from valkey-io/valkey-glide-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalkey_glide_x_common.h
More file actions
577 lines (506 loc) · 31.6 KB
/
valkey_glide_x_common.h
File metadata and controls
577 lines (506 loc) · 31.6 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/*
+----------------------------------------------------------------------+
| Valkey Glide X-Commands Common Utilities |
+----------------------------------------------------------------------+
| Copyright (c) 2023-2025 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#ifndef VALKEY_GLIDE_X_COMMON_H
#define VALKEY_GLIDE_X_COMMON_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "command_response.h"
#include "valkey_glide_commands_common.h"
/* ====================================================================
* STRUCTURES
* ==================================================================== */
/**
* Options for X* commands with COUNT option
*/
typedef struct _x_count_options_t {
long count; /* COUNT option value */
int has_count; /* Whether COUNT option is set */
} x_count_options_t;
/**
* Options for XREAD/XREADGROUP commands
*/
typedef struct _x_read_options_t {
long block; /* BLOCK option value (milliseconds) */
int has_block; /* Whether BLOCK option is set */
long count; /* COUNT option value */
int has_count; /* Whether COUNT option is set */
int noack; /* NOACK flag (for XREADGROUP) */
} x_read_options_t;
/**
* Options for XPENDING command
*/
typedef struct _x_pending_options_t {
const char* start; /* Start ID */
size_t start_len; /* Start ID length */
const char* end; /* End ID */
size_t end_len; /* End ID length */
long count; /* COUNT option value */
int has_count; /* Whether COUNT option is set */
const char* consumer; /* Consumer name */
size_t consumer_len; /* Consumer name length */
} x_pending_options_t;
/**
* Options for XTRIM command
*/
typedef struct _x_trim_options_t {
int approximate; /* Approximate flag (~) */
long limit; /* LIMIT option value */
int has_limit; /* Whether LIMIT option is set */
} x_trim_options_t;
/**
* Options for XADD command
*/
typedef struct _x_add_options_t {
long maxlen; /* MAXLEN option value */
int has_maxlen; /* Whether MAXLEN option is set */
int approximate; /* Approximate flag (~) */
int nomkstream; /* NOMKSTREAM flag */
int minid_strategy; /* Whether to use MINID instead of MAXLEN */
} x_add_options_t;
/**
* Options for XCLAIM/XAUTOCLAIM commands
*/
typedef struct _x_claim_options_t {
long idle; /* IDLE option value */
int has_idle; /* Whether IDLE option is set */
long time; /* TIME option value */
int has_time; /* Whether TIME option is set */
long retrycount; /* RETRYCOUNT option value */
int has_retrycount; /* Whether RETRYCOUNT option is set */
int force; /* FORCE flag */
int justid; /* JUSTID flag */
int has_count; /* Whether COUNT option is set */
long count; /* COUNT option value */
} x_claim_options_t;
typedef struct {
int justid;
} x_claim_result_context_t;
/**
* Generic command arguments structure for X commands
*/
typedef struct _x_command_args_t {
/* Common fields */
const void* glide_client; /* GlideClient instance */
const char* key; /* Key argument */
size_t key_len; /* Key argument length */
/* For XLEN command - no additional fields needed */
/* For XDEL command */
zval* ids; /* Array of IDs */
int id_count; /* Number of IDs */
/* For XACK command */
const char* group; /* Group name */
size_t group_len; /* Group name length */
/* ids and id_count are reused from XDEL */
/* For XADD command */
const char* id; /* ID to add */
size_t id_len; /* ID length */
zval* field_values; /* Field-value pairs to add */
int fv_count; /* Number of field-value pairs */
x_add_options_t add_opts; /* XADD options */
/* For XTRIM command */
const char* strategy; /* Strategy (MAXLEN, MINID) */
size_t strategy_len; /* Strategy length */
const char* threshold; /* Threshold value */
size_t threshold_len; /* Threshold length */
x_trim_options_t trim_opts; /* XTRIM options */
/* For XRANGE/XREVRANGE commands */
const char* start; /* Start ID */
size_t start_len; /* Start ID length */
const char* end; /* End ID */
size_t end_len; /* End ID length */
x_count_options_t range_opts; /* XRANGE options */
/* For XPENDING command */
/* key, group, and group_len reused from above */
x_pending_options_t pending_opts; /* XPENDING options */
/* For XREAD command */
zval* streams; /* Array of stream keys */
/* ids reused from above (contains stream IDs) */
x_read_options_t read_opts; /* XREAD options */
/* For XREADGROUP command */
/* group and group_len reused from above */
const char* consumer; /* Consumer name */
size_t consumer_len; /* Consumer name length */
/* streams and ids reused from above */
/* read_opts reused from above */
/* For XAUTOCLAIM command */
/* key, group, group_len, consumer, consumer_len reused from above */
long min_idle_time; /* Minimum idle time */
/* start and start_len reused from above */
x_claim_options_t claim_opts; /* XCLAIM options */
/* For XINFO command */
const char* subcommand; /* Subcommand (CONSUMERS, GROUPS, STREAM) */
size_t subcommand_len; /* Subcommand length */
zval* args; /* Additional arguments */
int args_count; /* Number of additional arguments */
/* For XGROUP command */
/* subcommand, subcommand_len, args, args_count reused from above */
/* General options */
zval* options; /* Raw options array from PHP */
} x_command_args_t;
/* Function pointer types */
typedef int (*x_result_processor_t)(CommandResponse* response, void* output, zval* return_value);
typedef int (*x_arg_preparation_func_t)(x_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out,
char*** allocated_strings,
int* allocated_count);
typedef int (*x_simple_arg_preparation_func_t)(x_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out);
/**
* Command definition structure to encapsulate command properties
*/
typedef struct _x_command_def_t {
enum RequestType cmd_type; /* ValkeyGlide command type */
x_arg_preparation_func_t prepare_args; /* Function to prepare arguments */
} x_command_def_t;
/* Utility functions */
int allocate_command_args(int count, uintptr_t** args_out, unsigned long** args_len_out);
void free_command_args(uintptr_t* args, unsigned long* args_len);
/* Generic command execution framework */
int execute_x_generic_command(valkey_glide_object* valkey_glide,
enum RequestType cmd_type,
x_command_args_t* args,
void* result_ptr,
x_result_processor_t process_result,
zval* return_value);
/* Argument preparation */
int prepare_x_len_args(x_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out,
char*** allocated_strings,
int* allocated_count);
int prepare_x_del_args(x_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out,
char*** allocated_strings,
int* allocated_count);
int prepare_x_ack_args(x_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out,
char*** allocated_strings,
int* allocated_count);
int prepare_x_add_args(x_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out,
char*** allocated_strings,
int* allocated_count);
int prepare_x_trim_args(x_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out,
char*** allocated_strings,
int* allocated_count);
int prepare_x_range_args(x_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out,
char*** allocated_strings,
int* allocated_count);
int prepare_x_claim_args(x_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out,
char*** allocated_strings,
int* allocated_count);
int prepare_x_autoclaim_args(x_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out,
char*** allocated_strings,
int* allocated_count);
int prepare_x_group_args(x_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out,
char*** allocated_strings,
int* allocated_count);
int prepare_x_pending_args(x_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out,
char*** allocated_strings,
int* allocated_count);
int prepare_x_read_args(x_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out,
char*** allocated_strings,
int* allocated_count);
int prepare_x_readgroup_args(x_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out,
char*** allocated_strings,
int* allocated_count);
int prepare_x_info_args(x_command_args_t* args,
uintptr_t** args_out,
unsigned long** args_len_out,
char*** allocated_strings,
int* allocated_count);
int parse_x_add_options(zval* options, x_add_options_t* opts);
int parse_x_claim_options(zval* options, x_claim_options_t* opts);
int parse_x_count_options(zval* options, x_count_options_t* opts);
int parse_x_read_options(zval* options, x_read_options_t* opts);
int parse_x_pending_options(zval* options, x_pending_options_t* opts);
int parse_x_trim_options(zval* options, x_trim_options_t* opts);
int parse_x_group_options(zval* options, x_command_args_t* args);
int parse_x_info_options(zval* options, x_command_args_t* args);
/* Result processing */
int process_x_int_result(CommandResponse* response, void* output, zval* return_value);
int process_x_double_result(CommandResponse* response, void* output, zval* return_value);
int process_x_array_result(CommandResponse* response, void* output, zval* return_value);
int process_x_stream_result(CommandResponse* response, void* output, zval* return_value);
int process_x_add_result(CommandResponse* response, void* output, zval* return_value);
int process_x_group_result(CommandResponse* response, void* output, zval* return_value);
int process_x_pending_result(CommandResponse* response, void* output, zval* return_value);
int process_x_readgroup_result(CommandResponse* response, void* output, zval* return_value);
int process_x_claim_result(CommandResponse* response, void* output, zval* return_value);
int process_x_autoclaim_result(CommandResponse* response, void* output, zval* return_value);
int process_x_info_result(CommandResponse* response, void* output, zval* return_value);
/* Command implementation functions */
int execute_xlen_command(zval* object, int argc, zval* return_value, zend_class_entry* ce);
int execute_xdel_command(zval* object, int argc, zval* return_value, zend_class_entry* ce);
int execute_xack_command(zval* object, int argc, zval* return_value, zend_class_entry* ce);
int execute_xadd_command(zval* object, int argc, zval* return_value, zend_class_entry* ce);
int execute_xtrim_command(zval* object, int argc, zval* return_value, zend_class_entry* ce);
int execute_xrange_command(zval* object, int argc, zval* return_value, zend_class_entry* ce);
int execute_xrevrange_command(zval* object, int argc, zval* return_value, zend_class_entry* ce);
int execute_xpending_command(zval* object, int argc, zval* return_value, zend_class_entry* ce);
int execute_xread_command(zval* object, int argc, zval* return_value, zend_class_entry* ce);
int execute_xreadgroup_command(zval* object, int argc, zval* return_value, zend_class_entry* ce);
int execute_xautoclaim_command(zval* object, int argc, zval* return_value, zend_class_entry* ce);
int execute_xclaim_command(zval* object, int argc, zval* return_value, zend_class_entry* ce);
int execute_xgroup_command(zval* object, int argc, zval* return_value, zend_class_entry* ce);
int execute_xinfo_command(zval* object, int argc, zval* return_value, zend_class_entry* ce);
/* ====================================================================
* X COMMAND MACROS
* ==================================================================== */
#define XREADGROUP_METHOD_IMPL(class_name) \
PHP_METHOD(class_name, xreadgroup) { \
if (execute_xreadgroup_command(getThis(), \
ZEND_NUM_ARGS(), \
return_value, \
strcmp(#class_name, "ValkeyGlideCluster") == 0 \
? get_valkey_glide_cluster_ce() \
: get_valkey_glide_ce())) { \
return; \
} \
zval_dtor(return_value); \
RETURN_FALSE; \
}
#define XACK_METHOD_IMPL(class_name) \
PHP_METHOD(class_name, xack) { \
if (execute_xack_command(getThis(), \
ZEND_NUM_ARGS(), \
return_value, \
strcmp(#class_name, "ValkeyGlideCluster") == 0 \
? get_valkey_glide_cluster_ce() \
: get_valkey_glide_ce())) { \
return; \
} \
zval_dtor(return_value); \
RETURN_FALSE; \
}
#define XADD_METHOD_IMPL(class_name) \
PHP_METHOD(class_name, xadd) { \
if (execute_xadd_command(getThis(), \
ZEND_NUM_ARGS(), \
return_value, \
strcmp(#class_name, "ValkeyGlideCluster") == 0 \
? get_valkey_glide_cluster_ce() \
: get_valkey_glide_ce())) { \
return; \
} \
zval_dtor(return_value); \
RETURN_FALSE; \
}
#define XAUTOCLAIM_METHOD_IMPL(class_name) \
PHP_METHOD(class_name, xautoclaim) { \
if (execute_xautoclaim_command(getThis(), \
ZEND_NUM_ARGS(), \
return_value, \
strcmp(#class_name, "ValkeyGlideCluster") == 0 \
? get_valkey_glide_cluster_ce() \
: get_valkey_glide_ce())) { \
return; \
} \
zval_dtor(return_value); \
RETURN_FALSE; \
}
#define XDEL_METHOD_IMPL(class_name) \
PHP_METHOD(class_name, xdel) { \
if (execute_xdel_command(getThis(), \
ZEND_NUM_ARGS(), \
return_value, \
strcmp(#class_name, "ValkeyGlideCluster") == 0 \
? get_valkey_glide_cluster_ce() \
: get_valkey_glide_ce())) { \
return; \
} \
zval_dtor(return_value); \
RETURN_FALSE; \
}
#define XACK_METHOD_IMPL(class_name) \
PHP_METHOD(class_name, xack) { \
if (execute_xack_command(getThis(), \
ZEND_NUM_ARGS(), \
return_value, \
strcmp(#class_name, "ValkeyGlideCluster") == 0 \
? get_valkey_glide_cluster_ce() \
: get_valkey_glide_ce())) { \
return; \
} \
zval_dtor(return_value); \
RETURN_FALSE; \
}
#define XAUTOCLAIM_METHOD_IMPL(class_name) \
PHP_METHOD(class_name, xautoclaim) { \
if (execute_xautoclaim_command(getThis(), \
ZEND_NUM_ARGS(), \
return_value, \
strcmp(#class_name, "ValkeyGlideCluster") == 0 \
? get_valkey_glide_cluster_ce() \
: get_valkey_glide_ce())) { \
return; \
} \
zval_dtor(return_value); \
RETURN_FALSE; \
}
#define XCLAIM_METHOD_IMPL(class_name) \
PHP_METHOD(class_name, xclaim) { \
if (execute_xclaim_command(getThis(), \
ZEND_NUM_ARGS(), \
return_value, \
strcmp(#class_name, "ValkeyGlideCluster") == 0 \
? get_valkey_glide_cluster_ce() \
: get_valkey_glide_ce())) { \
return; \
} \
zval_dtor(return_value); \
RETURN_FALSE; \
}
#define XDEL_METHOD_IMPL(class_name) \
PHP_METHOD(class_name, xdel) { \
if (execute_xdel_command(getThis(), \
ZEND_NUM_ARGS(), \
return_value, \
strcmp(#class_name, "ValkeyGlideCluster") == 0 \
? get_valkey_glide_cluster_ce() \
: get_valkey_glide_ce())) { \
return; \
} \
zval_dtor(return_value); \
RETURN_FALSE; \
}
#define XGROUP_METHOD_IMPL(class_name) \
PHP_METHOD(class_name, xgroup) { \
if (execute_xgroup_command(getThis(), \
ZEND_NUM_ARGS(), \
return_value, \
strcmp(#class_name, "ValkeyGlideCluster") == 0 \
? get_valkey_glide_cluster_ce() \
: get_valkey_glide_ce())) { \
return; \
} \
zval_dtor(return_value); \
RETURN_FALSE; \
}
#define XINFO_METHOD_IMPL(class_name) \
PHP_METHOD(class_name, xinfo) { \
if (execute_xinfo_command(getThis(), \
ZEND_NUM_ARGS(), \
return_value, \
strcmp(#class_name, "ValkeyGlideCluster") == 0 \
? get_valkey_glide_cluster_ce() \
: get_valkey_glide_ce())) { \
return; \
} \
zval_dtor(return_value); \
RETURN_FALSE; \
}
#define XLEN_METHOD_IMPL(class_name) \
PHP_METHOD(class_name, xlen) { \
if (execute_xlen_command(getThis(), \
ZEND_NUM_ARGS(), \
return_value, \
strcmp(#class_name, "ValkeyGlideCluster") == 0 \
? get_valkey_glide_cluster_ce() \
: get_valkey_glide_ce())) { \
return; \
} \
zval_dtor(return_value); \
RETURN_FALSE; \
}
#define XPENDING_METHOD_IMPL(class_name) \
PHP_METHOD(class_name, xpending) { \
if (execute_xpending_command(getThis(), \
ZEND_NUM_ARGS(), \
return_value, \
strcmp(#class_name, "ValkeyGlideCluster") == 0 \
? get_valkey_glide_cluster_ce() \
: get_valkey_glide_ce())) { \
return; \
} \
zval_dtor(return_value); \
RETURN_FALSE; \
}
#define XRANGE_METHOD_IMPL(class_name) \
PHP_METHOD(class_name, xrange) { \
if (execute_xrange_command(getThis(), \
ZEND_NUM_ARGS(), \
return_value, \
strcmp(#class_name, "ValkeyGlideCluster") == 0 \
? get_valkey_glide_cluster_ce() \
: get_valkey_glide_ce())) { \
return; \
} \
zval_dtor(return_value); \
RETURN_FALSE; \
}
#define XREAD_METHOD_IMPL(class_name) \
PHP_METHOD(class_name, xread) { \
if (execute_xread_command(getThis(), \
ZEND_NUM_ARGS(), \
return_value, \
strcmp(#class_name, "ValkeyGlideCluster") == 0 \
? get_valkey_glide_cluster_ce() \
: get_valkey_glide_ce())) { \
return; \
} \
zval_dtor(return_value); \
RETURN_FALSE; \
}
#define XREVRANGE_METHOD_IMPL(class_name) \
PHP_METHOD(class_name, xrevrange) { \
if (execute_xrevrange_command(getThis(), \
ZEND_NUM_ARGS(), \
return_value, \
strcmp(#class_name, "ValkeyGlideCluster") == 0 \
? get_valkey_glide_cluster_ce() \
: get_valkey_glide_ce())) { \
return; \
} \
zval_dtor(return_value); \
RETURN_FALSE; \
}
#define XTRIM_METHOD_IMPL(class_name) \
PHP_METHOD(class_name, xtrim) { \
if (execute_xtrim_command(getThis(), \
ZEND_NUM_ARGS(), \
return_value, \
strcmp(#class_name, "ValkeyGlideCluster") == 0 \
? get_valkey_glide_cluster_ce() \
: get_valkey_glide_ce())) { \
return; \
} \
zval_dtor(return_value); \
RETURN_FALSE; \
}
#endif /* VALKEY_GLIDE_X_COMMON_H */