2 #include "lzf.h" /* LZF compression library */
4 #include "endianconv.h"
9 #include <sys/resource.h>
11 #include <arpa/inet.h>
14 static int rdbWriteRaw(rio
*rdb
, void *p
, size_t len
) {
15 if (rdb
&& rioWrite(rdb
,p
,len
) == 0)
20 int rdbSaveType(rio
*rdb
, unsigned char type
) {
21 return rdbWriteRaw(rdb
,&type
,1);
24 int rdbLoadType(rio
*rdb
) {
26 if (rioRead(rdb
,&type
,1) == 0) return -1;
30 time_t rdbLoadTime(rio
*rdb
) {
32 if (rioRead(rdb
,&t32
,4) == 0) return -1;
36 int rdbSaveMillisecondTime(rio
*rdb
, long long t
) {
37 int64_t t64
= (int64_t) t
;
38 return rdbWriteRaw(rdb
,&t64
,8);
41 long long rdbLoadMillisecondTime(rio
*rdb
) {
43 if (rioRead(rdb
,&t64
,8) == 0) return -1;
44 return (long long)t64
;
47 /* Saves an encoded length. The first two bits in the first byte are used to
48 * hold the encoding type. See the REDIS_RDB_* definitions for more information
49 * on the types of encoding. */
50 int rdbSaveLen(rio
*rdb
, uint32_t len
) {
55 /* Save a 6 bit len */
56 buf
[0] = (len
&0xFF)|(REDIS_RDB_6BITLEN
<<6);
57 if (rdbWriteRaw(rdb
,buf
,1) == -1) return -1;
59 } else if (len
< (1<<14)) {
60 /* Save a 14 bit len */
61 buf
[0] = ((len
>>8)&0xFF)|(REDIS_RDB_14BITLEN
<<6);
63 if (rdbWriteRaw(rdb
,buf
,2) == -1) return -1;
66 /* Save a 32 bit len */
67 buf
[0] = (REDIS_RDB_32BITLEN
<<6);
68 if (rdbWriteRaw(rdb
,buf
,1) == -1) return -1;
70 if (rdbWriteRaw(rdb
,&len
,4) == -4) return -1;
76 /* Load an encoded length. The "isencoded" argument is set to 1 if the length
77 * is not actually a length but an "encoding type". See the REDIS_RDB_ENC_*
78 * definitions in rdb.h for more information. */
79 uint32_t rdbLoadLen(rio
*rdb
, int *isencoded
) {
84 if (isencoded
) *isencoded
= 0;
85 if (rioRead(rdb
,buf
,1) == 0) return REDIS_RDB_LENERR
;
86 type
= (buf
[0]&0xC0)>>6;
87 if (type
== REDIS_RDB_ENCVAL
) {
88 /* Read a 6 bit encoding type. */
89 if (isencoded
) *isencoded
= 1;
91 } else if (type
== REDIS_RDB_6BITLEN
) {
92 /* Read a 6 bit len. */
94 } else if (type
== REDIS_RDB_14BITLEN
) {
95 /* Read a 14 bit len. */
96 if (rioRead(rdb
,buf
+1,1) == 0) return REDIS_RDB_LENERR
;
97 return ((buf
[0]&0x3F)<<8)|buf
[1];
99 /* Read a 32 bit len. */
100 if (rioRead(rdb
,&len
,4) == 0) return REDIS_RDB_LENERR
;
105 /* Encodes the "value" argument as integer when it fits in the supported ranges
106 * for encoded types. If the function successfully encodes the integer, the
107 * representation is stored in the buffer pointer to by "enc" and the string
108 * length is returned. Otherwise 0 is returned. */
109 int rdbEncodeInteger(long long value
, unsigned char *enc
) {
110 if (value
>= -(1<<7) && value
<= (1<<7)-1) {
111 enc
[0] = (REDIS_RDB_ENCVAL
<<6)|REDIS_RDB_ENC_INT8
;
114 } else if (value
>= -(1<<15) && value
<= (1<<15)-1) {
115 enc
[0] = (REDIS_RDB_ENCVAL
<<6)|REDIS_RDB_ENC_INT16
;
117 enc
[2] = (value
>>8)&0xFF;
119 } else if (value
>= -((long long)1<<31) && value
<= ((long long)1<<31)-1) {
120 enc
[0] = (REDIS_RDB_ENCVAL
<<6)|REDIS_RDB_ENC_INT32
;
122 enc
[2] = (value
>>8)&0xFF;
123 enc
[3] = (value
>>16)&0xFF;
124 enc
[4] = (value
>>24)&0xFF;
131 /* Loads an integer-encoded object with the specified encoding type "enctype".
132 * If the "encode" argument is set the function may return an integer-encoded
133 * string object, otherwise it always returns a raw string object. */
134 robj
*rdbLoadIntegerObject(rio
*rdb
, int enctype
, int encode
) {
135 unsigned char enc
[4];
138 if (enctype
== REDIS_RDB_ENC_INT8
) {
139 if (rioRead(rdb
,enc
,1) == 0) return NULL
;
140 val
= (signed char)enc
[0];
141 } else if (enctype
== REDIS_RDB_ENC_INT16
) {
143 if (rioRead(rdb
,enc
,2) == 0) return NULL
;
144 v
= enc
[0]|(enc
[1]<<8);
146 } else if (enctype
== REDIS_RDB_ENC_INT32
) {
148 if (rioRead(rdb
,enc
,4) == 0) return NULL
;
149 v
= enc
[0]|(enc
[1]<<8)|(enc
[2]<<16)|(enc
[3]<<24);
152 val
= 0; /* anti-warning */
153 redisPanic("Unknown RDB integer encoding type");
156 return createStringObjectFromLongLong(val
);
158 return createObject(REDIS_STRING
,sdsfromlonglong(val
));
161 /* String objects in the form "2391" "-100" without any space and with a
162 * range of values that can fit in an 8, 16 or 32 bit signed value can be
163 * encoded as integers to save space */
164 int rdbTryIntegerEncoding(char *s
, size_t len
, unsigned char *enc
) {
166 char *endptr
, buf
[32];
168 /* Check if it's possible to encode this value as a number */
169 value
= strtoll(s
, &endptr
, 10);
170 if (endptr
[0] != '\0') return 0;
171 ll2string(buf
,32,value
);
173 /* If the number converted back into a string is not identical
174 * then it's not possible to encode the string as integer */
175 if (strlen(buf
) != len
|| memcmp(buf
,s
,len
)) return 0;
177 return rdbEncodeInteger(value
,enc
);
180 int rdbSaveLzfStringObject(rio
*rdb
, unsigned char *s
, size_t len
) {
181 size_t comprlen
, outlen
;
186 /* We require at least four bytes compression for this to be worth it */
187 if (len
<= 4) return 0;
189 if ((out
= zmalloc(outlen
+1)) == NULL
) return 0;
190 comprlen
= lzf_compress(s
, len
, out
, outlen
);
195 /* Data compressed! Let's save it on disk */
196 byte
= (REDIS_RDB_ENCVAL
<<6)|REDIS_RDB_ENC_LZF
;
197 if ((n
= rdbWriteRaw(rdb
,&byte
,1)) == -1) goto writeerr
;
200 if ((n
= rdbSaveLen(rdb
,comprlen
)) == -1) goto writeerr
;
203 if ((n
= rdbSaveLen(rdb
,len
)) == -1) goto writeerr
;
206 if ((n
= rdbWriteRaw(rdb
,out
,comprlen
)) == -1) goto writeerr
;
217 robj
*rdbLoadLzfStringObject(rio
*rdb
) {
218 unsigned int len
, clen
;
219 unsigned char *c
= NULL
;
222 if ((clen
= rdbLoadLen(rdb
,NULL
)) == REDIS_RDB_LENERR
) return NULL
;
223 if ((len
= rdbLoadLen(rdb
,NULL
)) == REDIS_RDB_LENERR
) return NULL
;
224 if ((c
= zmalloc(clen
)) == NULL
) goto err
;
225 if ((val
= sdsnewlen(NULL
,len
)) == NULL
) goto err
;
226 if (rioRead(rdb
,c
,clen
) == 0) goto err
;
227 if (lzf_decompress(c
,clen
,val
,len
) == 0) goto err
;
229 return createObject(REDIS_STRING
,val
);
236 /* Save a string objet as [len][data] on disk. If the object is a string
237 * representation of an integer value we try to save it in a special form */
238 int rdbSaveRawString(rio
*rdb
, unsigned char *s
, size_t len
) {
242 /* Try integer encoding */
244 unsigned char buf
[5];
245 if ((enclen
= rdbTryIntegerEncoding((char*)s
,len
,buf
)) > 0) {
246 if (rdbWriteRaw(rdb
,buf
,enclen
) == -1) return -1;
251 /* Try LZF compression - under 20 bytes it's unable to compress even
252 * aaaaaaaaaaaaaaaaaa so skip it */
253 if (server
.rdb_compression
&& len
> 20) {
254 n
= rdbSaveLzfStringObject(rdb
,s
,len
);
255 if (n
== -1) return -1;
257 /* Return value of 0 means data can't be compressed, save the old way */
261 if ((n
= rdbSaveLen(rdb
,len
)) == -1) return -1;
264 if (rdbWriteRaw(rdb
,s
,len
) == -1) return -1;
270 /* Save a long long value as either an encoded string or a string. */
271 int rdbSaveLongLongAsStringObject(rio
*rdb
, long long value
) {
272 unsigned char buf
[32];
274 int enclen
= rdbEncodeInteger(value
,buf
);
276 return rdbWriteRaw(rdb
,buf
,enclen
);
278 /* Encode as string */
279 enclen
= ll2string((char*)buf
,32,value
);
280 redisAssert(enclen
< 32);
281 if ((n
= rdbSaveLen(rdb
,enclen
)) == -1) return -1;
283 if ((n
= rdbWriteRaw(rdb
,buf
,enclen
)) == -1) return -1;
289 /* Like rdbSaveStringObjectRaw() but handle encoded objects */
290 int rdbSaveStringObject(rio
*rdb
, robj
*obj
) {
291 /* Avoid to decode the object, then encode it again, if the
292 * object is alrady integer encoded. */
293 if (obj
->encoding
== REDIS_ENCODING_INT
) {
294 return rdbSaveLongLongAsStringObject(rdb
,(long)obj
->ptr
);
296 redisAssertWithInfo(NULL
,obj
,obj
->encoding
== REDIS_ENCODING_RAW
);
297 return rdbSaveRawString(rdb
,obj
->ptr
,sdslen(obj
->ptr
));
301 robj
*rdbGenericLoadStringObject(rio
*rdb
, int encode
) {
306 len
= rdbLoadLen(rdb
,&isencoded
);
309 case REDIS_RDB_ENC_INT8
:
310 case REDIS_RDB_ENC_INT16
:
311 case REDIS_RDB_ENC_INT32
:
312 return rdbLoadIntegerObject(rdb
,len
,encode
);
313 case REDIS_RDB_ENC_LZF
:
314 return rdbLoadLzfStringObject(rdb
);
316 redisPanic("Unknown RDB encoding type");
320 if (len
== REDIS_RDB_LENERR
) return NULL
;
321 val
= sdsnewlen(NULL
,len
);
322 if (len
&& rioRead(rdb
,val
,len
) == 0) {
326 return createObject(REDIS_STRING
,val
);
329 robj
*rdbLoadStringObject(rio
*rdb
) {
330 return rdbGenericLoadStringObject(rdb
,0);
333 robj
*rdbLoadEncodedStringObject(rio
*rdb
) {
334 return rdbGenericLoadStringObject(rdb
,1);
337 /* Save a double value. Doubles are saved as strings prefixed by an unsigned
338 * 8 bit integer specifing the length of the representation.
339 * This 8 bit integer has special values in order to specify the following
345 int rdbSaveDoubleValue(rio
*rdb
, double val
) {
346 unsigned char buf
[128];
352 } else if (!isfinite(val
)) {
354 buf
[0] = (val
< 0) ? 255 : 254;
356 #if (DBL_MANT_DIG >= 52) && (LLONG_MAX == 0x7fffffffffffffffLL)
357 /* Check if the float is in a safe range to be casted into a
358 * long long. We are assuming that long long is 64 bit here.
359 * Also we are assuming that there are no implementations around where
360 * double has precision < 52 bit.
362 * Under this assumptions we test if a double is inside an interval
363 * where casting to long long is safe. Then using two castings we
364 * make sure the decimal part is zero. If all this is true we use
365 * integer printing function that is much faster. */
366 double min
= -4503599627370495; /* (2^52)-1 */
367 double max
= 4503599627370496; /* -(2^52) */
368 if (val
> min
&& val
< max
&& val
== ((double)((long long)val
)))
369 ll2string((char*)buf
+1,sizeof(buf
),(long long)val
);
372 snprintf((char*)buf
+1,sizeof(buf
)-1,"%.17g",val
);
373 buf
[0] = strlen((char*)buf
+1);
376 return rdbWriteRaw(rdb
,buf
,len
);
379 /* For information about double serialization check rdbSaveDoubleValue() */
380 int rdbLoadDoubleValue(rio
*rdb
, double *val
) {
384 if (rioRead(rdb
,&len
,1) == 0) return -1;
386 case 255: *val
= R_NegInf
; return 0;
387 case 254: *val
= R_PosInf
; return 0;
388 case 253: *val
= R_Nan
; return 0;
390 if (rioRead(rdb
,buf
,len
) == 0) return -1;
392 sscanf(buf
, "%lg", val
);
397 /* Save the object type of object "o". */
398 int rdbSaveObjectType(rio
*rdb
, robj
*o
) {
401 return rdbSaveType(rdb
,REDIS_RDB_TYPE_STRING
);
403 if (o
->encoding
== REDIS_ENCODING_ZIPLIST
)
404 return rdbSaveType(rdb
,REDIS_RDB_TYPE_LIST_ZIPLIST
);
405 else if (o
->encoding
== REDIS_ENCODING_LINKEDLIST
)
406 return rdbSaveType(rdb
,REDIS_RDB_TYPE_LIST
);
408 redisPanic("Unknown list encoding");
410 if (o
->encoding
== REDIS_ENCODING_INTSET
)
411 return rdbSaveType(rdb
,REDIS_RDB_TYPE_SET_INTSET
);
412 else if (o
->encoding
== REDIS_ENCODING_HT
)
413 return rdbSaveType(rdb
,REDIS_RDB_TYPE_SET
);
415 redisPanic("Unknown set encoding");
417 if (o
->encoding
== REDIS_ENCODING_ZIPLIST
)
418 return rdbSaveType(rdb
,REDIS_RDB_TYPE_ZSET_ZIPLIST
);
419 else if (o
->encoding
== REDIS_ENCODING_SKIPLIST
)
420 return rdbSaveType(rdb
,REDIS_RDB_TYPE_ZSET
);
422 redisPanic("Unknown sorted set encoding");
424 if (o
->encoding
== REDIS_ENCODING_ZIPLIST
)
425 return rdbSaveType(rdb
,REDIS_RDB_TYPE_HASH_ZIPLIST
);
426 else if (o
->encoding
== REDIS_ENCODING_HT
)
427 return rdbSaveType(rdb
,REDIS_RDB_TYPE_HASH
);
429 redisPanic("Unknown hash encoding");
431 redisPanic("Unknown object type");
433 return -1; /* avoid warning */
436 /* Load object type. Return -1 when the byte doesn't contain an object type. */
437 int rdbLoadObjectType(rio
*rdb
) {
439 if ((type
= rdbLoadType(rdb
)) == -1) return -1;
440 if (!rdbIsObjectType(type
)) return -1;
444 /* Save a Redis object. Returns -1 on error, 0 on success. */
445 int rdbSaveObject(rio
*rdb
, robj
*o
) {
448 if (o
->type
== REDIS_STRING
) {
449 /* Save a string value */
450 if ((n
= rdbSaveStringObject(rdb
,o
)) == -1) return -1;
452 } else if (o
->type
== REDIS_LIST
) {
453 /* Save a list value */
454 if (o
->encoding
== REDIS_ENCODING_ZIPLIST
) {
455 size_t l
= ziplistBlobLen((unsigned char*)o
->ptr
);
457 if ((n
= rdbSaveRawString(rdb
,o
->ptr
,l
)) == -1) return -1;
459 } else if (o
->encoding
== REDIS_ENCODING_LINKEDLIST
) {
464 if ((n
= rdbSaveLen(rdb
,listLength(list
))) == -1) return -1;
467 listRewind(list
,&li
);
468 while((ln
= listNext(&li
))) {
469 robj
*eleobj
= listNodeValue(ln
);
470 if ((n
= rdbSaveStringObject(rdb
,eleobj
)) == -1) return -1;
474 redisPanic("Unknown list encoding");
476 } else if (o
->type
== REDIS_SET
) {
477 /* Save a set value */
478 if (o
->encoding
== REDIS_ENCODING_HT
) {
480 dictIterator
*di
= dictGetIterator(set
);
483 if ((n
= rdbSaveLen(rdb
,dictSize(set
))) == -1) return -1;
486 while((de
= dictNext(di
)) != NULL
) {
487 robj
*eleobj
= dictGetKey(de
);
488 if ((n
= rdbSaveStringObject(rdb
,eleobj
)) == -1) return -1;
491 dictReleaseIterator(di
);
492 } else if (o
->encoding
== REDIS_ENCODING_INTSET
) {
493 size_t l
= intsetBlobLen((intset
*)o
->ptr
);
495 if ((n
= rdbSaveRawString(rdb
,o
->ptr
,l
)) == -1) return -1;
498 redisPanic("Unknown set encoding");
500 } else if (o
->type
== REDIS_ZSET
) {
501 /* Save a sorted set value */
502 if (o
->encoding
== REDIS_ENCODING_ZIPLIST
) {
503 size_t l
= ziplistBlobLen((unsigned char*)o
->ptr
);
505 if ((n
= rdbSaveRawString(rdb
,o
->ptr
,l
)) == -1) return -1;
507 } else if (o
->encoding
== REDIS_ENCODING_SKIPLIST
) {
509 dictIterator
*di
= dictGetIterator(zs
->dict
);
512 if ((n
= rdbSaveLen(rdb
,dictSize(zs
->dict
))) == -1) return -1;
515 while((de
= dictNext(di
)) != NULL
) {
516 robj
*eleobj
= dictGetKey(de
);
517 double *score
= dictGetVal(de
);
519 if ((n
= rdbSaveStringObject(rdb
,eleobj
)) == -1) return -1;
521 if ((n
= rdbSaveDoubleValue(rdb
,*score
)) == -1) return -1;
524 dictReleaseIterator(di
);
526 redisPanic("Unknown sorted set encoding");
528 } else if (o
->type
== REDIS_HASH
) {
529 /* Save a hash value */
530 if (o
->encoding
== REDIS_ENCODING_ZIPLIST
) {
531 size_t l
= ziplistBlobLen((unsigned char*)o
->ptr
);
533 if ((n
= rdbSaveRawString(rdb
,o
->ptr
,l
)) == -1) return -1;
536 } else if (o
->encoding
== REDIS_ENCODING_HT
) {
537 dictIterator
*di
= dictGetIterator(o
->ptr
);
540 if ((n
= rdbSaveLen(rdb
,dictSize((dict
*)o
->ptr
))) == -1) return -1;
543 while((de
= dictNext(di
)) != NULL
) {
544 robj
*key
= dictGetKey(de
);
545 robj
*val
= dictGetVal(de
);
547 if ((n
= rdbSaveStringObject(rdb
,key
)) == -1) return -1;
549 if ((n
= rdbSaveStringObject(rdb
,val
)) == -1) return -1;
552 dictReleaseIterator(di
);
555 redisPanic("Unknown hash encoding");
559 redisPanic("Unknown object type");
564 /* Return the length the object will have on disk if saved with
565 * the rdbSaveObject() function. Currently we use a trick to get
566 * this length with very little changes to the code. In the future
567 * we could switch to a faster solution. */
568 off_t
rdbSavedObjectLen(robj
*o
) {
569 int len
= rdbSaveObject(NULL
,o
);
570 redisAssertWithInfo(NULL
,o
,len
!= -1);
574 /* Save a key-value pair, with expire time, type, key, value.
575 * On error -1 is returned.
576 * On success if the key was actaully saved 1 is returned, otherwise 0
577 * is returned (the key was already expired). */
578 int rdbSaveKeyValuePair(rio
*rdb
, robj
*key
, robj
*val
,
579 long long expiretime
, long long now
)
581 /* Save the expire time */
582 if (expiretime
!= -1) {
583 /* If this key is already expired skip it */
584 if (expiretime
< now
) return 0;
585 if (rdbSaveType(rdb
,REDIS_RDB_OPCODE_EXPIRETIME_MS
) == -1) return -1;
586 if (rdbSaveMillisecondTime(rdb
,expiretime
) == -1) return -1;
589 /* Save type, key, value */
590 if (rdbSaveObjectType(rdb
,val
) == -1) return -1;
591 if (rdbSaveStringObject(rdb
,key
) == -1) return -1;
592 if (rdbSaveObject(rdb
,val
) == -1) return -1;
596 /* Save the DB on disk. Return REDIS_ERR on error, REDIS_OK on success */
597 int rdbSave(char *filename
) {
598 dictIterator
*di
= NULL
;
603 long long now
= mstime();
608 snprintf(tmpfile
,256,"temp-%d.rdb", (int) getpid());
609 fp
= fopen(tmpfile
,"w");
611 redisLog(REDIS_WARNING
, "Failed opening .rdb for saving: %s",
616 rioInitWithFile(&rdb
,fp
);
617 if (server
.rdb_checksum
)
618 rdb
.update_cksum
= rioGenericUpdateChecksum
;
619 snprintf(magic
,sizeof(magic
),"REDIS%04d",REDIS_RDB_VERSION
);
620 if (rdbWriteRaw(&rdb
,magic
,9) == -1) goto werr
;
622 for (j
= 0; j
< server
.dbnum
; j
++) {
623 redisDb
*db
= server
.db
+j
;
625 if (dictSize(d
) == 0) continue;
626 di
= dictGetSafeIterator(d
);
632 /* Write the SELECT DB opcode */
633 if (rdbSaveType(&rdb
,REDIS_RDB_OPCODE_SELECTDB
) == -1) goto werr
;
634 if (rdbSaveLen(&rdb
,j
) == -1) goto werr
;
636 /* Iterate this DB writing every entry */
637 while((de
= dictNext(di
)) != NULL
) {
638 sds keystr
= dictGetKey(de
);
639 robj key
, *o
= dictGetVal(de
);
642 initStaticStringObject(key
,keystr
);
643 expire
= getExpire(db
,&key
);
644 if (rdbSaveKeyValuePair(&rdb
,&key
,o
,expire
,now
) == -1) goto werr
;
646 dictReleaseIterator(di
);
648 di
= NULL
; /* So that we don't release it again on error. */
651 if (rdbSaveType(&rdb
,REDIS_RDB_OPCODE_EOF
) == -1) goto werr
;
653 /* CRC64 checksum. It will be zero if checksum computation is disabled, the
654 * loading code skips the check in this case. */
656 memrev64ifbe(&cksum
);
657 rioWrite(&rdb
,&cksum
,8);
659 /* Make sure data will not remain on the OS's output buffers */
664 /* Use RENAME to make sure the DB file is changed atomically only
665 * if the generate DB file is ok. */
666 if (rename(tmpfile
,filename
) == -1) {
667 redisLog(REDIS_WARNING
,"Error moving temp DB file on the final destination: %s", strerror(errno
));
671 redisLog(REDIS_NOTICE
,"DB saved on disk");
673 server
.lastsave
= time(NULL
);
674 server
.lastbgsave_status
= REDIS_OK
;
680 redisLog(REDIS_WARNING
,"Write error saving DB on disk: %s", strerror(errno
));
681 if (di
) dictReleaseIterator(di
);
685 int rdbSaveBackground(char *filename
) {
689 if (server
.rdb_child_pid
!= -1) return REDIS_ERR
;
691 server
.dirty_before_bgsave
= server
.dirty
;
694 if ((childpid
= fork()) == 0) {
698 if (server
.ipfd
> 0) close(server
.ipfd
);
699 if (server
.sofd
> 0) close(server
.sofd
);
700 retval
= rdbSave(filename
);
701 exitFromChild((retval
== REDIS_OK
) ? 0 : 1);
704 server
.stat_fork_time
= ustime()-start
;
705 if (childpid
== -1) {
706 redisLog(REDIS_WARNING
,"Can't save in background: fork: %s",
710 redisLog(REDIS_NOTICE
,"Background saving started by pid %d",childpid
);
711 server
.rdb_child_pid
= childpid
;
712 updateDictResizePolicy();
715 return REDIS_OK
; /* unreached */
718 void rdbRemoveTempFile(pid_t childpid
) {
721 snprintf(tmpfile
,256,"temp-%d.rdb", (int) childpid
);
725 /* Load a Redis object of the specified type from the specified file.
726 * On success a newly allocated object is returned, otherwise NULL. */
727 robj
*rdbLoadObject(int rdbtype
, rio
*rdb
) {
732 redisLog(REDIS_DEBUG
,"LOADING OBJECT %d (at %d)\n",rdbtype
,rioTell(rdb
));
733 if (rdbtype
== REDIS_RDB_TYPE_STRING
) {
734 /* Read string value */
735 if ((o
= rdbLoadEncodedStringObject(rdb
)) == NULL
) return NULL
;
736 o
= tryObjectEncoding(o
);
737 } else if (rdbtype
== REDIS_RDB_TYPE_LIST
) {
738 /* Read list value */
739 if ((len
= rdbLoadLen(rdb
,NULL
)) == REDIS_RDB_LENERR
) return NULL
;
741 /* Use a real list when there are too many entries */
742 if (len
> server
.list_max_ziplist_entries
) {
743 o
= createListObject();
745 o
= createZiplistObject();
748 /* Load every single element of the list */
750 if ((ele
= rdbLoadEncodedStringObject(rdb
)) == NULL
) return NULL
;
752 /* If we are using a ziplist and the value is too big, convert
753 * the object to a real list. */
754 if (o
->encoding
== REDIS_ENCODING_ZIPLIST
&&
755 ele
->encoding
== REDIS_ENCODING_RAW
&&
756 sdslen(ele
->ptr
) > server
.list_max_ziplist_value
)
757 listTypeConvert(o
,REDIS_ENCODING_LINKEDLIST
);
759 if (o
->encoding
== REDIS_ENCODING_ZIPLIST
) {
760 dec
= getDecodedObject(ele
);
761 o
->ptr
= ziplistPush(o
->ptr
,dec
->ptr
,sdslen(dec
->ptr
),REDIS_TAIL
);
765 ele
= tryObjectEncoding(ele
);
766 listAddNodeTail(o
->ptr
,ele
);
769 } else if (rdbtype
== REDIS_RDB_TYPE_SET
) {
770 /* Read list/set value */
771 if ((len
= rdbLoadLen(rdb
,NULL
)) == REDIS_RDB_LENERR
) return NULL
;
773 /* Use a regular set when there are too many entries. */
774 if (len
> server
.set_max_intset_entries
) {
775 o
= createSetObject();
776 /* It's faster to expand the dict to the right size asap in order
777 * to avoid rehashing */
778 if (len
> DICT_HT_INITIAL_SIZE
)
779 dictExpand(o
->ptr
,len
);
781 o
= createIntsetObject();
784 /* Load every single element of the list/set */
785 for (i
= 0; i
< len
; i
++) {
787 if ((ele
= rdbLoadEncodedStringObject(rdb
)) == NULL
) return NULL
;
788 ele
= tryObjectEncoding(ele
);
790 if (o
->encoding
== REDIS_ENCODING_INTSET
) {
791 /* Fetch integer value from element */
792 if (isObjectRepresentableAsLongLong(ele
,&llval
) == REDIS_OK
) {
793 o
->ptr
= intsetAdd(o
->ptr
,llval
,NULL
);
795 setTypeConvert(o
,REDIS_ENCODING_HT
);
796 dictExpand(o
->ptr
,len
);
800 /* This will also be called when the set was just converted
801 * to regular hashtable encoded set */
802 if (o
->encoding
== REDIS_ENCODING_HT
) {
803 dictAdd((dict
*)o
->ptr
,ele
,NULL
);
808 } else if (rdbtype
== REDIS_RDB_TYPE_ZSET
) {
809 /* Read list/set value */
811 size_t maxelelen
= 0;
814 if ((zsetlen
= rdbLoadLen(rdb
,NULL
)) == REDIS_RDB_LENERR
) return NULL
;
815 o
= createZsetObject();
818 /* Load every single element of the list/set */
822 zskiplistNode
*znode
;
824 if ((ele
= rdbLoadEncodedStringObject(rdb
)) == NULL
) return NULL
;
825 ele
= tryObjectEncoding(ele
);
826 if (rdbLoadDoubleValue(rdb
,&score
) == -1) return NULL
;
828 /* Don't care about integer-encoded strings. */
829 if (ele
->encoding
== REDIS_ENCODING_RAW
&&
830 sdslen(ele
->ptr
) > maxelelen
)
831 maxelelen
= sdslen(ele
->ptr
);
833 znode
= zslInsert(zs
->zsl
,score
,ele
);
834 dictAdd(zs
->dict
,ele
,&znode
->score
);
835 incrRefCount(ele
); /* added to skiplist */
838 /* Convert *after* loading, since sorted sets are not stored ordered. */
839 if (zsetLength(o
) <= server
.zset_max_ziplist_entries
&&
840 maxelelen
<= server
.zset_max_ziplist_value
)
841 zsetConvert(o
,REDIS_ENCODING_ZIPLIST
);
842 } else if (rdbtype
== REDIS_RDB_TYPE_HASH
) {
846 len
= rdbLoadLen(rdb
, NULL
);
847 if (len
== REDIS_RDB_LENERR
) return NULL
;
849 o
= createHashObject();
851 /* Too many entries? Use an hash table. */
852 if (len
> server
.hash_max_ziplist_entries
)
853 hashTypeConvert(o
, REDIS_ENCODING_HT
);
855 /* Load every field and value into the ziplist */
856 while (o
->encoding
== REDIS_ENCODING_ZIPLIST
&& len
> 0) {
860 /* Load raw strings */
861 field
= rdbLoadStringObject(rdb
);
862 if (field
== NULL
) return NULL
;
863 redisAssert(field
->encoding
== REDIS_ENCODING_RAW
);
864 value
= rdbLoadStringObject(rdb
);
865 if (value
== NULL
) return NULL
;
866 redisAssert(field
->encoding
== REDIS_ENCODING_RAW
);
868 /* Add pair to ziplist */
869 o
->ptr
= ziplistPush(o
->ptr
, field
->ptr
, sdslen(field
->ptr
), ZIPLIST_TAIL
);
870 o
->ptr
= ziplistPush(o
->ptr
, value
->ptr
, sdslen(value
->ptr
), ZIPLIST_TAIL
);
871 /* Convert to hash table if size threshold is exceeded */
872 if (sdslen(field
->ptr
) > server
.hash_max_ziplist_value
||
873 sdslen(value
->ptr
) > server
.hash_max_ziplist_value
)
877 hashTypeConvert(o
, REDIS_ENCODING_HT
);
884 /* Load remaining fields and values into the hash table */
885 while (o
->encoding
== REDIS_ENCODING_HT
&& len
> 0) {
889 /* Load encoded strings */
890 field
= rdbLoadEncodedStringObject(rdb
);
891 if (field
== NULL
) return NULL
;
892 value
= rdbLoadEncodedStringObject(rdb
);
893 if (value
== NULL
) return NULL
;
895 field
= tryObjectEncoding(field
);
896 value
= tryObjectEncoding(value
);
898 /* Add pair to hash table */
899 ret
= dictAdd((dict
*)o
->ptr
, field
, value
);
900 redisAssert(ret
== REDIS_OK
);
903 /* All pairs should be read by now */
904 redisAssert(len
== 0);
906 } else if (rdbtype
== REDIS_RDB_TYPE_HASH_ZIPMAP
||
907 rdbtype
== REDIS_RDB_TYPE_LIST_ZIPLIST
||
908 rdbtype
== REDIS_RDB_TYPE_SET_INTSET
||
909 rdbtype
== REDIS_RDB_TYPE_ZSET_ZIPLIST
||
910 rdbtype
== REDIS_RDB_TYPE_HASH_ZIPLIST
)
912 robj
*aux
= rdbLoadStringObject(rdb
);
914 if (aux
== NULL
) return NULL
;
915 o
= createObject(REDIS_STRING
,NULL
); /* string is just placeholder */
916 o
->ptr
= zmalloc(sdslen(aux
->ptr
));
917 memcpy(o
->ptr
,aux
->ptr
,sdslen(aux
->ptr
));
920 /* Fix the object encoding, and make sure to convert the encoded
921 * data type into the base type if accordingly to the current
922 * configuration there are too many elements in the encoded data
923 * type. Note that we only check the length and not max element
924 * size as this is an O(N) scan. Eventually everything will get
927 case REDIS_RDB_TYPE_HASH_ZIPMAP
:
928 /* Convert to ziplist encoded hash. This must be deprecated
929 * when loading dumps created by Redis 2.4 gets deprecated. */
931 unsigned char *zl
= ziplistNew();
932 unsigned char *zi
= zipmapRewind(o
->ptr
);
933 unsigned char *fstr
, *vstr
;
934 unsigned int flen
, vlen
;
935 unsigned int maxlen
= 0;
937 while ((zi
= zipmapNext(zi
, &fstr
, &flen
, &vstr
, &vlen
)) != NULL
) {
938 if (flen
> maxlen
) maxlen
= flen
;
939 if (vlen
> maxlen
) maxlen
= vlen
;
940 zl
= ziplistPush(zl
, fstr
, flen
, ZIPLIST_TAIL
);
941 zl
= ziplistPush(zl
, vstr
, vlen
, ZIPLIST_TAIL
);
946 o
->type
= REDIS_HASH
;
947 o
->encoding
= REDIS_ENCODING_ZIPLIST
;
949 if (hashTypeLength(o
) > server
.hash_max_ziplist_entries
||
950 maxlen
> server
.hash_max_ziplist_value
)
952 hashTypeConvert(o
, REDIS_ENCODING_HT
);
956 case REDIS_RDB_TYPE_LIST_ZIPLIST
:
957 o
->type
= REDIS_LIST
;
958 o
->encoding
= REDIS_ENCODING_ZIPLIST
;
959 if (ziplistLen(o
->ptr
) > server
.list_max_ziplist_entries
)
960 listTypeConvert(o
,REDIS_ENCODING_LINKEDLIST
);
962 case REDIS_RDB_TYPE_SET_INTSET
:
964 o
->encoding
= REDIS_ENCODING_INTSET
;
965 if (intsetLen(o
->ptr
) > server
.set_max_intset_entries
)
966 setTypeConvert(o
,REDIS_ENCODING_HT
);
968 case REDIS_RDB_TYPE_ZSET_ZIPLIST
:
969 o
->type
= REDIS_ZSET
;
970 o
->encoding
= REDIS_ENCODING_ZIPLIST
;
971 if (zsetLength(o
) > server
.zset_max_ziplist_entries
)
972 zsetConvert(o
,REDIS_ENCODING_SKIPLIST
);
974 case REDIS_RDB_TYPE_HASH_ZIPLIST
:
975 o
->type
= REDIS_HASH
;
976 o
->encoding
= REDIS_ENCODING_ZIPLIST
;
977 if (hashTypeLength(o
) > server
.hash_max_ziplist_entries
)
978 hashTypeConvert(o
, REDIS_ENCODING_HT
);
981 redisPanic("Unknown encoding");
985 redisPanic("Unknown object type");
990 /* Mark that we are loading in the global state and setup the fields
991 * needed to provide loading stats. */
992 void startLoading(FILE *fp
) {
997 server
.loading_start_time
= time(NULL
);
998 if (fstat(fileno(fp
), &sb
) == -1) {
999 server
.loading_total_bytes
= 1; /* just to avoid division by zero */
1001 server
.loading_total_bytes
= sb
.st_size
;
1005 /* Refresh the loading progress info */
1006 void loadingProgress(off_t pos
) {
1007 server
.loading_loaded_bytes
= pos
;
1010 /* Loading finished */
1011 void stopLoading(void) {
1015 int rdbLoad(char *filename
) {
1018 redisDb
*db
= server
.db
+0;
1020 long long expiretime
, now
= mstime();
1025 fp
= fopen(filename
,"r");
1030 rioInitWithFile(&rdb
,fp
);
1031 if (server
.rdb_checksum
)
1032 rdb
.update_cksum
= rioGenericUpdateChecksum
;
1033 if (rioRead(&rdb
,buf
,9) == 0) goto eoferr
;
1035 if (memcmp(buf
,"REDIS",5) != 0) {
1037 redisLog(REDIS_WARNING
,"Wrong signature trying to load DB from file");
1041 rdbver
= atoi(buf
+5);
1042 if (rdbver
< 1 || rdbver
> REDIS_RDB_VERSION
) {
1044 redisLog(REDIS_WARNING
,"Can't handle RDB format version %d",rdbver
);
1054 /* Serve the clients from time to time */
1055 if (!(loops
++ % 1000)) {
1056 loadingProgress(rioTell(&rdb
));
1057 aeProcessEvents(server
.el
, AE_FILE_EVENTS
|AE_DONT_WAIT
);
1061 if ((type
= rdbLoadType(&rdb
)) == -1) goto eoferr
;
1062 if (type
== REDIS_RDB_OPCODE_EXPIRETIME
) {
1063 if ((expiretime
= rdbLoadTime(&rdb
)) == -1) goto eoferr
;
1064 /* We read the time so we need to read the object type again. */
1065 if ((type
= rdbLoadType(&rdb
)) == -1) goto eoferr
;
1066 /* the EXPIRETIME opcode specifies time in seconds, so convert
1067 * into milliesconds. */
1069 } else if (type
== REDIS_RDB_OPCODE_EXPIRETIME_MS
) {
1070 /* Milliseconds precision expire times introduced with RDB
1072 if ((expiretime
= rdbLoadMillisecondTime(&rdb
)) == -1) goto eoferr
;
1073 /* We read the time so we need to read the object type again. */
1074 if ((type
= rdbLoadType(&rdb
)) == -1) goto eoferr
;
1077 if (type
== REDIS_RDB_OPCODE_EOF
)
1080 /* Handle SELECT DB opcode as a special case */
1081 if (type
== REDIS_RDB_OPCODE_SELECTDB
) {
1082 if ((dbid
= rdbLoadLen(&rdb
,NULL
)) == REDIS_RDB_LENERR
)
1084 if (dbid
>= (unsigned)server
.dbnum
) {
1085 redisLog(REDIS_WARNING
,"FATAL: Data file was created with a Redis server configured to handle more than %d databases. Exiting\n", server
.dbnum
);
1088 db
= server
.db
+dbid
;
1092 if ((key
= rdbLoadStringObject(&rdb
)) == NULL
) goto eoferr
;
1094 if ((val
= rdbLoadObject(type
,&rdb
)) == NULL
) goto eoferr
;
1095 /* Check if the key already expired. This function is used when loading
1096 * an RDB file from disk, either at startup, or when an RDB was
1097 * received from the master. In the latter case, the master is
1098 * responsible for key expiry. If we would expire keys here, the
1099 * snapshot taken by the master may not be reflected on the slave. */
1100 if (server
.masterhost
== NULL
&& expiretime
!= -1 && expiretime
< now
) {
1105 /* Add the new object in the hash table */
1108 /* Set the expire time if needed */
1109 if (expiretime
!= -1) setExpire(db
,key
,expiretime
);
1113 /* Verify the checksum if RDB version is >= 5 */
1114 if (rdbver
>= 5 && server
.rdb_checksum
) {
1115 uint64_t cksum
, expected
= rdb
.cksum
;
1117 if (rioRead(&rdb
,&cksum
,8) == 0) goto eoferr
;
1118 memrev64ifbe(&cksum
);
1120 redisLog(REDIS_WARNING
,"RDB file was saved with checksum disabled: no check performed.");
1121 } else if (cksum
!= expected
) {
1122 redisLog(REDIS_WARNING
,"Wrong RDB checksum. Aborting now.");
1131 eoferr
: /* unexpected end of file is handled here with a fatal exit */
1132 redisLog(REDIS_WARNING
,"Short read or OOM loading DB. Unrecoverable error, aborting now.");
1134 return REDIS_ERR
; /* Just to avoid warning */
1137 /* A background saving child (BGSAVE) terminated its work. Handle this. */
1138 void backgroundSaveDoneHandler(int exitcode
, int bysignal
) {
1139 if (!bysignal
&& exitcode
== 0) {
1140 redisLog(REDIS_NOTICE
,
1141 "Background saving terminated with success");
1142 server
.dirty
= server
.dirty
- server
.dirty_before_bgsave
;
1143 server
.lastsave
= time(NULL
);
1144 server
.lastbgsave_status
= REDIS_OK
;
1145 } else if (!bysignal
&& exitcode
!= 0) {
1146 redisLog(REDIS_WARNING
, "Background saving error");
1147 server
.lastbgsave_status
= REDIS_ERR
;
1149 redisLog(REDIS_WARNING
,
1150 "Background saving terminated by signal %d", bysignal
);
1151 rdbRemoveTempFile(server
.rdb_child_pid
);
1152 server
.lastbgsave_status
= REDIS_ERR
;
1154 server
.rdb_child_pid
= -1;
1155 /* Possibly there are slaves waiting for a BGSAVE in order to be served
1156 * (the first stage of SYNC is a bulk transfer of dump.rdb) */
1157 updateSlavesWaitingBgsave(exitcode
== 0 ? REDIS_OK
: REDIS_ERR
);
1160 void saveCommand(redisClient
*c
) {
1161 if (server
.rdb_child_pid
!= -1) {
1162 addReplyError(c
,"Background save already in progress");
1165 if (rdbSave(server
.rdb_filename
) == REDIS_OK
) {
1166 addReply(c
,shared
.ok
);
1168 addReply(c
,shared
.err
);
1172 void bgsaveCommand(redisClient
*c
) {
1173 if (server
.rdb_child_pid
!= -1) {
1174 addReplyError(c
,"Background save already in progress");
1175 } else if (server
.aof_child_pid
!= -1) {
1176 addReplyError(c
,"Can't BGSAVE while AOF log rewriting is in progress");
1177 } else if (rdbSaveBackground(server
.rdb_filename
) == REDIS_OK
) {
1178 addReplyStatus(c
,"Background saving started");
1180 addReply(c
,shared
.err
);