From 79a898fb7ba91433043aba74ab5e4981e0a61ed2 Mon Sep 17 00:00:00 2001 From: Kenneth Cheng Date: Sun, 3 May 2020 13:04:46 +0800 Subject: [PATCH] fix "no output if no top clause" --- src/Makefile | 2 +- src/dbx.c | 40 ++++++++++++++++++++++++---------------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/Makefile b/src/Makefile index 05b890a..586a87b 100644 --- a/src/Makefile +++ b/src/Makefile @@ -21,7 +21,7 @@ else SHOBJ_CFLAGS ?= -dynamic -fno-common -g -ggdb -lc -lm SHOBJ_LDFLAGS ?= -bundle -undefined dynamic_lookup endif -CFLAGS = -I$(RM_INCLUDE_DIR) -Wall -g -fPIC -std=gnu99 +CFLAGS = -I$(RM_INCLUDE_DIR) -Wall -g -fPIC -std=gnu99 CC=gcc all: rmutil dbx.so diff --git a/src/dbx.c b/src/dbx.c index b8ae798..0a7036c 100644 --- a/src/dbx.c +++ b/src/dbx.c @@ -284,7 +284,7 @@ int SelectCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { return RedisModule_WrongArity(ctx); // Table - long top = 0; + long top = -1; RedisModuleString *fromKeys; char intoKey[32] = ""; @@ -452,28 +452,36 @@ int SelectCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { char setName[32]; sprintf(setName, "__db_tempset_%i", rn++); - char stmt[128]; RedisModuleCallReply *rep; if (buildSetByPattern(ctx, ®ex, setName, vWhere) > 0) { - size_t n = 0; - // Sort the fields under the key and send the resulting array to processRecords module char *field; - Vector_Get(vOrder, 0, &field); - if (field[strlen(field)-1] == '-') { - field[strlen(field)-1] = 0; - sprintf(stmt, "*->%s", field); - rep = RedisModule_Call(ctx, "SORT", "ccccc", setName, "by", stmt, "desc", "alpha"); + int nSortField = Vector_Size(vOrder); + int cap = 3 * nSortField + 2; + + RedisModuleString *param[cap]; + param[0] = RedisModule_CreateString(ctx, setName, strlen(setName)); + + for(int sf = 0; sf < nSortField; sf++) { + Vector_Get(vOrder, sf, &field); + if (field[strlen(field)-1] == '-') { + field[strlen(field)-1] = 0; + param[3+sf*3] = RedisModule_CreateString(ctx, "desc", 4); + } + else + param[3+sf*3] = RedisModule_CreateString(ctx, "asc", 3); + param[1+sf*3] = RedisModule_CreateString(ctx, "by", 2); + param[2+sf*3] = RedisModule_CreateStringPrintf(ctx, "*->%s", field, 3 + strlen(field)); } - else { - sprintf(stmt, "*->%s", field); - rep = RedisModule_Call(ctx, "SORT", "cccc", setName, "by", stmt, "alpha"); - } - n += processRecords(ctx, rep, ®ex, vSelect, NULL, intoKey, &top); + param[cap-1] = RedisModule_CreateString(ctx, "alpha", 5); + rep = RedisModule_Call(ctx, "SORT", "v", ¶m, cap); + + for(int i = 0; i < cap; i++) + RedisModule_FreeString(ctx, param[i]); + + size_t n = processRecords(ctx, rep, ®ex, vSelect, NULL, intoKey, &top); RedisModule_FreeCallReply(rep); - - // set number of output RedisModule_ReplySetArrayLength(ctx, n); } else