Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6b34d17

Browse files
Add new Redis 6.2.0 XTRIM options
Fixes #1961
1 parent d2044c9 commit 6b34d17

10 files changed

Lines changed: 81 additions & 21 deletions

library.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,10 @@ int redis_cmd_append_sstr_key(smart_string *str, char *key, size_t len, RedisSoc
10621062
return retval;
10631063
}
10641064

1065+
int redis_cmd_append_sstr_key_zstr(smart_string *dst, zend_string *key, RedisSock *redis_sock, short *slot) {
1066+
return redis_cmd_append_sstr_key(dst, ZSTR_VAL(key), ZSTR_LEN(key), redis_sock, slot);
1067+
}
1068+
10651069
/* Append an array key to a redis smart string command. This function
10661070
* handles the boilerplate conditionals around string or integer keys */
10671071
int redis_cmd_append_sstr_arrkey(smart_string *cmd, zend_string *kstr, zend_ulong idx)

library.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ int redis_cmd_append_sstr_dbl(smart_string *str, double value);
5050
int redis_cmd_append_sstr_zstr(smart_string *str, zend_string *zstr);
5151
int redis_cmd_append_sstr_zval(smart_string *str, zval *z, RedisSock *redis_sock);
5252
int redis_cmd_append_sstr_key(smart_string *str, char *key, size_t len, RedisSock *redis_sock, short *slot);
53+
int redis_cmd_append_sstr_key_zstr(smart_string *str, zend_string *key, RedisSock *redis_sock, short *slot);
5354
int redis_cmd_append_sstr_arrkey(smart_string *cmd, zend_string *kstr, zend_ulong idx);
5455

5556
PHP_REDIS_API int redis_spprintf(RedisSock *redis_sock, short *slot, char **ret, char *kw, char *fmt, ...);

redis.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ public function xreadgroup(string $group, string $consumer, array $streams, int
515515

516516
public function xrevrange(string $key, string $start, string $end, int $count = -1): bool|array;
517517

518-
public function xtrim(string $key, int $maxlen, bool $approx = false): int;
518+
public function xtrim(string $key, int $maxlen, bool $approx = false, bool $minid = false, int $limit = -1): Redis|int|false;
519519

520520
public function zAdd(string $key, array|float $score_or_options, mixed ...$more_scores_and_mems): Redis|int|false;
521521

redis_arginfo.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 2c4ee6dc4a5aa66b1700df8859233c349aa00519 */
2+
* Stub hash: 0ace014dc4f3f94eedd835f1d6895703aea3e607 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis___construct, 0, 0, 0)
55
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_ARRAY, 0, "null")
@@ -920,10 +920,12 @@ ZEND_END_ARG_INFO()
920920

921921
#define arginfo_class_Redis_xrevrange arginfo_class_Redis_xrange
922922

923-
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_Redis_xtrim, 0, 2, IS_LONG, 0)
923+
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_Redis_xtrim, 0, 2, Redis, MAY_BE_LONG|MAY_BE_FALSE)
924924
ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0)
925925
ZEND_ARG_TYPE_INFO(0, maxlen, IS_LONG, 0)
926926
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, approx, _IS_BOOL, 0, "false")
927+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, minid, _IS_BOOL, 0, "false")
928+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, limit, IS_LONG, 0, "-1")
927929
ZEND_END_ARG_INFO()
928930

929931
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_Redis_zAdd, 0, 2, Redis, MAY_BE_LONG|MAY_BE_FALSE)

redis_cluster.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ public function xreadgroup(string $group, string $consumer, array $streams, int
358358

359359
public function xrevrange(string $key, string $start, string $end, int $count = -1): bool|array;
360360

361-
public function xtrim(string $key, int $maxlen, bool $approx = false): int;
361+
public function xtrim(string $key, int $maxlen, bool $approx = false, bool $minid = false, int $limit = -1): RedisCluster|int|false;
362362

363363
public function zadd(string $key, float $score, string $member, mixed ...$extra_args): int;
364364

redis_cluster_arginfo.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 956f295e74025def86150d0acdf7a11594c72d47 */
2+
* Stub hash: 39c0741e5bf358e116f5ed2caa35c1ca226fd593 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisCluster___construct, 0, 0, 1)
55
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 1)
@@ -780,10 +780,12 @@ ZEND_END_ARG_INFO()
780780

781781
#define arginfo_class_RedisCluster_xrevrange arginfo_class_RedisCluster_xrange
782782

783-
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_RedisCluster_xtrim, 0, 2, IS_LONG, 0)
783+
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_RedisCluster_xtrim, 0, 2, RedisCluster, MAY_BE_LONG|MAY_BE_FALSE)
784784
ZEND_ARG_TYPE_INFO(0, key, IS_STRING, 0)
785785
ZEND_ARG_TYPE_INFO(0, maxlen, IS_LONG, 0)
786786
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, approx, _IS_BOOL, 0, "false")
787+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, minid, _IS_BOOL, 0, "false")
788+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, limit, IS_LONG, 0, "-1")
787789
ZEND_END_ARG_INFO()
788790

789791
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_RedisCluster_zadd, 0, 3, IS_LONG, 0)

redis_cluster_legacy_arginfo.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 956f295e74025def86150d0acdf7a11594c72d47 */
2+
* Stub hash: 39c0741e5bf358e116f5ed2caa35c1ca226fd593 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisCluster___construct, 0, 0, 1)
55
ZEND_ARG_INFO(0, name)
@@ -684,6 +684,8 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisCluster_xtrim, 0, 0, 2)
684684
ZEND_ARG_INFO(0, key)
685685
ZEND_ARG_INFO(0, maxlen)
686686
ZEND_ARG_INFO(0, approx)
687+
ZEND_ARG_INFO(0, minid)
688+
ZEND_ARG_INFO(0, limit)
687689
ZEND_END_ARG_INFO()
688690

