]> git.saurik.com Git - redis.git/blame - src/redis.h
fix a typo in redis.h line 595 comment
[redis.git] / src / redis.h
CommitLineData
e2641e09 1#ifndef __REDIS_H
2#define __REDIS_H
3
4#include "fmacros.h"
5#include "config.h"
6
7#if defined(__sun)
8#include "solarisfixes.h"
9#endif
10
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <time.h>
15#include <limits.h>
16#include <unistd.h>
17#include <errno.h>
3688d7f3 18#include <inttypes.h>
d06a5b23 19#include <pthread.h>
e1a586ee 20#include <syslog.h>
ecc91094 21#include <netinet/in.h>
7585836e 22#include <lua.h>
eea8c7a4 23#include <signal.h>
e2641e09 24
daa70b17 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 */
e2641e09 29#include "zmalloc.h" /* total memory usage aware version of malloc/free */
daa70b17 30#include "anet.h" /* Networking the easy way */
e2641e09 31#include "ziplist.h" /* Compact list data structure */
daa70b17 32#include "intset.h" /* Compact integer set structure */
33#include "version.h" /* Version macro */
34#include "util.h" /* Misc functions useful in many places */
e2641e09 35
36/* Error codes */
37#define REDIS_OK 0
38#define REDIS_ERR -1
39
40/* Static server configuration */
a8a981a8 41#define REDIS_HZ 100 /* Time interrupt calls/sec. */
e2641e09 42#define REDIS_SERVERPORT 6379 /* TCP port */
3570629f 43#define REDIS_MAXIDLETIME 0 /* default client timeout: infinite */
e2641e09 44#define REDIS_DEFAULT_DBNUM 16
45#define REDIS_CONFIGLINE_MAX 1024
e2641e09 46#define REDIS_EXPIRELOOKUPS_PER_CRON 10 /* lookup 10 expires per loop */
f7f2b261 47#define REDIS_EXPIRELOOKUPS_TIME_PERC 25 /* CPU max % for keys collection */
e2641e09 48#define REDIS_MAX_WRITE_PER_EVENT (1024*64)
ae15f750 49#define REDIS_SHARED_SELECT_CMDS 10
e2641e09 50#define REDIS_SHARED_INTEGERS 10000
355f8591 51#define REDIS_SHARED_BULKHDR_LEN 32
e1a586ee 52#define REDIS_MAX_LOGMSG_LEN 1024 /* Default maximum length of syslog messages */
2c915bcf 53#define REDIS_AOF_REWRITE_PERC 100
54#define REDIS_AOF_REWRITE_MIN_SIZE (1024*1024)
55#define REDIS_AOF_REWRITE_ITEMS_PER_CMD 64
daa70b17 56#define REDIS_SLOWLOG_LOG_SLOWER_THAN 10000
590d55a2 57#define REDIS_SLOWLOG_MAX_LEN 128
58732c23 58#define REDIS_MAX_CLIENTS 10000
4b3865cb 59#define REDIS_AUTHPASS_MAX_LEN 512
48d26a48 60#define REDIS_DEFAULT_SLAVE_PRIORITY 100
8996bf77 61#define REDIS_REPL_TIMEOUT 60
62#define REDIS_REPL_PING_SLAVE_PERIOD 10
44f508f1 63#define REDIS_RUN_ID_SIZE 40
250e7f69 64#define REDIS_OPS_SEC_SAMPLES 16
44f508f1 65
f42e2f1b 66/* Protocol and I/O related defines */
67#define REDIS_MAX_QUERYBUF_LEN (1024*1024*1024) /* 1GB max query buffer. */
68#define REDIS_IOBUF_LEN (1024*16) /* Generic I/O buffer size */
69#define REDIS_REPLY_CHUNK_BYTES (16*1024) /* 16k output buffer */
11e0c4c5 70#define REDIS_INLINE_MAX_SIZE (1024*64) /* Max size of inline reads */
f42e2f1b 71#define REDIS_MBULK_BIG_ARG (1024*32)
8996bf77 72
e2641e09 73/* Hash table parameters */
74#define REDIS_HT_MINFILL 10 /* Minimal hash table fill 10% */
75
5d02b00f 76/* Command flags. Please check the command table defined in the redis.c file
77 * for more information about the meaning of every flag. */
78#define REDIS_CMD_WRITE 1 /* "w" flag */
79#define REDIS_CMD_READONLY 2 /* "r" flag */
80#define REDIS_CMD_DENYOOM 4 /* "m" flag */
81#define REDIS_CMD_FORCE_REPLICATION 8 /* "f" flag */
82#define REDIS_CMD_ADMIN 16 /* "a" flag */
83#define REDIS_CMD_PUBSUB 32 /* "p" flag */
b60ed6e8 84#define REDIS_CMD_NOSCRIPT 64 /* "s" flag */
85#define REDIS_CMD_RANDOM 128 /* "R" flag */
548efd91 86#define REDIS_CMD_SORT_FOR_SCRIPT 256 /* "S" flag */
3bb3f125 87#define REDIS_CMD_LOADING 512 /* "l" flag */
88#define REDIS_CMD_STALE 1024 /* "t" flag */
21645232 89#define REDIS_CMD_SKIP_MONITOR 2048 /* "M" flag */
e2641e09 90
91/* Object types */
92#define REDIS_STRING 0
93#define REDIS_LIST 1
94#define REDIS_SET 2
95#define REDIS_ZSET 3
96#define REDIS_HASH 4
e12b27ac 97
e2641e09 98/* Objects encoding. Some kind of objects like Strings and Hashes can be
99 * internally represented in multiple ways. The 'encoding' field of the object
100 * is set to one of this fields for this object. */
101#define REDIS_ENCODING_RAW 0 /* Raw representation */
102#define REDIS_ENCODING_INT 1 /* Encoded as integer */
103#define REDIS_ENCODING_HT 2 /* Encoded as hash table */
104#define REDIS_ENCODING_ZIPMAP 3 /* Encoded as zipmap */
105#define REDIS_ENCODING_LINKEDLIST 4 /* Encoded as regular linked list */
106#define REDIS_ENCODING_ZIPLIST 5 /* Encoded as ziplist */
96ffb2fe 107#define REDIS_ENCODING_INTSET 6 /* Encoded as intset */
0b7f6d09 108#define REDIS_ENCODING_SKIPLIST 7 /* Encoded as skiplist */
e2641e09 109
e2641e09 110/* Defines related to the dump file format. To store 32 bits lengths for short
111 * keys requires a lot of space, so we check the most significant 2 bits of
112 * the first byte to interpreter the length:
113 *
114 * 00|000000 => if the two MSB are 00 the len is the 6 bits of this byte
115 * 01|000000 00000000 => 01, the len is 14 byes, 6 bits + 8 bits of next byte
116 * 10|000000 [32 bit integer] => if it's 01, a full 32 bit len will follow
117 * 11|000000 this means: specially encoded object will follow. The six bits
118 * number specify the kind of object that follows.
119 * See the REDIS_RDB_ENC_* defines.
120 *
121 * Lenghts up to 63 are stored using a single byte, most DB keys, and may
122 * values, will fit inside. */
123#define REDIS_RDB_6BITLEN 0
124#define REDIS_RDB_14BITLEN 1
125#define REDIS_RDB_32BITLEN 2
126#define REDIS_RDB_ENCVAL 3
127#define REDIS_RDB_LENERR UINT_MAX
128
129/* When a length of a string object stored on disk has the first two bits
130 * set, the remaining two bits specify a special encoding for the object
131 * accordingly to the following defines: */
132#define REDIS_RDB_ENC_INT8 0 /* 8 bit signed integer */
133#define REDIS_RDB_ENC_INT16 1 /* 16 bit signed integer */
134#define REDIS_RDB_ENC_INT32 2 /* 32 bit signed integer */
135#define REDIS_RDB_ENC_LZF 3 /* string compressed with FASTLZ */
136
c6ac7d03 137/* AOF states */
138#define REDIS_AOF_OFF 0 /* AOF is off */
139#define REDIS_AOF_ON 1 /* AOF is on */
140#define REDIS_AOF_WAIT_REWRITE 2 /* AOF waits rewrite to start appending */
141
e2641e09 142/* Client flags */
143#define REDIS_SLAVE 1 /* This client is a slave server */
144#define REDIS_MASTER 2 /* This client is a master server */
145#define REDIS_MONITOR 4 /* This client is a slave monitor, see MONITOR */
146#define REDIS_MULTI 8 /* This client is in a MULTI context */
147#define REDIS_BLOCKED 16 /* The client is waiting in a blocking operation */
e2641e09 148#define REDIS_DIRTY_CAS 64 /* Watched keys modified. EXEC will fail. */
5e78edb3 149#define REDIS_CLOSE_AFTER_REPLY 128 /* Close after writing entire reply. */
3bcffcbe
PN
150#define REDIS_UNBLOCKED 256 /* This client was unblocked and is stored in
151 server.unblocked_clients */
7156f43c 152#define REDIS_LUA_CLIENT 512 /* This is a non connected client used by Lua */
6856c7b4 153#define REDIS_ASKING 1024 /* Client issued the ASKING command */
7eac2a75 154#define REDIS_CLOSE_ASAP 2048 /* Close this client ASAP */
b6ffa85f 155#define REDIS_UNIX_SOCKET 4096 /* Client connected via Unix domain socket */
cd8788f2
PN
156
157/* Client request types */
158#define REDIS_REQ_INLINE 1
159#define REDIS_REQ_MULTIBULK 2
e2641e09 160
498dc555 161/* Client classes for client limits, currently used only for
162 * the max-client-output-buffer limit implementation. */
163#define REDIS_CLIENT_LIMIT_CLASS_NORMAL 0
164#define REDIS_CLIENT_LIMIT_CLASS_SLAVE 1
165#define REDIS_CLIENT_LIMIT_CLASS_PUBSUB 2
7eac2a75 166#define REDIS_CLIENT_LIMIT_NUM_CLASSES 3
498dc555 167
e2641e09 168/* Slave replication state - slave side */
a3309139
PN
169#define REDIS_REPL_NONE 0 /* No active replication */
170#define REDIS_REPL_CONNECT 1 /* Must connect to master */
b075621f 171#define REDIS_REPL_CONNECTING 2 /* Connecting to master */
fd2a8951 172#define REDIS_REPL_RECEIVE_PONG 3 /* Wait for PING reply */
173#define REDIS_REPL_TRANSFER 4 /* Receiving .rdb from master */
174#define REDIS_REPL_CONNECTED 5 /* Connected to master */
e2641e09 175
890a2ed9
PN
176/* Synchronous read timeout - slave side */
177#define REDIS_REPL_SYNCIO_TIMEOUT 5
e2641e09 178
179/* Slave replication state - from the point of view of master
180 * Note that in SEND_BULK and ONLINE state the slave receives new updates
181 * in its output queue. In the WAIT_BGSAVE state instead the server is waiting
182 * to start the next background saving in order to send updates to it. */
183#define REDIS_REPL_WAIT_BGSAVE_START 3 /* master waits bgsave to start feeding it */
184#define REDIS_REPL_WAIT_BGSAVE_END 4 /* master waits bgsave to start bulk DB transmission */
185#define REDIS_REPL_SEND_BULK 5 /* master is sending the bulk DB */
186#define REDIS_REPL_ONLINE 6 /* bulk DB already transmitted, receive updates */
187
188/* List related stuff */
189#define REDIS_HEAD 0
190#define REDIS_TAIL 1
191
192/* Sort operations */
193#define REDIS_SORT_GET 0
194#define REDIS_SORT_ASC 1
195#define REDIS_SORT_DESC 2
196#define REDIS_SORTKEY_MAX 1024
197
198/* Log levels */
199#define REDIS_DEBUG 0
200#define REDIS_VERBOSE 1
201#define REDIS_NOTICE 2
202#define REDIS_WARNING 3
996d503d 203#define REDIS_LOG_RAW (1<<10) /* Modifier to log without timestamp */
e2641e09 204
205/* Anti-warning macro... */
206#define REDIS_NOTUSED(V) ((void) V)
207
208#define ZSKIPLIST_MAXLEVEL 32 /* Should be enough for 2^32 elements */
209#define ZSKIPLIST_P 0.25 /* Skiplist P = 1/4 */
210
211/* Append only defines */
2c915bcf 212#define AOF_FSYNC_NO 0
213#define AOF_FSYNC_ALWAYS 1
214#define AOF_FSYNC_EVERYSEC 2
e2641e09 215
216/* Zip structure related defaults */
ebd85e9a
PN
217#define REDIS_HASH_MAX_ZIPLIST_ENTRIES 512
218#define REDIS_HASH_MAX_ZIPLIST_VALUE 64
6a246b1e 219#define REDIS_LIST_MAX_ZIPLIST_ENTRIES 512
220#define REDIS_LIST_MAX_ZIPLIST_VALUE 64
221#define REDIS_SET_MAX_INTSET_ENTRIES 512
3ea204e1
PN
222#define REDIS_ZSET_MAX_ZIPLIST_ENTRIES 128
223#define REDIS_ZSET_MAX_ZIPLIST_VALUE 64
e2641e09 224
225/* Sets operations codes */
226#define REDIS_OP_UNION 0
227#define REDIS_OP_DIFF 1
228#define REDIS_OP_INTER 2
229
165346ca 230/* Redis maxmemory strategies */
231#define REDIS_MAXMEMORY_VOLATILE_LRU 0
232#define REDIS_MAXMEMORY_VOLATILE_TTL 1
233#define REDIS_MAXMEMORY_VOLATILE_RANDOM 2
234#define REDIS_MAXMEMORY_ALLKEYS_LRU 3
235#define REDIS_MAXMEMORY_ALLKEYS_RANDOM 4
5402c426 236#define REDIS_MAXMEMORY_NO_EVICTION 5
165346ca 237
eeffcf38 238/* Scripting */
8cb8d417 239#define REDIS_LUA_TIME_LIMIT 5000 /* milliseconds */
eeffcf38 240
12d293ca 241/* Units */
242#define UNIT_SECONDS 0
243#define UNIT_MILLISECONDS 1
244
4ab8695d 245/* SHUTDOWN flags */
246#define REDIS_SHUTDOWN_SAVE 1 /* Force SAVE on SHUTDOWN even if no save
247 points are configured. */
248#define REDIS_SHUTDOWN_NOSAVE 2 /* Don't SAVE on SHUTDOWN. */
249
ce8b772b 250/* Command call flags, see call() function */
251#define REDIS_CALL_NONE 0
252#define REDIS_CALL_SLOWLOG 1
253#define REDIS_CALL_STATS 2
254#define REDIS_CALL_PROPAGATE 4
255#define REDIS_CALL_FULL (REDIS_CALL_SLOWLOG | REDIS_CALL_STATS | REDIS_CALL_PROPAGATE)
256
ad08d059 257/* Command propagation flags, see propagate() function */
258#define REDIS_PROPAGATE_NONE 0
259#define REDIS_PROPAGATE_AOF 1
260#define REDIS_PROPAGATE_REPL 2
261
120ba392 262/* Using the following macro you can run code inside serverCron() with the
263 * specified period, specified in milliseconds.
264 * The actual resolution depends on REDIS_HZ. */
265#define run_with_period(_ms_) if (!(server.cronloops%((_ms_)/(1000/REDIS_HZ))))
266
e2641e09 267/* We can print the stacktrace, so our assert is defined this way: */
bab205f7 268#define redisAssertWithInfo(_c,_o,_e) ((_e)?(void)0 : (_redisAssertWithInfo(_c,_o,#_e,__FILE__,__LINE__),_exit(1)))
e2641e09 269#define redisAssert(_e) ((_e)?(void)0 : (_redisAssert(#_e,__FILE__,__LINE__),_exit(1)))
270#define redisPanic(_e) _redisPanic(#_e,__FILE__,__LINE__),_exit(1)
e2641e09 271
272/*-----------------------------------------------------------------------------
273 * Data types
274 *----------------------------------------------------------------------------*/
275
276/* A redis object, that is a type able to hold a string / list / set */
277
278/* The actual Redis Object */
ef59a8bc 279#define REDIS_LRU_CLOCK_MAX ((1<<21)-1) /* Max value of obj->lru */
165346ca 280#define REDIS_LRU_CLOCK_RESOLUTION 10 /* LRU clock resolution in seconds */
e2641e09 281typedef struct redisObject {
282 unsigned type:4;
3be00d7e 283 unsigned notused:2; /* Not used */
e2641e09 284 unsigned encoding:4;
285 unsigned lru:22; /* lru time (relative to server.lruclock) */
286 int refcount;
287 void *ptr;
e2641e09 288} robj;
289
e2641e09 290/* Macro used to initalize a Redis object allocated on the stack.
291 * Note that this macro is taken near the structure definition to make sure
292 * we'll update it when the structure is changed, to avoid bugs like
293 * bug #85 introduced exactly in this way. */
294#define initStaticStringObject(_var,_ptr) do { \
295 _var.refcount = 1; \
296 _var.type = REDIS_STRING; \
297 _var.encoding = REDIS_ENCODING_RAW; \
298 _var.ptr = _ptr; \
e2641e09 299} while(0);
300
301typedef struct redisDb {
302 dict *dict; /* The keyspace for this DB */
303 dict *expires; /* Timeout of keys with a timeout set */
304 dict *blocking_keys; /* Keys with clients waiting for data (BLPOP) */
f444e2af 305 dict *ready_keys; /* Blocked keys that received a PUSH */
e2641e09 306 dict *watched_keys; /* WATCHED keys for MULTI/EXEC CAS */
307 int id;
308} redisDb;
309
310/* Client MULTI/EXEC state */
311typedef struct multiCmd {
312 robj **argv;
313 int argc;
314 struct redisCommand *cmd;
315} multiCmd;
316
317typedef struct multiState {
318 multiCmd *commands; /* Array of MULTI commands */
319 int count; /* Total number of MULTI commands */
320} multiState;
321
357a8417
DJMM
322typedef struct blockingState {
323 robj **keys; /* The key we are waiting to terminate a blocking
324 * operation such as BLPOP. Otherwise NULL. */
325 int count; /* Number of blocking keys */
326 time_t timeout; /* Blocking operation timeout. If UNIX current time
327 * is >= timeout then the operation timed out. */
328 robj *target; /* The key that should receive the element,
329 * for BRPOPLPUSH. */
330} blockingState;
331
f444e2af 332/* The following structure represents a node in the server.ready_keys list,
333 * where we accumulate all the keys that had clients blocked with a blocking
334 * operation such as B[LR]POP, but received new data in the context of the
335 * last executed command.
336 *
337 * After the execution of every command or script, we run this list to check
338 * if as a result we should serve data to clients blocked, unblocking them.
339 * Note that server.ready_keys will not have duplicates as there dictionary
340 * also called ready_keys in every structure representing a Redis database,
341 * where we make sure to remember if a given key was already added in the
342 * server.ready_keys list. */
343typedef struct readyList {
344 redisDb *db;
345 robj *key;
346} readyList;
347
e2641e09 348/* With multiplexing we need to take per-clinet state.
349 * Clients are taken in a liked list. */
350typedef struct redisClient {
351 int fd;
352 redisDb *db;
353 int dictid;
354 sds querybuf;
9fa9ccb0 355 size_t querybuf_peak; /* Recent (100ms or more) peak of querybuf size */
cd8788f2
PN
356 int argc;
357 robj **argv;
2c74a9f9 358 struct redisCommand *cmd, *lastcmd;
cd8788f2
PN
359 int reqtype;
360 int multibulklen; /* number of multi bulk arguments left to read */
361 long bulklen; /* length of bulk argument in multi bulk request */
e2641e09 362 list *reply;
3853c168 363 unsigned long reply_bytes; /* Tot bytes of objects in reply list */
e2641e09 364 int sentlen;
41e8e5cb 365 time_t ctime; /* Client creation time */
e2641e09 366 time_t lastinteraction; /* time of the last interaction, used for timeout */
7eac2a75 367 time_t obuf_soft_limit_reached_time;
e2641e09 368 int flags; /* REDIS_SLAVE | REDIS_MONITOR | REDIS_MULTI ... */
369 int slaveseldb; /* slave selected db, if this client is a slave */
370 int authenticated; /* when requirepass is non-NULL */
371 int replstate; /* replication state if this is a slave */
372 int repldbfd; /* replication DB file descriptor */
373 long repldboff; /* replication DB file offset */
374 off_t repldbsize; /* replication DB file size */
dbd8c753 375 int slave_listening_port; /* As configured with: SLAVECONF listening-port */
e2641e09 376 multiState mstate; /* MULTI/EXEC state */
e3c51c4b 377 blockingState bpop; /* blocking state */
e2641e09 378 list *io_keys; /* Keys this client is waiting to be loaded from the
379 * swap file in order to continue. */
380 list *watched_keys; /* Keys WATCHED for MULTI/EXEC CAS */
381 dict *pubsub_channels; /* channels a client is interested in (SUBSCRIBE) */
382 list *pubsub_patterns; /* patterns a client is interested in (SUBSCRIBE) */
834ef78e
PN
383
384 /* Response buffer */
385 int bufpos;
f3357792 386 char buf[REDIS_REPLY_CHUNK_BYTES];
e2641e09 387} redisClient;
388
389struct saveparam {
390 time_t seconds;
391 int changes;
392};
393
394struct sharedObjectsStruct {
395 robj *crlf, *ok, *err, *emptybulk, *czero, *cone, *cnegone, *pong, *space,
396 *colon, *nullbulk, *nullmultibulk, *queued,
397 *emptymultibulk, *wrongtypeerr, *nokeyerr, *syntaxerr, *sameobjecterr,
c25e7eaf 398 *outofrangeerr, *noscripterr, *loadingerr, *slowscripterr, *bgsaveerr,
0b08d648 399 *masterdownerr, *roslaveerr,
400 *oomerr, *plus, *messagebulk, *pmessagebulk, *subscribebulk,
ae15f750 401 *unsubscribebulk, *psubscribebulk, *punsubscribebulk, *del, *rpop, *lpop,
f444e2af 402 *lpush,
ae15f750 403 *select[REDIS_SHARED_SELECT_CMDS],
355f8591 404 *integers[REDIS_SHARED_INTEGERS],
405 *mbulkhdr[REDIS_SHARED_BULKHDR_LEN], /* "*<value>\r\n" */
406 *bulkhdr[REDIS_SHARED_BULKHDR_LEN]; /* "$<value>\r\n" */
e2641e09 407};
408
c772d9c6 409/* ZSETs use a specialized version of Skiplists */
410typedef struct zskiplistNode {
411 robj *obj;
412 double score;
413 struct zskiplistNode *backward;
414 struct zskiplistLevel {
415 struct zskiplistNode *forward;
416 unsigned int span;
417 } level[];
418} zskiplistNode;
419
420typedef struct zskiplist {
421 struct zskiplistNode *header, *tail;
422 unsigned long length;
423 int level;
424} zskiplist;
425
426typedef struct zset {
427 dict *dict;
428 zskiplist *zsl;
429} zset;
430
7eac2a75 431typedef struct clientBufferLimitsConfig {
7fe8d49a 432 unsigned long long hard_limit_bytes;
433 unsigned long long soft_limit_bytes;
7eac2a75 434 time_t soft_limit_seconds;
435} clientBufferLimitsConfig;
436
78d6a22d 437/* The redisOp structure defines a Redis Operation, that is an instance of
438 * a command with an argument vector, database ID, propagation target
439 * (REDIS_PROPAGATE_*), and command pointer.
440 *
441 * Currently only used to additionally propagate more commands to AOF/Replication
442 * after the propagation of the executed command. */
443typedef struct redisOp {
eeb34eff 444 robj **argv;
445 int argc, dbid, target;
446 struct redisCommand *cmd;
78d6a22d 447} redisOp;
448
449/* Defines an array of Redis operations. There is an API to add to this
450 * structure in a easy way.
451 *
452 * redisOpArrayInit();
453 * redisOpArrayAppend();
454 * redisOpArrayFree();
455 */
456typedef struct redisOpArray {
457 redisOp *ops;
458 int numops;
459} redisOpArray;
eeb34eff 460
ecc91094 461/*-----------------------------------------------------------------------------
462 * Global server state
463 *----------------------------------------------------------------------------*/
464
e2641e09 465struct redisServer {
5b831607 466 /* General */
5b831607 467 redisDb *db;
3f006679 468 dict *commands; /* Command table hash table */
5b831607 469 aeEventLoop *el;
c6ac7d03 470 unsigned lruclock:22; /* Clock incrementing every minute, for LRU */
471 unsigned lruclock_padding:10;
472 int shutdown_asap; /* SHUTDOWN needed ASAP */
473 int activerehashing; /* Incremental rehash in serverCron() */
474 char *requirepass; /* Pass for AUTH command, or NULL */
475 char *pidfile; /* PID file path */
75eaac5c 476 int arch_bits; /* 32 or 64 depending on sizeof(long) */
c25e7eaf 477 int cronloops; /* Number of times the cron function run */
91d664d6 478 char runid[REDIS_RUN_ID_SIZE+1]; /* ID always different at every exec. */
120ba392 479 int sentinel_mode; /* True if this instance is a Sentinel. */
5b831607 480 /* Networking */
c6ac7d03 481 int port; /* TCP listening port */
482 char *bindaddr; /* Bind address or NULL */
483 char *unixsocket; /* UNIX socket path */
484 mode_t unixsocketperm; /* UNIX socket permission */
485 int ipfd; /* TCP socket file descriptor */
486 int sofd; /* Unix socket file descriptor */
c6ac7d03 487 list *clients; /* List of active clients */
7eac2a75 488 list *clients_to_close; /* Clients to close asynchronously */
c6ac7d03 489 list *slaves, *monitors; /* List of slaves and MONITORs */
00010fa9 490 redisClient *current_client; /* Current client, only used on crash report */
c6ac7d03 491 char neterr[ANET_ERR_LEN]; /* Error buffer for anet.c */
97e7f8ae 492 /* RDB / AOF loading information */
c6ac7d03 493 int loading; /* We are loading data from disk if true */
97e7f8ae 494 off_t loading_total_bytes;
495 off_t loading_loaded_bytes;
496 time_t loading_start_time;
4ebfc455 497 /* Fast pointers to often looked up command */
f444e2af 498 struct redisCommand *delCommand, *multiCommand, *lpushCommand, *lpopCommand,
499 *rpopCommand;
e2641e09 500 /* Fields used only for stats */
c6ac7d03 501 time_t stat_starttime; /* Server start time */
502 long long stat_numcommands; /* Number of processed commands */
503 long long stat_numconnections; /* Number of connections received */
504 long long stat_expiredkeys; /* Number of expired keys */
505 long long stat_evictedkeys; /* Number of evicted keys (maxmemory) */
506 long long stat_keyspace_hits; /* Number of successful lookups of keys */
507 long long stat_keyspace_misses; /* Number of failed lookups of keys */
508 size_t stat_peak_memory; /* Max used memory record */
509 long long stat_fork_time; /* Time needed to perform latets fork() */
510 long long stat_rejected_conn; /* Clients rejected because of maxclients */
511 list *slowlog; /* SLOWLOG list of commands */
512 long long slowlog_entry_id; /* SLOWLOG current entry ID */
513 long long slowlog_log_slower_than; /* SLOWLOG time limit (to get logged) */
514 unsigned long slowlog_max_len; /* SLOWLOG max number of items logged */
250e7f69 515 /* The following two are used to track instantaneous "load" in terms
516 * of operations per second. */
517 long long ops_sec_last_sample_time; /* Timestamp of last sample (in ms) */
518 long long ops_sec_last_sample_ops; /* numcommands in last sample */
519 long long ops_sec_samples[REDIS_OPS_SEC_SAMPLES];
520 int ops_sec_idx;
e2641e09 521 /* Configuration */
c6ac7d03 522 int verbosity; /* Loglevel in redis.conf */
523 int maxidletime; /* Client timeout in seconds */
524 size_t client_max_querybuf_len; /* Limit for client query buffer length */
525 int dbnum; /* Total number of configured DBs */
526 int daemonize; /* True if running as a daemon */
7eac2a75 527 clientBufferLimitsConfig client_obuf_limits[REDIS_CLIENT_LIMIT_NUM_CLASSES];
c6ac7d03 528 /* AOF persistence */
529 int aof_state; /* REDIS_AOF_(ON|OFF|WAIT_REWRITE) */
2c915bcf 530 int aof_fsync; /* Kind of fsync() policy */
531 char *aof_filename; /* Name of the AOF file */
532 int aof_no_fsync_on_rewrite; /* Don't fsync if a rewrite is in prog. */
533 int aof_rewrite_perc; /* Rewrite AOF if % growth is > M and... */
534 off_t aof_rewrite_min_size; /* the AOF file is at least N bytes. */
535 off_t aof_rewrite_base_size; /* AOF size on latest startup or rewrite. */
536 off_t aof_current_size; /* AOF current size. */
537 int aof_rewrite_scheduled; /* Rewrite once BGSAVE terminates. */
ff2145ad 538 pid_t aof_child_pid; /* PID if rewriting process */
edb95253 539 list *aof_rewrite_buf_blocks; /* Hold changes during an AOF rewrite. */
ff2145ad 540 sds aof_buf; /* AOF buffer, written before entering the event loop */
541 int aof_fd; /* File descriptor of currently selected AOF file */
542 int aof_selected_db; /* Currently selected DB in AOF */
c6ac7d03 543 time_t aof_flush_postponed_start; /* UNIX time of postponed AOF flush */
ff2145ad 544 time_t aof_last_fsync; /* UNIX time of last fsync() */
6b4d92e2 545 time_t aof_rewrite_time_last; /* Time used by last AOF rewrite run. */
546 time_t aof_rewrite_time_start; /* Current AOF rewrite start time. */
f00b0844 547 int aof_lastbgrewrite_status; /* REDIS_OK or REDIS_ERR */
81f32c7b 548 unsigned long aof_delayed_fsync; /* delayed AOF fsync() counter */
c6ac7d03 549 /* RDB persistence */
550 long long dirty; /* Changes to DB from the last save */
551 long long dirty_before_bgsave; /* Used to restore dirty on failed BGSAVE */
f48cd4b9 552 pid_t rdb_child_pid; /* PID of RDB saving child */
c6ac7d03 553 struct saveparam *saveparams; /* Save points array for RDB */
554 int saveparamslen; /* Number of saving points */
f48cd4b9 555 char *rdb_filename; /* Name of RDB file */
556 int rdb_compression; /* Use compression in RDB? */
39d1e350 557 int rdb_checksum; /* Use RDB checksum? */
c25e7eaf 558 time_t lastsave; /* Unix time of last save succeeede */
6b4d92e2 559 time_t rdb_save_time_last; /* Time used by last RDB save run. */
560 time_t rdb_save_time_start; /* Current RDB save start time. */
c25e7eaf 561 int lastbgsave_status; /* REDIS_OK or REDIS_ERR */
4d3bbf35 562 int stop_writes_on_bgsave_err; /* Don't allow writes if can't BGSAVE */
eeb34eff 563 /* Propagation of commands in AOF / replication */
78d6a22d 564 redisOpArray also_propagate; /* Additional command to propagate. */
36c17a53 565 /* Logging */
c6ac7d03 566 char *logfile; /* Path of log file */
567 int syslog_enabled; /* Is syslog enabled? */
568 char *syslog_ident; /* Syslog ident */
569 int syslog_facility; /* Syslog facility */
f4aa600b 570 /* Slave specific fields */
c6ac7d03 571 char *masterauth; /* AUTH with this password with master */
572 char *masterhost; /* Hostname of master */
573 int masterport; /* Port of master */
0671d88c 574 int repl_ping_slave_period; /* Master pings the slave every N seconds */
c6ac7d03 575 int repl_timeout; /* Timeout after N seconds of master idle */
576 redisClient *master; /* Client that is master for this slave */
577 int repl_syncio_timeout; /* Timeout for synchronous I/O calls */
1844f990 578 int repl_state; /* Replication status if the instance is a slave */
13732168 579 off_t repl_transfer_size; /* Size of RDB to read from master during sync. */
580 off_t repl_transfer_read; /* Amount of RDB read from master during sync. */
581 off_t repl_transfer_last_fsync_off; /* Offset when we fsync-ed last time. */
c6ac7d03 582 int repl_transfer_s; /* Slave -> Master SYNC socket */
583 int repl_transfer_fd; /* Slave -> Master SYNC temp file descriptor */
584 char *repl_transfer_tmpfile; /* Slave-> master SYNC temp file name */
585 time_t repl_transfer_lastio; /* Unix time of the latest read, for timeout */
4ebfc455 586 int repl_serve_stale_data; /* Serve stale data when link is down? */
05406168 587 int repl_slave_ro; /* Slave is read only? */
c6ac7d03 588 time_t repl_down_since; /* Unix time at which link with master went down */
48d26a48 589 int slave_priority; /* Reported in INFO and used by Sentinel. */
f4aa600b 590 /* Limits */
c6ac7d03 591 unsigned int maxclients; /* Max number of simultaneous clients */
592 unsigned long long maxmemory; /* Max number of memory bytes to use */
593 int maxmemory_policy; /* Policy for key evition */
594 int maxmemory_samples; /* Pricision of random sampling */
f4aa600b 595 /* Blocked clients */
c6ac7d03 596 unsigned int bpop_blocked_clients; /* Number of clients blocked by lists */
cea8c5cd 597 list *unblocked_clients; /* list of clients to unblock before next loop */
f444e2af 598 list *ready_keys; /* List of readyList structures for BLPOP & co */
e2641e09 599 /* Sort parameters - qsort_r() is only available under BSD so we
600 * have to take this state global, in order to pass it to sortCompare() */
601 int sort_desc;
602 int sort_alpha;
603 int sort_bypattern;
c6ac7d03 604 /* Zip structure config, see redis.conf for more information */
ebd85e9a
PN
605 size_t hash_max_ziplist_entries;
606 size_t hash_max_ziplist_value;
e2641e09 607 size_t list_max_ziplist_entries;
608 size_t list_max_ziplist_value;
96ffb2fe 609 size_t set_max_intset_entries;
3ea204e1
PN
610 size_t zset_max_ziplist_entries;
611 size_t zset_max_ziplist_value;
c6ac7d03 612 time_t unixtime; /* Unix time sampled every second. */
e2641e09 613 /* Pubsub */
c6ac7d03 614 dict *pubsub_channels; /* Map channels to list of subscribed clients */
615 list *pubsub_patterns; /* A list of pubsub_patterns */
7585836e 616 /* Scripting */
4dd444bb 617 lua_State *lua; /* The Lua interpreter. We use just one for all clients */
c6ac7d03 618 redisClient *lua_client; /* The "fake client" to query Redis from Lua */
619 redisClient *lua_caller; /* The client running EVAL right now, or NULL */
620 dict *lua_scripts; /* A dictionary of SHA1 -> Lua scripts */
621 long long lua_time_limit; /* Script timeout in seconds */
622 long long lua_time_start; /* Start time of script */
4ab8695d 623 int lua_write_dirty; /* True if a write command was called during the
624 execution of the current script. */
9f772cc2 625 int lua_random_dirty; /* True if a random command was called during the
4ab8695d 626 execution of the current script. */
115e3ff3 627 int lua_timedout; /* True if we reached the time limit for script
628 execution. */
4ab8695d 629 int lua_kill; /* Kill the script if true. */
fa5af017 630 /* Assert & bug reportign */
631 char *assert_failed;
632 char *assert_file;
633 int assert_line;
c6ac7d03 634 int bug_report_start; /* True if bug report header was already logged. */
fc030ac7 635 int watchdog_period; /* Software watchdog period in ms. 0 = off */
e2641e09 636};
637
638typedef struct pubsubPattern {
639 redisClient *client;
640 robj *pattern;
641} pubsubPattern;
642
643typedef void redisCommandProc(redisClient *c);
9791f0f8 644typedef int *redisGetKeysProc(struct redisCommand *cmd, robj **argv, int argc, int *numkeys, int flags);
e2641e09 645struct redisCommand {
646 char *name;
647 redisCommandProc *proc;
648 int arity;
5d02b00f 649 char *sflags; /* Flags as string represenation, one char per flag. */
650 int flags; /* The actual flags, obtained from the 'sflags' field. */
571e257d 651 /* Use a function to determine keys arguments in a command line. */
9791f0f8 652 redisGetKeysProc *getkeys_proc;
e2641e09 653 /* What keys should be loaded in background when calling this command? */
9791f0f8 654 int firstkey; /* The first argument that's a key (0 = no keys) */
655 int lastkey; /* THe last argument that's a key */
656 int keystep; /* The step between first and last key */
0d808ef2 657 long long microseconds, calls;
e2641e09 658};
659
660struct redisFunctionSym {
661 char *name;
662 unsigned long pointer;
663};
664
665typedef struct _redisSortObject {
666 robj *obj;
667 union {
668 double score;
669 robj *cmpobj;
670 } u;
671} redisSortObject;
672
673typedef struct _redisSortOperation {
674 int type;
675 robj *pattern;
676} redisSortOperation;
677
e2641e09 678/* Structure to hold list iteration abstraction. */
679typedef struct {
680 robj *subject;
681 unsigned char encoding;
682 unsigned char direction; /* Iteration direction */
683 unsigned char *zi;
684 listNode *ln;
685} listTypeIterator;
686
687/* Structure for an entry while iterating over a list. */
688typedef struct {
689 listTypeIterator *li;
690 unsigned char *zi; /* Entry in ziplist */
691 listNode *ln; /* Entry in linked list */
692} listTypeEntry;
693
96ffb2fe
PN
694/* Structure to hold set iteration abstraction. */
695typedef struct {
696 robj *subject;
697 int encoding;
698 int ii; /* intset iterator */
699 dictIterator *di;
cb72d0f1 700} setTypeIterator;
96ffb2fe 701
e2641e09 702/* Structure to hold hash iteration abstration. Note that iteration over
703 * hashes involves both fields and values. Because it is possible that
704 * not both are required, store pointers in the iterator to avoid
705 * unnecessary memory allocation for fields/values. */
706typedef struct {
ebd85e9a 707 robj *subject;
e2641e09 708 int encoding;
ebd85e9a
PN
709
710 unsigned char *fptr, *vptr;
e2641e09 711
712 dictIterator *di;
713 dictEntry *de;
714} hashTypeIterator;
715
716#define REDIS_HASH_KEY 1
717#define REDIS_HASH_VALUE 2
718
719/*-----------------------------------------------------------------------------
720 * Extern declarations
721 *----------------------------------------------------------------------------*/
722
723extern struct redisServer server;
724extern struct sharedObjectsStruct shared;
725extern dictType setDictType;
726extern dictType zsetDictType;
4dd444bb 727extern dictType dbDictType;
e2641e09 728extern double R_Zero, R_PosInf, R_NegInf, R_Nan;
38d6976c 729extern dictType hashDictType;
e2641e09 730
731/*-----------------------------------------------------------------------------
732 * Functions prototypes
733 *----------------------------------------------------------------------------*/
734
419e1cca 735/* Utils */
736long long ustime(void);
2c2b2085 737long long mstime(void);
44f508f1 738void getRandomHexChars(char *p, unsigned int len);
bb99f425 739uint64_t crc64(uint64_t crc, const unsigned char *s, uint64_t l);
55951f90 740void exitFromChild(int retcode);
419e1cca 741
e2641e09 742/* networking.c -- Networking and Client related operations */
743redisClient *createClient(int fd);
744void closeTimedoutClients(void);
745void freeClient(redisClient *c);
746void resetClient(redisClient *c);
747void sendReplyToClient(aeEventLoop *el, int fd, void *privdata, int mask);
e2641e09 748void addReply(redisClient *c, robj *obj);
b301c1fc
PN
749void *addDeferredMultiBulkLength(redisClient *c);
750void setDeferredMultiBulkLength(redisClient *c, void *node, long length);
e2641e09 751void addReplySds(redisClient *c, sds s);
752void processInputBuffer(redisClient *c);
ab17b909
PN
753void acceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask);
754void acceptUnixHandler(aeEventLoop *el, int fd, void *privdata, int mask);
e2641e09 755void readQueryFromClient(aeEventLoop *el, int fd, void *privdata, int mask);
756void addReplyBulk(redisClient *c, robj *obj);
757void addReplyBulkCString(redisClient *c, char *s);
d51ebef5 758void addReplyBulkCBuffer(redisClient *c, void *p, size_t len);
759void addReplyBulkLongLong(redisClient *c, long long ll);
e2641e09 760void acceptHandler(aeEventLoop *el, int fd, void *privdata, int mask);
761void addReply(redisClient *c, robj *obj);
762void addReplySds(redisClient *c, sds s);
3ab20376
PN
763void addReplyError(redisClient *c, char *err);
764void addReplyStatus(redisClient *c, char *status);
e2641e09 765void addReplyDouble(redisClient *c, double d);
766void addReplyLongLong(redisClient *c, long long ll);
0537e7bf 767void addReplyMultiBulkLen(redisClient *c, long length);
1824e3a3 768void copyClientOutputBuffer(redisClient *dst, redisClient *src);
e2641e09 769void *dupClientReplyValue(void *o);
7a1fd61e 770void getClientsMaxBuffers(unsigned long *longest_output_list,
771 unsigned long *biggest_input_buffer);
becf5fdb 772sds getClientInfoString(redisClient *client);
45e7a1ce 773sds getAllClientsInfoString(void);
c1c9d551 774void rewriteClientCommandVector(redisClient *c, int argc, ...);
4dd444bb 775void rewriteClientCommandArgument(redisClient *c, int i, robj *newval);
3853c168 776unsigned long getClientOutputBufferMemoryUsage(redisClient *c);
7eac2a75 777void freeClientsInAsyncFreeQueue(void);
06b3dced 778void asyncCloseClientOnOutputBufferLimitReached(redisClient *c);
7fe8d49a 779int getClientLimitClassByName(char *name);
780char *getClientLimitClassName(int class);
8b7c3455 781void flushSlavesOutputBuffers(void);
ed4d4f11 782void disconnectSlaves(void);
e2641e09 783
3ab20376
PN
784#ifdef __GNUC__
785void addReplyErrorFormat(redisClient *c, const char *fmt, ...)
786 __attribute__((format(printf, 2, 3)));
787void addReplyStatusFormat(redisClient *c, const char *fmt, ...)
788 __attribute__((format(printf, 2, 3)));
789#else
790void addReplyErrorFormat(redisClient *c, const char *fmt, ...);
791void addReplyStatusFormat(redisClient *c, const char *fmt, ...);
792#endif
793
e2641e09 794/* List data type */
795void listTypeTryConversion(robj *subject, robj *value);
796void listTypePush(robj *subject, robj *value, int where);
797robj *listTypePop(robj *subject, int where);
798unsigned long listTypeLength(robj *subject);
3c08fdae 799listTypeIterator *listTypeInitIterator(robj *subject, long index, unsigned char direction);
e2641e09 800void listTypeReleaseIterator(listTypeIterator *li);
801int listTypeNext(listTypeIterator *li, listTypeEntry *entry);
802robj *listTypeGet(listTypeEntry *entry);
803void listTypeInsert(listTypeEntry *entry, robj *value, int where);
804int listTypeEqual(listTypeEntry *entry, robj *o);
805void listTypeDelete(listTypeEntry *entry);
806void listTypeConvert(robj *subject, int enc);
807void unblockClientWaitingData(redisClient *c);
f444e2af 808void handleClientsBlockedOnLists(void);
e2641e09 809void popGenericCommand(redisClient *c, int where);
810
811/* MULTI/EXEC/WATCH... */
812void unwatchAllKeys(redisClient *c);
813void initClientMultiState(redisClient *c);
814void freeClientMultiState(redisClient *c);
09e2d9ee 815void queueMultiCommand(redisClient *c);
e2641e09 816void touchWatchedKey(redisDb *db, robj *key);
817void touchWatchedKeysOnFlush(int dbid);
05406168 818void discardTransaction(redisClient *c);
e2641e09 819
820/* Redis object implementation */
821void decrRefCount(void *o);
822void incrRefCount(robj *o);
4dd444bb 823robj *resetRefCount(robj *obj);
e2641e09 824void freeStringObject(robj *o);
825void freeListObject(robj *o);
826void freeSetObject(robj *o);
827void freeZsetObject(robj *o);
828void freeHashObject(robj *o);
829robj *createObject(int type, void *ptr);
830robj *createStringObject(char *ptr, size_t len);
831robj *dupStringObject(robj *o);
5d081931 832int isObjectRepresentableAsLongLong(robj *o, long long *llongval);
e2641e09 833robj *tryObjectEncoding(robj *o);
834robj *getDecodedObject(robj *o);
835size_t stringObjectLen(robj *o);
e2641e09 836robj *createStringObjectFromLongLong(long long value);
5574b53e 837robj *createStringObjectFromLongDouble(long double value);
e2641e09 838robj *createListObject(void);
839robj *createZiplistObject(void);
840robj *createSetObject(void);
96ffb2fe 841robj *createIntsetObject(void);
e2641e09 842robj *createHashObject(void);
843robj *createZsetObject(void);
9e7cee0e 844robj *createZsetZiplistObject(void);
e2641e09 845int getLongFromObjectOrReply(redisClient *c, robj *o, long *target, const char *msg);
846int checkType(redisClient *c, robj *o, int type);
847int getLongLongFromObjectOrReply(redisClient *c, robj *o, long long *target, const char *msg);
848int getDoubleFromObjectOrReply(redisClient *c, robj *o, double *target, const char *msg);
849int getLongLongFromObject(robj *o, long long *target);
5574b53e 850int getLongDoubleFromObject(robj *o, long double *target);
851int getLongDoubleFromObjectOrReply(redisClient *c, robj *o, long double *target, const char *msg);
e2641e09 852char *strEncoding(int encoding);
853int compareStringObjects(robj *a, robj *b);
854int equalStringObjects(robj *a, robj *b);
ef59a8bc 855unsigned long estimateObjectIdleTime(robj *o);
e2641e09 856
19e61097 857/* Synchronous I/O with timeout */
4ccf671c 858ssize_t syncWrite(int fd, char *ptr, ssize_t size, long long timeout);
859ssize_t syncRead(int fd, char *ptr, ssize_t size, long long timeout);
860ssize_t syncReadLine(int fd, char *ptr, ssize_t size, long long timeout);
19e61097 861
e2641e09 862/* Replication */
863void replicationFeedSlaves(list *slaves, int dictid, robj **argv, int argc);
e31b615e 864void replicationFeedMonitors(redisClient *c, list *monitors, int dictid, robj **argv, int argc);
e2641e09 865void updateSlavesWaitingBgsave(int bgsaveerr);
f4aa600b 866void replicationCron(void);
e2641e09 867
97e7f8ae 868/* Generic persistence functions */
869void startLoading(FILE *fp);
870void loadingProgress(off_t pos);
871void stopLoading(void);
872
e2641e09 873/* RDB persistence */
2e4b0e77 874#include "rdb.h"
e2641e09 875
876/* AOF persistence */
a3fcd6bc 877void flushAppendOnlyFile(int force);
e2641e09 878void feedAppendOnlyFile(struct redisCommand *cmd, int dictid, robj **argv, int argc);
879void aofRemoveTempFile(pid_t childpid);
880int rewriteAppendOnlyFileBackground(void);
881int loadAppendOnlyFile(char *filename);
882void stopAppendOnly(void);
883int startAppendOnly(void);
36c17a53 884void backgroundRewriteDoneHandler(int exitcode, int bysignal);
edb95253 885void aofRewriteBufferReset(void);
886unsigned long aofRewriteBufferSize(void);
e2641e09 887
888/* Sorted sets data type */
c772d9c6 889
890/* Struct to hold a inclusive/exclusive range spec. */
891typedef struct {
892 double min, max;
893 int minex, maxex; /* are min or max exclusive? */
894} zrangespec;
895
e2641e09 896zskiplist *zslCreate(void);
897void zslFree(zskiplist *zsl);
69ef89f2 898zskiplistNode *zslInsert(zskiplist *zsl, double score, robj *obj);
8588bfa3 899unsigned char *zzlInsert(unsigned char *zl, robj *ele, double score);
c772d9c6 900int zslDelete(zskiplist *zsl, double score, robj *obj);
901zskiplistNode *zslFirstInRange(zskiplist *zsl, zrangespec range);
dddf5335
PN
902double zzlGetScore(unsigned char *sptr);
903void zzlNext(unsigned char *zl, unsigned char **eptr, unsigned char **sptr);
904void zzlPrev(unsigned char *zl, unsigned char **eptr, unsigned char **sptr);
df26a0ae
PN
905unsigned int zsetLength(robj *zobj);
906void zsetConvert(robj *zobj, int encoding);
e2641e09 907
908/* Core functions */
f6b32c14 909int freeMemoryIfNeeded(void);
e2641e09 910int processCommand(redisClient *c);
633a9410 911void setupSignalHandlers(void);
1b1f47c9 912struct redisCommand *lookupCommand(sds name);
913struct redisCommand *lookupCommandByCString(char *s);
ce8b772b 914void call(redisClient *c, int flags);
d8b1228b 915void propagate(struct redisCommand *cmd, int dbid, robj **argv, int argc, int flags);
eeb34eff 916void alsoPropagate(struct redisCommand *cmd, int dbid, robj **argv, int argc, int target);
e2641e09 917int prepareForShutdown();
918void redisLog(int level, const char *fmt, ...);
288f811f 919void redisLogRaw(int level, const char *msg);
be4f8ccc 920void redisLogFromHandler(int level, const char *msg);
e2641e09 921void usage();
922void updateDictResizePolicy(void);
923int htNeedsResize(dict *dict);
924void oom(const char *msg);
1b1f47c9 925void populateCommandTable(void);
d7ed7fd2 926void resetCommandTableStats(void);
e2641e09 927
96ffb2fe
PN
928/* Set data type */
929robj *setTypeCreate(robj *value);
930int setTypeAdd(robj *subject, robj *value);
931int setTypeRemove(robj *subject, robj *value);
932int setTypeIsMember(robj *subject, robj *value);
cb72d0f1
PN
933setTypeIterator *setTypeInitIterator(robj *subject);
934void setTypeReleaseIterator(setTypeIterator *si);
1b508da7 935int setTypeNext(setTypeIterator *si, robj **objele, int64_t *llele);
936robj *setTypeNextObject(setTypeIterator *si);
dd48de74 937int setTypeRandomElement(robj *setobj, robj **objele, int64_t *llele);
96ffb2fe
PN
938unsigned long setTypeSize(robj *subject);
939void setTypeConvert(robj *subject, int enc);
940
e2641e09 941/* Hash data type */
ebd85e9a 942void hashTypeConvert(robj *o, int enc);
e2641e09 943void hashTypeTryConversion(robj *subject, robj **argv, int start, int end);
944void hashTypeTryObjectEncoding(robj *subject, robj **o1, robj **o2);
3d24304f 945robj *hashTypeGetObject(robj *o, robj *key);
e2641e09 946int hashTypeExists(robj *o, robj *key);
947int hashTypeSet(robj *o, robj *key, robj *value);
948int hashTypeDelete(robj *o, robj *key);
949unsigned long hashTypeLength(robj *o);
950hashTypeIterator *hashTypeInitIterator(robj *subject);
951void hashTypeReleaseIterator(hashTypeIterator *hi);
952int hashTypeNext(hashTypeIterator *hi);
ebd85e9a
PN
953void hashTypeCurrentFromZiplist(hashTypeIterator *hi, int what,
954 unsigned char **vstr,
955 unsigned int *vlen,
956 long long *vll);
957void hashTypeCurrentFromHashTable(hashTypeIterator *hi, int what, robj **dst);
8c304be3 958robj *hashTypeCurrentObject(hashTypeIterator *hi, int what);
e2641e09 959robj *hashTypeLookupWriteOrCreate(redisClient *c, robj *key);
960
961/* Pub / Sub */
962int pubsubUnsubscribeAllChannels(redisClient *c, int notify);
963int pubsubUnsubscribeAllPatterns(redisClient *c, int notify);
964void freePubsubPattern(void *p);
965int listMatchPubsubPattern(void *a, void *b);
d38ef520 966int pubsubPublishMessage(robj *channel, robj *message);
e2641e09 967
e2641e09 968/* Configuration */
67c6f0f6 969void loadServerConfig(char *filename, char *options);
e2641e09 970void appendServerSaveParams(time_t seconds, int changes);
971void resetServerSaveParams();
972
973/* db.c -- Keyspace access API */
974int removeExpire(redisDb *db, robj *key);
bcf2995c 975void propagateExpire(redisDb *db, robj *key);
e2641e09 976int expireIfNeeded(redisDb *db, robj *key);
7dcc10b6 977long long getExpire(redisDb *db, robj *key);
978void setExpire(redisDb *db, robj *key, long long when);
e2641e09 979robj *lookupKey(redisDb *db, robj *key);
980robj *lookupKeyRead(redisDb *db, robj *key);
981robj *lookupKeyWrite(redisDb *db, robj *key);
982robj *lookupKeyReadOrReply(redisClient *c, robj *key, robj *reply);
983robj *lookupKeyWriteOrReply(redisClient *c, robj *key, robj *reply);
f85cd526 984void dbAdd(redisDb *db, robj *key, robj *val);
985void dbOverwrite(redisDb *db, robj *key, robj *val);
986void setKey(redisDb *db, robj *key, robj *val);
e2641e09 987int dbExists(redisDb *db, robj *key);
988robj *dbRandomKey(redisDb *db);
989int dbDelete(redisDb *db, robj *key);
990long long emptyDb();
991int selectDb(redisClient *c, int id);
cea8c5cd 992void signalModifiedKey(redisDb *db, robj *key);
993void signalFlushedDb(int dbid);
484354ff 994unsigned int GetKeysInSlot(unsigned int hashslot, robj **keys, unsigned int count);
e2641e09 995
9791f0f8 996/* API to get key arguments from commands */
997#define REDIS_GETKEYS_ALL 0
998#define REDIS_GETKEYS_PRELOAD 1
999int *getKeysFromCommand(struct redisCommand *cmd, robj **argv, int argc, int *numkeys, int flags);
1000void getKeysFreeResult(int *result);
1001int *noPreloadGetKeys(struct redisCommand *cmd,robj **argv, int argc, int *numkeys, int flags);
1002int *renameGetKeys(struct redisCommand *cmd,robj **argv, int argc, int *numkeys, int flags);
1003int *zunionInterGetKeys(struct redisCommand *cmd,robj **argv, int argc, int *numkeys, int flags);
1004
120ba392 1005/* Sentinel */
1006void initSentinelConfig(void);
1007void initSentinel(void);
1008void sentinelTimer(void);
1009char *sentinelHandleConfiguration(char **argv, int argc);
1010
7585836e 1011/* Scripting */
1012void scriptingInit(void);
1013
e2641e09 1014/* Git SHA1 */
1015char *redisGitSHA1(void);
1016char *redisGitDirty(void);
1017
1018/* Commands prototypes */
1019void authCommand(redisClient *c);
1020void pingCommand(redisClient *c);
1021void echoCommand(redisClient *c);
1022void setCommand(redisClient *c);
1023void setnxCommand(redisClient *c);
1024void setexCommand(redisClient *c);
12d293ca 1025void psetexCommand(redisClient *c);
e2641e09 1026void getCommand(redisClient *c);
1027void delCommand(redisClient *c);
1028void existsCommand(redisClient *c);
3c1bf495
PN
1029void setbitCommand(redisClient *c);
1030void getbitCommand(redisClient *c);
9f9e1cea 1031void setrangeCommand(redisClient *c);
ef11bccc 1032void getrangeCommand(redisClient *c);
e2641e09 1033void incrCommand(redisClient *c);
1034void decrCommand(redisClient *c);
1035void incrbyCommand(redisClient *c);
1036void decrbyCommand(redisClient *c);
5574b53e 1037void incrbyfloatCommand(redisClient *c);
e2641e09 1038void selectCommand(redisClient *c);
1039void randomkeyCommand(redisClient *c);
1040void keysCommand(redisClient *c);
1041void dbsizeCommand(redisClient *c);
1042void lastsaveCommand(redisClient *c);
1043void saveCommand(redisClient *c);
1044void bgsaveCommand(redisClient *c);
1045void bgrewriteaofCommand(redisClient *c);
1046void shutdownCommand(redisClient *c);
1047void moveCommand(redisClient *c);
1048void renameCommand(redisClient *c);
1049void renamenxCommand(redisClient *c);
1050void lpushCommand(redisClient *c);
1051void rpushCommand(redisClient *c);
1052void lpushxCommand(redisClient *c);
1053void rpushxCommand(redisClient *c);
1054void linsertCommand(redisClient *c);
1055void lpopCommand(redisClient *c);
1056void rpopCommand(redisClient *c);
1057void llenCommand(redisClient *c);
1058void lindexCommand(redisClient *c);
1059void lrangeCommand(redisClient *c);
1060void ltrimCommand(redisClient *c);
1061void typeCommand(redisClient *c);
1062void lsetCommand(redisClient *c);
1063void saddCommand(redisClient *c);
1064void sremCommand(redisClient *c);
1065void smoveCommand(redisClient *c);
1066void sismemberCommand(redisClient *c);
1067void scardCommand(redisClient *c);
1068void spopCommand(redisClient *c);
1069void srandmemberCommand(redisClient *c);
1070void sinterCommand(redisClient *c);
1071void sinterstoreCommand(redisClient *c);
1072void sunionCommand(redisClient *c);
1073void sunionstoreCommand(redisClient *c);
1074void sdiffCommand(redisClient *c);
1075void sdiffstoreCommand(redisClient *c);
1076void syncCommand(redisClient *c);
1077void flushdbCommand(redisClient *c);
1078void flushallCommand(redisClient *c);
1079void sortCommand(redisClient *c);
1080void lremCommand(redisClient *c);
8a979f03 1081void rpoplpushCommand(redisClient *c);
e2641e09 1082void infoCommand(redisClient *c);
1083void mgetCommand(redisClient *c);
1084void monitorCommand(redisClient *c);
1085void expireCommand(redisClient *c);
1086void expireatCommand(redisClient *c);
12d293ca 1087void pexpireCommand(redisClient *c);
1088void pexpireatCommand(redisClient *c);
e2641e09 1089void getsetCommand(redisClient *c);
1090void ttlCommand(redisClient *c);
12d293ca 1091void pttlCommand(redisClient *c);
a539d29a 1092void persistCommand(redisClient *c);
e2641e09 1093void slaveofCommand(redisClient *c);
1094void debugCommand(redisClient *c);
1095void msetCommand(redisClient *c);
1096void msetnxCommand(redisClient *c);
1097void zaddCommand(redisClient *c);
1098void zincrbyCommand(redisClient *c);
1099void zrangeCommand(redisClient *c);
1100void zrangebyscoreCommand(redisClient *c);
25bb8a44 1101void zrevrangebyscoreCommand(redisClient *c);
e2641e09 1102void zcountCommand(redisClient *c);
1103void zrevrangeCommand(redisClient *c);
1104void zcardCommand(redisClient *c);
1105void zremCommand(redisClient *c);
1106void zscoreCommand(redisClient *c);
1107void zremrangebyscoreCommand(redisClient *c);
1108void multiCommand(redisClient *c);
1109void execCommand(redisClient *c);
1110void discardCommand(redisClient *c);
1111void blpopCommand(redisClient *c);
1112void brpopCommand(redisClient *c);
b2a7fd0c 1113void brpoplpushCommand(redisClient *c);
e2641e09 1114void appendCommand(redisClient *c);
80091bba 1115void strlenCommand(redisClient *c);
e2641e09 1116void zrankCommand(redisClient *c);
1117void zrevrankCommand(redisClient *c);
1118void hsetCommand(redisClient *c);
1119void hsetnxCommand(redisClient *c);
1120void hgetCommand(redisClient *c);
1121void hmsetCommand(redisClient *c);
1122void hmgetCommand(redisClient *c);
1123void hdelCommand(redisClient *c);
1124void hlenCommand(redisClient *c);
1125void zremrangebyrankCommand(redisClient *c);
1126void zunionstoreCommand(redisClient *c);
1127void zinterstoreCommand(redisClient *c);
1128void hkeysCommand(redisClient *c);
1129void hvalsCommand(redisClient *c);
1130void hgetallCommand(redisClient *c);
1131void hexistsCommand(redisClient *c);
1132void configCommand(redisClient *c);
1133void hincrbyCommand(redisClient *c);
68bfe993 1134void hincrbyfloatCommand(redisClient *c);
e2641e09 1135void subscribeCommand(redisClient *c);
1136void unsubscribeCommand(redisClient *c);
1137void psubscribeCommand(redisClient *c);
1138void punsubscribeCommand(redisClient *c);
1139void publishCommand(redisClient *c);
1140void watchCommand(redisClient *c);
1141void unwatchCommand(redisClient *c);
ecc91094 1142void restoreCommand(redisClient *c);
1143void migrateCommand(redisClient *c);
626f6b2d 1144void dumpCommand(redisClient *c);
ece74202 1145void objectCommand(redisClient *c);
3cd12b56 1146void clientCommand(redisClient *c);
7585836e 1147void evalCommand(redisClient *c);
7229d60d 1148void evalShaCommand(redisClient *c);
070e3945 1149void scriptCommand(redisClient *c);
9494f1f1 1150void timeCommand(redisClient *c);
4a789dec 1151void bitopCommand(redisClient *c);
1152void bitcountCommand(redisClient *c);
dbd8c753 1153void replconfCommand(redisClient *c);
e2641e09 1154
b3aa6d71 1155#if defined(__GNUC__)
b3aa6d71 1156void *calloc(size_t count, size_t size) __attribute__ ((deprecated));
1157void free(void *ptr) __attribute__ ((deprecated));
1158void *malloc(size_t size) __attribute__ ((deprecated));
1159void *realloc(void *ptr, size_t size) __attribute__ ((deprecated));
1160#endif
1161
e3e69935 1162/* Debugging stuff */
bab205f7 1163void _redisAssertWithInfo(redisClient *c, robj *o, char *estr, char *file, int line);
e3e69935 1164void _redisAssert(char *estr, char *file, int line);
1165void _redisPanic(char *msg, char *file, int line);
fa5af017 1166void bugReportStart(void);
00010fa9 1167void redisLogObjectDebugInfo(robj *o);
d4d20859 1168void sigsegvHandler(int sig, siginfo_t *info, void *secret);
1169sds genRedisInfoString(char *section);
fc030ac7 1170void enableWatchdog(int period);
1171void disableWatchdog(void);
1172void watchdogScheduleSignal(int period);
0b8441c1 1173void redisLogHexDump(int level, char *descr, void *value, size_t len);
120ba392 1174
1175#define redisDebug(fmt, ...) \
1176 printf("DEBUG %s:%d > " fmt "\n", __FILE__, __LINE__, __VA_ARGS__)
1177#define redisDebugMark() \
1178 printf("-- MARK %s:%d --\n", __FILE__, __LINE__)
1179
e2641e09 1180#endif