自顶向下redis4.0(3)命令与dict

redis4.0的命令

简介

目录

正文

redisCommand与redisCommandTable

所有的命令一开始都位于server.c文件起始位置的redisCommandTable中,在观察redisCommandTable表之前,我们先看一下redisCommand结构体。

typedef void redisCommandProc(struct client *c);
typedef int *redisGetKeysProc(struct redisCommand *cmd, robj **argv, int argc, int *numkeys);
struct redisCommand {
    char *name;
    redisCommandProc *proc;
    int arity;
    char *sflags; /* Flags as string representation, one char per flag. */
    int flags;    /* The actual flags, obtained from the 'sflags' field. */
    /* Use a function to determine keys arguments in a command line.
     * Used for Redis Cluster redirect. */
    redisGetKeysProc *getkeys_proc;
    /* What keys should be loaded in background when calling this command? */
    int firstkey; /* The first argument that's a key (0 = no keys) */
    int lastkey;  /* The last argument that's a key */
    int keystep;  /* The step between first and last key */
    long long microseconds; /* total execution time */
    long long calls;/*total execution count */
};

接着我们回到redisCommandTable,此处仅展示其中的getset命令。

struct redisCommand redisCommandTable[] = {
    {"get",getCommand,3,"rF",0,NULL,1,1,1,0,0},
    {"set",setCommand,-3,"wm",0,NULL,1,1,1,0,0}
};

name字段为指令的名称,客户端发送请求第一个参数就是指令的名称,name在字典中也被用于key来找到对应的redisCommand

proc字段为对应的处理函数,在解析完querybuf里的参数后,redis会执行c->cmd->proc(c)调用对应的处理函数,以set指令为例,在这里执行的就是setCommand。下文会对getset指令做详细的解析。

arity字段遵循以下规则:1. 如果为正,那么指令拥有固定的参数个数。 2.如果为负,那么指令有着最小的参数个数,但参数个数可能会更多。 【参数个数包含指令名称自己】

比如 set指令的最小参数个数为3,set msg "hello" , 但指令参数可能会更多, SET anotherkey "will expire in a minute" EX 60

Command arity follows a simple pattern:

  • positive if command has fixed number of required arguments.
  • negative if command has minimum number of required arguments, but may have more.

Command arity includes counting the command name itself.

sflagsflags代表相同的标志,只是数据的表示形式不同,不同flag代表的意义如下:

  • write - command may result in modifications
  • readonly - command will never modify keys
  • denyoom - reject command if currently out of memory
  • admin - server admin command
  • pubsub - pubsub-related command
  • noscript - deny this command from scripts
  • random - command has random results, dangerous for scripts
  • sort_for_script - if called from script, sort output
  • loading - allow command while database is loading
  • stale - allow command while replica has stale data
  • skip_monitor - do not show this command in MONITOR
  • asking - cluster related - accept even if importing
  • fast - command operates in constant or log(N) time. Used for latency monitoring.
  • movablekeys - keys have no pre-determined position. You must discover keys yourself.

初始化命令

虽然初始化的工作大多数在redisCommandTable配置中已经完成,但还需要在initserverconfig函数中会调用populateCommandTablesflags中的字符值转化为flags中的枚举值,并将命令注册到server.commands字典中。

void populateCommandTable(void) {
    int j;
    int numcommands = sizeof(redisCommandTable)/sizeof(struct redisCommand);

    for (j = 0; j < numcommands; j++) {
        struct redisCommand *c = redisCommandTable+j;
        char *f = c->sflags;
        int retval1; 

        while(*f != '\0') {
            switch(*f) {
            case 'w': c->flags |= CMD_WRITE; break;
            case 'r': c->flags |= CMD_READONLY; break;
            case 'm': c->flags |= CMD_DENYOOM; break;
            case 'a': c->flags |= CMD_ADMIN; break;
            case 'p': c->flags |= CMD_PUBSUB; break;
            case 's': c->flags |= CMD_NOSCRIPT; break;
            case 'R': c->flags |= CMD_RANDOM; break;
            case 'S': c->flags |= CMD_SORT_FOR_SCRIPT; break;
            case 'l': c->flags |= CMD_LOADING; break;
            case 't': c->flags |= CMD_STALE; break;
            case 'M': c->flags |= CMD_SKIP_MONITOR; break;
            case 'k': c->flags |= CMD_ASKING; break;
            case 'F': c->flags |= CMD_FAST; break;
            default: serverPanic("Unsupported command flag"); break;
            }
            f++;
        }

        retval1 = dictAdd(server.commands, sdsnew(c->name), c);

        serverAssert(retval1 == DICT_OK );
    }
}

执行命令

processCommand函数在上篇文章中已经提到过,在解析完querybuf中的字符串,将其转化为client->argcclient->argv中的值后会调用。

processCommand本身做的工作大多数是条件检测,指令是否存在,参数个数是否合法,是否需要验证密码等等。

条件检测通过后,会调用call函数真正调用对应的处理函数。

set指令与字典

如果客户端向服务端发送set msg "hello"请求,在进入到processCommand指令的时候client->argc会被设置为3,client->argv 数组会被填充对应的redisObject lookupCommand会使用client->argv[0]->ptr也就是setserver.commands表中查找对应的redisCommand

server.commands的类型是dict,在服务器启动时初始化。

typedef struct dict {
    dictType *type;
    void *privdata;
    dictht ht[2];
    long rehashidx; /* rehashing not in progress if rehashidx == -1 */
    unsigned long iterators; /* number of iterators currently running */
} dict;

创建一个dict对象时会传入对应的dictType对象,dictType对象就是存储了几个函数指针的对象,redis通过它实现了多态的效果。

typedef struct dictType {
    uint64_t (*hashFunction)(const void *key);
    void *(*keyDup)(void *privdata, const void *key);
    void *(*valDup)(void *privdata, const void *obj);
    int (*keyCompare)(void *privdata, const void *key1, const void *key2);
    void (*keyDestructor)(void *privdata, void *key);
    void (*valDestructor)(void *privdata, void *obj);
} dictType;

当创建command表时,使用的是commandTableDictType,这意味着当对一个key哈希的时候,使用的是dictSdsCaseHash将会无视大小写,比较key值使用的函数是dictSdsKeyCaseCompre

参考文献

redis文档 command

上一篇:python正则表达式:re库的使用


下一篇:python学习(十二)python正则表达式