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