For a specified command, parse its arguments and return an array that * contains the indexes of all key name arguments. This function is * essnetially a more efficient way to do COMMAND GETKEYS. * * A NULL return value indicates the specified command has no keys, or * an error condition. Error conditions are indicated by setting errno * as folllows: * * * ENOENT: Specified command does not
| 9181 | * must explicitly call RM_Free() to free it. |
| 9182 | */ |
| 9183 | int *RM_GetCommandKeys(RedisModuleCtx *ctx, RedisModuleString **argv, int argc, int *num_keys) { |
| 9184 | UNUSED(ctx); |
| 9185 | struct redisCommand *cmd; |
| 9186 | int *res = NULL; |
| 9187 | |
| 9188 | /* Find command */ |
| 9189 | if ((cmd = lookupCommand(szFromObj(argv[0]))) == NULL) { |
| 9190 | errno = ENOENT; |
| 9191 | return NULL; |
| 9192 | } |
| 9193 | |
| 9194 | /* Bail out if command has no keys */ |
| 9195 | if (cmd->getkeys_proc == NULL && cmd->firstkey == 0) { |
| 9196 | errno = 0; |
| 9197 | return NULL; |
| 9198 | } |
| 9199 | |
| 9200 | if ((cmd->arity > 0 && cmd->arity != argc) || (argc < -cmd->arity)) { |
| 9201 | errno = EINVAL; |
| 9202 | return NULL; |
| 9203 | } |
| 9204 | |
| 9205 | getKeysResult result = GETKEYS_RESULT_INIT; |
| 9206 | getKeysFromCommand(cmd, argv, argc, &result); |
| 9207 | |
| 9208 | *num_keys = result.numkeys; |
| 9209 | if (!result.numkeys) { |
| 9210 | errno = 0; |
| 9211 | getKeysFreeResult(&result); |
| 9212 | return NULL; |
| 9213 | } |
| 9214 | |
| 9215 | if (result.keys == result.keysbuf) { |
| 9216 | /* If the result is using a stack based array, copy it. */ |
| 9217 | unsigned long int size = sizeof(int) * result.numkeys; |
| 9218 | res = (int*)zmalloc(size); |
| 9219 | memcpy(res, result.keys, size); |
| 9220 | } else { |
| 9221 | /* We return the heap based array and intentionally avoid calling |
| 9222 | * getKeysFreeResult() here, as it is the caller's responsibility |
| 9223 | * to free this array. |
| 9224 | */ |
| 9225 | res = result.keys; |
| 9226 | } |
| 9227 | |
| 9228 | return res; |
| 9229 | } |
| 9230 | |
| 9231 | /* Return the name of the command currently running */ |
| 9232 | const char *RM_GetCurrentCommandName(RedisModuleCtx *ctx) { |
nothing calls this directly
no test coverage detected