8 #include "solarisfixes.h" 
  21 #include <netinet/in.h> 
  25 #include "ae.h"      /* Event driven programming library */ 
  26 #include "sds.h"     /* Dynamic safe strings */ 
  27 #include "dict.h"    /* Hash tables */ 
  28 #include "adlist.h"  /* Linked lists */ 
  29 #include "zmalloc.h" /* total memory usage aware version of malloc/free */ 
  30 #include "anet.h"    /* Networking the easy way */ 
  31 #include "zipmap.h"  /* Compact string -> string data structure */ 
  32 #include "ziplist.h" /* Compact list data structure */ 
  33 #include "intset.h"  /* Compact integer set structure */ 
  34 #include "version.h" /* Version macro */ 
  35 #include "util.h"    /* Misc functions useful in many places */ 
  41 /* Static server configuration */ 
  42 #define REDIS_SERVERPORT        6379    /* TCP port */ 
  43 #define REDIS_MAXIDLETIME       0       /* default client timeout: infinite */ 
  44 #define REDIS_DEFAULT_DBNUM     16 
  45 #define REDIS_CONFIGLINE_MAX    1024 
  46 #define REDIS_EXPIRELOOKUPS_PER_CRON    10 /* lookup 10 expires per loop */ 
  47 #define REDIS_MAX_WRITE_PER_EVENT (1024*64) 
  48 #define REDIS_SHARED_INTEGERS 10000 
  49 #define REDIS_SHARED_BULKHDR_LEN 32 
  50 #define REDIS_MAX_LOGMSG_LEN    1024 /* Default maximum length of syslog messages */ 
  51 #define REDIS_AOF_REWRITE_PERC  100 
  52 #define REDIS_AOF_REWRITE_MIN_SIZE (1024*1024) 
  53 #define REDIS_AOF_REWRITE_ITEMS_PER_CMD 64 
  54 #define REDIS_SLOWLOG_LOG_SLOWER_THAN 10000 
  55 #define REDIS_SLOWLOG_MAX_LEN 64 
  56 #define REDIS_MAX_CLIENTS 10000 
  58 #define REDIS_REPL_TIMEOUT 60 
  59 #define REDIS_REPL_PING_SLAVE_PERIOD 10 
  61 /* Protocol and I/O related defines */ 
  62 #define REDIS_MAX_QUERYBUF_LEN  (1024*1024*1024) /* 1GB max query buffer. */ 
  63 #define REDIS_IOBUF_LEN         (1024*16)  /* Generic I/O buffer size */ 
  64 #define REDIS_REPLY_CHUNK_BYTES (16*1024) /* 16k output buffer */ 
  65 #define REDIS_INLINE_MAX_SIZE   (1024*64) /* Max size of inline reads */ 
  66 #define REDIS_MBULK_BIG_ARG     (1024*32) 
  68 /* Hash table parameters */ 
  69 #define REDIS_HT_MINFILL        10      /* Minimal hash table fill 10% */ 
  71 /* Command flags. Please check the command table defined in the redis.c file 
  72  * for more information about the meaning of every flag. */ 
  73 #define REDIS_CMD_WRITE 1                   /* "w" flag */ 
  74 #define REDIS_CMD_READONLY 2                /* "r" flag */ 
  75 #define REDIS_CMD_DENYOOM 4                 /* "m" flag */ 
  76 #define REDIS_CMD_FORCE_REPLICATION 8       /* "f" flag */ 
  77 #define REDIS_CMD_ADMIN 16                  /* "a" flag */ 
  78 #define REDIS_CMD_PUBSUB 32                 /* "p" flag */ 
  79 #define REDIS_CMD_NOSCRIPT  64              /* "s" flag */ 
  80 #define REDIS_CMD_RANDOM 128                /* "R" flag */ 
  81 #define REDIS_CMD_SORT_FOR_SCRIPT 256       /* "S" flag */ 
  84 #define REDIS_STRING 0 
  89 #define REDIS_VMPOINTER 8 
  91 /* Objects encoding. Some kind of objects like Strings and Hashes can be 
  92  * internally represented in multiple ways. The 'encoding' field of the object 
  93  * is set to one of this fields for this object. */ 
  94 #define REDIS_ENCODING_RAW 0     /* Raw representation */ 
  95 #define REDIS_ENCODING_INT 1     /* Encoded as integer */ 
  96 #define REDIS_ENCODING_HT 2      /* Encoded as hash table */ 
  97 #define REDIS_ENCODING_ZIPMAP 3  /* Encoded as zipmap */ 
  98 #define REDIS_ENCODING_LINKEDLIST 4 /* Encoded as regular linked list */ 
  99 #define REDIS_ENCODING_ZIPLIST 5 /* Encoded as ziplist */ 
 100 #define REDIS_ENCODING_INTSET 6  /* Encoded as intset */ 
 101 #define REDIS_ENCODING_SKIPLIST 7  /* Encoded as skiplist */ 
 103 /* Defines related to the dump file format. To store 32 bits lengths for short 
 104  * keys requires a lot of space, so we check the most significant 2 bits of 
 105  * the first byte to interpreter the length: 
 107  * 00|000000 => if the two MSB are 00 the len is the 6 bits of this byte 
 108  * 01|000000 00000000 =>  01, the len is 14 byes, 6 bits + 8 bits of next byte 
 109  * 10|000000 [32 bit integer] => if it's 01, a full 32 bit len will follow 
 110  * 11|000000 this means: specially encoded object will follow. The six bits 
 111  *           number specify the kind of object that follows. 
 112  *           See the REDIS_RDB_ENC_* defines. 
 114  * Lenghts up to 63 are stored using a single byte, most DB keys, and may 
 115  * values, will fit inside. */ 
 116 #define REDIS_RDB_6BITLEN 0 
 117 #define REDIS_RDB_14BITLEN 1 
 118 #define REDIS_RDB_32BITLEN 2 
 119 #define REDIS_RDB_ENCVAL 3 
 120 #define REDIS_RDB_LENERR UINT_MAX 
 122 /* When a length of a string object stored on disk has the first two bits 
 123  * set, the remaining two bits specify a special encoding for the object 
 124  * accordingly to the following defines: */ 
 125 #define REDIS_RDB_ENC_INT8 0        /* 8 bit signed integer */ 
 126 #define REDIS_RDB_ENC_INT16 1       /* 16 bit signed integer */ 
 127 #define REDIS_RDB_ENC_INT32 2       /* 32 bit signed integer */ 
 128 #define REDIS_RDB_ENC_LZF 3         /* string compressed with FASTLZ */ 
 131 #define REDIS_AOF_OFF 0             /* AOF is off */ 
 132 #define REDIS_AOF_ON 1              /* AOF is on */ 
 133 #define REDIS_AOF_WAIT_REWRITE 2    /* AOF waits rewrite to start appending */ 
 136 #define REDIS_SLAVE 1       /* This client is a slave server */ 
 137 #define REDIS_MASTER 2      /* This client is a master server */ 
 138 #define REDIS_MONITOR 4     /* This client is a slave monitor, see MONITOR */ 
 139 #define REDIS_MULTI 8       /* This client is in a MULTI context */ 
 140 #define REDIS_BLOCKED 16    /* The client is waiting in a blocking operation */ 
 141 #define REDIS_DIRTY_CAS 64  /* Watched keys modified. EXEC will fail. */ 
 142 #define REDIS_CLOSE_AFTER_REPLY 128 /* Close after writing entire reply. */ 
 143 #define REDIS_UNBLOCKED 256 /* This client was unblocked and is stored in 
 144                                server.unblocked_clients */ 
 145 #define REDIS_LUA_CLIENT 512 /* This is a non connected client used by Lua */ 
 146 #define REDIS_ASKING 1024   /* Client issued the ASKING command */ 
 147 #define REDIS_CLOSE_ASAP 2048 /* Close this client ASAP */ 
 149 /* Client request types */ 
 150 #define REDIS_REQ_INLINE 1 
 151 #define REDIS_REQ_MULTIBULK 2 
 153 /* Client classes for client limits, currently used only for 
 154  * the max-client-output-buffer limit implementation. */ 
 155 #define REDIS_CLIENT_LIMIT_CLASS_NORMAL 0 
 156 #define REDIS_CLIENT_LIMIT_CLASS_SLAVE 1 
 157 #define REDIS_CLIENT_LIMIT_CLASS_PUBSUB 2 
 158 #define REDIS_CLIENT_LIMIT_NUM_CLASSES 3 
 160 /* Slave replication state - slave side */ 
 161 #define REDIS_REPL_NONE 0 /* No active replication */ 
 162 #define REDIS_REPL_CONNECT 1 /* Must connect to master */ 
 163 #define REDIS_REPL_CONNECTING 2 /* Connecting to master */ 
 164 #define REDIS_REPL_TRANSFER 3 /* Receiving .rdb from master */ 
 165 #define REDIS_REPL_CONNECTED 4 /* Connected to master */ 
 167 /* Synchronous read timeout - slave side */ 
 168 #define REDIS_REPL_SYNCIO_TIMEOUT 5 
 170 /* Slave replication state - from the point of view of master 
 171  * Note that in SEND_BULK and ONLINE state the slave receives new updates 
 172  * in its output queue. In the WAIT_BGSAVE state instead the server is waiting 
 173  * to start the next background saving in order to send updates to it. */ 
 174 #define REDIS_REPL_WAIT_BGSAVE_START 3 /* master waits bgsave to start feeding it */ 
 175 #define REDIS_REPL_WAIT_BGSAVE_END 4 /* master waits bgsave to start bulk DB transmission */ 
 176 #define REDIS_REPL_SEND_BULK 5 /* master is sending the bulk DB */ 
 177 #define REDIS_REPL_ONLINE 6 /* bulk DB already transmitted, receive updates */ 
 179 /* List related stuff */ 
 183 /* Sort operations */ 
 184 #define REDIS_SORT_GET 0 
 185 #define REDIS_SORT_ASC 1 
 186 #define REDIS_SORT_DESC 2 
 187 #define REDIS_SORTKEY_MAX 1024 
 190 #define REDIS_DEBUG 0 
 191 #define REDIS_VERBOSE 1 
 192 #define REDIS_NOTICE 2 
 193 #define REDIS_WARNING 3 
 194 #define REDIS_LOG_RAW (1<<10) /* Modifier to log without timestamp */ 
 196 /* Anti-warning macro... */ 
 197 #define REDIS_NOTUSED(V) ((void) V) 
 199 #define ZSKIPLIST_MAXLEVEL 32 /* Should be enough for 2^32 elements */ 
 200 #define ZSKIPLIST_P 0.25      /* Skiplist P = 1/4 */ 
 202 /* Append only defines */ 
 203 #define AOF_FSYNC_NO 0 
 204 #define AOF_FSYNC_ALWAYS 1 
 205 #define AOF_FSYNC_EVERYSEC 2 
 207 /* Zip structure related defaults */ 
 208 #define REDIS_HASH_MAX_ZIPMAP_ENTRIES 512 
 209 #define REDIS_HASH_MAX_ZIPMAP_VALUE 64 
 210 #define REDIS_LIST_MAX_ZIPLIST_ENTRIES 512 
 211 #define REDIS_LIST_MAX_ZIPLIST_VALUE 64 
 212 #define REDIS_SET_MAX_INTSET_ENTRIES 512 
 213 #define REDIS_ZSET_MAX_ZIPLIST_ENTRIES 128 
 214 #define REDIS_ZSET_MAX_ZIPLIST_VALUE 64 
 216 /* Sets operations codes */ 
 217 #define REDIS_OP_UNION 0 
 218 #define REDIS_OP_DIFF 1 
 219 #define REDIS_OP_INTER 2 
 221 /* Redis maxmemory strategies */ 
 222 #define REDIS_MAXMEMORY_VOLATILE_LRU 0 
 223 #define REDIS_MAXMEMORY_VOLATILE_TTL 1 
 224 #define REDIS_MAXMEMORY_VOLATILE_RANDOM 2 
 225 #define REDIS_MAXMEMORY_ALLKEYS_LRU 3 
 226 #define REDIS_MAXMEMORY_ALLKEYS_RANDOM 4 
 227 #define REDIS_MAXMEMORY_NO_EVICTION 5 
 230 #define REDIS_LUA_TIME_LIMIT 5000 /* milliseconds */ 
 233 #define UNIT_SECONDS 0 
 234 #define UNIT_MILLISECONDS 1 
 237 #define REDIS_SHUTDOWN_SAVE 1       /* Force SAVE on SHUTDOWN even if no save 
 238                                        points are configured. */ 
 239 #define REDIS_SHUTDOWN_NOSAVE 2     /* Don't SAVE on SHUTDOWN. */ 
 241 /* Command call flags, see call() function */ 
 242 #define REDIS_CALL_NONE 0 
 243 #define REDIS_CALL_SLOWLOG 1 
 244 #define REDIS_CALL_STATS 2 
 245 #define REDIS_CALL_PROPAGATE 4 
 246 #define REDIS_CALL_FULL (REDIS_CALL_SLOWLOG | REDIS_CALL_STATS | REDIS_CALL_PROPAGATE) 
 248 /* Command propagation flags, see propagate() function */ 
 249 #define REDIS_PROPAGATE_NONE 0 
 250 #define REDIS_PROPAGATE_AOF 1 
 251 #define REDIS_PROPAGATE_REPL 2 
 253 /* We can print the stacktrace, so our assert is defined this way: */ 
 254 #define redisAssertWithInfo(_c,_o,_e) ((_e)?(void)0 : (_redisAssertWithInfo(_c,_o,#_e,__FILE__,__LINE__),_exit(1))) 
 255 #define redisAssert(_e) ((_e)?(void)0 : (_redisAssert(#_e,__FILE__,__LINE__),_exit(1))) 
 256 #define redisPanic(_e) _redisPanic(#_e,__FILE__,__LINE__),_exit(1) 
 258 /*----------------------------------------------------------------------------- 
 260  *----------------------------------------------------------------------------*/ 
 262 /* A redis object, that is a type able to hold a string / list / set */ 
 264 /* The actual Redis Object */ 
 265 #define REDIS_LRU_CLOCK_MAX ((1<<21)-1) /* Max value of obj->lru */ 
 266 #define REDIS_LRU_CLOCK_RESOLUTION 10 /* LRU clock resolution in seconds */ 
 267 typedef struct redisObject 
{ 
 269     unsigned notused
:2;     /* Not used */ 
 271     unsigned lru
:22;        /* lru time (relative to server.lruclock) */ 
 276 /* Macro used to initalize a Redis object allocated on the stack. 
 277  * Note that this macro is taken near the structure definition to make sure 
 278  * we'll update it when the structure is changed, to avoid bugs like 
 279  * bug #85 introduced exactly in this way. */ 
 280 #define initStaticStringObject(_var,_ptr) do { \ 
 282     _var.type = REDIS_STRING; \ 
 283     _var.encoding = REDIS_ENCODING_RAW; \ 
 287 typedef struct redisDb 
{ 
 288     dict 
*dict
;                 /* The keyspace for this DB */ 
 289     dict 
*expires
;              /* Timeout of keys with a timeout set */ 
 290     dict 
*blocking_keys
;        /* Keys with clients waiting for data (BLPOP) */ 
 291     dict 
*watched_keys
;         /* WATCHED keys for MULTI/EXEC CAS */ 
 295 /* Client MULTI/EXEC state */ 
 296 typedef struct multiCmd 
{ 
 299     struct redisCommand 
*cmd
; 
 302 typedef struct multiState 
{ 
 303     multiCmd 
*commands
;     /* Array of MULTI commands */ 
 304     int count
;              /* Total number of MULTI commands */ 
 307 typedef struct blockingState 
{ 
 308     robj 
**keys
;            /* The key we are waiting to terminate a blocking 
 309                              * operation such as BLPOP. Otherwise NULL. */ 
 310     int count
;              /* Number of blocking keys */ 
 311     time_t timeout
;         /* Blocking operation timeout. If UNIX current time 
 312                              * is >= timeout then the operation timed out. */ 
 313     robj 
*target
;           /* The key that should receive the element, 
 317 /* With multiplexing we need to take per-clinet state. 
 318  * Clients are taken in a liked list. */ 
 319 typedef struct redisClient 
{ 
 326     struct redisCommand 
*cmd
, *lastcmd
; 
 328     int multibulklen
;       /* number of multi bulk arguments left to read */ 
 329     long bulklen
;           /* length of bulk argument in multi bulk request */ 
 331     unsigned long reply_bytes
; /* Tot bytes of objects in reply list */ 
 333     time_t lastinteraction
; /* time of the last interaction, used for timeout */ 
 334     time_t obuf_soft_limit_reached_time
; 
 335     int flags
;              /* REDIS_SLAVE | REDIS_MONITOR | REDIS_MULTI ... */ 
 336     int slaveseldb
;         /* slave selected db, if this client is a slave */ 
 337     int authenticated
;      /* when requirepass is non-NULL */ 
 338     int replstate
;          /* replication state if this is a slave */ 
 339     int repldbfd
;           /* replication DB file descriptor */ 
 340     long repldboff
;         /* replication DB file offset */ 
 341     off_t repldbsize
;       /* replication DB file size */ 
 342     multiState mstate
;      /* MULTI/EXEC state */ 
 343     blockingState bpop
;   /* blocking state */ 
 344     list 
*io_keys
;          /* Keys this client is waiting to be loaded from the 
 345                              * swap file in order to continue. */ 
 346     list 
*watched_keys
;     /* Keys WATCHED for MULTI/EXEC CAS */ 
 347     dict 
*pubsub_channels
;  /* channels a client is interested in (SUBSCRIBE) */ 
 348     list 
*pubsub_patterns
;  /* patterns a client is interested in (SUBSCRIBE) */ 
 350     /* Response buffer */ 
 352     char buf
[REDIS_REPLY_CHUNK_BYTES
]; 
 360 struct sharedObjectsStruct 
{ 
 361     robj 
*crlf
, *ok
, *err
, *emptybulk
, *czero
, *cone
, *cnegone
, *pong
, *space
, 
 362     *colon
, *nullbulk
, *nullmultibulk
, *queued
, 
 363     *emptymultibulk
, *wrongtypeerr
, *nokeyerr
, *syntaxerr
, *sameobjecterr
, 
 364     *outofrangeerr
, *noscripterr
, *loadingerr
, *slowscripterr
, *plus
, 
 365     *select0
, *select1
, *select2
, *select3
, *select4
, 
 366     *select5
, *select6
, *select7
, *select8
, *select9
, 
 367     *messagebulk
, *pmessagebulk
, *subscribebulk
, *unsubscribebulk
, 
 368     *psubscribebulk
, *punsubscribebulk
, *del
, 
 369     *integers
[REDIS_SHARED_INTEGERS
], 
 370     *mbulkhdr
[REDIS_SHARED_BULKHDR_LEN
], /* "*<value>\r\n" */ 
 371     *bulkhdr
[REDIS_SHARED_BULKHDR_LEN
];  /* "$<value>\r\n" */ 
 374 /* ZSETs use a specialized version of Skiplists */ 
 375 typedef struct zskiplistNode 
{ 
 378     struct zskiplistNode 
*backward
; 
 379     struct zskiplistLevel 
{ 
 380         struct zskiplistNode 
*forward
; 
 385 typedef struct zskiplist 
{ 
 386     struct zskiplistNode 
*header
, *tail
; 
 387     unsigned long length
; 
 391 typedef struct zset 
{ 
 396 typedef struct clientBufferLimitsConfig 
{ 
 397     unsigned long long hard_limit_bytes
; 
 398     unsigned long long soft_limit_bytes
; 
 399     time_t soft_limit_seconds
; 
 400 } clientBufferLimitsConfig
; 
 402 /* Currently only used to additionally propagate more commands to AOF/Replication 
 403  * after the propagation of the executed command. 
 404  * The structure contains everything needed to propagate a command: 
 405  * argv and argc, the ID of the database, pointer to the command table entry, 
 406  * and finally the target, that is an xor between REDIS_PROPAGATE_* flags. */ 
 407 typedef struct propagatedItem 
{ 
 409     int argc
, dbid
, target
; 
 410     struct redisCommand 
*cmd
; 
 413 /*----------------------------------------------------------------------------- 
 414  * Redis cluster data structures 
 415  *----------------------------------------------------------------------------*/ 
 417 #define REDIS_CLUSTER_SLOTS 4096 
 418 #define REDIS_CLUSTER_OK 0          /* Everything looks ok */ 
 419 #define REDIS_CLUSTER_FAIL 1        /* The cluster can't work */ 
 420 #define REDIS_CLUSTER_NEEDHELP 2    /* The cluster works, but needs some help */ 
 421 #define REDIS_CLUSTER_NAMELEN 40    /* sha1 hex length */ 
 422 #define REDIS_CLUSTER_PORT_INCR 10000 /* Cluster port = baseport + PORT_INCR */ 
 426 /* clusterLink encapsulates everything needed to talk with a remote node. */ 
 427 typedef struct clusterLink 
{ 
 428     int fd
;                     /* TCP socket file descriptor */ 
 429     sds sndbuf
;                 /* Packet send buffer */ 
 430     sds rcvbuf
;                 /* Packet reception buffer */ 
 431     struct clusterNode 
*node
;   /* Node related to this link if any, or NULL */ 
 435 #define REDIS_NODE_MASTER 1     /* The node is a master */ 
 436 #define REDIS_NODE_SLAVE 2      /* The node is a slave */ 
 437 #define REDIS_NODE_PFAIL 4      /* Failure? Need acknowledge */ 
 438 #define REDIS_NODE_FAIL 8       /* The node is believed to be malfunctioning */ 
 439 #define REDIS_NODE_MYSELF 16    /* This node is myself */ 
 440 #define REDIS_NODE_HANDSHAKE 32 /* We have still to exchange the first ping */ 
 441 #define REDIS_NODE_NOADDR   64  /* We don't know the address of this node */ 
 442 #define REDIS_NODE_MEET 128     /* Send a MEET message to this node */ 
 443 #define REDIS_NODE_NULL_NAME "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000" 
 446     char name
[REDIS_CLUSTER_NAMELEN
]; /* Node name, hex string, sha1-size */ 
 447     int flags
;      /* REDIS_NODE_... */ 
 448     unsigned char slots
[REDIS_CLUSTER_SLOTS
/8]; /* slots handled by this node */ 
 449     int numslaves
;  /* Number of slave nodes, if this is a master */ 
 450     struct clusterNode 
**slaves
; /* pointers to slave nodes */ 
 451     struct clusterNode 
*slaveof
; /* pointer to the master node */ 
 452     time_t ping_sent
;       /* Unix time we sent latest ping */ 
 453     time_t pong_received
;   /* Unix time we received the pong */ 
 454     char *configdigest
;         /* Configuration digest of this node */ 
 455     time_t configdigest_ts
;     /* Configuration digest timestamp */ 
 456     char ip
[16];                /* Latest known IP address of this node */ 
 457     int port
;                   /* Latest known port of this node */ 
 458     clusterLink 
*link
;          /* TCP/IP link with this node */ 
 460 typedef struct clusterNode clusterNode
; 
 464     clusterNode 
*myself
;  /* This node */ 
 465     int state
;            /* REDIS_CLUSTER_OK, REDIS_CLUSTER_FAIL, ... */ 
 467     dict 
*nodes
;          /* Hash table of name -> clusterNode structures */ 
 468     clusterNode 
*migrating_slots_to
[REDIS_CLUSTER_SLOTS
]; 
 469     clusterNode 
*importing_slots_from
[REDIS_CLUSTER_SLOTS
]; 
 470     clusterNode 
*slots
[REDIS_CLUSTER_SLOTS
]; 
 471     zskiplist 
*slots_to_keys
; 
 474 /* Redis cluster messages header */ 
 476 /* Note that the PING, PONG and MEET messages are actually the same exact 
 477  * kind of packet. PONG is the reply to ping, in the extact format as a PING, 
 478  * while MEET is a special PING that forces the receiver to add the sender 
 479  * as a node (if it is not already in the list). */ 
 480 #define CLUSTERMSG_TYPE_PING 0          /* Ping */ 
 481 #define CLUSTERMSG_TYPE_PONG 1          /* Pong (reply to Ping) */ 
 482 #define CLUSTERMSG_TYPE_MEET 2          /* Meet "let's join" message */ 
 483 #define CLUSTERMSG_TYPE_FAIL 3          /* Mark node xxx as failing */ 
 484 #define CLUSTERMSG_TYPE_PUBLISH 4       /* Pub/Sub Publish propatagion */ 
 486 /* Initially we don't know our "name", but we'll find it once we connect 
 487  * to the first node, using the getsockname() function. Then we'll use this 
 488  * address for all the next messages. */ 
 490     char nodename
[REDIS_CLUSTER_NAMELEN
]; 
 492     uint32_t pong_received
; 
 493     char ip
[16];    /* IP address last time it was seen */ 
 494     uint16_t port
;  /* port last time it was seen */ 
 496     uint32_t notused
; /* for 64 bit alignment */ 
 497 } clusterMsgDataGossip
; 
 500     char nodename
[REDIS_CLUSTER_NAMELEN
]; 
 501 } clusterMsgDataFail
; 
 504     uint32_t channel_len
; 
 505     uint32_t message_len
; 
 506     unsigned char bulk_data
[8]; /* defined as 8 just for alignment concerns. */ 
 507 } clusterMsgDataPublish
; 
 509 union clusterMsgData 
{ 
 510     /* PING, MEET and PONG */ 
 512         /* Array of N clusterMsgDataGossip structures */ 
 513         clusterMsgDataGossip gossip
[1]; 
 518         clusterMsgDataFail about
; 
 523         clusterMsgDataPublish msg
; 
 528     uint32_t totlen
;    /* Total length of this message */ 
 529     uint16_t type
;      /* Message type */ 
 530     uint16_t count
;     /* Only used for some kind of messages. */ 
 531     char sender
[REDIS_CLUSTER_NAMELEN
]; /* Name of the sender node */ 
 532     unsigned char myslots
[REDIS_CLUSTER_SLOTS
/8]; 
 533     char slaveof
[REDIS_CLUSTER_NAMELEN
]; 
 534     char configdigest
[32]; 
 535     uint16_t port
;      /* Sender TCP base port */ 
 536     unsigned char state
; /* Cluster state from the POV of the sender */ 
 537     unsigned char notused
[5]; /* Reserved for future use. For alignment. */ 
 538     union clusterMsgData data
; 
 541 /*----------------------------------------------------------------------------- 
 542  * Global server state 
 543  *----------------------------------------------------------------------------*/ 
 548     dict 
*commands
;             /* Command table hahs table */ 
 550     unsigned lruclock
:22;       /* Clock incrementing every minute, for LRU */ 
 551     unsigned lruclock_padding
:10; 
 552     int shutdown_asap
;          /* SHUTDOWN needed ASAP */ 
 553     int activerehashing
;        /* Incremental rehash in serverCron() */ 
 554     char *requirepass
;          /* Pass for AUTH command, or NULL */ 
 555     char *pidfile
;              /* PID file path */ 
 556     int arch_bits
;              /* 32 or 64 depending on sizeof(long) */ 
 558     int port
;                   /* TCP listening port */ 
 559     char *bindaddr
;             /* Bind address or NULL */ 
 560     char *unixsocket
;           /* UNIX socket path */ 
 561     mode_t unixsocketperm
;      /* UNIX socket permission */ 
 562     int ipfd
;                   /* TCP socket file descriptor */ 
 563     int sofd
;                   /* Unix socket file descriptor */ 
 564     int cfd
;                    /* Cluster bus lisetning socket */ 
 565     list 
*clients
;              /* List of active clients */ 
 566     list 
*clients_to_close
;     /* Clients to close asynchronously */ 
 567     list 
*slaves
, *monitors
;    /* List of slaves and MONITORs */ 
 568     redisClient 
*current_client
; /* Current client, only used on crash report */ 
 569     char neterr
[ANET_ERR_LEN
];  /* Error buffer for anet.c */ 
 570     /* RDB / AOF loading information */ 
 571     int loading
;                /* We are loading data from disk if true */ 
 572     off_t loading_total_bytes
; 
 573     off_t loading_loaded_bytes
; 
 574     time_t loading_start_time
; 
 575     /* Fast pointers to often looked up command */ 
 576     struct redisCommand 
*delCommand
, *multiCommand
, *lpushCommand
; 
 577     int cronloops
;                  /* Number of times the cron function run */ 
 578     time_t lastsave
;                /* Unix time of last save succeeede */ 
 579     /* Fields used only for stats */ 
 580     time_t stat_starttime
;          /* Server start time */ 
 581     long long stat_numcommands
;     /* Number of processed commands */ 
 582     long long stat_numconnections
;  /* Number of connections received */ 
 583     long long stat_expiredkeys
;     /* Number of expired keys */ 
 584     long long stat_evictedkeys
;     /* Number of evicted keys (maxmemory) */ 
 585     long long stat_keyspace_hits
;   /* Number of successful lookups of keys */ 
 586     long long stat_keyspace_misses
; /* Number of failed lookups of keys */ 
 587     size_t stat_peak_memory
;        /* Max used memory record */ 
 588     long long stat_fork_time
;       /* Time needed to perform latets fork() */ 
 589     long long stat_rejected_conn
;   /* Clients rejected because of maxclients */ 
 590     list 
*slowlog
;                  /* SLOWLOG list of commands */ 
 591     long long slowlog_entry_id
;     /* SLOWLOG current entry ID */ 
 592     long long slowlog_log_slower_than
; /* SLOWLOG time limit (to get logged) */ 
 593     unsigned long slowlog_max_len
;     /* SLOWLOG max number of items logged */ 
 595     int verbosity
;                  /* Loglevel in redis.conf */ 
 596     int maxidletime
;                /* Client timeout in seconds */ 
 597     size_t client_max_querybuf_len
; /* Limit for client query buffer length */ 
 598     int dbnum
;                      /* Total number of configured DBs */ 
 599     int daemonize
;                  /* True if running as a daemon */ 
 600     clientBufferLimitsConfig client_obuf_limits
[REDIS_CLIENT_LIMIT_NUM_CLASSES
]; 
 601     /* AOF persistence */ 
 602     int aof_state
;                  /* REDIS_AOF_(ON|OFF|WAIT_REWRITE) */ 
 603     int aof_fsync
;                  /* Kind of fsync() policy */ 
 604     char *aof_filename
;             /* Name of the AOF file */ 
 605     int aof_no_fsync_on_rewrite
;    /* Don't fsync if a rewrite is in prog. */ 
 606     int aof_rewrite_perc
;           /* Rewrite AOF if % growth is > M and... */ 
 607     off_t aof_rewrite_min_size
;     /* the AOF file is at least N bytes. */ 
 608     off_t aof_rewrite_base_size
;    /* AOF size on latest startup or rewrite. */ 
 609     off_t aof_current_size
;         /* AOF current size. */ 
 610     int aof_rewrite_scheduled
;      /* Rewrite once BGSAVE terminates. */ 
 611     pid_t aof_child_pid
;            /* PID if rewriting process */ 
 612     sds aof_rewrite_buf
; /* buffer taken by parent during oppend only rewrite */ 
 613     sds aof_buf
;      /* AOF buffer, written before entering the event loop */ 
 614     int aof_fd
;       /* File descriptor of currently selected AOF file */ 
 615     int aof_selected_db
; /* Currently selected DB in AOF */ 
 616     time_t aof_flush_postponed_start
; /* UNIX time of postponed AOF flush */ 
 617     time_t aof_last_fsync
;            /* UNIX time of last fsync() */ 
 618     /* RDB persistence */ 
 619     long long dirty
;                /* Changes to DB from the last save */ 
 620     long long dirty_before_bgsave
;  /* Used to restore dirty on failed BGSAVE */ 
 621     pid_t rdb_child_pid
;            /* PID of RDB saving child */ 
 622     struct saveparam 
*saveparams
;   /* Save points array for RDB */ 
 623     int saveparamslen
;              /* Number of saving points */ 
 624     char *rdb_filename
;             /* Name of RDB file */ 
 625     int rdb_compression
;            /* Use compression in RDB? */ 
 626     /* Propagation of commands in AOF / replication */ 
 627     propagatedItem also_propagate
;  /* Additional command to propagate. */ 
 629     char *logfile
;                  /* Path of log file */ 
 630     int syslog_enabled
;             /* Is syslog enabled? */ 
 631     char *syslog_ident
;             /* Syslog ident */ 
 632     int syslog_facility
;            /* Syslog facility */ 
 633     /* Slave specific fields */ 
 634     char *masterauth
;               /* AUTH with this password with master */ 
 635     char *masterhost
;               /* Hostname of master */ 
 636     int masterport
;                 /* Port of master */ 
 637     int repl_ping_slave_period
;     /* Master pings the salve every N seconds */ 
 638     int repl_timeout
;               /* Timeout after N seconds of master idle */ 
 639     redisClient 
*master
;     /* Client that is master for this slave */ 
 640     int repl_syncio_timeout
; /* Timeout for synchronous I/O calls */ 
 641     int repl_state
;          /* Replication status if the instance is a slave */ 
 642     off_t repl_transfer_left
;  /* Bytes left reading .rdb  */ 
 643     int repl_transfer_s
;     /* Slave -> Master SYNC socket */ 
 644     int repl_transfer_fd
;    /* Slave -> Master SYNC temp file descriptor */ 
 645     char *repl_transfer_tmpfile
; /* Slave-> master SYNC temp file name */ 
 646     time_t repl_transfer_lastio
; /* Unix time of the latest read, for timeout */ 
 647     int repl_serve_stale_data
; /* Serve stale data when link is down? */ 
 648     time_t repl_down_since
; /* Unix time at which link with master went down */ 
 650     unsigned int maxclients
;        /* Max number of simultaneous clients */ 
 651     unsigned long long maxmemory
;   /* Max number of memory bytes to use */ 
 652     int maxmemory_policy
;           /* Policy for key evition */ 
 653     int maxmemory_samples
;          /* Pricision of random sampling */ 
 654     /* Blocked clients */ 
 655     unsigned int bpop_blocked_clients
; /* Number of clients blocked by lists */ 
 656     list 
*unblocked_clients
; /* list of clients to unblock before next loop */ 
 657     /* Sort parameters - qsort_r() is only available under BSD so we 
 658      * have to take this state global, in order to pass it to sortCompare() */ 
 663     /* Zip structure config, see redis.conf for more information  */ 
 664     size_t hash_max_zipmap_entries
; 
 665     size_t hash_max_zipmap_value
; 
 666     size_t list_max_ziplist_entries
; 
 667     size_t list_max_ziplist_value
; 
 668     size_t set_max_intset_entries
; 
 669     size_t zset_max_ziplist_entries
; 
 670     size_t zset_max_ziplist_value
; 
 671     time_t unixtime
;        /* Unix time sampled every second. */ 
 673     dict 
*pubsub_channels
;  /* Map channels to list of subscribed clients */ 
 674     list 
*pubsub_patterns
;  /* A list of pubsub_patterns */ 
 676     int cluster_enabled
;    /* Is cluster enabled? */ 
 677     clusterState cluster
;   /* State of the cluster */ 
 679     lua_State 
*lua
; /* The Lua interpreter. We use just one for all clients */ 
 680     redisClient 
*lua_client
;   /* The "fake client" to query Redis from Lua */ 
 681     redisClient 
*lua_caller
;   /* The client running EVAL right now, or NULL */ 
 682     dict 
*lua_scripts
;         /* A dictionary of SHA1 -> Lua scripts */ 
 683     long long lua_time_limit
;  /* Script timeout in seconds */ 
 684     long long lua_time_start
;  /* Start time of script */ 
 685     int lua_write_dirty
;  /* True if a write command was called during the 
 686                              execution of the current script. */ 
 687     int lua_random_dirty
; /* True if a random command was called during the 
 688                              execution of the current script. */ 
 689     int lua_timedout
;     /* True if we reached the time limit for script 
 691     int lua_kill
;         /* Kill the script if true. */ 
 692     /* Assert & bug reportign */ 
 696     int bug_report_start
; /* True if bug report header was already logged. */ 
 699 typedef struct pubsubPattern 
{ 
 704 typedef void redisCommandProc(redisClient 
*c
); 
 705 typedef int *redisGetKeysProc(struct redisCommand 
*cmd
, robj 
**argv
, int argc
, int *numkeys
, int flags
); 
 706 struct redisCommand 
{ 
 708     redisCommandProc 
*proc
; 
 710     char *sflags
; /* Flags as string represenation, one char per flag. */ 
 711     int flags
;    /* The actual flags, obtained from the 'sflags' field. */ 
 712     /* Use a function to determine keys arguments in a command line. 
 713      * Used for Redis Cluster redirect. */ 
 714     redisGetKeysProc 
*getkeys_proc
; 
 715     /* What keys should be loaded in background when calling this command? */ 
 716     int firstkey
; /* The first argument that's a key (0 = no keys) */ 
 717     int lastkey
;  /* THe last argument that's a key */ 
 718     int keystep
;  /* The step between first and last key */ 
 719     long long microseconds
, calls
; 
 722 struct redisFunctionSym 
{ 
 724     unsigned long pointer
; 
 727 typedef struct _redisSortObject 
{ 
 735 typedef struct _redisSortOperation 
{ 
 738 } redisSortOperation
; 
 740 /* Structure to hold list iteration abstraction. */ 
 743     unsigned char encoding
; 
 744     unsigned char direction
; /* Iteration direction */ 
 749 /* Structure for an entry while iterating over a list. */ 
 751     listTypeIterator 
*li
; 
 752     unsigned char *zi
;  /* Entry in ziplist */ 
 753     listNode 
*ln
;       /* Entry in linked list */ 
 756 /* Structure to hold set iteration abstraction. */ 
 760     int ii
; /* intset iterator */ 
 764 /* Structure to hold hash iteration abstration. Note that iteration over 
 765  * hashes involves both fields and values. Because it is possible that 
 766  * not both are required, store pointers in the iterator to avoid 
 767  * unnecessary memory allocation for fields/values. */ 
 771     unsigned char *zk
, *zv
; 
 772     unsigned int zklen
, zvlen
; 
 778 #define REDIS_HASH_KEY 1 
 779 #define REDIS_HASH_VALUE 2 
 781 /*----------------------------------------------------------------------------- 
 782  * Extern declarations 
 783  *----------------------------------------------------------------------------*/ 
 785 extern struct redisServer server
; 
 786 extern struct sharedObjectsStruct shared
; 
 787 extern dictType setDictType
; 
 788 extern dictType zsetDictType
; 
 789 extern dictType clusterNodesDictType
; 
 790 extern dictType dbDictType
; 
 791 extern double R_Zero
, R_PosInf
, R_NegInf
, R_Nan
; 
 792 dictType hashDictType
; 
 794 /*----------------------------------------------------------------------------- 
 795  * Functions prototypes 
 796  *----------------------------------------------------------------------------*/ 
 799 long long ustime(void); 
 800 long long mstime(void); 
 802 /* networking.c -- Networking and Client related operations */ 
 803 redisClient 
*createClient(int fd
); 
 804 void closeTimedoutClients(void); 
 805 void freeClient(redisClient 
*c
); 
 806 void resetClient(redisClient 
*c
); 
 807 void sendReplyToClient(aeEventLoop 
*el
, int fd
, void *privdata
, int mask
); 
 808 void addReply(redisClient 
*c
, robj 
*obj
); 
 809 void *addDeferredMultiBulkLength(redisClient 
*c
); 
 810 void setDeferredMultiBulkLength(redisClient 
*c
, void *node
, long length
); 
 811 void addReplySds(redisClient 
*c
, sds s
); 
 812 void processInputBuffer(redisClient 
*c
); 
 813 void acceptTcpHandler(aeEventLoop 
*el
, int fd
, void *privdata
, int mask
); 
 814 void acceptUnixHandler(aeEventLoop 
*el
, int fd
, void *privdata
, int mask
); 
 815 void readQueryFromClient(aeEventLoop 
*el
, int fd
, void *privdata
, int mask
); 
 816 void addReplyBulk(redisClient 
*c
, robj 
*obj
); 
 817 void addReplyBulkCString(redisClient 
*c
, char *s
); 
 818 void addReplyBulkCBuffer(redisClient 
*c
, void *p
, size_t len
); 
 819 void addReplyBulkLongLong(redisClient 
*c
, long long ll
); 
 820 void acceptHandler(aeEventLoop 
*el
, int fd
, void *privdata
, int mask
); 
 821 void addReply(redisClient 
*c
, robj 
*obj
); 
 822 void addReplySds(redisClient 
*c
, sds s
); 
 823 void addReplyError(redisClient 
*c
, char *err
); 
 824 void addReplyStatus(redisClient 
*c
, char *status
); 
 825 void addReplyDouble(redisClient 
*c
, double d
); 
 826 void addReplyLongLong(redisClient 
*c
, long long ll
); 
 827 void addReplyMultiBulkLen(redisClient 
*c
, long length
); 
 828 void copyClientOutputBuffer(redisClient 
*dst
, redisClient 
*src
); 
 829 void *dupClientReplyValue(void *o
); 
 830 void getClientsMaxBuffers(unsigned long *longest_output_list
, 
 831                           unsigned long *biggest_input_buffer
); 
 832 sds 
getClientInfoString(redisClient 
*client
); 
 833 sds 
getAllClientsInfoString(void); 
 834 void rewriteClientCommandVector(redisClient 
*c
, int argc
, ...); 
 835 void rewriteClientCommandArgument(redisClient 
*c
, int i
, robj 
*newval
); 
 836 unsigned long getClientOutputBufferMemoryUsage(redisClient 
*c
); 
 837 void freeClientsInAsyncFreeQueue(void); 
 838 void asyncCloseClientOnOutputBufferLimitReached(redisClient 
*c
); 
 839 int getClientLimitClassByName(char *name
); 
 840 char *getClientLimitClassName(int class); 
 841 void flushSlavesOutputBuffers(void); 
 844 void addReplyErrorFormat(redisClient 
*c
, const char *fmt
, ...) 
 845     __attribute__((format(printf
, 2, 3))); 
 846 void addReplyStatusFormat(redisClient 
*c
, const char *fmt
, ...) 
 847     __attribute__((format(printf
, 2, 3))); 
 849 void addReplyErrorFormat(redisClient 
*c
, const char *fmt
, ...); 
 850 void addReplyStatusFormat(redisClient 
*c
, const char *fmt
, ...); 
 854 void listTypeTryConversion(robj 
*subject
, robj 
*value
); 
 855 void listTypePush(robj 
*subject
, robj 
*value
, int where
); 
 856 robj 
*listTypePop(robj 
*subject
, int where
); 
 857 unsigned long listTypeLength(robj 
*subject
); 
 858 listTypeIterator 
*listTypeInitIterator(robj 
*subject
, long index
, unsigned char direction
); 
 859 void listTypeReleaseIterator(listTypeIterator 
*li
); 
 860 int listTypeNext(listTypeIterator 
*li
, listTypeEntry 
*entry
); 
 861 robj 
*listTypeGet(listTypeEntry 
*entry
); 
 862 void listTypeInsert(listTypeEntry 
*entry
, robj 
*value
, int where
); 
 863 int listTypeEqual(listTypeEntry 
*entry
, robj 
*o
); 
 864 void listTypeDelete(listTypeEntry 
*entry
); 
 865 void listTypeConvert(robj 
*subject
, int enc
); 
 866 void unblockClientWaitingData(redisClient 
*c
); 
 867 int handleClientsWaitingListPush(redisClient 
*c
, robj 
*key
, robj 
*ele
); 
 868 void popGenericCommand(redisClient 
*c
, int where
); 
 870 /* MULTI/EXEC/WATCH... */ 
 871 void unwatchAllKeys(redisClient 
*c
); 
 872 void initClientMultiState(redisClient 
*c
); 
 873 void freeClientMultiState(redisClient 
*c
); 
 874 void queueMultiCommand(redisClient 
*c
); 
 875 void touchWatchedKey(redisDb 
*db
, robj 
*key
); 
 876 void touchWatchedKeysOnFlush(int dbid
); 
 878 /* Redis object implementation */ 
 879 void decrRefCount(void *o
); 
 880 void incrRefCount(robj 
*o
); 
 881 robj 
*resetRefCount(robj 
*obj
); 
 882 void freeStringObject(robj 
*o
); 
 883 void freeListObject(robj 
*o
); 
 884 void freeSetObject(robj 
*o
); 
 885 void freeZsetObject(robj 
*o
); 
 886 void freeHashObject(robj 
*o
); 
 887 robj 
*createObject(int type
, void *ptr
); 
 888 robj 
*createStringObject(char *ptr
, size_t len
); 
 889 robj 
*dupStringObject(robj 
*o
); 
 890 int isObjectRepresentableAsLongLong(robj 
*o
, long long *llongval
); 
 891 robj 
*tryObjectEncoding(robj 
*o
); 
 892 robj 
*getDecodedObject(robj 
*o
); 
 893 size_t stringObjectLen(robj 
*o
); 
 894 robj 
*createStringObjectFromLongLong(long long value
); 
 895 robj 
*createStringObjectFromLongDouble(long double value
); 
 896 robj 
*createListObject(void); 
 897 robj 
*createZiplistObject(void); 
 898 robj 
*createSetObject(void); 
 899 robj 
*createIntsetObject(void); 
 900 robj 
*createHashObject(void); 
 901 robj 
*createZsetObject(void); 
 902 robj 
*createZsetZiplistObject(void); 
 903 int getLongFromObjectOrReply(redisClient 
*c
, robj 
*o
, long *target
, const char *msg
); 
 904 int checkType(redisClient 
*c
, robj 
*o
, int type
); 
 905 int getLongLongFromObjectOrReply(redisClient 
*c
, robj 
*o
, long long *target
, const char *msg
); 
 906 int getDoubleFromObjectOrReply(redisClient 
*c
, robj 
*o
, double *target
, const char *msg
); 
 907 int getLongLongFromObject(robj 
*o
, long long *target
); 
 908 int getLongDoubleFromObject(robj 
*o
, long double *target
); 
 909 int getLongDoubleFromObjectOrReply(redisClient 
*c
, robj 
*o
, long double *target
, const char *msg
); 
 910 char *strEncoding(int encoding
); 
 911 int compareStringObjects(robj 
*a
, robj 
*b
); 
 912 int equalStringObjects(robj 
*a
, robj 
*b
); 
 913 unsigned long estimateObjectIdleTime(robj 
*o
); 
 915 /* Synchronous I/O with timeout */ 
 916 int syncWrite(int fd
, char *ptr
, ssize_t size
, int timeout
); 
 917 int syncRead(int fd
, char *ptr
, ssize_t size
, int timeout
); 
 918 int syncReadLine(int fd
, char *ptr
, ssize_t size
, int timeout
); 
 921 void replicationFeedSlaves(list 
*slaves
, int dictid
, robj 
**argv
, int argc
); 
 922 void replicationFeedMonitors(list 
*monitors
, int dictid
, robj 
**argv
, int argc
); 
 923 void updateSlavesWaitingBgsave(int bgsaveerr
); 
 924 void replicationCron(void); 
 926 /* Generic persistence functions */ 
 927 void startLoading(FILE *fp
); 
 928 void loadingProgress(off_t pos
); 
 929 void stopLoading(void); 
 931 /* RDB persistence */ 
 934 /* AOF persistence */ 
 935 void flushAppendOnlyFile(int force
); 
 936 void feedAppendOnlyFile(struct redisCommand 
*cmd
, int dictid
, robj 
**argv
, int argc
); 
 937 void aofRemoveTempFile(pid_t childpid
); 
 938 int rewriteAppendOnlyFileBackground(void); 
 939 int loadAppendOnlyFile(char *filename
); 
 940 void stopAppendOnly(void); 
 941 int startAppendOnly(void); 
 942 void backgroundRewriteDoneHandler(int exitcode
, int bysignal
); 
 944 /* Sorted sets data type */ 
 946 /* Struct to hold a inclusive/exclusive range spec. */ 
 949     int minex
, maxex
; /* are min or max exclusive? */ 
 952 zskiplist 
*zslCreate(void); 
 953 void zslFree(zskiplist 
*zsl
); 
 954 zskiplistNode 
*zslInsert(zskiplist 
*zsl
, double score
, robj 
*obj
); 
 955 unsigned char *zzlInsert(unsigned char *zl
, robj 
*ele
, double score
); 
 956 int zslDelete(zskiplist 
*zsl
, double score
, robj 
*obj
); 
 957 zskiplistNode 
*zslFirstInRange(zskiplist 
*zsl
, zrangespec range
); 
 958 double zzlGetScore(unsigned char *sptr
); 
 959 void zzlNext(unsigned char *zl
, unsigned char **eptr
, unsigned char **sptr
); 
 960 void zzlPrev(unsigned char *zl
, unsigned char **eptr
, unsigned char **sptr
); 
 961 unsigned int zsetLength(robj 
*zobj
); 
 962 void zsetConvert(robj 
*zobj
, int encoding
); 
 965 int freeMemoryIfNeeded(void); 
 966 int processCommand(redisClient 
*c
); 
 967 void setupSignalHandlers(void); 
 968 struct redisCommand 
*lookupCommand(sds name
); 
 969 struct redisCommand 
*lookupCommandByCString(char *s
); 
 970 void call(redisClient 
*c
, int flags
); 
 971 void propagate(struct redisCommand 
*cmd
, int dbid
, robj 
**argv
, int argc
, int flags
); 
 972 void alsoPropagate(struct redisCommand 
*cmd
, int dbid
, robj 
**argv
, int argc
, int target
); 
 973 int prepareForShutdown(); 
 974 void redisLog(int level
, const char *fmt
, ...); 
 975 void redisLogRaw(int level
, const char *msg
); 
 977 void updateDictResizePolicy(void); 
 978 int htNeedsResize(dict 
*dict
); 
 979 void oom(const char *msg
); 
 980 void populateCommandTable(void); 
 981 void resetCommandTableStats(void); 
 984 robj 
*setTypeCreate(robj 
*value
); 
 985 int setTypeAdd(robj 
*subject
, robj 
*value
); 
 986 int setTypeRemove(robj 
*subject
, robj 
*value
); 
 987 int setTypeIsMember(robj 
*subject
, robj 
*value
); 
 988 setTypeIterator 
*setTypeInitIterator(robj 
*subject
); 
 989 void setTypeReleaseIterator(setTypeIterator 
*si
); 
 990 int setTypeNext(setTypeIterator 
*si
, robj 
**objele
, int64_t *llele
); 
 991 robj 
*setTypeNextObject(setTypeIterator 
*si
); 
 992 int setTypeRandomElement(robj 
*setobj
, robj 
**objele
, int64_t *llele
); 
 993 unsigned long setTypeSize(robj 
*subject
); 
 994 void setTypeConvert(robj 
*subject
, int enc
); 
 997 void convertToRealHash(robj 
*o
); 
 998 void hashTypeTryConversion(robj 
*subject
, robj 
**argv
, int start
, int end
); 
 999 void hashTypeTryObjectEncoding(robj 
*subject
, robj 
**o1
, robj 
**o2
); 
1000 int hashTypeGet(robj 
*o
, robj 
*key
, robj 
**objval
, unsigned char **v
, unsigned int *vlen
); 
1001 robj 
*hashTypeGetObject(robj 
*o
, robj 
*key
); 
1002 int hashTypeExists(robj 
*o
, robj 
*key
); 
1003 int hashTypeSet(robj 
*o
, robj 
*key
, robj 
*value
); 
1004 int hashTypeDelete(robj 
*o
, robj 
*key
); 
1005 unsigned long hashTypeLength(robj 
*o
); 
1006 hashTypeIterator 
*hashTypeInitIterator(robj 
*subject
); 
1007 void hashTypeReleaseIterator(hashTypeIterator 
*hi
); 
1008 int hashTypeNext(hashTypeIterator 
*hi
); 
1009 int hashTypeCurrent(hashTypeIterator 
*hi
, int what
, robj 
**objval
, unsigned char **v
, unsigned int *vlen
); 
1010 robj 
*hashTypeCurrentObject(hashTypeIterator 
*hi
, int what
); 
1011 robj 
*hashTypeLookupWriteOrCreate(redisClient 
*c
, robj 
*key
); 
1014 int pubsubUnsubscribeAllChannels(redisClient 
*c
, int notify
); 
1015 int pubsubUnsubscribeAllPatterns(redisClient 
*c
, int notify
); 
1016 void freePubsubPattern(void *p
); 
1017 int listMatchPubsubPattern(void *a
, void *b
); 
1018 int pubsubPublishMessage(robj 
*channel
, robj 
*message
); 
1021 void loadServerConfig(char *filename
, char *options
); 
1022 void appendServerSaveParams(time_t seconds
, int changes
); 
1023 void resetServerSaveParams(); 
1025 /* db.c -- Keyspace access API */ 
1026 int removeExpire(redisDb 
*db
, robj 
*key
); 
1027 void propagateExpire(redisDb 
*db
, robj 
*key
); 
1028 int expireIfNeeded(redisDb 
*db
, robj 
*key
); 
1029 long long getExpire(redisDb 
*db
, robj 
*key
); 
1030 void setExpire(redisDb 
*db
, robj 
*key
, long long when
); 
1031 robj 
*lookupKey(redisDb 
*db
, robj 
*key
); 
1032 robj 
*lookupKeyRead(redisDb 
*db
, robj 
*key
); 
1033 robj 
*lookupKeyWrite(redisDb 
*db
, robj 
*key
); 
1034 robj 
*lookupKeyReadOrReply(redisClient 
*c
, robj 
*key
, robj 
*reply
); 
1035 robj 
*lookupKeyWriteOrReply(redisClient 
*c
, robj 
*key
, robj 
*reply
); 
1036 void dbAdd(redisDb 
*db
, robj 
*key
, robj 
*val
); 
1037 void dbOverwrite(redisDb 
*db
, robj 
*key
, robj 
*val
); 
1038 void setKey(redisDb 
*db
, robj 
*key
, robj 
*val
); 
1039 int dbExists(redisDb 
*db
, robj 
*key
); 
1040 robj 
*dbRandomKey(redisDb 
*db
); 
1041 int dbDelete(redisDb 
*db
, robj 
*key
); 
1042 long long emptyDb(); 
1043 int selectDb(redisClient 
*c
, int id
); 
1044 void signalModifiedKey(redisDb 
*db
, robj 
*key
); 
1045 void signalFlushedDb(int dbid
); 
1046 unsigned int GetKeysInSlot(unsigned int hashslot
, robj 
**keys
, unsigned int count
); 
1048 /* API to get key arguments from commands */ 
1049 #define REDIS_GETKEYS_ALL 0 
1050 #define REDIS_GETKEYS_PRELOAD 1 
1051 int *getKeysFromCommand(struct redisCommand 
*cmd
, robj 
**argv
, int argc
, int *numkeys
, int flags
); 
1052 void getKeysFreeResult(int *result
); 
1053 int *noPreloadGetKeys(struct redisCommand 
*cmd
,robj 
**argv
, int argc
, int *numkeys
, int flags
); 
1054 int *renameGetKeys(struct redisCommand 
*cmd
,robj 
**argv
, int argc
, int *numkeys
, int flags
); 
1055 int *zunionInterGetKeys(struct redisCommand 
*cmd
,robj 
**argv
, int argc
, int *numkeys
, int flags
); 
1058 void clusterInit(void); 
1059 unsigned short crc16(const char *buf
, int len
); 
1060 unsigned int keyHashSlot(char *key
, int keylen
); 
1061 clusterNode 
*createClusterNode(char *nodename
, int flags
); 
1062 int clusterAddNode(clusterNode 
*node
); 
1063 void clusterCron(void); 
1064 clusterNode 
*getNodeByQuery(redisClient 
*c
, struct redisCommand 
*cmd
, robj 
**argv
, int argc
, int *hashslot
, int *ask
); 
1065 void clusterPropagatePublish(robj 
*channel
, robj 
*message
); 
1068 void scriptingInit(void); 
1071 char *redisGitSHA1(void); 
1072 char *redisGitDirty(void); 
1074 /* Commands prototypes */ 
1075 void authCommand(redisClient 
*c
); 
1076 void pingCommand(redisClient 
*c
); 
1077 void echoCommand(redisClient 
*c
); 
1078 void setCommand(redisClient 
*c
); 
1079 void setnxCommand(redisClient 
*c
); 
1080 void setexCommand(redisClient 
*c
); 
1081 void psetexCommand(redisClient 
*c
); 
1082 void getCommand(redisClient 
*c
); 
1083 void delCommand(redisClient 
*c
); 
1084 void existsCommand(redisClient 
*c
); 
1085 void setbitCommand(redisClient 
*c
); 
1086 void getbitCommand(redisClient 
*c
); 
1087 void setrangeCommand(redisClient 
*c
); 
1088 void getrangeCommand(redisClient 
*c
); 
1089 void incrCommand(redisClient 
*c
); 
1090 void decrCommand(redisClient 
*c
); 
1091 void incrbyCommand(redisClient 
*c
); 
1092 void decrbyCommand(redisClient 
*c
); 
1093 void incrbyfloatCommand(redisClient 
*c
); 
1094 void selectCommand(redisClient 
*c
); 
1095 void randomkeyCommand(redisClient 
*c
); 
1096 void keysCommand(redisClient 
*c
); 
1097 void dbsizeCommand(redisClient 
*c
); 
1098 void lastsaveCommand(redisClient 
*c
); 
1099 void saveCommand(redisClient 
*c
); 
1100 void bgsaveCommand(redisClient 
*c
); 
1101 void bgrewriteaofCommand(redisClient 
*c
); 
1102 void shutdownCommand(redisClient 
*c
); 
1103 void moveCommand(redisClient 
*c
); 
1104 void renameCommand(redisClient 
*c
); 
1105 void renamenxCommand(redisClient 
*c
); 
1106 void lpushCommand(redisClient 
*c
); 
1107 void rpushCommand(redisClient 
*c
); 
1108 void lpushxCommand(redisClient 
*c
); 
1109 void rpushxCommand(redisClient 
*c
); 
1110 void linsertCommand(redisClient 
*c
); 
1111 void lpopCommand(redisClient 
*c
); 
1112 void rpopCommand(redisClient 
*c
); 
1113 void llenCommand(redisClient 
*c
); 
1114 void lindexCommand(redisClient 
*c
); 
1115 void lrangeCommand(redisClient 
*c
); 
1116 void ltrimCommand(redisClient 
*c
); 
1117 void typeCommand(redisClient 
*c
); 
1118 void lsetCommand(redisClient 
*c
); 
1119 void saddCommand(redisClient 
*c
); 
1120 void sremCommand(redisClient 
*c
); 
1121 void smoveCommand(redisClient 
*c
); 
1122 void sismemberCommand(redisClient 
*c
); 
1123 void scardCommand(redisClient 
*c
); 
1124 void spopCommand(redisClient 
*c
); 
1125 void srandmemberCommand(redisClient 
*c
); 
1126 void sinterCommand(redisClient 
*c
); 
1127 void sinterstoreCommand(redisClient 
*c
); 
1128 void sunionCommand(redisClient 
*c
); 
1129 void sunionstoreCommand(redisClient 
*c
); 
1130 void sdiffCommand(redisClient 
*c
); 
1131 void sdiffstoreCommand(redisClient 
*c
); 
1132 void syncCommand(redisClient 
*c
); 
1133 void flushdbCommand(redisClient 
*c
); 
1134 void flushallCommand(redisClient 
*c
); 
1135 void sortCommand(redisClient 
*c
); 
1136 void lremCommand(redisClient 
*c
); 
1137 void rpoplpushCommand(redisClient 
*c
); 
1138 void infoCommand(redisClient 
*c
); 
1139 void mgetCommand(redisClient 
*c
); 
1140 void monitorCommand(redisClient 
*c
); 
1141 void expireCommand(redisClient 
*c
); 
1142 void expireatCommand(redisClient 
*c
); 
1143 void pexpireCommand(redisClient 
*c
); 
1144 void pexpireatCommand(redisClient 
*c
); 
1145 void getsetCommand(redisClient 
*c
); 
1146 void ttlCommand(redisClient 
*c
); 
1147 void pttlCommand(redisClient 
*c
); 
1148 void persistCommand(redisClient 
*c
); 
1149 void slaveofCommand(redisClient 
*c
); 
1150 void debugCommand(redisClient 
*c
); 
1151 void msetCommand(redisClient 
*c
); 
1152 void msetnxCommand(redisClient 
*c
); 
1153 void zaddCommand(redisClient 
*c
); 
1154 void zincrbyCommand(redisClient 
*c
); 
1155 void zrangeCommand(redisClient 
*c
); 
1156 void zrangebyscoreCommand(redisClient 
*c
); 
1157 void zrevrangebyscoreCommand(redisClient 
*c
); 
1158 void zcountCommand(redisClient 
*c
); 
1159 void zrevrangeCommand(redisClient 
*c
); 
1160 void zcardCommand(redisClient 
*c
); 
1161 void zremCommand(redisClient 
*c
); 
1162 void zscoreCommand(redisClient 
*c
); 
1163 void zremrangebyscoreCommand(redisClient 
*c
); 
1164 void multiCommand(redisClient 
*c
); 
1165 void execCommand(redisClient 
*c
); 
1166 void discardCommand(redisClient 
*c
); 
1167 void blpopCommand(redisClient 
*c
); 
1168 void brpopCommand(redisClient 
*c
); 
1169 void brpoplpushCommand(redisClient 
*c
); 
1170 void appendCommand(redisClient 
*c
); 
1171 void strlenCommand(redisClient 
*c
); 
1172 void zrankCommand(redisClient 
*c
); 
1173 void zrevrankCommand(redisClient 
*c
); 
1174 void hsetCommand(redisClient 
*c
); 
1175 void hsetnxCommand(redisClient 
*c
); 
1176 void hgetCommand(redisClient 
*c
); 
1177 void hmsetCommand(redisClient 
*c
); 
1178 void hmgetCommand(redisClient 
*c
); 
1179 void hdelCommand(redisClient 
*c
); 
1180 void hlenCommand(redisClient 
*c
); 
1181 void zremrangebyrankCommand(redisClient 
*c
); 
1182 void zunionstoreCommand(redisClient 
*c
); 
1183 void zinterstoreCommand(redisClient 
*c
); 
1184 void hkeysCommand(redisClient 
*c
); 
1185 void hvalsCommand(redisClient 
*c
); 
1186 void hgetallCommand(redisClient 
*c
); 
1187 void hexistsCommand(redisClient 
*c
); 
1188 void configCommand(redisClient 
*c
); 
1189 void hincrbyCommand(redisClient 
*c
); 
1190 void hincrbyfloatCommand(redisClient 
*c
); 
1191 void subscribeCommand(redisClient 
*c
); 
1192 void unsubscribeCommand(redisClient 
*c
); 
1193 void psubscribeCommand(redisClient 
*c
); 
1194 void punsubscribeCommand(redisClient 
*c
); 
1195 void publishCommand(redisClient 
*c
); 
1196 void watchCommand(redisClient 
*c
); 
1197 void unwatchCommand(redisClient 
*c
); 
1198 void clusterCommand(redisClient 
*c
); 
1199 void restoreCommand(redisClient 
*c
); 
1200 void migrateCommand(redisClient 
*c
); 
1201 void askingCommand(redisClient 
*c
); 
1202 void dumpCommand(redisClient 
*c
); 
1203 void objectCommand(redisClient 
*c
); 
1204 void clientCommand(redisClient 
*c
); 
1205 void evalCommand(redisClient 
*c
); 
1206 void evalShaCommand(redisClient 
*c
); 
1207 void scriptCommand(redisClient 
*c
); 
1209 #if defined(__GNUC__) 
1210 void *calloc(size_t count
, size_t size
) __attribute__ ((deprecated
)); 
1211 void free(void *ptr
) __attribute__ ((deprecated
)); 
1212 void *malloc(size_t size
) __attribute__ ((deprecated
)); 
1213 void *realloc(void *ptr
, size_t size
) __attribute__ ((deprecated
)); 
1216 /* Debugging stuff */ 
1217 void _redisAssertWithInfo(redisClient 
*c
, robj 
*o
, char *estr
, char *file
, int line
); 
1218 void _redisAssert(char *estr
, char *file
, int line
); 
1219 void _redisPanic(char *msg
, char *file
, int line
); 
1220 void bugReportStart(void); 
1221 void redisLogObjectDebugInfo(robj 
*o
); 
1222 void sigsegvHandler(int sig
, siginfo_t 
*info
, void *secret
); 
1223 sds 
genRedisInfoString(char *section
);