]>
Commit | Line | Data |
---|---|---|
9e6a9f30 | 1 | #include "redis.h" |
2 | #include "lzf.h" /* LZF compression library */ | |
ebd85e9a | 3 | #include "zipmap.h" |
7f4f86f4 | 4 | #include "endianconv.h" |
9e6a9f30 | 5 | |
e2641e09 | 6 | #include <math.h> |
3688d7f3 | 7 | #include <sys/types.h> |
8 | #include <sys/time.h> | |
9 | #include <sys/resource.h> | |
10 | #include <sys/wait.h> | |
11 | #include <arpa/inet.h> | |
97e7f8ae | 12 | #include <sys/stat.h> |
e2641e09 | 13 | |
2e4b0e77 | 14 | static int rdbWriteRaw(rio *rdb, void *p, size_t len) { |
041d8e2a | 15 | if (rdb && rioWrite(rdb,p,len) == 0) |
2e4b0e77 | 16 | return -1; |
9a68cf91 PN |
17 | return len; |
18 | } | |
19 | ||
2e4b0e77 PN |
20 | int rdbSaveType(rio *rdb, unsigned char type) { |
21 | return rdbWriteRaw(rdb,&type,1); | |
e2641e09 | 22 | } |
23 | ||
ebdcd723 | 24 | /* Load a "type" in RDB format, that is a one byte unsigned integer. |
25 | * This function is not only used to load object types, but also special | |
26 | * "types" like the end-of-file type, the EXPIRE type, and so forth. */ | |
221782cc PN |
27 | int rdbLoadType(rio *rdb) { |
28 | unsigned char type; | |
29 | if (rioRead(rdb,&type,1) == 0) return -1; | |
30 | return type; | |
e2641e09 | 31 | } |
32 | ||
221782cc PN |
33 | time_t rdbLoadTime(rio *rdb) { |
34 | int32_t t32; | |
35 | if (rioRead(rdb,&t32,4) == 0) return -1; | |
36 | return (time_t)t32; | |
e2641e09 | 37 | } |
38 | ||
bdbdb02e | 39 | int rdbSaveMillisecondTime(rio *rdb, long long t) { |
7dcc10b6 | 40 | int64_t t64 = (int64_t) t; |
41 | return rdbWriteRaw(rdb,&t64,8); | |
42 | } | |
43 | ||
44 | long long rdbLoadMillisecondTime(rio *rdb) { | |
45 | int64_t t64; | |
46 | if (rioRead(rdb,&t64,8) == 0) return -1; | |
47 | return (long long)t64; | |
48 | } | |
49 | ||
221782cc PN |
50 | /* Saves an encoded length. The first two bits in the first byte are used to |
51 | * hold the encoding type. See the REDIS_RDB_* definitions for more information | |
52 | * on the types of encoding. */ | |
2e4b0e77 | 53 | int rdbSaveLen(rio *rdb, uint32_t len) { |
e2641e09 | 54 | unsigned char buf[2]; |
2e4b0e77 | 55 | size_t nwritten; |
e2641e09 | 56 | |
57 | if (len < (1<<6)) { | |
58 | /* Save a 6 bit len */ | |
59 | buf[0] = (len&0xFF)|(REDIS_RDB_6BITLEN<<6); | |
2e4b0e77 | 60 | if (rdbWriteRaw(rdb,buf,1) == -1) return -1; |
8a623a98 | 61 | nwritten = 1; |
e2641e09 | 62 | } else if (len < (1<<14)) { |
63 | /* Save a 14 bit len */ | |
64 | buf[0] = ((len>>8)&0xFF)|(REDIS_RDB_14BITLEN<<6); | |
65 | buf[1] = len&0xFF; | |
2e4b0e77 | 66 | if (rdbWriteRaw(rdb,buf,2) == -1) return -1; |
8a623a98 | 67 | nwritten = 2; |
e2641e09 | 68 | } else { |
69 | /* Save a 32 bit len */ | |
70 | buf[0] = (REDIS_RDB_32BITLEN<<6); | |
2e4b0e77 | 71 | if (rdbWriteRaw(rdb,buf,1) == -1) return -1; |
e2641e09 | 72 | len = htonl(len); |
2e4b0e77 | 73 | if (rdbWriteRaw(rdb,&len,4) == -4) return -1; |
8a623a98 | 74 | nwritten = 1+4; |
e2641e09 | 75 | } |
8a623a98 | 76 | return nwritten; |
e2641e09 | 77 | } |
78 | ||
221782cc PN |
79 | /* Load an encoded length. The "isencoded" argument is set to 1 if the length |
80 | * is not actually a length but an "encoding type". See the REDIS_RDB_ENC_* | |
81 | * definitions in rdb.h for more information. */ | |
82 | uint32_t rdbLoadLen(rio *rdb, int *isencoded) { | |
83 | unsigned char buf[2]; | |
84 | uint32_t len; | |
85 | int type; | |
86 | ||
87 | if (isencoded) *isencoded = 0; | |
88 | if (rioRead(rdb,buf,1) == 0) return REDIS_RDB_LENERR; | |
89 | type = (buf[0]&0xC0)>>6; | |
90 | if (type == REDIS_RDB_ENCVAL) { | |
91 | /* Read a 6 bit encoding type. */ | |
92 | if (isencoded) *isencoded = 1; | |
93 | return buf[0]&0x3F; | |
94 | } else if (type == REDIS_RDB_6BITLEN) { | |
95 | /* Read a 6 bit len. */ | |
96 | return buf[0]&0x3F; | |
97 | } else if (type == REDIS_RDB_14BITLEN) { | |
98 | /* Read a 14 bit len. */ | |
99 | if (rioRead(rdb,buf+1,1) == 0) return REDIS_RDB_LENERR; | |
100 | return ((buf[0]&0x3F)<<8)|buf[1]; | |
101 | } else { | |
102 | /* Read a 32 bit len. */ | |
103 | if (rioRead(rdb,&len,4) == 0) return REDIS_RDB_LENERR; | |
104 | return ntohl(len); | |
105 | } | |
106 | } | |
107 | ||
108 | /* Encodes the "value" argument as integer when it fits in the supported ranges | |
109 | * for encoded types. If the function successfully encodes the integer, the | |
110 | * representation is stored in the buffer pointer to by "enc" and the string | |
111 | * length is returned. Otherwise 0 is returned. */ | |
e2641e09 | 112 | int rdbEncodeInteger(long long value, unsigned char *enc) { |
e2641e09 | 113 | if (value >= -(1<<7) && value <= (1<<7)-1) { |
114 | enc[0] = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_INT8; | |
115 | enc[1] = value&0xFF; | |
116 | return 2; | |
117 | } else if (value >= -(1<<15) && value <= (1<<15)-1) { | |
118 | enc[0] = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_INT16; | |
119 | enc[1] = value&0xFF; | |
120 | enc[2] = (value>>8)&0xFF; | |
121 | return 3; | |
122 | } else if (value >= -((long long)1<<31) && value <= ((long long)1<<31)-1) { | |
123 | enc[0] = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_INT32; | |
124 | enc[1] = value&0xFF; | |
125 | enc[2] = (value>>8)&0xFF; | |
126 | enc[3] = (value>>16)&0xFF; | |
127 | enc[4] = (value>>24)&0xFF; | |
128 | return 5; | |
129 | } else { | |
130 | return 0; | |
131 | } | |
132 | } | |
133 | ||
221782cc PN |
134 | /* Loads an integer-encoded object with the specified encoding type "enctype". |
135 | * If the "encode" argument is set the function may return an integer-encoded | |
136 | * string object, otherwise it always returns a raw string object. */ | |
137 | robj *rdbLoadIntegerObject(rio *rdb, int enctype, int encode) { | |
138 | unsigned char enc[4]; | |
139 | long long val; | |
140 | ||
141 | if (enctype == REDIS_RDB_ENC_INT8) { | |
142 | if (rioRead(rdb,enc,1) == 0) return NULL; | |
143 | val = (signed char)enc[0]; | |
144 | } else if (enctype == REDIS_RDB_ENC_INT16) { | |
145 | uint16_t v; | |
146 | if (rioRead(rdb,enc,2) == 0) return NULL; | |
147 | v = enc[0]|(enc[1]<<8); | |
148 | val = (int16_t)v; | |
149 | } else if (enctype == REDIS_RDB_ENC_INT32) { | |
150 | uint32_t v; | |
151 | if (rioRead(rdb,enc,4) == 0) return NULL; | |
152 | v = enc[0]|(enc[1]<<8)|(enc[2]<<16)|(enc[3]<<24); | |
153 | val = (int32_t)v; | |
154 | } else { | |
155 | val = 0; /* anti-warning */ | |
156 | redisPanic("Unknown RDB integer encoding type"); | |
157 | } | |
158 | if (encode) | |
159 | return createStringObjectFromLongLong(val); | |
160 | else | |
161 | return createObject(REDIS_STRING,sdsfromlonglong(val)); | |
162 | } | |
163 | ||
e2641e09 | 164 | /* String objects in the form "2391" "-100" without any space and with a |
165 | * range of values that can fit in an 8, 16 or 32 bit signed value can be | |
166 | * encoded as integers to save space */ | |
167 | int rdbTryIntegerEncoding(char *s, size_t len, unsigned char *enc) { | |
168 | long long value; | |
169 | char *endptr, buf[32]; | |
170 | ||
171 | /* Check if it's possible to encode this value as a number */ | |
172 | value = strtoll(s, &endptr, 10); | |
173 | if (endptr[0] != '\0') return 0; | |
174 | ll2string(buf,32,value); | |
175 | ||
176 | /* If the number converted back into a string is not identical | |
177 | * then it's not possible to encode the string as integer */ | |
178 | if (strlen(buf) != len || memcmp(buf,s,len)) return 0; | |
179 | ||
180 | return rdbEncodeInteger(value,enc); | |
181 | } | |
182 | ||
2e4b0e77 | 183 | int rdbSaveLzfStringObject(rio *rdb, unsigned char *s, size_t len) { |
e2641e09 | 184 | size_t comprlen, outlen; |
185 | unsigned char byte; | |
8a623a98 | 186 | int n, nwritten = 0; |
e2641e09 | 187 | void *out; |
188 | ||
189 | /* We require at least four bytes compression for this to be worth it */ | |
190 | if (len <= 4) return 0; | |
191 | outlen = len-4; | |
192 | if ((out = zmalloc(outlen+1)) == NULL) return 0; | |
193 | comprlen = lzf_compress(s, len, out, outlen); | |
194 | if (comprlen == 0) { | |
195 | zfree(out); | |
196 | return 0; | |
197 | } | |
198 | /* Data compressed! Let's save it on disk */ | |
199 | byte = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_LZF; | |
2e4b0e77 | 200 | if ((n = rdbWriteRaw(rdb,&byte,1)) == -1) goto writeerr; |
9a68cf91 | 201 | nwritten += n; |
8a623a98 | 202 | |
2e4b0e77 | 203 | if ((n = rdbSaveLen(rdb,comprlen)) == -1) goto writeerr; |
8a623a98 PN |
204 | nwritten += n; |
205 | ||
2e4b0e77 | 206 | if ((n = rdbSaveLen(rdb,len)) == -1) goto writeerr; |
8a623a98 PN |
207 | nwritten += n; |
208 | ||
2e4b0e77 | 209 | if ((n = rdbWriteRaw(rdb,out,comprlen)) == -1) goto writeerr; |
9a68cf91 | 210 | nwritten += n; |
8a623a98 | 211 | |
e2641e09 | 212 | zfree(out); |
8a623a98 | 213 | return nwritten; |
e2641e09 | 214 | |
215 | writeerr: | |
216 | zfree(out); | |
217 | return -1; | |
218 | } | |
219 | ||
221782cc PN |
220 | robj *rdbLoadLzfStringObject(rio *rdb) { |
221 | unsigned int len, clen; | |
222 | unsigned char *c = NULL; | |
223 | sds val = NULL; | |
224 | ||
225 | if ((clen = rdbLoadLen(rdb,NULL)) == REDIS_RDB_LENERR) return NULL; | |
226 | if ((len = rdbLoadLen(rdb,NULL)) == REDIS_RDB_LENERR) return NULL; | |
227 | if ((c = zmalloc(clen)) == NULL) goto err; | |
228 | if ((val = sdsnewlen(NULL,len)) == NULL) goto err; | |
229 | if (rioRead(rdb,c,clen) == 0) goto err; | |
230 | if (lzf_decompress(c,clen,val,len) == 0) goto err; | |
231 | zfree(c); | |
232 | return createObject(REDIS_STRING,val); | |
233 | err: | |
234 | zfree(c); | |
235 | sdsfree(val); | |
236 | return NULL; | |
237 | } | |
238 | ||
e2641e09 | 239 | /* Save a string objet as [len][data] on disk. If the object is a string |
2cc99365 | 240 | * representation of an integer value we try to save it in a special form */ |
2e4b0e77 | 241 | int rdbSaveRawString(rio *rdb, unsigned char *s, size_t len) { |
e2641e09 | 242 | int enclen; |
8a623a98 | 243 | int n, nwritten = 0; |
e2641e09 | 244 | |
245 | /* Try integer encoding */ | |
246 | if (len <= 11) { | |
247 | unsigned char buf[5]; | |
248 | if ((enclen = rdbTryIntegerEncoding((char*)s,len,buf)) > 0) { | |
2e4b0e77 | 249 | if (rdbWriteRaw(rdb,buf,enclen) == -1) return -1; |
8a623a98 | 250 | return enclen; |
e2641e09 | 251 | } |
252 | } | |
253 | ||
254 | /* Try LZF compression - under 20 bytes it's unable to compress even | |
255 | * aaaaaaaaaaaaaaaaaa so skip it */ | |
f48cd4b9 | 256 | if (server.rdb_compression && len > 20) { |
2e4b0e77 | 257 | n = rdbSaveLzfStringObject(rdb,s,len); |
8a623a98 PN |
258 | if (n == -1) return -1; |
259 | if (n > 0) return n; | |
260 | /* Return value of 0 means data can't be compressed, save the old way */ | |
e2641e09 | 261 | } |
262 | ||
263 | /* Store verbatim */ | |
2e4b0e77 | 264 | if ((n = rdbSaveLen(rdb,len)) == -1) return -1; |
8a623a98 PN |
265 | nwritten += n; |
266 | if (len > 0) { | |
2e4b0e77 | 267 | if (rdbWriteRaw(rdb,s,len) == -1) return -1; |
8a623a98 PN |
268 | nwritten += len; |
269 | } | |
270 | return nwritten; | |
e2641e09 | 271 | } |
272 | ||
273 | /* Save a long long value as either an encoded string or a string. */ | |
2e4b0e77 | 274 | int rdbSaveLongLongAsStringObject(rio *rdb, long long value) { |
e2641e09 | 275 | unsigned char buf[32]; |
8a623a98 | 276 | int n, nwritten = 0; |
e2641e09 | 277 | int enclen = rdbEncodeInteger(value,buf); |
278 | if (enclen > 0) { | |
2e4b0e77 | 279 | return rdbWriteRaw(rdb,buf,enclen); |
e2641e09 | 280 | } else { |
281 | /* Encode as string */ | |
282 | enclen = ll2string((char*)buf,32,value); | |
283 | redisAssert(enclen < 32); | |
2e4b0e77 | 284 | if ((n = rdbSaveLen(rdb,enclen)) == -1) return -1; |
8a623a98 | 285 | nwritten += n; |
2e4b0e77 | 286 | if ((n = rdbWriteRaw(rdb,buf,enclen)) == -1) return -1; |
9a68cf91 | 287 | nwritten += n; |
e2641e09 | 288 | } |
8a623a98 | 289 | return nwritten; |
e2641e09 | 290 | } |
291 | ||
292 | /* Like rdbSaveStringObjectRaw() but handle encoded objects */ | |
2e4b0e77 | 293 | int rdbSaveStringObject(rio *rdb, robj *obj) { |
e2641e09 | 294 | /* Avoid to decode the object, then encode it again, if the |
295 | * object is alrady integer encoded. */ | |
296 | if (obj->encoding == REDIS_ENCODING_INT) { | |
2e4b0e77 | 297 | return rdbSaveLongLongAsStringObject(rdb,(long)obj->ptr); |
e2641e09 | 298 | } else { |
eab0e26e | 299 | redisAssertWithInfo(NULL,obj,obj->encoding == REDIS_ENCODING_RAW); |
2e4b0e77 | 300 | return rdbSaveRawString(rdb,obj->ptr,sdslen(obj->ptr)); |
e2641e09 | 301 | } |
302 | } | |
303 | ||
221782cc PN |
304 | robj *rdbGenericLoadStringObject(rio *rdb, int encode) { |
305 | int isencoded; | |
306 | uint32_t len; | |
307 | sds val; | |
308 | ||
309 | len = rdbLoadLen(rdb,&isencoded); | |
310 | if (isencoded) { | |
311 | switch(len) { | |
312 | case REDIS_RDB_ENC_INT8: | |
313 | case REDIS_RDB_ENC_INT16: | |
314 | case REDIS_RDB_ENC_INT32: | |
315 | return rdbLoadIntegerObject(rdb,len,encode); | |
316 | case REDIS_RDB_ENC_LZF: | |
317 | return rdbLoadLzfStringObject(rdb); | |
318 | default: | |
319 | redisPanic("Unknown RDB encoding type"); | |
320 | } | |
321 | } | |
322 | ||
323 | if (len == REDIS_RDB_LENERR) return NULL; | |
324 | val = sdsnewlen(NULL,len); | |
325 | if (len && rioRead(rdb,val,len) == 0) { | |
326 | sdsfree(val); | |
327 | return NULL; | |
328 | } | |
329 | return createObject(REDIS_STRING,val); | |
330 | } | |
331 | ||
332 | robj *rdbLoadStringObject(rio *rdb) { | |
333 | return rdbGenericLoadStringObject(rdb,0); | |
334 | } | |
335 | ||
336 | robj *rdbLoadEncodedStringObject(rio *rdb) { | |
337 | return rdbGenericLoadStringObject(rdb,1); | |
338 | } | |
339 | ||
e2641e09 | 340 | /* Save a double value. Doubles are saved as strings prefixed by an unsigned |
341 | * 8 bit integer specifing the length of the representation. | |
342 | * This 8 bit integer has special values in order to specify the following | |
343 | * conditions: | |
344 | * 253: not a number | |
345 | * 254: + inf | |
346 | * 255: - inf | |
347 | */ | |
2e4b0e77 | 348 | int rdbSaveDoubleValue(rio *rdb, double val) { |
e2641e09 | 349 | unsigned char buf[128]; |
350 | int len; | |
351 | ||
352 | if (isnan(val)) { | |
353 | buf[0] = 253; | |
354 | len = 1; | |
355 | } else if (!isfinite(val)) { | |
356 | len = 1; | |
357 | buf[0] = (val < 0) ? 255 : 254; | |
358 | } else { | |
359 | #if (DBL_MANT_DIG >= 52) && (LLONG_MAX == 0x7fffffffffffffffLL) | |
360 | /* Check if the float is in a safe range to be casted into a | |
361 | * long long. We are assuming that long long is 64 bit here. | |
362 | * Also we are assuming that there are no implementations around where | |
363 | * double has precision < 52 bit. | |
364 | * | |
365 | * Under this assumptions we test if a double is inside an interval | |
366 | * where casting to long long is safe. Then using two castings we | |
367 | * make sure the decimal part is zero. If all this is true we use | |
368 | * integer printing function that is much faster. */ | |
369 | double min = -4503599627370495; /* (2^52)-1 */ | |
370 | double max = 4503599627370496; /* -(2^52) */ | |
371 | if (val > min && val < max && val == ((double)((long long)val))) | |
372 | ll2string((char*)buf+1,sizeof(buf),(long long)val); | |
373 | else | |
374 | #endif | |
375 | snprintf((char*)buf+1,sizeof(buf)-1,"%.17g",val); | |
376 | buf[0] = strlen((char*)buf+1); | |
377 | len = buf[0]+1; | |
378 | } | |
2e4b0e77 | 379 | return rdbWriteRaw(rdb,buf,len); |
e2641e09 | 380 | } |
381 | ||
221782cc PN |
382 | /* For information about double serialization check rdbSaveDoubleValue() */ |
383 | int rdbLoadDoubleValue(rio *rdb, double *val) { | |
384 | char buf[128]; | |
385 | unsigned char len; | |
386 | ||
387 | if (rioRead(rdb,&len,1) == 0) return -1; | |
388 | switch(len) { | |
389 | case 255: *val = R_NegInf; return 0; | |
390 | case 254: *val = R_PosInf; return 0; | |
391 | case 253: *val = R_Nan; return 0; | |
392 | default: | |
393 | if (rioRead(rdb,buf,len) == 0) return -1; | |
394 | buf[len] = '\0'; | |
395 | sscanf(buf, "%lg", val); | |
396 | return 0; | |
397 | } | |
398 | } | |
399 | ||
400 | /* Save the object type of object "o". */ | |
401 | int rdbSaveObjectType(rio *rdb, robj *o) { | |
402 | switch (o->type) { | |
403 | case REDIS_STRING: | |
404 | return rdbSaveType(rdb,REDIS_RDB_TYPE_STRING); | |
405 | case REDIS_LIST: | |
406 | if (o->encoding == REDIS_ENCODING_ZIPLIST) | |
407 | return rdbSaveType(rdb,REDIS_RDB_TYPE_LIST_ZIPLIST); | |
408 | else if (o->encoding == REDIS_ENCODING_LINKEDLIST) | |
409 | return rdbSaveType(rdb,REDIS_RDB_TYPE_LIST); | |
410 | else | |
411 | redisPanic("Unknown list encoding"); | |
412 | case REDIS_SET: | |
413 | if (o->encoding == REDIS_ENCODING_INTSET) | |
414 | return rdbSaveType(rdb,REDIS_RDB_TYPE_SET_INTSET); | |
415 | else if (o->encoding == REDIS_ENCODING_HT) | |
416 | return rdbSaveType(rdb,REDIS_RDB_TYPE_SET); | |
417 | else | |
418 | redisPanic("Unknown set encoding"); | |
419 | case REDIS_ZSET: | |
420 | if (o->encoding == REDIS_ENCODING_ZIPLIST) | |
421 | return rdbSaveType(rdb,REDIS_RDB_TYPE_ZSET_ZIPLIST); | |
422 | else if (o->encoding == REDIS_ENCODING_SKIPLIST) | |
423 | return rdbSaveType(rdb,REDIS_RDB_TYPE_ZSET); | |
424 | else | |
425 | redisPanic("Unknown sorted set encoding"); | |
426 | case REDIS_HASH: | |
ebd85e9a PN |
427 | if (o->encoding == REDIS_ENCODING_ZIPLIST) |
428 | return rdbSaveType(rdb,REDIS_RDB_TYPE_HASH_ZIPLIST); | |
221782cc PN |
429 | else if (o->encoding == REDIS_ENCODING_HT) |
430 | return rdbSaveType(rdb,REDIS_RDB_TYPE_HASH); | |
431 | else | |
432 | redisPanic("Unknown hash encoding"); | |
433 | default: | |
434 | redisPanic("Unknown object type"); | |
435 | } | |
436 | return -1; /* avoid warning */ | |
437 | } | |
438 | ||
ebdcd723 | 439 | /* Use rdbLoadType() to load a TYPE in RDB format, but returns -1 if the |
440 | * type is not specifically a valid Object Type. */ | |
221782cc PN |
441 | int rdbLoadObjectType(rio *rdb) { |
442 | int type; | |
443 | if ((type = rdbLoadType(rdb)) == -1) return -1; | |
444 | if (!rdbIsObjectType(type)) return -1; | |
445 | return type; | |
e2641e09 | 446 | } |
447 | ||
ecc91094 | 448 | /* Save a Redis object. Returns -1 on error, 0 on success. */ |
2e4b0e77 | 449 | int rdbSaveObject(rio *rdb, robj *o) { |
8a623a98 PN |
450 | int n, nwritten = 0; |
451 | ||
e2641e09 | 452 | if (o->type == REDIS_STRING) { |
453 | /* Save a string value */ | |
2e4b0e77 | 454 | if ((n = rdbSaveStringObject(rdb,o)) == -1) return -1; |
8a623a98 | 455 | nwritten += n; |
e2641e09 | 456 | } else if (o->type == REDIS_LIST) { |
457 | /* Save a list value */ | |
458 | if (o->encoding == REDIS_ENCODING_ZIPLIST) { | |
26117e84 | 459 | size_t l = ziplistBlobLen((unsigned char*)o->ptr); |
e2641e09 | 460 | |
2e4b0e77 | 461 | if ((n = rdbSaveRawString(rdb,o->ptr,l)) == -1) return -1; |
8a623a98 | 462 | nwritten += n; |
e2641e09 | 463 | } else if (o->encoding == REDIS_ENCODING_LINKEDLIST) { |
464 | list *list = o->ptr; | |
465 | listIter li; | |
466 | listNode *ln; | |
467 | ||
2e4b0e77 | 468 | if ((n = rdbSaveLen(rdb,listLength(list))) == -1) return -1; |
8a623a98 PN |
469 | nwritten += n; |
470 | ||
e2641e09 | 471 | listRewind(list,&li); |
472 | while((ln = listNext(&li))) { | |
473 | robj *eleobj = listNodeValue(ln); | |
2e4b0e77 | 474 | if ((n = rdbSaveStringObject(rdb,eleobj)) == -1) return -1; |
8a623a98 | 475 | nwritten += n; |
e2641e09 | 476 | } |
477 | } else { | |
478 | redisPanic("Unknown list encoding"); | |
479 | } | |
480 | } else if (o->type == REDIS_SET) { | |
481 | /* Save a set value */ | |
96ffb2fe PN |
482 | if (o->encoding == REDIS_ENCODING_HT) { |
483 | dict *set = o->ptr; | |
484 | dictIterator *di = dictGetIterator(set); | |
485 | dictEntry *de; | |
e2641e09 | 486 | |
2e4b0e77 | 487 | if ((n = rdbSaveLen(rdb,dictSize(set))) == -1) return -1; |
8a623a98 PN |
488 | nwritten += n; |
489 | ||
96ffb2fe | 490 | while((de = dictNext(di)) != NULL) { |
c0ba9ebe | 491 | robj *eleobj = dictGetKey(de); |
2e4b0e77 | 492 | if ((n = rdbSaveStringObject(rdb,eleobj)) == -1) return -1; |
8a623a98 | 493 | nwritten += n; |
96ffb2fe PN |
494 | } |
495 | dictReleaseIterator(di); | |
496 | } else if (o->encoding == REDIS_ENCODING_INTSET) { | |
26117e84 | 497 | size_t l = intsetBlobLen((intset*)o->ptr); |
96ffb2fe | 498 | |
2e4b0e77 | 499 | if ((n = rdbSaveRawString(rdb,o->ptr,l)) == -1) return -1; |
8a623a98 | 500 | nwritten += n; |
96ffb2fe PN |
501 | } else { |
502 | redisPanic("Unknown set encoding"); | |
e2641e09 | 503 | } |
e2641e09 | 504 | } else if (o->type == REDIS_ZSET) { |
e12b27ac PN |
505 | /* Save a sorted set value */ |
506 | if (o->encoding == REDIS_ENCODING_ZIPLIST) { | |
507 | size_t l = ziplistBlobLen((unsigned char*)o->ptr); | |
e2641e09 | 508 | |
2e4b0e77 | 509 | if ((n = rdbSaveRawString(rdb,o->ptr,l)) == -1) return -1; |
8a623a98 | 510 | nwritten += n; |
100ed062 | 511 | } else if (o->encoding == REDIS_ENCODING_SKIPLIST) { |
e12b27ac PN |
512 | zset *zs = o->ptr; |
513 | dictIterator *di = dictGetIterator(zs->dict); | |
514 | dictEntry *de; | |
515 | ||
2e4b0e77 | 516 | if ((n = rdbSaveLen(rdb,dictSize(zs->dict))) == -1) return -1; |
8a623a98 | 517 | nwritten += n; |
e12b27ac PN |
518 | |
519 | while((de = dictNext(di)) != NULL) { | |
c0ba9ebe | 520 | robj *eleobj = dictGetKey(de); |
521 | double *score = dictGetVal(de); | |
e12b27ac | 522 | |
2e4b0e77 | 523 | if ((n = rdbSaveStringObject(rdb,eleobj)) == -1) return -1; |
e12b27ac | 524 | nwritten += n; |
2e4b0e77 | 525 | if ((n = rdbSaveDoubleValue(rdb,*score)) == -1) return -1; |
e12b27ac PN |
526 | nwritten += n; |
527 | } | |
528 | dictReleaseIterator(di); | |
529 | } else { | |
4cc4d164 | 530 | redisPanic("Unknown sorted set encoding"); |
e2641e09 | 531 | } |
e2641e09 | 532 | } else if (o->type == REDIS_HASH) { |
533 | /* Save a hash value */ | |
ebd85e9a PN |
534 | if (o->encoding == REDIS_ENCODING_ZIPLIST) { |
535 | size_t l = ziplistBlobLen((unsigned char*)o->ptr); | |
e2641e09 | 536 | |
2e4b0e77 | 537 | if ((n = rdbSaveRawString(rdb,o->ptr,l)) == -1) return -1; |
8a623a98 | 538 | nwritten += n; |
ebd85e9a PN |
539 | |
540 | } else if (o->encoding == REDIS_ENCODING_HT) { | |
e2641e09 | 541 | dictIterator *di = dictGetIterator(o->ptr); |
542 | dictEntry *de; | |
543 | ||
2e4b0e77 | 544 | if ((n = rdbSaveLen(rdb,dictSize((dict*)o->ptr))) == -1) return -1; |
8a623a98 PN |
545 | nwritten += n; |
546 | ||
e2641e09 | 547 | while((de = dictNext(di)) != NULL) { |
c0ba9ebe | 548 | robj *key = dictGetKey(de); |
549 | robj *val = dictGetVal(de); | |
e2641e09 | 550 | |
2e4b0e77 | 551 | if ((n = rdbSaveStringObject(rdb,key)) == -1) return -1; |
8a623a98 | 552 | nwritten += n; |
2e4b0e77 | 553 | if ((n = rdbSaveStringObject(rdb,val)) == -1) return -1; |
8a623a98 | 554 | nwritten += n; |
e2641e09 | 555 | } |
556 | dictReleaseIterator(di); | |
ebd85e9a PN |
557 | |
558 | } else { | |
559 | redisPanic("Unknown hash encoding"); | |
e2641e09 | 560 | } |
ebd85e9a | 561 | |
e2641e09 | 562 | } else { |
563 | redisPanic("Unknown object type"); | |
564 | } | |
8a623a98 | 565 | return nwritten; |
e2641e09 | 566 | } |
567 | ||
568 | /* Return the length the object will have on disk if saved with | |
569 | * the rdbSaveObject() function. Currently we use a trick to get | |
570 | * this length with very little changes to the code. In the future | |
571 | * we could switch to a faster solution. */ | |
bd70a5f5 PN |
572 | off_t rdbSavedObjectLen(robj *o) { |
573 | int len = rdbSaveObject(NULL,o); | |
eab0e26e | 574 | redisAssertWithInfo(NULL,o,len != -1); |
bd70a5f5 | 575 | return len; |
e2641e09 | 576 | } |
577 | ||
4ab98823 | 578 | /* Save a key-value pair, with expire time, type, key, value. |
579 | * On error -1 is returned. | |
580 | * On success if the key was actaully saved 1 is returned, otherwise 0 | |
581 | * is returned (the key was already expired). */ | |
2e4b0e77 | 582 | int rdbSaveKeyValuePair(rio *rdb, robj *key, robj *val, |
7dcc10b6 | 583 | long long expiretime, long long now) |
4ab98823 | 584 | { |
4ab98823 | 585 | /* Save the expire time */ |
586 | if (expiretime != -1) { | |
587 | /* If this key is already expired skip it */ | |
588 | if (expiretime < now) return 0; | |
7dcc10b6 | 589 | if (rdbSaveType(rdb,REDIS_RDB_OPCODE_EXPIRETIME_MS) == -1) return -1; |
590 | if (rdbSaveMillisecondTime(rdb,expiretime) == -1) return -1; | |
4ab98823 | 591 | } |
f1d8e496 | 592 | |
4ab98823 | 593 | /* Save type, key, value */ |
f1d8e496 | 594 | if (rdbSaveObjectType(rdb,val) == -1) return -1; |
2e4b0e77 PN |
595 | if (rdbSaveStringObject(rdb,key) == -1) return -1; |
596 | if (rdbSaveObject(rdb,val) == -1) return -1; | |
4ab98823 | 597 | return 1; |
598 | } | |
599 | ||
e2641e09 | 600 | /* Save the DB on disk. Return REDIS_ERR on error, REDIS_OK on success */ |
601 | int rdbSave(char *filename) { | |
602 | dictIterator *di = NULL; | |
603 | dictEntry *de; | |
e2641e09 | 604 | char tmpfile[256]; |
11dae171 | 605 | char magic[10]; |
e2641e09 | 606 | int j; |
4be855e7 | 607 | long long now = mstime(); |
2e4b0e77 PN |
608 | FILE *fp; |
609 | rio rdb; | |
7f4f86f4 | 610 | uint64_t cksum; |
e2641e09 | 611 | |
e2641e09 | 612 | snprintf(tmpfile,256,"temp-%d.rdb", (int) getpid()); |
613 | fp = fopen(tmpfile,"w"); | |
614 | if (!fp) { | |
5b8ce853 | 615 | redisLog(REDIS_WARNING, "Failed opening .rdb for saving: %s", |
616 | strerror(errno)); | |
e2641e09 | 617 | return REDIS_ERR; |
618 | } | |
2e4b0e77 | 619 | |
f96a8a80 | 620 | rioInitWithFile(&rdb,fp); |
39d1e350 | 621 | if (server.rdb_checksum) |
622 | rdb.update_cksum = rioGenericUpdateChecksum; | |
11dae171 | 623 | snprintf(magic,sizeof(magic),"REDIS%04d",REDIS_RDB_VERSION); |
624 | if (rdbWriteRaw(&rdb,magic,9) == -1) goto werr; | |
2e4b0e77 | 625 | |
e2641e09 | 626 | for (j = 0; j < server.dbnum; j++) { |
627 | redisDb *db = server.db+j; | |
628 | dict *d = db->dict; | |
629 | if (dictSize(d) == 0) continue; | |
591f29e0 | 630 | di = dictGetSafeIterator(d); |
e2641e09 | 631 | if (!di) { |
632 | fclose(fp); | |
633 | return REDIS_ERR; | |
634 | } | |
635 | ||
636 | /* Write the SELECT DB opcode */ | |
f1d8e496 | 637 | if (rdbSaveType(&rdb,REDIS_RDB_OPCODE_SELECTDB) == -1) goto werr; |
2e4b0e77 | 638 | if (rdbSaveLen(&rdb,j) == -1) goto werr; |
e2641e09 | 639 | |
640 | /* Iterate this DB writing every entry */ | |
641 | while((de = dictNext(di)) != NULL) { | |
c0ba9ebe | 642 | sds keystr = dictGetKey(de); |
643 | robj key, *o = dictGetVal(de); | |
7dcc10b6 | 644 | long long expire; |
e2641e09 | 645 | |
646 | initStaticStringObject(key,keystr); | |
05600eb8 | 647 | expire = getExpire(db,&key); |
2e4b0e77 | 648 | if (rdbSaveKeyValuePair(&rdb,&key,o,expire,now) == -1) goto werr; |
e2641e09 | 649 | } |
650 | dictReleaseIterator(di); | |
651 | } | |
7f4f86f4 | 652 | di = NULL; /* So that we don't release it again on error. */ |
653 | ||
e2641e09 | 654 | /* EOF opcode */ |
f1d8e496 | 655 | if (rdbSaveType(&rdb,REDIS_RDB_OPCODE_EOF) == -1) goto werr; |
e2641e09 | 656 | |
39d1e350 | 657 | /* CRC64 checksum. It will be zero if checksum computation is disabled, the |
658 | * loading code skips the check in this case. */ | |
7f4f86f4 | 659 | cksum = rdb.cksum; |
660 | memrev64ifbe(&cksum); | |
661 | rioWrite(&rdb,&cksum,8); | |
662 | ||
e2641e09 | 663 | /* Make sure data will not remain on the OS's output buffers */ |
664 | fflush(fp); | |
665 | fsync(fileno(fp)); | |
666 | fclose(fp); | |
667 | ||
668 | /* Use RENAME to make sure the DB file is changed atomically only | |
669 | * if the generate DB file is ok. */ | |
670 | if (rename(tmpfile,filename) == -1) { | |
671 | redisLog(REDIS_WARNING,"Error moving temp DB file on the final destination: %s", strerror(errno)); | |
672 | unlink(tmpfile); | |
673 | return REDIS_ERR; | |
674 | } | |
675 | redisLog(REDIS_NOTICE,"DB saved on disk"); | |
676 | server.dirty = 0; | |
677 | server.lastsave = time(NULL); | |
c25e7eaf | 678 | server.lastbgsave_status = REDIS_OK; |
e2641e09 | 679 | return REDIS_OK; |
680 | ||
681 | werr: | |
682 | fclose(fp); | |
683 | unlink(tmpfile); | |
684 | redisLog(REDIS_WARNING,"Write error saving DB on disk: %s", strerror(errno)); | |
685 | if (di) dictReleaseIterator(di); | |
686 | return REDIS_ERR; | |
687 | } | |
688 | ||
689 | int rdbSaveBackground(char *filename) { | |
690 | pid_t childpid; | |
615e414c | 691 | long long start; |
e2641e09 | 692 | |
f48cd4b9 | 693 | if (server.rdb_child_pid != -1) return REDIS_ERR; |
249ad25f | 694 | |
2f6b31c3 | 695 | server.dirty_before_bgsave = server.dirty; |
249ad25f | 696 | |
615e414c | 697 | start = ustime(); |
e2641e09 | 698 | if ((childpid = fork()) == 0) { |
249ad25f | 699 | int retval; |
700 | ||
e2641e09 | 701 | /* Child */ |
a5639e7d PN |
702 | if (server.ipfd > 0) close(server.ipfd); |
703 | if (server.sofd > 0) close(server.sofd); | |
36c17a53 | 704 | retval = rdbSave(filename); |
55951f90 | 705 | exitFromChild((retval == REDIS_OK) ? 0 : 1); |
e2641e09 | 706 | } else { |
707 | /* Parent */ | |
615e414c | 708 | server.stat_fork_time = ustime()-start; |
e2641e09 | 709 | if (childpid == -1) { |
710 | redisLog(REDIS_WARNING,"Can't save in background: fork: %s", | |
711 | strerror(errno)); | |
712 | return REDIS_ERR; | |
713 | } | |
714 | redisLog(REDIS_NOTICE,"Background saving started by pid %d",childpid); | |
6b4d92e2 | 715 | server.rdb_save_time_start = time(NULL); |
f48cd4b9 | 716 | server.rdb_child_pid = childpid; |
e2641e09 | 717 | updateDictResizePolicy(); |
718 | return REDIS_OK; | |
719 | } | |
720 | return REDIS_OK; /* unreached */ | |
721 | } | |
722 | ||
723 | void rdbRemoveTempFile(pid_t childpid) { | |
724 | char tmpfile[256]; | |
725 | ||
726 | snprintf(tmpfile,256,"temp-%d.rdb", (int) childpid); | |
727 | unlink(tmpfile); | |
728 | } | |
729 | ||
e2641e09 | 730 | /* Load a Redis object of the specified type from the specified file. |
731 | * On success a newly allocated object is returned, otherwise NULL. */ | |
f1d8e496 | 732 | robj *rdbLoadObject(int rdbtype, rio *rdb) { |
e2641e09 | 733 | robj *o, *ele, *dec; |
734 | size_t len; | |
96ffb2fe | 735 | unsigned int i; |
e2641e09 | 736 | |
1bcb45d1 | 737 | redisLog(REDIS_DEBUG,"LOADING OBJECT %d (at %d)\n",rdbtype,rioTell(rdb)); |
f1d8e496 | 738 | if (rdbtype == REDIS_RDB_TYPE_STRING) { |
e2641e09 | 739 | /* Read string value */ |
2e4b0e77 | 740 | if ((o = rdbLoadEncodedStringObject(rdb)) == NULL) return NULL; |
e2641e09 | 741 | o = tryObjectEncoding(o); |
f1d8e496 | 742 | } else if (rdbtype == REDIS_RDB_TYPE_LIST) { |
e2641e09 | 743 | /* Read list value */ |
2e4b0e77 | 744 | if ((len = rdbLoadLen(rdb,NULL)) == REDIS_RDB_LENERR) return NULL; |
e2641e09 | 745 | |
746 | /* Use a real list when there are too many entries */ | |
747 | if (len > server.list_max_ziplist_entries) { | |
748 | o = createListObject(); | |
749 | } else { | |
750 | o = createZiplistObject(); | |
751 | } | |
752 | ||
753 | /* Load every single element of the list */ | |
754 | while(len--) { | |
2e4b0e77 | 755 | if ((ele = rdbLoadEncodedStringObject(rdb)) == NULL) return NULL; |
e2641e09 | 756 | |
757 | /* If we are using a ziplist and the value is too big, convert | |
758 | * the object to a real list. */ | |
759 | if (o->encoding == REDIS_ENCODING_ZIPLIST && | |
760 | ele->encoding == REDIS_ENCODING_RAW && | |
761 | sdslen(ele->ptr) > server.list_max_ziplist_value) | |
762 | listTypeConvert(o,REDIS_ENCODING_LINKEDLIST); | |
763 | ||
764 | if (o->encoding == REDIS_ENCODING_ZIPLIST) { | |
765 | dec = getDecodedObject(ele); | |
766 | o->ptr = ziplistPush(o->ptr,dec->ptr,sdslen(dec->ptr),REDIS_TAIL); | |
767 | decrRefCount(dec); | |
768 | decrRefCount(ele); | |
769 | } else { | |
770 | ele = tryObjectEncoding(ele); | |
771 | listAddNodeTail(o->ptr,ele); | |
772 | } | |
773 | } | |
f1d8e496 | 774 | } else if (rdbtype == REDIS_RDB_TYPE_SET) { |
e2641e09 | 775 | /* Read list/set value */ |
2e4b0e77 | 776 | if ((len = rdbLoadLen(rdb,NULL)) == REDIS_RDB_LENERR) return NULL; |
96ffb2fe PN |
777 | |
778 | /* Use a regular set when there are too many entries. */ | |
779 | if (len > server.set_max_intset_entries) { | |
780 | o = createSetObject(); | |
781 | /* It's faster to expand the dict to the right size asap in order | |
782 | * to avoid rehashing */ | |
783 | if (len > DICT_HT_INITIAL_SIZE) | |
784 | dictExpand(o->ptr,len); | |
785 | } else { | |
786 | o = createIntsetObject(); | |
787 | } | |
788 | ||
e2641e09 | 789 | /* Load every single element of the list/set */ |
96ffb2fe PN |
790 | for (i = 0; i < len; i++) { |
791 | long long llval; | |
2e4b0e77 | 792 | if ((ele = rdbLoadEncodedStringObject(rdb)) == NULL) return NULL; |
e2641e09 | 793 | ele = tryObjectEncoding(ele); |
96ffb2fe PN |
794 | |
795 | if (o->encoding == REDIS_ENCODING_INTSET) { | |
796 | /* Fetch integer value from element */ | |
2df84b72 | 797 | if (isObjectRepresentableAsLongLong(ele,&llval) == REDIS_OK) { |
96ffb2fe PN |
798 | o->ptr = intsetAdd(o->ptr,llval,NULL); |
799 | } else { | |
800 | setTypeConvert(o,REDIS_ENCODING_HT); | |
801 | dictExpand(o->ptr,len); | |
802 | } | |
803 | } | |
804 | ||
805 | /* This will also be called when the set was just converted | |
806 | * to regular hashtable encoded set */ | |
807 | if (o->encoding == REDIS_ENCODING_HT) { | |
808 | dictAdd((dict*)o->ptr,ele,NULL); | |
bad7d097 | 809 | } else { |
810 | decrRefCount(ele); | |
96ffb2fe | 811 | } |
e2641e09 | 812 | } |
f1d8e496 | 813 | } else if (rdbtype == REDIS_RDB_TYPE_ZSET) { |
e2641e09 | 814 | /* Read list/set value */ |
815 | size_t zsetlen; | |
df26a0ae | 816 | size_t maxelelen = 0; |
e2641e09 | 817 | zset *zs; |
818 | ||
2e4b0e77 | 819 | if ((zsetlen = rdbLoadLen(rdb,NULL)) == REDIS_RDB_LENERR) return NULL; |
e2641e09 | 820 | o = createZsetObject(); |
821 | zs = o->ptr; | |
df26a0ae | 822 | |
e2641e09 | 823 | /* Load every single element of the list/set */ |
824 | while(zsetlen--) { | |
825 | robj *ele; | |
56e52b69 PN |
826 | double score; |
827 | zskiplistNode *znode; | |
e2641e09 | 828 | |
2e4b0e77 | 829 | if ((ele = rdbLoadEncodedStringObject(rdb)) == NULL) return NULL; |
e2641e09 | 830 | ele = tryObjectEncoding(ele); |
2e4b0e77 | 831 | if (rdbLoadDoubleValue(rdb,&score) == -1) return NULL; |
df26a0ae PN |
832 | |
833 | /* Don't care about integer-encoded strings. */ | |
834 | if (ele->encoding == REDIS_ENCODING_RAW && | |
835 | sdslen(ele->ptr) > maxelelen) | |
836 | maxelelen = sdslen(ele->ptr); | |
837 | ||
56e52b69 PN |
838 | znode = zslInsert(zs->zsl,score,ele); |
839 | dictAdd(zs->dict,ele,&znode->score); | |
e2641e09 | 840 | incrRefCount(ele); /* added to skiplist */ |
841 | } | |
df26a0ae PN |
842 | |
843 | /* Convert *after* loading, since sorted sets are not stored ordered. */ | |
844 | if (zsetLength(o) <= server.zset_max_ziplist_entries && | |
845 | maxelelen <= server.zset_max_ziplist_value) | |
846 | zsetConvert(o,REDIS_ENCODING_ZIPLIST); | |
f1d8e496 | 847 | } else if (rdbtype == REDIS_RDB_TYPE_HASH) { |
ebd85e9a PN |
848 | size_t len; |
849 | int ret; | |
850 | ||
851 | len = rdbLoadLen(rdb, NULL); | |
852 | if (len == REDIS_RDB_LENERR) return NULL; | |
e2641e09 | 853 | |
e2641e09 | 854 | o = createHashObject(); |
ebd85e9a | 855 | |
e2641e09 | 856 | /* Too many entries? Use an hash table. */ |
ebd85e9a PN |
857 | if (len > server.hash_max_ziplist_entries) |
858 | hashTypeConvert(o, REDIS_ENCODING_HT); | |
859 | ||
860 | /* Load every field and value into the ziplist */ | |
ee61a4b9 | 861 | while (o->encoding == REDIS_ENCODING_ZIPLIST && len > 0) { |
ebd85e9a PN |
862 | robj *field, *value; |
863 | ||
ee61a4b9 | 864 | len--; |
ebd85e9a PN |
865 | /* Load raw strings */ |
866 | field = rdbLoadStringObject(rdb); | |
867 | if (field == NULL) return NULL; | |
868 | redisAssert(field->encoding == REDIS_ENCODING_RAW); | |
869 | value = rdbLoadStringObject(rdb); | |
870 | if (value == NULL) return NULL; | |
871 | redisAssert(field->encoding == REDIS_ENCODING_RAW); | |
872 | ||
a74ab647 | 873 | /* Add pair to ziplist */ |
874 | o->ptr = ziplistPush(o->ptr, field->ptr, sdslen(field->ptr), ZIPLIST_TAIL); | |
875 | o->ptr = ziplistPush(o->ptr, value->ptr, sdslen(value->ptr), ZIPLIST_TAIL); | |
ebd85e9a PN |
876 | /* Convert to hash table if size threshold is exceeded */ |
877 | if (sdslen(field->ptr) > server.hash_max_ziplist_value || | |
878 | sdslen(value->ptr) > server.hash_max_ziplist_value) | |
e2641e09 | 879 | { |
9b962d10 | 880 | decrRefCount(field); |
881 | decrRefCount(value); | |
ebd85e9a PN |
882 | hashTypeConvert(o, REDIS_ENCODING_HT); |
883 | break; | |
e2641e09 | 884 | } |
9b962d10 | 885 | decrRefCount(field); |
886 | decrRefCount(value); | |
e2641e09 | 887 | } |
ebd85e9a PN |
888 | |
889 | /* Load remaining fields and values into the hash table */ | |
ee61a4b9 | 890 | while (o->encoding == REDIS_ENCODING_HT && len > 0) { |
ebd85e9a PN |
891 | robj *field, *value; |
892 | ||
ee61a4b9 | 893 | len--; |
ebd85e9a PN |
894 | /* Load encoded strings */ |
895 | field = rdbLoadEncodedStringObject(rdb); | |
896 | if (field == NULL) return NULL; | |
897 | value = rdbLoadEncodedStringObject(rdb); | |
898 | if (value == NULL) return NULL; | |
899 | ||
900 | field = tryObjectEncoding(field); | |
901 | value = tryObjectEncoding(value); | |
902 | ||
903 | /* Add pair to hash table */ | |
904 | ret = dictAdd((dict*)o->ptr, field, value); | |
905 | redisAssert(ret == REDIS_OK); | |
906 | } | |
907 | ||
908 | /* All pairs should be read by now */ | |
909 | redisAssert(len == 0); | |
910 | ||
f1d8e496 PN |
911 | } else if (rdbtype == REDIS_RDB_TYPE_HASH_ZIPMAP || |
912 | rdbtype == REDIS_RDB_TYPE_LIST_ZIPLIST || | |
913 | rdbtype == REDIS_RDB_TYPE_SET_INTSET || | |
ebd85e9a PN |
914 | rdbtype == REDIS_RDB_TYPE_ZSET_ZIPLIST || |
915 | rdbtype == REDIS_RDB_TYPE_HASH_ZIPLIST) | |
26117e84 | 916 | { |
2e4b0e77 | 917 | robj *aux = rdbLoadStringObject(rdb); |
2cc99365 | 918 | |
919 | if (aux == NULL) return NULL; | |
26117e84 | 920 | o = createObject(REDIS_STRING,NULL); /* string is just placeholder */ |
2cc99365 | 921 | o->ptr = zmalloc(sdslen(aux->ptr)); |
922 | memcpy(o->ptr,aux->ptr,sdslen(aux->ptr)); | |
923 | decrRefCount(aux); | |
26117e84 | 924 | |
925 | /* Fix the object encoding, and make sure to convert the encoded | |
926 | * data type into the base type if accordingly to the current | |
927 | * configuration there are too many elements in the encoded data | |
928 | * type. Note that we only check the length and not max element | |
929 | * size as this is an O(N) scan. Eventually everything will get | |
930 | * converted. */ | |
f1d8e496 PN |
931 | switch(rdbtype) { |
932 | case REDIS_RDB_TYPE_HASH_ZIPMAP: | |
ebd85e9a PN |
933 | /* Convert to ziplist encoded hash. This must be deprecated |
934 | * when loading dumps created by Redis 2.4 gets deprecated. */ | |
935 | { | |
936 | unsigned char *zl = ziplistNew(); | |
937 | unsigned char *zi = zipmapRewind(o->ptr); | |
80586cb8 PN |
938 | unsigned char *fstr, *vstr; |
939 | unsigned int flen, vlen; | |
940 | unsigned int maxlen = 0; | |
ebd85e9a | 941 | |
80586cb8 PN |
942 | while ((zi = zipmapNext(zi, &fstr, &flen, &vstr, &vlen)) != NULL) { |
943 | if (flen > maxlen) maxlen = flen; | |
944 | if (vlen > maxlen) maxlen = vlen; | |
ebd85e9a PN |
945 | zl = ziplistPush(zl, fstr, flen, ZIPLIST_TAIL); |
946 | zl = ziplistPush(zl, vstr, vlen, ZIPLIST_TAIL); | |
947 | } | |
948 | ||
949 | zfree(o->ptr); | |
950 | o->ptr = zl; | |
951 | o->type = REDIS_HASH; | |
952 | o->encoding = REDIS_ENCODING_ZIPLIST; | |
953 | ||
80586cb8 PN |
954 | if (hashTypeLength(o) > server.hash_max_ziplist_entries || |
955 | maxlen > server.hash_max_ziplist_value) | |
956 | { | |
ebd85e9a | 957 | hashTypeConvert(o, REDIS_ENCODING_HT); |
80586cb8 | 958 | } |
ebd85e9a | 959 | } |
26117e84 | 960 | break; |
f1d8e496 | 961 | case REDIS_RDB_TYPE_LIST_ZIPLIST: |
26117e84 | 962 | o->type = REDIS_LIST; |
963 | o->encoding = REDIS_ENCODING_ZIPLIST; | |
964 | if (ziplistLen(o->ptr) > server.list_max_ziplist_entries) | |
965 | listTypeConvert(o,REDIS_ENCODING_LINKEDLIST); | |
966 | break; | |
f1d8e496 | 967 | case REDIS_RDB_TYPE_SET_INTSET: |
26117e84 | 968 | o->type = REDIS_SET; |
969 | o->encoding = REDIS_ENCODING_INTSET; | |
970 | if (intsetLen(o->ptr) > server.set_max_intset_entries) | |
971 | setTypeConvert(o,REDIS_ENCODING_HT); | |
972 | break; | |
f1d8e496 | 973 | case REDIS_RDB_TYPE_ZSET_ZIPLIST: |
e12b27ac PN |
974 | o->type = REDIS_ZSET; |
975 | o->encoding = REDIS_ENCODING_ZIPLIST; | |
df26a0ae | 976 | if (zsetLength(o) > server.zset_max_ziplist_entries) |
d4d3a70d | 977 | zsetConvert(o,REDIS_ENCODING_SKIPLIST); |
e12b27ac | 978 | break; |
ebd85e9a PN |
979 | case REDIS_RDB_TYPE_HASH_ZIPLIST: |
980 | o->type = REDIS_HASH; | |
981 | o->encoding = REDIS_ENCODING_ZIPLIST; | |
982 | if (hashTypeLength(o) > server.hash_max_ziplist_entries) | |
983 | hashTypeConvert(o, REDIS_ENCODING_HT); | |
984 | break; | |
26117e84 | 985 | default: |
d4d3a70d | 986 | redisPanic("Unknown encoding"); |
26117e84 | 987 | break; |
f8956ed6 | 988 | } |
e2641e09 | 989 | } else { |
990 | redisPanic("Unknown object type"); | |
991 | } | |
992 | return o; | |
993 | } | |
994 | ||
97e7f8ae | 995 | /* Mark that we are loading in the global state and setup the fields |
996 | * needed to provide loading stats. */ | |
997 | void startLoading(FILE *fp) { | |
998 | struct stat sb; | |
999 | ||
1000 | /* Load the DB */ | |
1001 | server.loading = 1; | |
1002 | server.loading_start_time = time(NULL); | |
1003 | if (fstat(fileno(fp), &sb) == -1) { | |
1004 | server.loading_total_bytes = 1; /* just to avoid division by zero */ | |
1005 | } else { | |
1006 | server.loading_total_bytes = sb.st_size; | |
1007 | } | |
1008 | } | |
1009 | ||
1010 | /* Refresh the loading progress info */ | |
1011 | void loadingProgress(off_t pos) { | |
1012 | server.loading_loaded_bytes = pos; | |
1013 | } | |
1014 | ||
1015 | /* Loading finished */ | |
1016 | void stopLoading(void) { | |
1017 | server.loading = 0; | |
1018 | } | |
1019 | ||
e2641e09 | 1020 | int rdbLoad(char *filename) { |
e2641e09 | 1021 | uint32_t dbid; |
f85cd526 | 1022 | int type, rdbver; |
e2641e09 | 1023 | redisDb *db = server.db+0; |
1024 | char buf[1024]; | |
7dcc10b6 | 1025 | long long expiretime, now = mstime(); |
97e7f8ae | 1026 | long loops = 0; |
2e4b0e77 PN |
1027 | FILE *fp; |
1028 | rio rdb; | |
e2641e09 | 1029 | |
1030 | fp = fopen(filename,"r"); | |
6d61e5bf | 1031 | if (!fp) { |
1032 | errno = ENOENT; | |
1033 | return REDIS_ERR; | |
1034 | } | |
f96a8a80 | 1035 | rioInitWithFile(&rdb,fp); |
39d1e350 | 1036 | if (server.rdb_checksum) |
1037 | rdb.update_cksum = rioGenericUpdateChecksum; | |
fd535c58 | 1038 | if (rioRead(&rdb,buf,9) == 0) goto eoferr; |
e2641e09 | 1039 | buf[9] = '\0'; |
1040 | if (memcmp(buf,"REDIS",5) != 0) { | |
1041 | fclose(fp); | |
1042 | redisLog(REDIS_WARNING,"Wrong signature trying to load DB from file"); | |
6d61e5bf | 1043 | errno = EINVAL; |
e2641e09 | 1044 | return REDIS_ERR; |
1045 | } | |
1046 | rdbver = atoi(buf+5); | |
62bfa662 | 1047 | if (rdbver < 1 || rdbver > REDIS_RDB_VERSION) { |
e2641e09 | 1048 | fclose(fp); |
1049 | redisLog(REDIS_WARNING,"Can't handle RDB format version %d",rdbver); | |
6d61e5bf | 1050 | errno = EINVAL; |
e2641e09 | 1051 | return REDIS_ERR; |
1052 | } | |
97e7f8ae | 1053 | |
1054 | startLoading(fp); | |
e2641e09 | 1055 | while(1) { |
1056 | robj *key, *val; | |
e2641e09 | 1057 | expiretime = -1; |
97e7f8ae | 1058 | |
1059 | /* Serve the clients from time to time */ | |
1060 | if (!(loops++ % 1000)) { | |
1bcb45d1 | 1061 | loadingProgress(rioTell(&rdb)); |
97e7f8ae | 1062 | aeProcessEvents(server.el, AE_FILE_EVENTS|AE_DONT_WAIT); |
1063 | } | |
1064 | ||
e2641e09 | 1065 | /* Read type. */ |
2e4b0e77 | 1066 | if ((type = rdbLoadType(&rdb)) == -1) goto eoferr; |
f1d8e496 | 1067 | if (type == REDIS_RDB_OPCODE_EXPIRETIME) { |
2e4b0e77 | 1068 | if ((expiretime = rdbLoadTime(&rdb)) == -1) goto eoferr; |
f1d8e496 | 1069 | /* We read the time so we need to read the object type again. */ |
2e4b0e77 | 1070 | if ((type = rdbLoadType(&rdb)) == -1) goto eoferr; |
dab5332f | 1071 | /* the EXPIRETIME opcode specifies time in seconds, so convert |
7dcc10b6 | 1072 | * into milliesconds. */ |
1073 | expiretime *= 1000; | |
1074 | } else if (type == REDIS_RDB_OPCODE_EXPIRETIME_MS) { | |
1075 | /* Milliseconds precision expire times introduced with RDB | |
1076 | * version 3. */ | |
1077 | if ((expiretime = rdbLoadMillisecondTime(&rdb)) == -1) goto eoferr; | |
1078 | /* We read the time so we need to read the object type again. */ | |
1079 | if ((type = rdbLoadType(&rdb)) == -1) goto eoferr; | |
e2641e09 | 1080 | } |
f1d8e496 PN |
1081 | |
1082 | if (type == REDIS_RDB_OPCODE_EOF) | |
1083 | break; | |
1084 | ||
e2641e09 | 1085 | /* Handle SELECT DB opcode as a special case */ |
f1d8e496 | 1086 | if (type == REDIS_RDB_OPCODE_SELECTDB) { |
2e4b0e77 | 1087 | if ((dbid = rdbLoadLen(&rdb,NULL)) == REDIS_RDB_LENERR) |
e2641e09 | 1088 | goto eoferr; |
1089 | if (dbid >= (unsigned)server.dbnum) { | |
1090 | redisLog(REDIS_WARNING,"FATAL: Data file was created with a Redis server configured to handle more than %d databases. Exiting\n", server.dbnum); | |
1091 | exit(1); | |
1092 | } | |
1093 | db = server.db+dbid; | |
1094 | continue; | |
1095 | } | |
1096 | /* Read key */ | |
2e4b0e77 | 1097 | if ((key = rdbLoadStringObject(&rdb)) == NULL) goto eoferr; |
e2641e09 | 1098 | /* Read value */ |
2e4b0e77 | 1099 | if ((val = rdbLoadObject(type,&rdb)) == NULL) goto eoferr; |
cb598cdd PN |
1100 | /* Check if the key already expired. This function is used when loading |
1101 | * an RDB file from disk, either at startup, or when an RDB was | |
1102 | * received from the master. In the latter case, the master is | |
1103 | * responsible for key expiry. If we would expire keys here, the | |
1104 | * snapshot taken by the master may not be reflected on the slave. */ | |
1105 | if (server.masterhost == NULL && expiretime != -1 && expiretime < now) { | |
e2641e09 | 1106 | decrRefCount(key); |
1107 | decrRefCount(val); | |
1108 | continue; | |
1109 | } | |
1110 | /* Add the new object in the hash table */ | |
f85cd526 | 1111 | dbAdd(db,key,val); |
1112 | ||
e2641e09 | 1113 | /* Set the expire time if needed */ |
1114 | if (expiretime != -1) setExpire(db,key,expiretime); | |
1115 | ||
e2641e09 | 1116 | decrRefCount(key); |
e2641e09 | 1117 | } |
7f4f86f4 | 1118 | /* Verify the checksum if RDB version is >= 5 */ |
39d1e350 | 1119 | if (rdbver >= 5 && server.rdb_checksum) { |
7f4f86f4 | 1120 | uint64_t cksum, expected = rdb.cksum; |
1121 | ||
1122 | if (rioRead(&rdb,&cksum,8) == 0) goto eoferr; | |
1123 | memrev64ifbe(&cksum); | |
39d1e350 | 1124 | if (cksum == 0) { |
1125 | redisLog(REDIS_WARNING,"RDB file was saved with checksum disabled: no check performed."); | |
1126 | } else if (cksum != expected) { | |
7f4f86f4 | 1127 | redisLog(REDIS_WARNING,"Wrong RDB checksum. Aborting now."); |
1128 | exit(1); | |
1129 | } | |
1130 | } | |
1131 | ||
e2641e09 | 1132 | fclose(fp); |
97e7f8ae | 1133 | stopLoading(); |
e2641e09 | 1134 | return REDIS_OK; |
1135 | ||
1136 | eoferr: /* unexpected end of file is handled here with a fatal exit */ | |
1137 | redisLog(REDIS_WARNING,"Short read or OOM loading DB. Unrecoverable error, aborting now."); | |
1138 | exit(1); | |
1139 | return REDIS_ERR; /* Just to avoid warning */ | |
1140 | } | |
1141 | ||
1142 | /* A background saving child (BGSAVE) terminated its work. Handle this. */ | |
36c17a53 | 1143 | void backgroundSaveDoneHandler(int exitcode, int bysignal) { |
e2641e09 | 1144 | if (!bysignal && exitcode == 0) { |
1145 | redisLog(REDIS_NOTICE, | |
1146 | "Background saving terminated with success"); | |
2f6b31c3 | 1147 | server.dirty = server.dirty - server.dirty_before_bgsave; |
e2641e09 | 1148 | server.lastsave = time(NULL); |
c25e7eaf | 1149 | server.lastbgsave_status = REDIS_OK; |
e2641e09 | 1150 | } else if (!bysignal && exitcode != 0) { |
1151 | redisLog(REDIS_WARNING, "Background saving error"); | |
c25e7eaf | 1152 | server.lastbgsave_status = REDIS_ERR; |
e2641e09 | 1153 | } else { |
1154 | redisLog(REDIS_WARNING, | |
36c17a53 | 1155 | "Background saving terminated by signal %d", bysignal); |
f48cd4b9 | 1156 | rdbRemoveTempFile(server.rdb_child_pid); |
c25e7eaf | 1157 | server.lastbgsave_status = REDIS_ERR; |
e2641e09 | 1158 | } |
f48cd4b9 | 1159 | server.rdb_child_pid = -1; |
6b4d92e2 | 1160 | server.rdb_save_time_last = time(NULL)-server.rdb_save_time_start; |
1161 | server.rdb_save_time_start = -1; | |
e2641e09 | 1162 | /* Possibly there are slaves waiting for a BGSAVE in order to be served |
1163 | * (the first stage of SYNC is a bulk transfer of dump.rdb) */ | |
1164 | updateSlavesWaitingBgsave(exitcode == 0 ? REDIS_OK : REDIS_ERR); | |
1165 | } | |
36c17a53 | 1166 | |
1167 | void saveCommand(redisClient *c) { | |
f48cd4b9 | 1168 | if (server.rdb_child_pid != -1) { |
36c17a53 | 1169 | addReplyError(c,"Background save already in progress"); |
1170 | return; | |
1171 | } | |
f48cd4b9 | 1172 | if (rdbSave(server.rdb_filename) == REDIS_OK) { |
36c17a53 | 1173 | addReply(c,shared.ok); |
1174 | } else { | |
1175 | addReply(c,shared.err); | |
1176 | } | |
1177 | } | |
1178 | ||
1179 | void bgsaveCommand(redisClient *c) { | |
f48cd4b9 | 1180 | if (server.rdb_child_pid != -1) { |
36c17a53 | 1181 | addReplyError(c,"Background save already in progress"); |
ff2145ad | 1182 | } else if (server.aof_child_pid != -1) { |
b333e239 | 1183 | addReplyError(c,"Can't BGSAVE while AOF log rewriting is in progress"); |
f48cd4b9 | 1184 | } else if (rdbSaveBackground(server.rdb_filename) == REDIS_OK) { |
36c17a53 | 1185 | addReplyStatus(c,"Background saving started"); |
1186 | } else { | |
1187 | addReply(c,shared.err); | |
1188 | } | |
1189 | } |