MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / sortGetKeys

Function sortGetKeys

src/db.cpp:2271–2310  ·  view source on GitHub ↗

Helper function to extract keys from the SORT command. * * SORT ... STORE ... * * 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. */

Source from the content-addressed store, hash-verified

2269 * follow in SQL-alike style. Here we parse just the minimum in order to
2270 * correctly identify keys in the "STORE" option. */
2271int sortGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result) {
2272 int i, j, num, *keys, found_store = 0;
2273 UNUSED(cmd);
2274
2275 num = 0;
2276 keys = getKeysPrepareResult(result, 2); /* Alloc 2 places for the worst case. */
2277 keys[num++] = 1; /* <sort-key> is always present. */
2278
2279 /* Search for STORE option. By default we consider options to don't
2280 * have arguments, so if we find an unknown option name we scan the
2281 * next. However there are options with 1 or 2 arguments, so we
2282 * provide a list here in order to skip the right number of args. */
2283 struct {
2284 const char *name;
2285 int skip;
2286 } skiplist[] = {
2287 {"limit", 2},
2288 {"get", 1},
2289 {"by", 1},
2290 {NULL, 0} /* End of elements. */
2291 };
2292
2293 for (i = 2; i < argc; i++) {
2294 for (j = 0; skiplist[j].name != NULL; j++) {
2295 if (!strcasecmp(szFromObj(argv[i]),skiplist[j].name)) {
2296 i += skiplist[j].skip;
2297 break;
2298 } else if (!strcasecmp(szFromObj(argv[i]),"store") && i+1 < argc) {
2299 /* Note: we don't increment "num" here and continue the loop
2300 * to be sure to process the *last* "STORE" option if multiple
2301 * ones are provided. This is same behavior as SORT. */
2302 found_store = 1;
2303 keys[num] = i+1; /* <store-key> */
2304 break;
2305 }
2306 }
2307 }
2308 result->numkeys = num + found_store;
2309 return result->numkeys;
2310}
2311
2312int migrateGetKeys(struct redisCommand *cmd, robj **argv, int argc, getKeysResult *result) {
2313 int i, num, first, *keys;

Callers

nothing calls this directly

Calls 2

getKeysPrepareResultFunction · 0.85
szFromObjFunction · 0.85

Tested by

no test coverage detected