1 | /* |
2 | * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com> |
3 | * All rights reserved. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions are met: |
7 | * |
8 | * * Redistributions of source code must retain the above copyright notice, |
9 | * this list of conditions and the following disclaimer. |
10 | * * Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. |
13 | * * Neither the name of Redis nor the names of its contributors may be used |
14 | * to endorse or promote products derived from this software without |
15 | * specific prior written permission. |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
21 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
27 | * POSSIBILITY OF SUCH DAMAGE. |
28 | */ |
29 | |
30 | #ifndef __RDB_H |
31 | #define __RDB_H |
32 | |
33 | #include <stdio.h> |
34 | #include "rio.h" |
35 | |
36 | /* TBD: include only necessary headers. */ |
37 | #include "server.h" |
38 | |
39 | /* The current RDB version. When the format changes in a way that is no longer |
40 | * backward compatible this number gets incremented. */ |
41 | #define RDB_VERSION 10 |
42 | |
43 | /* Defines related to the dump file format. To store 32 bits lengths for short |
44 | * keys requires a lot of space, so we check the most significant 2 bits of |
45 | * the first byte to interpreter the length: |
46 | * |
47 | * 00|XXXXXX => if the two MSB are 00 the len is the 6 bits of this byte |
48 | * 01|XXXXXX XXXXXXXX => 01, the len is 14 bits, 6 bits + 8 bits of next byte |
49 | * 10|000000 [32 bit integer] => A full 32 bit len in net byte order will follow |
50 | * 10|000001 [64 bit integer] => A full 64 bit len in net byte order will follow |
51 | * 11|OBKIND this means: specially encoded object will follow. The six bits |
52 | * number specify the kind of object that follows. |
53 | * See the RDB_ENC_* defines. |
54 | * |
55 | * Lengths up to 63 are stored using a single byte, most DB keys, and may |
56 | * values, will fit inside. */ |
57 | #define RDB_6BITLEN 0 |
58 | #define RDB_14BITLEN 1 |
59 | #define RDB_32BITLEN 0x80 |
60 | #define RDB_64BITLEN 0x81 |
61 | #define RDB_ENCVAL 3 |
62 | #define RDB_LENERR UINT64_MAX |
63 | |
64 | /* When a length of a string object stored on disk has the first two bits |
65 | * set, the remaining six bits specify a special encoding for the object |
66 | * accordingly to the following defines: */ |
67 | #define RDB_ENC_INT8 0 /* 8 bit signed integer */ |
68 | #define RDB_ENC_INT16 1 /* 16 bit signed integer */ |
69 | #define RDB_ENC_INT32 2 /* 32 bit signed integer */ |
70 | #define RDB_ENC_LZF 3 /* string compressed with FASTLZ */ |
71 | |
72 | /* Map object types to RDB object types. Macros starting with OBJ_ are for |
73 | * memory storage and may change. Instead RDB types must be fixed because |
74 | * we store them on disk. */ |
75 | #define RDB_TYPE_STRING 0 |
76 | #define RDB_TYPE_LIST 1 |
77 | #define RDB_TYPE_SET 2 |
78 | #define RDB_TYPE_ZSET 3 |
79 | #define RDB_TYPE_HASH 4 |
80 | #define RDB_TYPE_ZSET_2 5 /* ZSET version 2 with doubles stored in binary. */ |
81 | #define RDB_TYPE_MODULE 6 |
82 | #define RDB_TYPE_MODULE_2 7 /* Module value with annotations for parsing without |
83 | the generating module being loaded. */ |
84 | /* NOTE: WHEN ADDING NEW RDB TYPE, UPDATE rdbIsObjectType() BELOW */ |
85 | |
86 | /* Object types for encoded objects. */ |
87 | #define RDB_TYPE_HASH_ZIPMAP 9 |
88 | #define RDB_TYPE_LIST_ZIPLIST 10 |
89 | #define RDB_TYPE_SET_INTSET 11 |
90 | #define RDB_TYPE_ZSET_ZIPLIST 12 |
91 | #define RDB_TYPE_HASH_ZIPLIST 13 |
92 | #define RDB_TYPE_LIST_QUICKLIST 14 |
93 | #define RDB_TYPE_STREAM_LISTPACKS 15 |
94 | #define RDB_TYPE_HASH_LISTPACK 16 |
95 | #define RDB_TYPE_ZSET_LISTPACK 17 |
96 | #define RDB_TYPE_LIST_QUICKLIST_2 18 |
97 | #define RDB_TYPE_STREAM_LISTPACKS_2 19 |
98 | /* NOTE: WHEN ADDING NEW RDB TYPE, UPDATE rdbIsObjectType() BELOW */ |
99 | |
100 | /* Test if a type is an object type. */ |
101 | #define rdbIsObjectType(t) ((t >= 0 && t <= 7) || (t >= 9 && t <= 19)) |
102 | |
103 | /* Special RDB opcodes (saved/loaded with rdbSaveType/rdbLoadType). */ |
104 | #define RDB_OPCODE_FUNCTION2 245 /* function library data */ |
105 | #define RDB_OPCODE_FUNCTION 246 /* old function library data for 7.0 rc1 and rc2 */ |
106 | #define RDB_OPCODE_MODULE_AUX 247 /* Module auxiliary data. */ |
107 | #define RDB_OPCODE_IDLE 248 /* LRU idle time. */ |
108 | #define RDB_OPCODE_FREQ 249 /* LFU frequency. */ |
109 | #define RDB_OPCODE_AUX 250 /* RDB aux field. */ |
110 | #define RDB_OPCODE_RESIZEDB 251 /* Hash table resize hint. */ |
111 | #define RDB_OPCODE_EXPIRETIME_MS 252 /* Expire time in milliseconds. */ |
112 | #define RDB_OPCODE_EXPIRETIME 253 /* Old expire time in seconds. */ |
113 | #define RDB_OPCODE_SELECTDB 254 /* DB number of the following keys. */ |
114 | #define RDB_OPCODE_EOF 255 /* End of the RDB file. */ |
115 | |
116 | /* Module serialized values sub opcodes */ |
117 | #define RDB_MODULE_OPCODE_EOF 0 /* End of module value. */ |
118 | #define RDB_MODULE_OPCODE_SINT 1 /* Signed integer. */ |
119 | #define RDB_MODULE_OPCODE_UINT 2 /* Unsigned integer. */ |
120 | #define RDB_MODULE_OPCODE_FLOAT 3 /* Float. */ |
121 | #define RDB_MODULE_OPCODE_DOUBLE 4 /* Double. */ |
122 | #define RDB_MODULE_OPCODE_STRING 5 /* String. */ |
123 | |
124 | /* rdbLoad...() functions flags. */ |
125 | #define RDB_LOAD_NONE 0 |
126 | #define RDB_LOAD_ENC (1<<0) |
127 | #define RDB_LOAD_PLAIN (1<<1) |
128 | #define RDB_LOAD_SDS (1<<2) |
129 | |
130 | /* flags on the purpose of rdb save or load */ |
131 | #define RDBFLAGS_NONE 0 /* No special RDB loading. */ |
132 | #define RDBFLAGS_AOF_PREAMBLE (1<<0) /* Load/save the RDB as AOF preamble. */ |
133 | #define RDBFLAGS_REPLICATION (1<<1) /* Load/save for SYNC. */ |
134 | #define RDBFLAGS_ALLOW_DUP (1<<2) /* Allow duplicated keys when loading.*/ |
135 | #define RDBFLAGS_FEED_REPL (1<<3) /* Feed replication stream when loading.*/ |
136 | |
137 | /* When rdbLoadObject() returns NULL, the err flag is |
138 | * set to hold the type of error that occurred */ |
139 | #define RDB_LOAD_ERR_EMPTY_KEY 1 /* Error of empty key */ |
140 | #define RDB_LOAD_ERR_OTHER 2 /* Any other errors */ |
141 | |
142 | int rdbSaveType(rio *rdb, unsigned char type); |
143 | int rdbLoadType(rio *rdb); |
144 | time_t rdbLoadTime(rio *rdb); |
145 | int rdbSaveLen(rio *rdb, uint64_t len); |
146 | int rdbSaveMillisecondTime(rio *rdb, long long t); |
147 | long long rdbLoadMillisecondTime(rio *rdb, int rdbver); |
148 | uint64_t rdbLoadLen(rio *rdb, int *isencoded); |
149 | int rdbLoadLenByRef(rio *rdb, int *isencoded, uint64_t *lenptr); |
150 | int rdbSaveObjectType(rio *rdb, robj *o); |
151 | int rdbLoadObjectType(rio *rdb); |
152 | int rdbLoad(char *filename, rdbSaveInfo *rsi, int rdbflags); |
153 | int rdbSaveBackground(int req, char *filename, rdbSaveInfo *rsi); |
154 | int rdbSaveToSlavesSockets(int req, rdbSaveInfo *rsi); |
155 | void rdbRemoveTempFile(pid_t childpid, int from_signal); |
156 | int rdbSave(int req, char *filename, rdbSaveInfo *rsi); |
157 | ssize_t rdbSaveObject(rio *rdb, robj *o, robj *key, int dbid); |
158 | size_t rdbSavedObjectLen(robj *o, robj *key, int dbid); |
159 | robj *rdbLoadObject(int type, rio *rdb, sds key, int dbid, int *error); |
160 | void backgroundSaveDoneHandler(int exitcode, int bysignal); |
161 | int rdbSaveKeyValuePair(rio *rdb, robj *key, robj *val, long long expiretime,int dbid); |
162 | ssize_t rdbSaveSingleModuleAux(rio *rdb, int when, moduleType *mt); |
163 | robj *rdbLoadCheckModuleValue(rio *rdb, char *modulename); |
164 | robj *rdbLoadStringObject(rio *rdb); |
165 | ssize_t rdbSaveStringObject(rio *rdb, robj *obj); |
166 | ssize_t rdbSaveRawString(rio *rdb, unsigned char *s, size_t len); |
167 | void *rdbGenericLoadStringObject(rio *rdb, int flags, size_t *lenptr); |
168 | int rdbSaveBinaryDoubleValue(rio *rdb, double val); |
169 | int rdbLoadBinaryDoubleValue(rio *rdb, double *val); |
170 | int rdbSaveBinaryFloatValue(rio *rdb, float val); |
171 | int rdbLoadBinaryFloatValue(rio *rdb, float *val); |
172 | int rdbLoadRio(rio *rdb, int rdbflags, rdbSaveInfo *rsi); |
173 | int rdbLoadRioWithLoadingCtx(rio *rdb, int rdbflags, rdbSaveInfo *rsi, rdbLoadingCtx *rdb_loading_ctx); |
174 | int rdbFunctionLoad(rio *rdb, int ver, functionsLibCtx* lib_ctx, int type, int rdbflags, sds *err); |
175 | int rdbSaveRio(int req, rio *rdb, int *error, int rdbflags, rdbSaveInfo *rsi); |
176 | ssize_t rdbSaveFunctions(rio *rdb); |
177 | rdbSaveInfo *rdbPopulateSaveInfo(rdbSaveInfo *rsi); |
178 | |
179 | #endif |
180 | |