2012-11-08 18:25:23 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* * Neither the name of Redis nor the names of its contributors may be used
|
|
|
|
* to endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2015-07-26 15:14:57 +02:00
|
|
|
#include "server.h"
|
2019-07-11 20:20:01 -04:00
|
|
|
#include <cmath> /* isnan(), isinf() */
|
2010-06-22 00:07:48 +02:00
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------------
|
|
|
|
* String Commands
|
|
|
|
*----------------------------------------------------------------------------*/
|
|
|
|
|
2015-07-26 15:20:46 +02:00
|
|
|
static int checkStringLength(client *c, long long size) {
|
2020-09-30 19:47:55 +00:00
|
|
|
if (!(c->flags & CLIENT_MASTER) && size > g_pserver->proto_max_bulk_len) {
|
2018-10-30 00:38:20 +08:00
|
|
|
addReplyError(c,"string exceeds maximum allowed size (proto-max-bulk-len)");
|
2015-07-26 23:17:55 +02:00
|
|
|
return C_ERR;
|
2010-12-14 14:20:51 +01:00
|
|
|
}
|
2015-07-26 23:17:55 +02:00
|
|
|
return C_OK;
|
2010-12-14 14:20:51 +01:00
|
|
|
}
|
|
|
|
|
2013-03-28 15:40:19 +01:00
|
|
|
/* The setGenericCommand() function implements the SET operation with different
|
|
|
|
* options and variants. This function is called in order to implement the
|
|
|
|
* following commands: SET, SETEX, PSETEX, SETNX.
|
|
|
|
*
|
2019-12-18 14:49:38 +08:00
|
|
|
* 'flags' changes the behavior of the command (NX or XX, see below).
|
2013-03-28 15:40:19 +01:00
|
|
|
*
|
|
|
|
* 'expire' represents an expire to set in form of a Redis object as passed
|
|
|
|
* by the user. It is interpreted according to the specified 'unit'.
|
|
|
|
*
|
|
|
|
* 'ok_reply' and 'abort_reply' is what the function will reply to the client
|
|
|
|
* if the operation is performed, or when it is not because of NX or
|
|
|
|
* XX flags.
|
|
|
|
*
|
|
|
|
* If ok_reply is NULL "+OK" is used.
|
|
|
|
* If abort_reply is NULL, "$-1" is used. */
|
|
|
|
|
2015-07-26 15:28:00 +02:00
|
|
|
#define OBJ_SET_NO_FLAGS 0
|
2019-12-18 14:49:38 +08:00
|
|
|
#define OBJ_SET_NX (1<<0) /* Set if key not exists. */
|
|
|
|
#define OBJ_SET_XX (1<<1) /* Set if key exists. */
|
|
|
|
#define OBJ_SET_EX (1<<2) /* Set if time in seconds is given */
|
|
|
|
#define OBJ_SET_PX (1<<3) /* Set if time in ms in given */
|
|
|
|
#define OBJ_SET_KEEPTTL (1<<4) /* Set and keep the ttl */
|
2013-03-28 15:40:19 +01:00
|
|
|
|
2015-07-26 15:20:46 +02:00
|
|
|
void setGenericCommand(client *c, int flags, robj *key, robj *val, robj *expire, int unit, robj *ok_reply, robj *abort_reply) {
|
2013-01-17 01:00:20 +08:00
|
|
|
long long milliseconds = 0; /* initialized to avoid any harmness warning */
|
2010-06-22 00:07:48 +02:00
|
|
|
|
|
|
|
if (expire) {
|
2015-07-26 23:17:55 +02:00
|
|
|
if (getLongLongFromObjectOrReply(c, expire, &milliseconds, NULL) != C_OK)
|
2010-06-22 00:07:48 +02:00
|
|
|
return;
|
2011-11-10 17:52:02 +01:00
|
|
|
if (milliseconds <= 0) {
|
2014-08-18 11:15:50 +02:00
|
|
|
addReplyErrorFormat(c,"invalid expire time in %s",c->cmd->name);
|
2010-06-22 00:07:48 +02:00
|
|
|
return;
|
|
|
|
}
|
2011-11-10 17:52:02 +01:00
|
|
|
if (unit == UNIT_SECONDS) milliseconds *= 1000;
|
2010-06-22 00:07:48 +02:00
|
|
|
}
|
|
|
|
|
2015-07-26 15:28:00 +02:00
|
|
|
if ((flags & OBJ_SET_NX && lookupKeyWrite(c->db,key) != NULL) ||
|
|
|
|
(flags & OBJ_SET_XX && lookupKeyWrite(c->db,key) == NULL))
|
2013-03-28 15:40:19 +01:00
|
|
|
{
|
2018-11-30 09:41:54 +01:00
|
|
|
addReply(c, abort_reply ? abort_reply : shared.null[c->resp]);
|
2011-06-14 15:34:27 +02:00
|
|
|
return;
|
2010-06-22 00:07:48 +02:00
|
|
|
}
|
2020-04-21 10:51:46 +02:00
|
|
|
genericSetKey(c,c->db,key,val,flags & OBJ_SET_KEEPTTL,1);
|
2019-04-21 14:01:10 -04:00
|
|
|
g_pserver->dirty++;
|
2019-07-13 20:11:49 -04:00
|
|
|
if (expire) setExpire(c,c->db,key,nullptr,mstime()+milliseconds);
|
2015-07-27 09:41:48 +02:00
|
|
|
notifyKeyspaceEvent(NOTIFY_STRING,"set",key,c->db->id);
|
|
|
|
if (expire) notifyKeyspaceEvent(NOTIFY_GENERIC,
|
2013-01-25 13:19:08 +01:00
|
|
|
"expire",key,c->db->id);
|
2013-03-28 15:40:19 +01:00
|
|
|
addReply(c, ok_reply ? ok_reply : shared.ok);
|
2010-06-22 00:07:48 +02:00
|
|
|
}
|
|
|
|
|
2019-12-18 14:49:38 +08:00
|
|
|
/* SET key value [NX] [XX] [KEEPTTL] [EX <seconds>] [PX <milliseconds>] */
|
2015-07-26 15:20:46 +02:00
|
|
|
void setCommand(client *c) {
|
2013-03-28 15:40:19 +01:00
|
|
|
int j;
|
|
|
|
robj *expire = NULL;
|
|
|
|
int unit = UNIT_SECONDS;
|
2015-07-26 15:28:00 +02:00
|
|
|
int flags = OBJ_SET_NO_FLAGS;
|
2013-03-28 15:40:19 +01:00
|
|
|
|
|
|
|
for (j = 3; j < c->argc; j++) {
|
2019-04-07 15:24:23 -04:00
|
|
|
const char *a = (const char*)ptrFromObj(c->argv[j]);
|
2013-03-28 15:40:19 +01:00
|
|
|
robj *next = (j == c->argc-1) ? NULL : c->argv[j+1];
|
|
|
|
|
2013-03-28 14:41:46 -07:00
|
|
|
if ((a[0] == 'n' || a[0] == 'N') &&
|
2014-12-14 10:12:58 -05:00
|
|
|
(a[1] == 'x' || a[1] == 'X') && a[2] == '\0' &&
|
2015-07-26 15:28:00 +02:00
|
|
|
!(flags & OBJ_SET_XX))
|
2015-02-03 14:17:06 +01:00
|
|
|
{
|
2015-07-26 15:28:00 +02:00
|
|
|
flags |= OBJ_SET_NX;
|
2013-03-28 14:41:46 -07:00
|
|
|
} else if ((a[0] == 'x' || a[0] == 'X') &&
|
2014-12-14 10:12:58 -05:00
|
|
|
(a[1] == 'x' || a[1] == 'X') && a[2] == '\0' &&
|
2015-07-26 15:28:00 +02:00
|
|
|
!(flags & OBJ_SET_NX))
|
2015-02-03 14:17:06 +01:00
|
|
|
{
|
2015-07-26 15:28:00 +02:00
|
|
|
flags |= OBJ_SET_XX;
|
2020-01-27 02:55:48 -05:00
|
|
|
} else if (!strcasecmp(szFromObj(c->argv[j]),"KEEPTTL") &&
|
2019-12-18 14:49:38 +08:00
|
|
|
!(flags & OBJ_SET_EX) && !(flags & OBJ_SET_PX))
|
|
|
|
{
|
|
|
|
flags |= OBJ_SET_KEEPTTL;
|
2013-03-28 14:41:46 -07:00
|
|
|
} else if ((a[0] == 'e' || a[0] == 'E') &&
|
2014-12-14 10:12:58 -05:00
|
|
|
(a[1] == 'x' || a[1] == 'X') && a[2] == '\0' &&
|
2019-12-18 14:49:38 +08:00
|
|
|
!(flags & OBJ_SET_KEEPTTL) &&
|
2015-07-26 15:28:00 +02:00
|
|
|
!(flags & OBJ_SET_PX) && next)
|
2015-02-03 14:17:06 +01:00
|
|
|
{
|
2015-07-26 15:28:00 +02:00
|
|
|
flags |= OBJ_SET_EX;
|
2013-03-28 15:40:19 +01:00
|
|
|
unit = UNIT_SECONDS;
|
|
|
|
expire = next;
|
|
|
|
j++;
|
2013-03-28 14:41:46 -07:00
|
|
|
} else if ((a[0] == 'p' || a[0] == 'P') &&
|
2014-12-14 10:12:58 -05:00
|
|
|
(a[1] == 'x' || a[1] == 'X') && a[2] == '\0' &&
|
2019-12-18 14:49:38 +08:00
|
|
|
!(flags & OBJ_SET_KEEPTTL) &&
|
2015-07-26 15:28:00 +02:00
|
|
|
!(flags & OBJ_SET_EX) && next)
|
2015-02-03 14:17:06 +01:00
|
|
|
{
|
2015-07-26 15:28:00 +02:00
|
|
|
flags |= OBJ_SET_PX;
|
2013-03-28 15:40:19 +01:00
|
|
|
unit = UNIT_MILLISECONDS;
|
|
|
|
expire = next;
|
|
|
|
j++;
|
|
|
|
} else {
|
|
|
|
addReply(c,shared.syntaxerr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-17 17:21:41 +02:00
|
|
|
c->argv[2] = tryObjectEncoding(c->argv[2]);
|
2013-03-28 15:40:19 +01:00
|
|
|
setGenericCommand(c,flags,c->argv[1],c->argv[2],expire,unit,NULL,NULL);
|
2010-06-22 00:07:48 +02:00
|
|
|
}
|
|
|
|
|
2015-07-26 15:20:46 +02:00
|
|
|
void setnxCommand(client *c) {
|
2010-10-17 17:21:41 +02:00
|
|
|
c->argv[2] = tryObjectEncoding(c->argv[2]);
|
2015-07-26 15:28:00 +02:00
|
|
|
setGenericCommand(c,OBJ_SET_NX,c->argv[1],c->argv[2],NULL,0,shared.cone,shared.czero);
|
2010-06-22 00:07:48 +02:00
|
|
|
}
|
|
|
|
|
2015-07-26 15:20:46 +02:00
|
|
|
void setexCommand(client *c) {
|
2010-10-17 17:21:41 +02:00
|
|
|
c->argv[3] = tryObjectEncoding(c->argv[3]);
|
2015-07-26 15:28:00 +02:00
|
|
|
setGenericCommand(c,OBJ_SET_NO_FLAGS,c->argv[1],c->argv[3],c->argv[2],UNIT_SECONDS,NULL,NULL);
|
2011-11-10 17:52:02 +01:00
|
|
|
}
|
|
|
|
|
2015-07-26 15:20:46 +02:00
|
|
|
void psetexCommand(client *c) {
|
2011-11-10 17:52:02 +01:00
|
|
|
c->argv[3] = tryObjectEncoding(c->argv[3]);
|
2015-07-26 15:28:00 +02:00
|
|
|
setGenericCommand(c,OBJ_SET_NO_FLAGS,c->argv[1],c->argv[3],c->argv[2],UNIT_MILLISECONDS,NULL,NULL);
|
2010-06-22 00:07:48 +02:00
|
|
|
}
|
|
|
|
|
2015-07-26 15:20:46 +02:00
|
|
|
int getGenericCommand(client *c) {
|
2019-04-16 23:16:03 -04:00
|
|
|
robj_roptr o;
|
2010-06-22 00:07:48 +02:00
|
|
|
|
2019-04-16 23:16:03 -04:00
|
|
|
if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.null[c->resp])) == nullptr)
|
2015-07-26 23:17:55 +02:00
|
|
|
return C_OK;
|
2010-06-22 00:07:48 +02:00
|
|
|
|
2015-07-26 15:28:00 +02:00
|
|
|
if (o->type != OBJ_STRING) {
|
2010-06-22 00:07:48 +02:00
|
|
|
addReply(c,shared.wrongtypeerr);
|
2015-07-26 23:17:55 +02:00
|
|
|
return C_ERR;
|
2010-06-22 00:07:48 +02:00
|
|
|
} else {
|
|
|
|
addReplyBulk(c,o);
|
2015-07-26 23:17:55 +02:00
|
|
|
return C_OK;
|
2010-06-22 00:07:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-26 15:20:46 +02:00
|
|
|
void getCommand(client *c) {
|
2010-06-22 00:07:48 +02:00
|
|
|
getGenericCommand(c);
|
|
|
|
}
|
|
|
|
|
2015-07-26 15:20:46 +02:00
|
|
|
void getsetCommand(client *c) {
|
2015-07-26 23:17:55 +02:00
|
|
|
if (getGenericCommand(c) == C_ERR) return;
|
2010-10-17 17:21:41 +02:00
|
|
|
c->argv[2] = tryObjectEncoding(c->argv[2]);
|
2020-04-21 10:51:46 +02:00
|
|
|
setKey(c,c->db,c->argv[1],c->argv[2]);
|
2015-07-27 09:41:48 +02:00
|
|
|
notifyKeyspaceEvent(NOTIFY_STRING,"set",c->argv[1],c->db->id);
|
2019-04-21 14:01:10 -04:00
|
|
|
g_pserver->dirty++;
|
2010-12-09 16:39:33 +01:00
|
|
|
}
|
|
|
|
|
2015-07-26 15:20:46 +02:00
|
|
|
void setrangeCommand(client *c) {
|
2010-12-14 14:20:51 +01:00
|
|
|
robj *o;
|
|
|
|
long offset;
|
2019-04-07 15:24:23 -04:00
|
|
|
sds value = (sds)ptrFromObj(c->argv[3]);
|
2010-12-14 14:20:51 +01:00
|
|
|
|
2015-07-26 23:17:55 +02:00
|
|
|
if (getLongFromObjectOrReply(c,c->argv[2],&offset,NULL) != C_OK)
|
2010-12-14 14:20:51 +01:00
|
|
|
return;
|
|
|
|
|
2010-12-15 11:30:50 +01:00
|
|
|
if (offset < 0) {
|
|
|
|
addReplyError(c,"offset is out of range");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-12-14 14:20:51 +01:00
|
|
|
o = lookupKeyWrite(c->db,c->argv[1]);
|
|
|
|
if (o == NULL) {
|
|
|
|
/* Return 0 when setting nothing on a non-existing string */
|
|
|
|
if (sdslen(value) == 0) {
|
|
|
|
addReply(c,shared.czero);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return when the resulting string exceeds allowed size */
|
2015-07-26 23:17:55 +02:00
|
|
|
if (checkStringLength(c,offset+sdslen(value)) != C_OK)
|
2010-12-14 14:20:51 +01:00
|
|
|
return;
|
|
|
|
|
2015-07-26 15:28:00 +02:00
|
|
|
o = createObject(OBJ_STRING,sdsnewlen(NULL, offset+sdslen(value)));
|
2010-12-14 14:20:51 +01:00
|
|
|
dbAdd(c->db,c->argv[1],o);
|
|
|
|
} else {
|
2010-12-15 11:49:04 +01:00
|
|
|
size_t olen;
|
2010-12-14 14:20:51 +01:00
|
|
|
|
|
|
|
/* Key exists, check type */
|
2015-07-26 15:28:00 +02:00
|
|
|
if (checkType(c,o,OBJ_STRING))
|
2010-12-14 14:20:51 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* Return existing string length when setting nothing */
|
2010-12-15 11:49:04 +01:00
|
|
|
olen = stringObjectLen(o);
|
2010-12-14 14:20:51 +01:00
|
|
|
if (sdslen(value) == 0) {
|
|
|
|
addReplyLongLong(c,olen);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return when the resulting string exceeds allowed size */
|
2015-07-26 23:17:55 +02:00
|
|
|
if (checkStringLength(c,offset+sdslen(value)) != C_OK)
|
2010-12-14 14:20:51 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* Create a copy when the object is shared or encoded. */
|
2014-03-30 18:32:17 +02:00
|
|
|
o = dbUnshareStringValue(c->db,c->argv[1],o);
|
2010-12-14 14:20:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sdslen(value) > 0) {
|
2019-04-07 15:24:23 -04:00
|
|
|
o->m_ptr = sdsgrowzero((sds)ptrFromObj(o),offset+sdslen(value));
|
2019-02-09 13:04:18 -05:00
|
|
|
memcpy((char*)ptrFromObj(o)+offset,value,sdslen(value));
|
2020-04-21 10:51:46 +02:00
|
|
|
signalModifiedKey(c,c->db,c->argv[1]);
|
2015-07-27 09:41:48 +02:00
|
|
|
notifyKeyspaceEvent(NOTIFY_STRING,
|
2013-01-25 13:19:08 +01:00
|
|
|
"setrange",c->argv[1],c->db->id);
|
2019-04-21 14:01:10 -04:00
|
|
|
g_pserver->dirty++;
|
2010-12-14 14:20:51 +01:00
|
|
|
}
|
2019-04-07 15:24:23 -04:00
|
|
|
addReplyLongLong(c,sdslen((sds)ptrFromObj(o)));
|
2010-12-14 14:20:51 +01:00
|
|
|
}
|
|
|
|
|
2015-07-26 15:20:46 +02:00
|
|
|
void getrangeCommand(client *c) {
|
2019-04-16 23:16:03 -04:00
|
|
|
robj_roptr o;
|
2014-09-02 17:56:17 -04:00
|
|
|
long long start, end;
|
2019-04-16 23:16:03 -04:00
|
|
|
const char *str;
|
|
|
|
char llbuf[32];
|
2010-12-14 15:10:58 +01:00
|
|
|
size_t strlen;
|
|
|
|
|
2015-07-26 23:17:55 +02:00
|
|
|
if (getLongLongFromObjectOrReply(c,c->argv[2],&start,NULL) != C_OK)
|
2010-12-14 15:10:58 +01:00
|
|
|
return;
|
2015-07-26 23:17:55 +02:00
|
|
|
if (getLongLongFromObjectOrReply(c,c->argv[3],&end,NULL) != C_OK)
|
2010-12-14 15:10:58 +01:00
|
|
|
return;
|
2019-04-16 23:16:03 -04:00
|
|
|
if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.emptybulk)) == nullptr ||
|
2015-07-26 15:28:00 +02:00
|
|
|
checkType(c,o,OBJ_STRING)) return;
|
2010-12-14 15:10:58 +01:00
|
|
|
|
2015-07-26 15:28:00 +02:00
|
|
|
if (o->encoding == OBJ_ENCODING_INT) {
|
2010-12-14 15:10:58 +01:00
|
|
|
str = llbuf;
|
2019-02-09 13:04:18 -05:00
|
|
|
strlen = ll2string(llbuf,sizeof(llbuf),(long)ptrFromObj(o));
|
2010-12-14 15:10:58 +01:00
|
|
|
} else {
|
2019-04-16 23:16:03 -04:00
|
|
|
str = szFromObj(o);
|
2010-12-14 15:10:58 +01:00
|
|
|
strlen = sdslen(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert negative indexes */
|
2016-06-15 12:48:58 +02:00
|
|
|
if (start < 0 && end < 0 && start > end) {
|
|
|
|
addReply(c,shared.emptybulk);
|
|
|
|
return;
|
|
|
|
}
|
2010-12-14 15:10:58 +01:00
|
|
|
if (start < 0) start = strlen+start;
|
|
|
|
if (end < 0) end = strlen+end;
|
|
|
|
if (start < 0) start = 0;
|
|
|
|
if (end < 0) end = 0;
|
2014-09-02 17:56:17 -04:00
|
|
|
if ((unsigned long long)end >= strlen) end = strlen-1;
|
2010-12-14 15:10:58 +01:00
|
|
|
|
|
|
|
/* Precondition: end >= 0 && end < strlen, so the only condition where
|
|
|
|
* nothing can be returned is: start > end. */
|
2014-09-02 18:56:28 -04:00
|
|
|
if (start > end || strlen == 0) {
|
2011-03-04 16:22:50 +01:00
|
|
|
addReply(c,shared.emptybulk);
|
2010-12-14 15:10:58 +01:00
|
|
|
} else {
|
|
|
|
addReplyBulkCBuffer(c,(char*)str+start,end-start+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-26 15:20:46 +02:00
|
|
|
void mgetCommand(client *c) {
|
2010-06-22 00:07:48 +02:00
|
|
|
int j;
|
|
|
|
|
2018-11-08 13:28:04 +01:00
|
|
|
addReplyArrayLen(c,c->argc-1);
|
2010-06-22 00:07:48 +02:00
|
|
|
for (j = 1; j < c->argc; j++) {
|
2019-04-16 23:16:03 -04:00
|
|
|
robj_roptr o = lookupKeyRead(c->db,c->argv[j]);
|
|
|
|
if (o == nullptr) {
|
2018-11-30 09:41:54 +01:00
|
|
|
addReplyNull(c);
|
2010-06-22 00:07:48 +02:00
|
|
|
} else {
|
2015-07-26 15:28:00 +02:00
|
|
|
if (o->type != OBJ_STRING) {
|
2018-11-30 09:41:54 +01:00
|
|
|
addReplyNull(c);
|
2010-06-22 00:07:48 +02:00
|
|
|
} else {
|
|
|
|
addReplyBulk(c,o);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-26 15:20:46 +02:00
|
|
|
void msetGenericCommand(client *c, int nx) {
|
2018-10-22 12:24:02 +02:00
|
|
|
int j;
|
2010-06-22 00:07:48 +02:00
|
|
|
|
|
|
|
if ((c->argc % 2) == 0) {
|
2010-09-02 19:52:24 +02:00
|
|
|
addReplyError(c,"wrong number of arguments for MSET");
|
2010-06-22 00:07:48 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-10-22 12:24:02 +02:00
|
|
|
|
2010-06-22 00:07:48 +02:00
|
|
|
/* Handle the NX flag. The MSETNX semantic is to return zero and don't
|
2018-10-22 12:24:02 +02:00
|
|
|
* set anything if at least one key alerady exists. */
|
2010-06-22 00:07:48 +02:00
|
|
|
if (nx) {
|
|
|
|
for (j = 1; j < c->argc; j += 2) {
|
|
|
|
if (lookupKeyWrite(c->db,c->argv[j]) != NULL) {
|
2018-10-22 12:24:02 +02:00
|
|
|
addReply(c, shared.czero);
|
|
|
|
return;
|
2010-06-22 00:07:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 1; j < c->argc; j += 2) {
|
|
|
|
c->argv[j+1] = tryObjectEncoding(c->argv[j+1]);
|
2020-04-21 10:51:46 +02:00
|
|
|
setKey(c,c->db,c->argv[j],c->argv[j+1]);
|
2015-07-27 09:41:48 +02:00
|
|
|
notifyKeyspaceEvent(NOTIFY_STRING,"set",c->argv[j],c->db->id);
|
2010-06-22 00:07:48 +02:00
|
|
|
}
|
2019-04-21 14:01:10 -04:00
|
|
|
g_pserver->dirty += (c->argc-1)/2;
|
2010-06-22 00:07:48 +02:00
|
|
|
addReply(c, nx ? shared.cone : shared.ok);
|
|
|
|
}
|
|
|
|
|
2015-07-26 15:20:46 +02:00
|
|
|
void msetCommand(client *c) {
|
2010-06-22 00:07:48 +02:00
|
|
|
msetGenericCommand(c,0);
|
|
|
|
}
|
|
|
|
|
2015-07-26 15:20:46 +02:00
|
|
|
void msetnxCommand(client *c) {
|
2010-06-22 00:07:48 +02:00
|
|
|
msetGenericCommand(c,1);
|
|
|
|
}
|
|
|
|
|
2015-07-26 15:20:46 +02:00
|
|
|
void incrDecrCommand(client *c, long long incr) {
|
2010-12-19 12:22:12 +01:00
|
|
|
long long value, oldvalue;
|
2019-04-07 15:24:23 -04:00
|
|
|
robj *o, *newObj;
|
2010-06-22 00:07:48 +02:00
|
|
|
|
|
|
|
o = lookupKeyWrite(c->db,c->argv[1]);
|
2015-07-26 15:28:00 +02:00
|
|
|
if (o != NULL && checkType(c,o,OBJ_STRING)) return;
|
2015-07-26 23:17:55 +02:00
|
|
|
if (getLongLongFromObjectOrReply(c,o,&value,NULL) != C_OK) return;
|
2010-06-22 00:07:48 +02:00
|
|
|
|
2010-12-19 12:22:12 +01:00
|
|
|
oldvalue = value;
|
2012-02-21 18:25:49 +01:00
|
|
|
if ((incr < 0 && oldvalue < 0 && incr < (LLONG_MIN-oldvalue)) ||
|
|
|
|
(incr > 0 && oldvalue > 0 && incr > (LLONG_MAX-oldvalue))) {
|
2010-12-19 12:22:12 +01:00
|
|
|
addReplyError(c,"increment or decrement would overflow");
|
|
|
|
return;
|
|
|
|
}
|
2012-02-21 18:25:49 +01:00
|
|
|
value += incr;
|
2014-10-03 11:53:57 +01:00
|
|
|
|
2019-07-05 23:49:09 -04:00
|
|
|
if (o && o->getrefcount(std::memory_order_relaxed) == 1 && o->encoding == OBJ_ENCODING_INT &&
|
2015-07-27 09:41:48 +02:00
|
|
|
(value < 0 || value >= OBJ_SHARED_INTEGERS) &&
|
2014-10-03 11:53:57 +01:00
|
|
|
value >= LONG_MIN && value <= LONG_MAX)
|
|
|
|
{
|
2019-04-07 15:24:23 -04:00
|
|
|
newObj = o;
|
2019-02-09 13:04:18 -05:00
|
|
|
o->m_ptr = (void*)((long)value);
|
2014-10-03 11:53:57 +01:00
|
|
|
} else {
|
2019-04-07 15:24:23 -04:00
|
|
|
newObj = createStringObjectFromLongLongForValue(value);
|
2014-10-03 11:53:57 +01:00
|
|
|
if (o) {
|
2019-04-07 15:24:23 -04:00
|
|
|
dbOverwrite(c->db,c->argv[1],newObj);
|
2014-10-03 11:53:57 +01:00
|
|
|
} else {
|
2019-04-07 15:24:23 -04:00
|
|
|
dbAdd(c->db,c->argv[1],newObj);
|
2014-10-03 11:53:57 +01:00
|
|
|
}
|
|
|
|
}
|
2020-04-21 10:51:46 +02:00
|
|
|
signalModifiedKey(c,c->db,c->argv[1]);
|
2015-07-27 09:41:48 +02:00
|
|
|
notifyKeyspaceEvent(NOTIFY_STRING,"incrby",c->argv[1],c->db->id);
|
2019-04-21 14:01:10 -04:00
|
|
|
g_pserver->dirty++;
|
2010-06-22 00:07:48 +02:00
|
|
|
addReply(c,shared.colon);
|
2019-04-07 15:24:23 -04:00
|
|
|
addReply(c,newObj);
|
2010-06-22 00:07:48 +02:00
|
|
|
addReply(c,shared.crlf);
|
|
|
|
}
|
|
|
|
|
2015-07-26 15:20:46 +02:00
|
|
|
void incrCommand(client *c) {
|
2010-06-22 00:07:48 +02:00
|
|
|
incrDecrCommand(c,1);
|
|
|
|
}
|
|
|
|
|
2015-07-26 15:20:46 +02:00
|
|
|
void decrCommand(client *c) {
|
2010-06-22 00:07:48 +02:00
|
|
|
incrDecrCommand(c,-1);
|
|
|
|
}
|
|
|
|
|
2015-07-26 15:20:46 +02:00
|
|
|
void incrbyCommand(client *c) {
|
2010-06-22 00:07:48 +02:00
|
|
|
long long incr;
|
|
|
|
|
2015-07-26 23:17:55 +02:00
|
|
|
if (getLongLongFromObjectOrReply(c, c->argv[2], &incr, NULL) != C_OK) return;
|
2010-06-22 00:07:48 +02:00
|
|
|
incrDecrCommand(c,incr);
|
|
|
|
}
|
|
|
|
|
2015-07-26 15:20:46 +02:00
|
|
|
void decrbyCommand(client *c) {
|
2010-06-22 00:07:48 +02:00
|
|
|
long long incr;
|
|
|
|
|
2015-07-26 23:17:55 +02:00
|
|
|
if (getLongLongFromObjectOrReply(c, c->argv[2], &incr, NULL) != C_OK) return;
|
2010-06-22 00:07:48 +02:00
|
|
|
incrDecrCommand(c,-incr);
|
|
|
|
}
|
|
|
|
|
2015-07-26 15:20:46 +02:00
|
|
|
void incrbyfloatCommand(client *c) {
|
2011-11-12 19:27:35 +01:00
|
|
|
long double incr, value;
|
2020-01-27 02:55:48 -05:00
|
|
|
robj *o, *newObj, *aux1, *aux2;
|
2011-11-12 19:27:35 +01:00
|
|
|
|
|
|
|
o = lookupKeyWrite(c->db,c->argv[1]);
|
2015-07-26 15:28:00 +02:00
|
|
|
if (o != NULL && checkType(c,o,OBJ_STRING)) return;
|
2015-07-26 23:17:55 +02:00
|
|
|
if (getLongDoubleFromObjectOrReply(c,o,&value,NULL) != C_OK ||
|
|
|
|
getLongDoubleFromObjectOrReply(c,c->argv[2],&incr,NULL) != C_OK)
|
2011-11-12 19:27:35 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
value += incr;
|
2019-07-11 20:20:01 -04:00
|
|
|
if (std::isnan(value) || std::isinf(value)) {
|
2011-11-12 19:27:35 +01:00
|
|
|
addReplyError(c,"increment would produce NaN or Infinity");
|
|
|
|
return;
|
|
|
|
}
|
2019-04-07 15:24:23 -04:00
|
|
|
newObj = createStringObjectFromLongDouble(value,1);
|
2011-11-12 19:27:35 +01:00
|
|
|
if (o)
|
2019-04-07 15:24:23 -04:00
|
|
|
dbOverwrite(c->db,c->argv[1],newObj);
|
2011-11-12 19:27:35 +01:00
|
|
|
else
|
2019-04-07 15:24:23 -04:00
|
|
|
dbAdd(c->db,c->argv[1],newObj);
|
2020-04-21 10:51:46 +02:00
|
|
|
signalModifiedKey(c,c->db,c->argv[1]);
|
2015-07-27 09:41:48 +02:00
|
|
|
notifyKeyspaceEvent(NOTIFY_STRING,"incrbyfloat",c->argv[1],c->db->id);
|
2019-04-21 14:01:10 -04:00
|
|
|
g_pserver->dirty++;
|
2019-04-07 15:24:23 -04:00
|
|
|
addReplyBulk(c,newObj);
|
2011-11-14 10:15:13 +01:00
|
|
|
|
|
|
|
/* Always replicate INCRBYFLOAT as a SET command with the final value
|
2013-01-19 13:46:14 +01:00
|
|
|
* in order to make sure that differences in float precision or formatting
|
2011-11-14 10:15:13 +01:00
|
|
|
* will not create differences in replicas or after an AOF restart. */
|
2019-12-18 15:44:51 +08:00
|
|
|
aux1 = createStringObject("SET",3);
|
|
|
|
rewriteClientCommandArgument(c,0,aux1);
|
|
|
|
decrRefCount(aux1);
|
2019-04-07 15:24:23 -04:00
|
|
|
rewriteClientCommandArgument(c,2,newObj);
|
2019-12-18 15:44:51 +08:00
|
|
|
aux2 = createStringObject("KEEPTTL",7);
|
|
|
|
rewriteClientCommandArgument(c,3,aux2);
|
|
|
|
decrRefCount(aux2);
|
2011-11-12 19:27:35 +01:00
|
|
|
}
|
|
|
|
|
2015-07-26 15:20:46 +02:00
|
|
|
void appendCommand(client *c) {
|
2010-06-22 00:07:48 +02:00
|
|
|
size_t totlen;
|
2010-12-09 17:16:10 +01:00
|
|
|
robj *o, *append;
|
2010-06-22 00:07:48 +02:00
|
|
|
|
|
|
|
o = lookupKeyWrite(c->db,c->argv[1]);
|
|
|
|
if (o == NULL) {
|
|
|
|
/* Create the key */
|
2010-12-15 11:40:36 +01:00
|
|
|
c->argv[2] = tryObjectEncoding(c->argv[2]);
|
|
|
|
dbAdd(c->db,c->argv[1],c->argv[2]);
|
2010-06-22 00:07:48 +02:00
|
|
|
incrRefCount(c->argv[2]);
|
|
|
|
totlen = stringObjectLen(c->argv[2]);
|
|
|
|
} else {
|
2010-12-15 11:40:36 +01:00
|
|
|
/* Key exists, check type */
|
2015-07-26 15:28:00 +02:00
|
|
|
if (checkType(c,o,OBJ_STRING))
|
2010-06-22 00:07:48 +02:00
|
|
|
return;
|
2010-12-09 17:16:10 +01:00
|
|
|
|
2010-12-15 11:40:36 +01:00
|
|
|
/* "append" is an argument, so always an sds */
|
|
|
|
append = c->argv[2];
|
2019-04-07 15:24:23 -04:00
|
|
|
totlen = stringObjectLen(o)+sdslen((sds)ptrFromObj(append));
|
2015-07-26 23:17:55 +02:00
|
|
|
if (checkStringLength(c,totlen) != C_OK)
|
2010-12-09 17:16:10 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* Append the value */
|
2014-03-30 18:32:17 +02:00
|
|
|
o = dbUnshareStringValue(c->db,c->argv[1],o);
|
2019-04-07 15:24:23 -04:00
|
|
|
o->m_ptr = sdscatlen((sds)ptrFromObj(o),ptrFromObj(append),sdslen((sds)ptrFromObj(append)));
|
|
|
|
totlen = sdslen((sds)ptrFromObj(o));
|
2010-06-22 00:07:48 +02:00
|
|
|
}
|
2020-04-21 10:51:46 +02:00
|
|
|
signalModifiedKey(c,c->db,c->argv[1]);
|
2015-07-27 09:41:48 +02:00
|
|
|
notifyKeyspaceEvent(NOTIFY_STRING,"append",c->argv[1],c->db->id);
|
2019-04-21 14:01:10 -04:00
|
|
|
g_pserver->dirty++;
|
2010-09-02 14:30:56 +02:00
|
|
|
addReplyLongLong(c,totlen);
|
2010-06-22 00:07:48 +02:00
|
|
|
}
|
|
|
|
|
2015-07-26 15:20:46 +02:00
|
|
|
void strlenCommand(client *c) {
|
2019-04-16 23:16:03 -04:00
|
|
|
robj_roptr o;
|
|
|
|
if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == nullptr ||
|
2015-07-26 15:28:00 +02:00
|
|
|
checkType(c,o,OBJ_STRING)) return;
|
2010-12-15 11:49:04 +01:00
|
|
|
addReplyLongLong(c,stringObjectLen(o));
|
2010-07-27 10:09:26 +02:00
|
|
|
}
|
2020-04-01 16:10:18 +02:00
|
|
|
|
2020-04-24 16:49:27 +02:00
|
|
|
|
|
|
|
/* STRALGO -- Implement complex algorithms on strings.
|
2020-04-01 16:10:18 +02:00
|
|
|
*
|
2020-04-24 16:49:27 +02:00
|
|
|
* STRALGO <algorithm> ... arguments ... */
|
|
|
|
void stralgoLCS(client *c); /* This implements the LCS algorithm. */
|
|
|
|
void stralgoCommand(client *c) {
|
|
|
|
/* Select the algorithm. */
|
2020-05-21 22:09:06 -04:00
|
|
|
if (!strcasecmp(szFromObj(c->argv[1]),"lcs")) {
|
2020-04-24 16:49:27 +02:00
|
|
|
stralgoLCS(c);
|
|
|
|
} else {
|
|
|
|
addReply(c,shared.syntaxerr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* STRALGO <algo> [IDX] [MINMATCHLEN <len>] [WITHMATCHLEN]
|
|
|
|
* STRINGS <string> <string> | KEYS <keya> <keyb>
|
|
|
|
*/
|
|
|
|
void stralgoLCS(client *c) {
|
2020-04-01 16:10:18 +02:00
|
|
|
uint32_t i, j;
|
2020-04-02 13:37:35 +02:00
|
|
|
long long minmatchlen = 0;
|
2020-05-21 17:55:09 -04:00
|
|
|
const char *a = NULL;
|
|
|
|
const char *b = NULL;
|
2020-04-02 13:37:35 +02:00
|
|
|
int getlen = 0, getidx = 0, withmatchlen = 0;
|
2020-05-21 17:55:09 -04:00
|
|
|
robj_roptr obja, objb;
|
2020-09-30 19:47:55 +00:00
|
|
|
uint32_t arraylen = 0; /* Number of ranges emitted in the array. */
|
|
|
|
uint32_t alen, blen, *lcs, idx;
|
|
|
|
int computelcs;
|
|
|
|
sds result = NULL; /* Resulting LCS string. */
|
|
|
|
void *arraylenptr = NULL; /* Deffered length of the array for IDX. */
|
|
|
|
uint32_t arange_start, arange_end, brange_start = 0, brange_end = 0;
|
2020-04-01 16:10:18 +02:00
|
|
|
|
2020-04-24 16:49:27 +02:00
|
|
|
for (j = 2; j < (uint32_t)c->argc; j++) {
|
2020-05-21 17:55:09 -04:00
|
|
|
char *opt = szFromObj(c->argv[j]);
|
2020-04-01 16:10:18 +02:00
|
|
|
int moreargs = (c->argc-1) - j;
|
|
|
|
|
|
|
|
if (!strcasecmp(opt,"IDX")) {
|
|
|
|
getidx = 1;
|
2020-04-01 17:11:31 +02:00
|
|
|
} else if (!strcasecmp(opt,"LEN")) {
|
|
|
|
getlen = 1;
|
2020-04-02 13:37:35 +02:00
|
|
|
} else if (!strcasecmp(opt,"WITHMATCHLEN")) {
|
|
|
|
withmatchlen = 1;
|
|
|
|
} else if (!strcasecmp(opt,"MINMATCHLEN") && moreargs) {
|
|
|
|
if (getLongLongFromObjectOrReply(c,c->argv[j+1],&minmatchlen,NULL)
|
2020-06-12 12:34:44 +02:00
|
|
|
!= C_OK) goto cleanup;
|
2020-04-02 13:37:35 +02:00
|
|
|
if (minmatchlen < 0) minmatchlen = 0;
|
|
|
|
j++;
|
2020-05-21 18:13:35 -04:00
|
|
|
} else if (!strcasecmp(opt,"STRINGS") && moreargs > 1) {
|
2020-04-06 13:23:07 +02:00
|
|
|
if (a != NULL) {
|
|
|
|
addReplyError(c,"Either use STRINGS or KEYS");
|
2020-06-12 12:34:44 +02:00
|
|
|
goto cleanup;
|
2020-04-01 16:10:18 +02:00
|
|
|
}
|
2020-05-21 17:55:09 -04:00
|
|
|
a = szFromObj(c->argv[j+1]);
|
|
|
|
b = szFromObj(c->argv[j+2]);
|
2020-04-01 16:10:18 +02:00
|
|
|
j += 2;
|
2020-05-21 18:13:35 -04:00
|
|
|
} else if (!strcasecmp(opt,"KEYS") && moreargs > 1) {
|
2020-04-06 13:23:07 +02:00
|
|
|
if (a != NULL) {
|
|
|
|
addReplyError(c,"Either use STRINGS or KEYS");
|
2020-06-12 12:34:44 +02:00
|
|
|
goto cleanup;
|
2020-04-01 22:11:59 +02:00
|
|
|
}
|
|
|
|
obja = lookupKeyRead(c->db,c->argv[j+1]);
|
|
|
|
objb = lookupKeyRead(c->db,c->argv[j+2]);
|
2020-06-12 12:34:44 +02:00
|
|
|
if ((obja && obja->type != OBJ_STRING) ||
|
|
|
|
(objb && objb->type != OBJ_STRING))
|
|
|
|
{
|
|
|
|
addReplyError(c,
|
|
|
|
"The specified keys must contain string values");
|
|
|
|
/* Don't cleanup the objects, we need to do that
|
|
|
|
* only after callign getDecodedObject(). */
|
|
|
|
obja = NULL;
|
|
|
|
objb = NULL;
|
|
|
|
goto cleanup;
|
2020-06-08 23:36:01 -04:00
|
|
|
}
|
2020-04-01 22:11:59 +02:00
|
|
|
obja = obja ? getDecodedObject(obja) : createStringObject("",0);
|
|
|
|
objb = objb ? getDecodedObject(objb) : createStringObject("",0);
|
2020-05-21 17:55:09 -04:00
|
|
|
a = szFromObj(obja);
|
|
|
|
b = szFromObj(objb);
|
2020-04-01 22:11:59 +02:00
|
|
|
j += 2;
|
2020-04-01 16:10:18 +02:00
|
|
|
} else {
|
|
|
|
addReply(c,shared.syntaxerr);
|
2020-06-12 12:34:44 +02:00
|
|
|
goto cleanup;
|
2020-04-01 16:10:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 17:11:31 +02:00
|
|
|
/* Complain if the user passed ambiguous parameters. */
|
2020-04-01 16:10:18 +02:00
|
|
|
if (a == NULL) {
|
2020-04-01 22:11:59 +02:00
|
|
|
addReplyError(c,"Please specify two strings: "
|
|
|
|
"STRINGS or KEYS options are mandatory");
|
2020-06-12 12:34:44 +02:00
|
|
|
goto cleanup;
|
2020-04-06 13:23:07 +02:00
|
|
|
} else if (getlen && getidx) {
|
2020-04-01 17:11:31 +02:00
|
|
|
addReplyError(c,
|
2020-04-06 13:23:07 +02:00
|
|
|
"If you want both the length and indexes, please "
|
|
|
|
"just use IDX.");
|
2020-06-12 12:34:44 +02:00
|
|
|
goto cleanup;
|
2020-04-01 16:10:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute the LCS using the vanilla dynamic programming technique of
|
|
|
|
* building a table of LCS(x,y) substrings. */
|
2020-09-30 19:47:55 +00:00
|
|
|
alen = sdslen(a);
|
|
|
|
blen = sdslen(b);
|
2020-04-01 16:10:18 +02:00
|
|
|
|
|
|
|
/* Setup an uint32_t array to store at LCS[i,j] the length of the
|
|
|
|
* LCS A0..i-1, B0..j-1. Note that we have a linear array here, so
|
2020-04-02 21:17:31 +02:00
|
|
|
* we index it as LCS[j+(blen+1)*j] */
|
2020-09-30 19:47:55 +00:00
|
|
|
lcs = (uint32_t*)zmalloc((alen+1)*(blen+1)*sizeof(uint32_t));
|
2020-04-01 23:45:07 +02:00
|
|
|
#define LCS(A,B) lcs[(B)+((A)*(blen+1))]
|
2020-04-01 16:10:18 +02:00
|
|
|
|
|
|
|
/* Start building the LCS table. */
|
|
|
|
for (uint32_t i = 0; i <= alen; i++) {
|
|
|
|
for (uint32_t j = 0; j <= blen; j++) {
|
|
|
|
if (i == 0 || j == 0) {
|
|
|
|
/* If one substring has length of zero, the
|
|
|
|
* LCS length is zero. */
|
|
|
|
LCS(i,j) = 0;
|
|
|
|
} else if (a[i-1] == b[j-1]) {
|
|
|
|
/* The len LCS (and the LCS itself) of two
|
|
|
|
* sequences with the same final character, is the
|
|
|
|
* LCS of the two sequences without the last char
|
|
|
|
* plus that last char. */
|
|
|
|
LCS(i,j) = LCS(i-1,j-1)+1;
|
|
|
|
} else {
|
|
|
|
/* If the last character is different, take the longest
|
|
|
|
* between the LCS of the first string and the second
|
|
|
|
* minus the last char, and the reverse. */
|
|
|
|
uint32_t lcs1 = LCS(i-1,j);
|
|
|
|
uint32_t lcs2 = LCS(i,j-1);
|
|
|
|
LCS(i,j) = lcs1 > lcs2 ? lcs1 : lcs2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Store the actual LCS string in "result" if needed. We create
|
|
|
|
* it backward, but the length is already known, we store it into idx. */
|
2020-09-30 19:47:55 +00:00
|
|
|
idx = LCS(alen,blen);
|
|
|
|
arange_start = alen; /* alen signals that values are not set. */
|
|
|
|
arange_end = 0;
|
|
|
|
brange_start = 0;
|
|
|
|
brange_end = 0;
|
2020-04-01 16:10:18 +02:00
|
|
|
|
|
|
|
/* Do we need to compute the actual LCS string? Allocate it in that case. */
|
2020-09-30 19:47:55 +00:00
|
|
|
computelcs = getidx || !getlen;
|
2020-04-01 16:10:18 +02:00
|
|
|
if (computelcs) result = sdsnewlen(SDS_NOINIT,idx);
|
|
|
|
|
2020-04-01 17:11:31 +02:00
|
|
|
/* Start with a deferred array if we have to emit the ranges. */
|
2020-09-30 19:47:55 +00:00
|
|
|
arraylen = 0; /* Number of ranges emitted in the array. */
|
2020-04-06 13:23:07 +02:00
|
|
|
if (getidx) {
|
2020-04-02 16:15:17 +02:00
|
|
|
addReplyMapLen(c,2);
|
|
|
|
addReplyBulkCString(c,"matches");
|
2020-04-01 17:11:31 +02:00
|
|
|
arraylenptr = addReplyDeferredLen(c);
|
2020-04-02 16:15:17 +02:00
|
|
|
}
|
2020-04-01 17:11:31 +02:00
|
|
|
|
2020-04-01 16:10:18 +02:00
|
|
|
i = alen, j = blen;
|
|
|
|
while (computelcs && i > 0 && j > 0) {
|
2020-04-01 17:36:32 +02:00
|
|
|
int emit_range = 0;
|
2020-04-01 16:10:18 +02:00
|
|
|
if (a[i-1] == b[j-1]) {
|
|
|
|
/* If there is a match, store the character and reduce
|
|
|
|
* the indexes to look for a new match. */
|
|
|
|
result[idx-1] = a[i-1];
|
2020-04-01 17:36:32 +02:00
|
|
|
|
2020-04-01 17:11:31 +02:00
|
|
|
/* Track the current range. */
|
|
|
|
if (arange_start == alen) {
|
|
|
|
arange_start = i-1;
|
|
|
|
arange_end = i-1;
|
|
|
|
brange_start = j-1;
|
|
|
|
brange_end = j-1;
|
|
|
|
} else {
|
|
|
|
/* Let's see if we can extend the range backward since
|
|
|
|
* it is contiguous. */
|
|
|
|
if (arange_start == i && brange_start == j) {
|
|
|
|
arange_start--;
|
|
|
|
brange_start--;
|
|
|
|
} else {
|
|
|
|
emit_range = 1;
|
|
|
|
}
|
|
|
|
}
|
2020-04-01 17:36:32 +02:00
|
|
|
/* Emit the range if we matched with the first byte of
|
|
|
|
* one of the two strings. We'll exit the loop ASAP. */
|
2020-04-01 17:14:36 +02:00
|
|
|
if (arange_start == 0 || brange_start == 0) emit_range = 1;
|
2020-04-01 17:11:31 +02:00
|
|
|
idx--; i--; j--;
|
2020-04-01 16:10:18 +02:00
|
|
|
} else {
|
|
|
|
/* Otherwise reduce i and j depending on the largest
|
|
|
|
* LCS between, to understand what direction we need to go. */
|
|
|
|
uint32_t lcs1 = LCS(i-1,j);
|
|
|
|
uint32_t lcs2 = LCS(i,j-1);
|
|
|
|
if (lcs1 > lcs2)
|
|
|
|
i--;
|
|
|
|
else
|
|
|
|
j--;
|
2020-04-01 17:36:32 +02:00
|
|
|
if (arange_start != alen) emit_range = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Emit the current range if needed. */
|
2020-04-02 13:37:35 +02:00
|
|
|
uint32_t match_len = arange_end - arange_start + 1;
|
2020-04-01 17:36:32 +02:00
|
|
|
if (emit_range) {
|
2020-04-02 13:37:35 +02:00
|
|
|
if (minmatchlen == 0 || match_len >= minmatchlen) {
|
|
|
|
if (arraylenptr) {
|
|
|
|
addReplyArrayLen(c,2+withmatchlen);
|
|
|
|
addReplyArrayLen(c,2);
|
|
|
|
addReplyLongLong(c,arange_start);
|
|
|
|
addReplyLongLong(c,arange_end);
|
|
|
|
addReplyArrayLen(c,2);
|
|
|
|
addReplyLongLong(c,brange_start);
|
|
|
|
addReplyLongLong(c,brange_end);
|
|
|
|
if (withmatchlen) addReplyLongLong(c,match_len);
|
|
|
|
arraylen++;
|
|
|
|
}
|
2020-04-01 17:36:32 +02:00
|
|
|
}
|
|
|
|
arange_start = alen; /* Restart at the next match. */
|
2020-04-01 16:10:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Signal modified key, increment dirty, ... */
|
|
|
|
|
|
|
|
/* Reply depending on the given options. */
|
2020-04-01 17:11:31 +02:00
|
|
|
if (arraylenptr) {
|
2020-04-02 16:15:17 +02:00
|
|
|
addReplyBulkCString(c,"len");
|
|
|
|
addReplyLongLong(c,LCS(alen,blen));
|
2020-04-01 17:11:31 +02:00
|
|
|
setDeferredArrayLen(c,arraylenptr,arraylen);
|
|
|
|
} else if (getlen) {
|
2020-04-01 16:10:18 +02:00
|
|
|
addReplyLongLong(c,LCS(alen,blen));
|
|
|
|
} else {
|
|
|
|
addReplyBulkSds(c,result);
|
|
|
|
result = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Cleanup. */
|
|
|
|
sdsfree(result);
|
|
|
|
zfree(lcs);
|
2020-06-08 23:48:51 -04:00
|
|
|
|
2020-06-12 12:34:44 +02:00
|
|
|
cleanup:
|
2020-06-08 23:48:51 -04:00
|
|
|
if (obja) decrRefCount(obja);
|
|
|
|
if (objb) decrRefCount(objb);
|
2020-04-01 16:10:18 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|