From e08bf16637db65ed0f2cfc44d3dbc788d6fa89c0 Mon Sep 17 00:00:00 2001 From: "bodong.ybd" Date: Fri, 11 Sep 2020 16:59:15 +0800 Subject: [PATCH] Add ZINTER/ZUNION command Syntax: ZINTER/ZUNION numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX] [WITHSCORES] see #7624 --- src/db.c | 27 +++++++++++++- src/server.c | 10 +++++- src/server.h | 3 ++ src/t_zset.c | 77 ++++++++++++++++++++++++++++++---------- tests/unit/type/zset.tcl | 50 ++++++++++++++++++++++++++ 5 files changed, 146 insertions(+), 21 deletions(-) diff --git a/src/db.c b/src/db.c index 668a1193a..0a6481c8f 100644 --- a/src/db.c +++ b/src/db.c @@ -1388,7 +1388,7 @@ void getKeysFreeResult(int *result) { /* Helper function to extract keys from following commands: * ZUNIONSTORE ... * ZINTERSTORE ... */ -int *zunionInterGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys) { +int *zunionInterStoreGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys) { int i, num, *keys; UNUSED(cmd); @@ -1416,6 +1416,31 @@ int *zunionInterGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *nu return keys; } +/* Helper function to extract keys from following commands: + * ZUNION ... + * ZINTER ... */ +int *zunionInterGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys) { + int i, num, *keys; + UNUSED(cmd); + + num = atoi(argv[1]->ptr); + /* Sanity check. Don't return any key if the command is going to + * reply with syntax error. */ + if (num < 1 || num > (argc-2)) { + *numkeys = 0; + return NULL; + } + + keys = getKeysTempBuffer; + if (num>MAX_KEYS_BUFFER) + keys = zmalloc(sizeof(int)*(num)); + + /* Add all key positions for argv[2...n] to keys[] */ + for (i = 0; i < num; i++) keys[i] = 2+i; + *numkeys = num; + return keys; +} + /* Helper function to extract keys from the following commands: * EVAL