From a4460ef43f9625e6c09cdc327614d1b47c043489 Mon Sep 17 00:00:00 2001 From: antirez Date: Tue, 28 Apr 2009 20:36:08 +0200 Subject: [PATCH] SMOVE command implemented --- Changelog | 2 ++ TODO | 1 + doc/CommandReference.html | 2 +- doc/SaddCommand.html | 2 +- doc/ScardCommand.html | 2 +- doc/SinterCommand.html | 2 +- doc/SinterstoreCommand.html | 2 +- doc/SismemberCommand.html | 2 +- doc/SmembersCommand.html | 2 +- doc/SmoveCommand.html | 44 +++++++++++++++++++++++++++++++++++++ doc/SortCommand.html | 2 +- doc/SremCommand.html | 2 +- doc/SunionCommand.html | 2 +- doc/SunionstoreCommand.html | 2 +- redis-cli.c | 1 + redis.c | 37 +++++++++++++++++++++++++++++++ 16 files changed, 96 insertions(+), 11 deletions(-) create mode 100644 doc/SmoveCommand.html diff --git a/Changelog b/Changelog index bdd99731..ee11a10a 100644 --- a/Changelog +++ b/Changelog @@ -1,3 +1,5 @@ +2009-04-28 less CPU usage in command parsing, case insensitive config directives +2009-04-28 GETSET command doc added 2009-04-28 GETSET tests 2009-04-28 GETSET implemented 2009-04-27 ability to specify a different file name for the DB diff --git a/TODO b/TODO index bb162494..e236352c 100644 --- a/TODO +++ b/TODO @@ -1,5 +1,6 @@ BEFORE REDIS 1.0.0-rc1 +- SMOVE _key1_ _key2_ _ele_. Pop an element form _key1_ and push (SADD or LPUSH) it nto _key2_. - What happens if the saving child gets killed instead to end normally? Handle this. - Make sinterstore / unionstore / sdiffstore returning the cardinality of the resulting set. - Remove max number of args limit diff --git a/doc/CommandReference.html b/doc/CommandReference.html index 5ade73b9..724be24a 100644 --- a/doc/CommandReference.html +++ b/doc/CommandReference.html @@ -30,7 +30,7 @@

Commands operating on string values

Commands operating on the key space

Commands operating on lists

-

Commands operating on sets

+

Commands operating on sets

Multiple databases handling commands

Sorting

Persistence control commands

diff --git a/doc/SaddCommand.html b/doc/SaddCommand.html index 400d4187..db66cd8e 100644 --- a/doc/SaddCommand.html +++ b/doc/SaddCommand.html @@ -32,7 +32,7 @@ 1 if the new element was added 0 if the new element was already a member of the set

See also

- + diff --git a/doc/ScardCommand.html b/doc/ScardCommand.html index c6539745..ac992fa3 100644 --- a/doc/ScardCommand.html +++ b/doc/ScardCommand.html @@ -31,7 +31,7 @@

Return value

Integer reply, specifically:

 the cardinality (number of elements) of the set as an integer.
 

See also

- + diff --git a/doc/SinterCommand.html b/doc/SinterCommand.html index efbe7a5c..0d1267dc 100644 --- a/doc/SinterCommand.html +++ b/doc/SinterCommand.html @@ -30,7 +30,7 @@ Time complexity O(NM) worst case where N is the cardinality of the smallest set and M the number of sets_

Return the members of a set resulting from the intersection of all thesets hold at the specified keys. Like in LRANGE the result is sent tothe client as a multi-bulk reply (see the protocol specification formore information). If just a single key is specified, then this commandproduces the same result as SELEMENTS. Actually SELEMENTS is just syntaxsugar for SINTERSECT.
Non existing keys are considered like empty sets, so if one of the keys ismissing an empty set is returned (since the intersection with an emptyset always is an empty set).

Return value

Multi bulk reply, specifically the list of common elements.

See also