689691
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_RedisCluster_zadd, 0, 0, 3)

redis_commands.c

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5650,29 +5650,55 @@ int redis_xinfo_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
56505650
return SUCCESS;
56515651
}
56525652

5653-
/* XTRIM MAXLEN [~] count */
5653+
// XTRIM key <MAXLEN | MINID> [= | ~] threshold [LIMIT count]
56545654
int redis_xtrim_cmd(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
56555655
char **cmd, int *cmd_len, short *slot, void **ctx)
56565656
{
5657-
char *key;
5658-
size_t keylen;
5659-
zend_long maxlen;
5660-
zend_bool approx = 0;
5657+
zend_long threshold = 0, limit = -1;
5658+
zend_bool approx = 0, minid = 0;
5659+
smart_string cmdstr = {0};
5660+
zend_string *key = NULL;
5661+
int argc;
56615662

5662-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sl|b", &key, &keylen,
5663-
&maxlen, &approx) == FAILURE)
5664-
{
5665-
return FAILURE;
5663+
ZEND_PARSE_PARAMETERS_START(2, 5)
5664+
Z_PARAM_STR(key)
5665+
Z_PARAM_LONG(threshold)
5666+
Z_PARAM_OPTIONAL
5667+
Z_PARAM_BOOL(approx)
5668+
Z_PARAM_BOOL(minid)
5669+
Z_PARAM_LONG(limit)
5670+
ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
5671+
5672+
argc = 4 + (approx && limit > -1 ? 2 : 0);
5673+
REDIS_CMD_INIT_SSTR_STATIC(&cmdstr, argc, "XTRIM");
5674+
5675+
redis_cmd_append_sstr_key_zstr(&cmdstr, key, redis_sock, slot);
5676+
5677+
if (minid) {
5678+
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "MINID");
5679+
} else {
5680+
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "MAXLEN");
56665681
}
56675682

56685683
if (approx) {
5669-
*cmd_len = REDIS_CMD_SPPRINTF(cmd, "XTRIM", "kssl", key, keylen,
5670-
"MAXLEN", 6, "~", 1, maxlen);
5684+
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "~");
56715685
} else {
5672-
*cmd_len = REDIS_CMD_SPPRINTF(cmd, "XTRIM", "ksl", key, keylen,
5673-
"MAXLEN", 6, maxlen);
5686+
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "=");
56745687
}
56755688

5689+
redis_cmd_append_sstr_long(&cmdstr, threshold);
5690+
5691+
if (limit > -1 && approx) {
5692+
REDIS_CMD_APPEND_SSTR_STATIC(&cmdstr, "LIMIT");
5693+
redis_cmd_append_sstr_long(&cmdstr, limit);
5694+
} else if (limit > -1) {
5695+
php_error_docref(NULL, E_WARNING, "Cannot use LIMIT without an approximate match, ignoring");
5696+
} else if (ZEND_NUM_ARGS() == 5) {
5697+
php_error_docref(NULL, E_WARNING, "Limit must be >= 0");
5698+
}
5699+
5700+
*cmd = cmdstr.c;
5701+
*cmd_len = cmdstr.len;
56765702
return SUCCESS;
56775703
}
56785704

redis_legacy_arginfo.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 2c4ee6dc4a5aa66b1700df8859233c349aa00519 */
2+
* Stub hash: 0ace014dc4f3f94eedd835f1d6895703aea3e607 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis___construct, 0, 0, 0)
55
ZEND_ARG_INFO(0, options)
@@ -807,6 +807,8 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_xtrim, 0, 0, 2)
807807
ZEND_ARG_INFO(0, key)
808808
ZEND_ARG_INFO(0, maxlen)
809809
ZEND_ARG_INFO(0, approx)
810+
ZEND_ARG_INFO(0, minid)
811+
ZEND_ARG_INFO(0, limit)
810812
ZEND_END_ARG_INFO()
811813

812814
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Redis_zAdd, 0, 0, 2)

tests/RedisTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6625,6 +6625,27 @@ public function testXTrim() {
66256625
can call it with the flag */
66266626
$this->addStreamEntries('stream', 100);
66276627
$this->assertFalse($this->redis->xTrim('stream', 1, true) === false);
6628+
6629+
/* We need Redis >= 6.2.0 for MINID and LIMIT options */
6630+
if (!$this->minVersionCheck("6.2.0"))
6631+
return;
6632+
6633+
$this->assertEquals(1, $this->redis->del('stream'));
6634+
6635+
/* Test minid by generating a stream with more than one */
6636+
for ($i = 1; $i < 3; $i++) {
6637+
for ($j = 0; $j < 3; $j++) {
6638+
$this->redis->xadd('stream', "$i-$j", ['foo' => 'bar']);
6639+
}
6640+
}
6641+
6642+
/* MINID of 2-0 */
6643+
$this->assertEquals(3, $this->redis->xtrim('stream', 2, false, true));
6644+
$this->assertEquals(['2-0', '2-1', '2-2'], array_keys($this->redis->xrange('stream', '0', '+')));
6645+
6646+
/* TODO: Figure oiut how to test LIMIT deterministically. For now just
6647+
send a LIMIT and verify we don't get a failure from Redis. */
6648+
$this->assertTrue(is_int($this->redis->xtrim('stream', 2, true, false, 3)));
66286649
}
66296650

66306651
/* XCLAIM is one of the most complicated commands, with a great deal of different options

0 commit comments

Comments
 (0)