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