LCS: get rid of STOREIDX option. Fix get keys helper.

This commit is contained in:
antirez 2020-04-06 13:23:07 +02:00
parent 9682cd452f
commit e46275a1dc
2 changed files with 20 additions and 21 deletions

View File

@ -1554,30 +1554,30 @@ int *georadiusGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numk
return keys; return keys;
} }
/* LCS ... [STOREIDX <key>] ... */ /* LCS ... [KEYS <key1> <key2>] ... */
int *lcsGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys) int *lcsGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys)
{ {
int i; int i;
int *keys; int *keys = getKeysTempBuffer;
UNUSED(cmd); UNUSED(cmd);
/* We need to parse the options of the command in order to check for the /* We need to parse the options of the command in order to check for the
* "STOREIDX" argument before the STRINGS argument. */ * "KEYS" argument before the "STRINGS" argument. */
for (i = 1; i < argc; i++) { for (i = 1; i < argc; i++) {
char *arg = argv[i]->ptr; char *arg = argv[i]->ptr;
int moreargs = (argc-1) - i; int moreargs = (argc-1) - i;
if (!strcasecmp(arg, "strings")) { if (!strcasecmp(arg, "strings")) {
break; break;
} else if (!strcasecmp(arg, "storeidx") && moreargs) { } else if (!strcasecmp(arg, "keys") && moreargs >= 2) {
keys = getKeysTempBuffer;
keys[0] = i+1; keys[0] = i+1;
*numkeys = 1; keys[1] = i+2;
*numkeys = 2;
return keys; return keys;
} }
} }
*numkeys = 0; *numkeys = 0;
return NULL; return keys;
} }
/* Helper function to extract keys from memory command. /* Helper function to extract keys from memory command.

View File

@ -482,14 +482,13 @@ void strlenCommand(client *c) {
/* LCS -- Longest common subsequence. /* LCS -- Longest common subsequence.
* *
* LCS [IDX] [STOREIDX <key>] [MINMATCHLEN <len>] * LCS [IDX] [MINMATCHLEN <len>]
* STRINGS <string> <string> | KEYS <keya> <keyb> */ * STRINGS <string> <string> | KEYS <keya> <keyb> */
void lcsCommand(client *c) { void lcsCommand(client *c) {
uint32_t i, j; uint32_t i, j;
long long minmatchlen = 0; long long minmatchlen = 0;
sds a = NULL, b = NULL; sds a = NULL, b = NULL;
int getlen = 0, getidx = 0, withmatchlen = 0; int getlen = 0, getidx = 0, withmatchlen = 0;
robj *idxkey = NULL; /* STOREIDX will set this and getidx to 1. */
robj *obja = NULL, *objb = NULL; robj *obja = NULL, *objb = NULL;
for (j = 1; j < (uint32_t)c->argc; j++) { for (j = 1; j < (uint32_t)c->argc; j++) {
@ -502,17 +501,16 @@ void lcsCommand(client *c) {
getlen = 1; getlen = 1;
} else if (!strcasecmp(opt,"WITHMATCHLEN")) { } else if (!strcasecmp(opt,"WITHMATCHLEN")) {
withmatchlen = 1; withmatchlen = 1;
} else if (!strcasecmp(opt,"STOREIDX") && moreargs) {
getidx = 1;
idxkey = c->argv[j+1];
j++;
} else if (!strcasecmp(opt,"MINMATCHLEN") && moreargs) { } else if (!strcasecmp(opt,"MINMATCHLEN") && moreargs) {
if (getLongLongFromObjectOrReply(c,c->argv[j+1],&minmatchlen,NULL) if (getLongLongFromObjectOrReply(c,c->argv[j+1],&minmatchlen,NULL)
!= C_OK) return; != C_OK) return;
if (minmatchlen < 0) minmatchlen = 0; if (minmatchlen < 0) minmatchlen = 0;
j++; j++;
} else if (!strcasecmp(opt,"STRINGS")) { } else if (!strcasecmp(opt,"STRINGS")) {
if (moreargs != 2) { if (a != NULL) {
addReplyError(c,"Either use STRINGS or KEYS");
return;
} else if (moreargs != 2) {
addReplyError(c,"LCS requires exactly two strings"); addReplyError(c,"LCS requires exactly two strings");
return; return;
} }
@ -520,7 +518,10 @@ void lcsCommand(client *c) {
b = c->argv[j+2]->ptr; b = c->argv[j+2]->ptr;
j += 2; j += 2;
} else if (!strcasecmp(opt,"KEYS")) { } else if (!strcasecmp(opt,"KEYS")) {
if (moreargs != 2) { if (a != NULL) {
addReplyError(c,"Either use STRINGS or KEYS");
return;
} else if (moreargs != 2) {
addReplyError(c,"LCS requires exactly two keys"); addReplyError(c,"LCS requires exactly two keys");
return; return;
} }
@ -542,12 +543,10 @@ void lcsCommand(client *c) {
addReplyError(c,"Please specify two strings: " addReplyError(c,"Please specify two strings: "
"STRINGS or KEYS options are mandatory"); "STRINGS or KEYS options are mandatory");
return; return;
} else if (getlen && (getidx && idxkey == NULL)) { } else if (getlen && getidx) {
addReplyError(c, addReplyError(c,
"If you want both the LEN and indexes, please " "If you want both the length and indexes, please "
"store the indexes into a key with STOREIDX. However note " "just use IDX.");
"that the IDX output also includes both the LCS string and "
"its length");
return; return;
} }
@ -602,7 +601,7 @@ void lcsCommand(client *c) {
/* Start with a deferred array if we have to emit the ranges. */ /* Start with a deferred array if we have to emit the ranges. */
uint32_t arraylen = 0; /* Number of ranges emitted in the array. */ uint32_t arraylen = 0; /* Number of ranges emitted in the array. */
if (getidx && idxkey == NULL) { if (getidx) {
addReplyMapLen(c,2); addReplyMapLen(c,2);
addReplyBulkCString(c,"matches"); addReplyBulkCString(c,"matches");
arraylenptr = addReplyDeferredLen(c); arraylenptr = addReplyDeferredLen(c);