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

Function RM_CreateCommand

src/module.cpp:907–950  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

905 * though it's not a write command.
906 */
907int RM_CreateCommand(RedisModuleCtx *ctx, const char *name, RedisModuleCmdFunc cmdfunc, const char *strflags, int firstkey, int lastkey, int keystep) {
908 int64_t flags = strflags ? commandFlagsFromString((char*)strflags) : 0;
909 if (flags == -1) return REDISMODULE_ERR;
910 if ((flags & CMD_MODULE_NO_CLUSTER) && g_pserver->cluster_enabled)
911 return REDISMODULE_ERR;
912
913 struct redisCommand *rediscmd;
914 RedisModuleCommandProxy *cp;
915 sds cmdname = sdsnew(name);
916
917 /* Check if the command name is busy. */
918 if (lookupCommand(cmdname) != NULL) {
919 sdsfree(cmdname);
920 return REDISMODULE_ERR;
921 }
922
923 /* Create a command "proxy", which is a structure that is referenced
924 * in the command table, so that the generic command that works as
925 * binding between modules and Redis, can know what function to call
926 * and what the module is.
927 *
928 * Note that we use the Redis command table 'getkeys_proc' in order to
929 * pass a reference to the command proxy structure. */
930 cp = (RedisModuleCommandProxy*)zmalloc(sizeof(*cp), MALLOC_LOCAL);
931 cp->module = ctx->module;
932 cp->func = cmdfunc;
933 cp->rediscmd = (redisCommand*)zmalloc(sizeof(*rediscmd), MALLOC_LOCAL);
934 cp->rediscmd->name = cmdname;
935 cp->rediscmd->proc = RedisModuleCommandDispatcher;
936 cp->rediscmd->arity = -1;
937 cp->rediscmd->flags = flags | CMD_MODULE;
938 cp->rediscmd->getkeys_proc = (redisGetKeysProc*)(unsigned long)cp;
939 cp->rediscmd->firstkey = firstkey;
940 cp->rediscmd->lastkey = lastkey;
941 cp->rediscmd->keystep = keystep;
942 cp->rediscmd->microseconds = 0;
943 cp->rediscmd->calls = 0;
944 cp->rediscmd->rejected_calls = 0;
945 cp->rediscmd->failed_calls = 0;
946 dictAdd(g_pserver->commands,sdsdup(cmdname),cp->rediscmd);
947 dictAdd(g_pserver->orig_commands,sdsdup(cmdname),cp->rediscmd);
948 cp->rediscmd->id = ACLGetCommandID(cmdname); /* ID used for ACL. */
949 return REDISMODULE_OK;
950}
951
952/* --------------------------------------------------------------------------
953 * ## Module information and time measurement

Callers

nothing calls this directly

Calls 8

commandFlagsFromStringFunction · 0.85
sdsnewFunction · 0.85
lookupCommandFunction · 0.85
sdsfreeFunction · 0.85
zmallocFunction · 0.85
sdsdupFunction · 0.85
ACLGetCommandIDFunction · 0.85
dictAddFunction · 0.70

Tested by

no test coverage detected