summaryrefslogtreecommitdiff
path: root/src/db.c
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2014-03-10 16:26:08 +0100
committerantirez <antirez@gmail.com>2014-03-10 16:26:08 +0100
commit04cf02e8dc102105b9d302dc86f499e4be63cb55 (patch)
treed47f7c62a3a795452d63d1450a563cd2b0e00e70 /src/db.c
parent21765c858886bfeb1349ab83dccb1113b5e55d2c (diff)
downloadredis-04cf02e8dc102105b9d302dc86f499e4be63cb55.tar.gz
Cluster: SORT get keys helper implemented.
Diffstat (limited to 'src/db.c')
-rw-r--r--src/db.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/db.c b/src/db.c
index 94a85e9d0..8bb6df67d 100644
--- a/src/db.c
+++ b/src/db.c
@@ -1028,6 +1028,51 @@ int *evalGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys)
return keys;
}
+/* Helper function to extract keys from the SORT command.
+ *
+ * SORT <sort-key> ... STORE <store-key> ...
+ *
+ * The first argument of SORT is always a key, however a list of options
+ * follow in SQL-alike style. Here we parse just the minimum in order to
+ * correctly identify keys in the "STORE" option. */
+int *sortGetKeys(struct redisCommand *cmd, robj **argv, int argc, int *numkeys) {
+ int i, j, num, *keys;
+ REDIS_NOTUSED(cmd);
+
+ num = 0;
+ keys = zmalloc(sizeof(int)*2); /* Alloc 2 places for the worst case. */
+
+ keys[num++] = 1; /* <sort-key> is always present. */
+
+ /* Search for STORE option. By default we consider options to don't
+ * have arguments, so if we find an unknown option name we scan the
+ * next. However there are options with 1 or 2 arguments, so we
+ * provide a list here in order to skip the right number of args. */
+ struct {
+ char *name;
+ int skip;
+ } skiplist[] = {
+ {"limit", 2},
+ {"get", 1},
+ {"by", 1},
+ {NULL, 0} /* End of elements. */
+ };
+
+ for (i = 2; i < argc; i++) {
+ for (j = 0; skiplist[j].name != NULL; j++) {
+ if (!strcasecmp(argv[i]->ptr,skiplist[j].name)) {
+ i += skiplist[j].skip;
+ break;
+ } else if (!strcasecmp(argv[i]->ptr,"store") && i+1 < argc) {
+ keys[num++] = i+1; /* <store-key> */
+ break;
+ }
+ }
+ }
+ *numkeys = num;
+ return keys;
+}
+
/* Slot to Key API. This is used by Redis Cluster in order to obtain in
* a fast way a key that belongs to a specified hash slot. This is useful
* while rehashing the cluster. */