Register a new command in the Redis server, that will be handled by * calling the function pointer 'func' using the RedisModule calling * convention. The function returns REDISMODULE_ERR if the specified command * name is already busy or a set of invalid flags were passed, otherwise * REDISMODULE_OK is returned and the new command is registered. * * This function must be called during the in
| 878 | * though it's not a write command. |
| 879 | */ |
| 880 | int RM_CreateCommand(RedisModuleCtx *ctx, const char *name, RedisModuleCmdFunc cmdfunc, const char *strflags, int firstkey, int lastkey, int keystep) { |
| 881 | int64_t flags = strflags ? commandFlagsFromString((char*)strflags) : 0; |
| 882 | if (flags == -1) return REDISMODULE_ERR; |
| 883 | if ((flags & CMD_MODULE_NO_CLUSTER) && server.cluster_enabled) |
| 884 | return REDISMODULE_ERR; |
| 885 | |
| 886 | struct redisCommand *rediscmd; |
| 887 | RedisModuleCommandProxy *cp; |
| 888 | sds cmdname = sdsnew(name); |
| 889 | |
| 890 | /* Check if the command name is busy. */ |
| 891 | if (lookupCommand(cmdname) != NULL) { |
| 892 | sdsfree(cmdname); |
| 893 | return REDISMODULE_ERR; |
| 894 | } |
| 895 | |
| 896 | /* Create a command "proxy", which is a structure that is referenced |
| 897 | * in the command table, so that the generic command that works as |
| 898 | * binding between modules and Redis, can know what function to call |
| 899 | * and what the module is. |
| 900 | * |
| 901 | * Note that we use the Redis command table 'getkeys_proc' in order to |
| 902 | * pass a reference to the command proxy structure. */ |
| 903 | cp = zmalloc(sizeof(*cp)); |
| 904 | cp->module = ctx->module; |
| 905 | cp->func = cmdfunc; |
| 906 | cp->rediscmd = zmalloc(sizeof(*rediscmd)); |
| 907 | cp->rediscmd->name = cmdname; |
| 908 | cp->rediscmd->proc = RedisModuleCommandDispatcher; |
| 909 | cp->rediscmd->arity = -1; |
| 910 | cp->rediscmd->flags = flags | CMD_MODULE; |
| 911 | cp->rediscmd->getkeys_proc = (redisGetKeysProc*)(unsigned long)cp; |
| 912 | cp->rediscmd->firstkey = firstkey; |
| 913 | cp->rediscmd->lastkey = lastkey; |
| 914 | cp->rediscmd->keystep = keystep; |
| 915 | cp->rediscmd->microseconds = 0; |
| 916 | cp->rediscmd->calls = 0; |
| 917 | cp->rediscmd->rejected_calls = 0; |
| 918 | cp->rediscmd->failed_calls = 0; |
| 919 | dictAdd(server.commands,sdsdup(cmdname),cp->rediscmd); |
| 920 | dictAdd(server.orig_commands,sdsdup(cmdname),cp->rediscmd); |
| 921 | cp->rediscmd->id = ACLGetCommandID(cmdname); /* ID used for ACL. */ |
| 922 | return REDISMODULE_OK; |
| 923 | } |
| 924 | |
| 925 | /* -------------------------------------------------------------------------- |
| 926 | * ## Module information and time measurement |
nothing calls this directly
no test coverage detected