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

Skip to content

Commit 4852a51

Browse files
committed
xInfo response format
1 parent 1226303 commit 4852a51

4 files changed

Lines changed: 119 additions & 1 deletion

File tree

library.c

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,6 +1451,94 @@ redis_xclaim_reply(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock,
14511451
return -1;
14521452
}
14531453

1454+
static int
1455+
redis_read_xinfo_response(RedisSock *redis_sock, zval *z_ret, int elements)
1456+
{
1457+
zval zv;
1458+
int i, len;
1459+
char *key = NULL, *data;
1460+
REDIS_REPLY_TYPE type;
1461+
long li;
1462+
1463+
for (i = 0; i < elements; ++i) {
1464+
if (redis_read_reply_type(redis_sock, &type, &li TSRMLS_CC) < 0) {
1465+
goto failure;
1466+
}
1467+
switch (type) {
1468+
case TYPE_BULK:
1469+
if ((data = redis_sock_read_bulk_reply(redis_sock, li TSRMLS_CC)) == NULL) {
1470+
goto failure;
1471+
} else if (key) {
1472+
add_assoc_stringl_ex(z_ret, key, len, data, li);
1473+
efree(data);
1474+
efree(key);
1475+
key = NULL;
1476+
} else {
1477+
key = data;
1478+
len = li;
1479+
}
1480+
break;
1481+
case TYPE_INT:
1482+
if (key) {
1483+
add_assoc_long_ex(z_ret, key, len, li);
1484+
efree(key);
1485+
key = NULL;
1486+
} else {
1487+
len = spprintf(&key, 0, "%ld", li);
1488+
}
1489+
break;
1490+
case TYPE_MULTIBULK:
1491+
array_init(&zv);
1492+
if (redis_read_xinfo_response(redis_sock, &zv, li) != SUCCESS) {
1493+
zval_dtor(&zv);
1494+
goto failure;
1495+
}
1496+
if (key) {
1497+
add_assoc_zval_ex(z_ret, key, len, &zv);
1498+
efree(key);
1499+
key = NULL;
1500+
} else {
1501+
add_next_index_zval(z_ret, &zv);
1502+
}
1503+
break;
1504+
default:
1505+
goto failure;
1506+
}
1507+
}
1508+
1509+
return SUCCESS;
1510+
1511+
failure:
1512+
if (key) efree(key);
1513+
return FAILURE;
1514+
}
1515+
1516+
PHP_REDIS_API int
1517+
redis_xinfo_reply(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx)
1518+
{
1519+
zval z_ret;
1520+
int i, elements;
1521+
1522+
if (read_mbulk_header(redis_sock, &elements TSRMLS_CC) == SUCCESS) {
1523+
array_init(&z_ret);
1524+
if (redis_read_xinfo_response(redis_sock, &z_ret, elements TSRMLS_CC) == SUCCESS) {
1525+
if (IS_ATOMIC(redis_sock)) {
1526+
RETVAL_ZVAL(&z_ret, 0, 1);
1527+
} else {
1528+
add_next_index_zval(z_tab, &z_ret);
1529+
}
1530+
return SUCCESS;
1531+
}
1532+
zval_dtor(&z_ret);
1533+
}
1534+
if (IS_ATOMIC(redis_sock)) {
1535+
RETVAL_FALSE;
1536+
} else {
1537+
add_next_index_bool(z_tab, 0);
1538+
}
1539+
return FAILURE;
1540+
}
1541+
14541542
/* Zipped key => value reply but we don't touch anything (e.g. CONFIG GET) */
14551543
PHP_REDIS_API int redis_mbulk_reply_zipped_raw(INTERNAL_FUNCTION_PARAMETERS, RedisSock *redis_sock, zval *z_tab, void *ctx)
14561544
{

library.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ PHP_REDIS_API int redis_xread_reply(INTERNAL_FUNCTION_PARAMETERS,
7878
RedisSock *redis_sock, zval *z_tab, void *ctx);
7979
PHP_REDIS_API int redis_xclaim_reply(INTERNAL_FUNCTION_PARAMETERS,
8080
RedisSock *redis_sock, zval *z_tab, void *ctx);
81+
PHP_REDIS_API int redis_xinfo_reply(INTERNAL_FUNCTION_PARAMETERS,
82+
RedisSock *redis_sock, zval *z_tab, void *ctx);
8183

8284
PHP_REDIS_API int redis_subscribe_response(INTERNAL_FUNCTION_PARAMETERS,
8385
RedisSock *redis_sock, zval *z_tab, void *ctx);

redis.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3663,7 +3663,7 @@ PHP_METHOD(Redis, xgroup) {
36633663
}
36643664

36653665
PHP_METHOD(Redis, xinfo) {
3666-
REDIS_PROCESS_CMD(xinfo, redis_read_variant_reply);
3666+
REDIS_PROCESS_CMD(xinfo, redis_xinfo_reply);
36673667
}
36683668

36693669
PHP_METHOD(Redis, xlen) {

tests/RedisTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5854,6 +5854,34 @@ public function testXClaim() {
58545854
}
58555855
}
58565856

5857+
public function testXInfo()
5858+
{
5859+
if (!$this->minVersionCheck("5.0")) {
5860+
return $this->markTestSkipped();
5861+
}
5862+
/* Create some streams and groups */
5863+
$stream = 's';
5864+
$groups = ['g1' => 0, 'g2' => 0];
5865+
$this->addStreamsAndGroups([$stream], 1, $groups);
5866+
5867+
$info = $this->redis->xInfo('GROUPS', $stream);
5868+
$this->assertTrue(is_array($info));
5869+
$this->assertEquals(count($info), count($groups));
5870+
foreach ($info as $group) {
5871+
$this->assertTrue(array_key_exists('name', $group));
5872+
$this->assertTrue(array_key_exists($group['name'], $groups));
5873+
}
5874+
5875+
$info = $this->redis->xInfo('STREAM', $stream);
5876+
$this->assertTrue(is_array($info));
5877+
$this->assertTrue(array_key_exists('groups', $info));
5878+
$this->assertEquals($info['groups'], count($groups));
5879+
foreach (['first-entry', 'last-entry'] as $key) {
5880+
$this->assertTrue(array_key_exists($key, $info));
5881+
$this->assertTrue(is_array($info[$key]));
5882+
}
5883+
}
5884+
58575885
public function testSession_savedToRedis()
58585886
{
58595887
$this->setSessionHandler();

0 commit comments

Comments
 (0)