-
* SREM* SISMEMBER* SCARD* SMEMBERS* SINTERSTORE* SUNION* SUNIONSTORE
+
* SADD* SREM* SISMEMBER* SCARD* SMEMBERS* SINTERSTORE* SUNION* SUNIONSTORE* SMOVE
diff --git a/doc/SinterstoreCommand.html b/doc/SinterstoreCommand.html index 02757ccf..798e7519 100644 --- a/doc/SinterstoreCommand.html +++ b/doc/SinterstoreCommand.html @@ -29,7 +29,7 @@

SINTERSTORE _dstkey_ _key1_ _key2_ ... _keyN_

Time complexity O(NM) worst case where N is the cardinality of the smallest set and M the number of sets_

This commnad works exactly like SINTER but instead of being returned the resulting set is sotred as _dstkey_.

Return value

Status code reply

See also

-
* SREM* SISMEMBER* SCARD* SMEMBERS* SINTER* SINTERSTORE
+
* SADD* SREM* SISMEMBER* SCARD* SMEMBERS* SINTER* SINTERSTORE* SMOVE
diff --git a/doc/SismemberCommand.html b/doc/SismemberCommand.html index 0b321637..fb900c47 100644 --- a/doc/SismemberCommand.html +++ b/doc/SismemberCommand.html @@ -32,7 +32,7 @@ 1 if the element is a member of the set 0 if the element is not a member of the set OR if the key does not exist

See also

- + diff --git a/doc/SmembersCommand.html b/doc/SmembersCommand.html index 52bdcbc4..ddee1947 100644 --- a/doc/SmembersCommand.html +++ b/doc/SmembersCommand.html @@ -29,7 +29,7 @@

SMEMBERS _key_

Time complexity O(N)
Return all the members (elements) of the set value stored at key. Thisis just syntax glue for SINTERSECT.

Return value

Multi bulk reply

See also

- + diff --git a/doc/SmoveCommand.html b/doc/SmoveCommand.html new file mode 100644 index 00000000..9c9c54e4 --- /dev/null +++ b/doc/SmoveCommand.html @@ -0,0 +1,44 @@ + + + + + + + +
+ + + +
+
+ +SmoveCommand: Contents
  SMOVE _srckey_ _dstkey_ _member_
    Return value
    See also +
+ +

SmoveCommand

+ +
+ +
+ +
+

SMOVE _srckey_ _dstkey_ _member_

+Time complexity O(1)
Move the specifided member from the set at srckey to the set at dstkey.This operation is atomic, in every given moment the element will appear tobe in the source or destination set for accessing clients.
+
If the source set does not exist or does not contain the specified elementno operation is performed and zero is returned, otherwise the element isremoved from the source set and added to the destination set. On successone is returned, even if the element was already present in the destionationset.
+
An error is raised if the source or destination keys contain a non Set value.
+

Return value

Integer reply, specifically:

+1 if the element was moved
+0 if the element was not found on the first set and no operation was performed
+

See also

+ +
+ +
+
+ + + diff --git a/doc/SortCommand.html b/doc/SortCommand.html index 3d88e401..46c720b2 100644 --- a/doc/SortCommand.html +++ b/doc/SortCommand.html @@ -50,7 +50,7 @@ SORT mylist BY weight_* SORT mylist BY weight_* GET object_*
Note that GET can be used multiple times in order to get more keys forevery element of the original List or Set sorted.

Return value

Multi bulk reply, specifically a list of sorted elements.

See Also

- + diff --git a/doc/SremCommand.html b/doc/SremCommand.html index a16961de..4aef8291 100644 --- a/doc/SremCommand.html +++ b/doc/SremCommand.html @@ -32,7 +32,7 @@ 1 if the new element was removed 0 if the new element was not a member of the set

See also

- + diff --git a/doc/SunionCommand.html b/doc/SunionCommand.html index bc1e2564..7f3cc0e3 100644 --- a/doc/SunionCommand.html +++ b/doc/SunionCommand.html @@ -30,7 +30,7 @@ Time complexity O(N) where N is the total number of elements in all the provided sets
Return the members of a set resulting from the union of all thesets hold at the specified keys. Like in LRANGE the result is sent tothe client as a multi-bulk reply (see the protocol specification formore information). If just a single key is specified, then this commandproduces the same result as SELEMENTS.
Non existing keys are considered like empty sets.

Return value

Multi bulk reply, specifically the list of common elements.

See also

- + diff --git a/doc/SunionstoreCommand.html b/doc/SunionstoreCommand.html index f0519764..ea1052a6 100644 --- a/doc/SunionstoreCommand.html +++ b/doc/SunionstoreCommand.html @@ -29,7 +29,7 @@

SUNIONSTORE _dstkey_ _key1_ _key2_ ... _keyN_

Time complexity O(N) where N is the total number of elements in all the provided sets
This commnad works exactly like SUNION but instead of being returned the resulting set is sotred as dstkey.

Return value

Status code reply

See also

- + diff --git a/redis-cli.c b/redis-cli.c index 2d5eda98..e4ef342f 100644 --- a/redis-cli.c +++ b/redis-cli.c @@ -76,6 +76,7 @@ static struct redisCommand cmdTable[] = { {"lrem",4,REDIS_CMD_BULK}, {"sadd",3,REDIS_CMD_BULK}, {"srem",3,REDIS_CMD_BULK}, + {"smove",4,REDIS_CMD_BULK}, {"sismember",3,REDIS_CMD_BULK}, {"scard",2,REDIS_CMD_INLINE}, {"sinter",-2,REDIS_CMD_INLINE}, diff --git a/redis.c b/redis.c index 95828126..856a6cac 100644 --- a/redis.c +++ b/redis.c @@ -341,6 +341,7 @@ static void typeCommand(redisClient *c); static void lsetCommand(redisClient *c); static void saddCommand(redisClient *c); static void sremCommand(redisClient *c); +static void smoveCommand(redisClient *c); static void sismemberCommand(redisClient *c); static void scardCommand(redisClient *c); static void sinterCommand(redisClient *c); @@ -383,6 +384,7 @@ static struct redisCommand cmdTable[] = { {"lrem",lremCommand,4,REDIS_CMD_BULK}, {"sadd",saddCommand,3,REDIS_CMD_BULK}, {"srem",sremCommand,3,REDIS_CMD_BULK}, + {"smove",smoveCommand,4,REDIS_CMD_BULK}, {"sismember",sismemberCommand,3,REDIS_CMD_BULK}, {"scard",scardCommand,2,REDIS_CMD_INLINE}, {"sinter",sinterCommand,-2,REDIS_CMD_INLINE}, @@ -2819,6 +2821,41 @@ static void sremCommand(redisClient *c) { } } +static void smoveCommand(redisClient *c) { + robj *srcset, *dstset; + + srcset = lookupKeyWrite(c->db,c->argv[1]); + dstset = lookupKeyWrite(c->db,c->argv[2]); + + /* If the source key does not exist return 0, if it's of the wrong type + * raise an error */ + if (srcset == NULL || srcset->type != REDIS_SET) { + addReply(c, srcset ? shared.wrongtypeerr : shared.czero); + return; + } + /* Error if the destination key is not a set as well */ + if (dstset && dstset->type != REDIS_SET) { + addReply(c,shared.wrongtypeerr); + return; + } + /* Remove the element from the source set */ + if (dictDelete(srcset->ptr,c->argv[3]) == DICT_ERR) { + /* Key not found in the src set! return zero */ + addReply(c,shared.czero); + return; + } + server.dirty++; + /* Add the element to the destination set */ + if (!dstset) { + dstset = createSetObject(); + dictAdd(c->db->dict,c->argv[2],dstset); + incrRefCount(c->argv[2]); + } + if (dictAdd(dstset->ptr,c->argv[3],NULL) == DICT_OK) + incrRefCount(c->argv[3]); + addReply(c,shared.cone); +} + static void sismemberCommand(redisClient *c) { robj *set; -- 2.45.2