2 #include "lzf.h" /* LZF compression library */
7 #include <sys/resource.h>
12 static int rdbWriteRaw(rio
*rdb
, void *p
, size_t len
) {
13 if (rdb
&& rioWrite(rdb
,p
,len
) == 0)
18 int rdbSaveType(rio
*rdb
, unsigned char type
) {
19 return rdbWriteRaw(rdb
,&type
,1);
22 int rdbLoadType(rio
*rdb
) {
24 if (rioRead(rdb
,&type
,1) == 0) return -1;
28 int rdbSaveTime(rio
*rdb
, time_t t
) {
29 int32_t t32
= (int32_t) t
;
30 return rdbWriteRaw(rdb
,&t32
,4);
33 time_t rdbLoadTime(rio
*rdb
) {
35 if (rioRead(rdb
,&t32
,4) == 0) return -1;
39 int rdbSaveMillisecondTime(rio
*rdb
, long long t
) {
40 int64_t t64
= (int64_t) t
;
41 return rdbWriteRaw(rdb
,&t64
,8);
44 long long rdbLoadMillisecondTime(rio
*rdb
) {
46 if (rioRead(rdb
,&t64
,8) == 0) return -1;
47 return (long long)t64
;
50 /* Saves an encoded length. The first two bits in the first byte are used to
51 * hold the encoding type. See the REDIS_RDB_* definitions for more information
52 * on the types of encoding. */
53 int rdbSaveLen(rio
*rdb
, uint32_t len
) {
58 /* Save a 6 bit len */
59 buf
[0] = (len
&0xFF)|(REDIS_RDB_6BITLEN
<<6);
60 if (rdbWriteRaw(rdb
,buf
,1) == -1) return -1;
62 } else if (len
< (1<<14)) {
63 /* Save a 14 bit len */
64 buf
[0] = ((len
>>8)&0xFF)|(REDIS_RDB_14BITLEN
<<6);
66 if (rdbWriteRaw(rdb
,buf
,2) == -1) return -1;
69 /* Save a 32 bit len */
70 buf
[0] = (REDIS_RDB_32BITLEN
<<6);
71 if (rdbWriteRaw(rdb
,buf
,1) == -1) return -1;
73 if (rdbWriteRaw(rdb
,&len
,4) == -4) return -1;
79 /* Load an encoded length. The "isencoded" argument is set to 1 if the length
80 * is not actually a length but an "encoding type". See the REDIS_RDB_ENC_*
81 * definitions in rdb.h for more information. */
82 uint32_t rdbLoadLen(rio
*rdb
, int *isencoded
) {
87 if (isencoded
) *isencoded
= 0;
88 if (rioRead(rdb
,buf
,1) == 0) return REDIS_RDB_LENERR
;
89 type
= (buf
[0]&0xC0)>>6;
90 if (type
== REDIS_RDB_ENCVAL
) {
91 /* Read a 6 bit encoding type. */
92 if (isencoded
) *isencoded
= 1;
94 } else if (type
== REDIS_RDB_6BITLEN
) {
95 /* Read a 6 bit len. */
97 } else if (type
== REDIS_RDB_14BITLEN
) {
98 /* Read a 14 bit len. */
99 if (rioRead(rdb
,buf
+1,1) == 0) return REDIS_RDB_LENERR
;
100 return ((buf
[0]&0x3F)<<8)|buf
[1];
102 /* Read a 32 bit len. */
103 if (rioRead(rdb
,&len
,4) == 0) return REDIS_RDB_LENERR
;
108 /* Encodes the "value" argument as integer when it fits in the supported ranges
109 * for encoded types. If the function successfully encodes the integer, the
110 * representation is stored in the buffer pointer to by "enc" and the string
111 * length is returned. Otherwise 0 is returned. */
112 int rdbEncodeInteger(long long value
, unsigned char *enc
) {
113 if (value
>= -(1<<7) && value
<= (1<<7)-1) {
114 enc
[0] = (REDIS_RDB_ENCVAL
<<6)|REDIS_RDB_ENC_INT8
;
117 } else if (value
>= -(1<<15) && value
<= (1<<15)-1) {
118 enc
[0] = (REDIS_RDB_ENCVAL
<<6)|REDIS_RDB_ENC_INT16
;
120 enc
[2] = (value
>>8)&0xFF;
122 } else if (value
>= -((long long)1<<31) && value
<= ((long long)1<<31)-1) {
123 enc
[0] = (REDIS_RDB_ENCVAL
<<6)|REDIS_RDB_ENC_INT32
;
125 enc
[2] = (value
>>8)&0xFF;
126 enc
[3] = (value
>>16)&0xFF;
127 enc
[4] = (value
>>24)&0xFF;
134 /* Loads an integer-encoded object with the specified encoding type "enctype".
135 * If the "encode" argument is set the function may return an integer-encoded
136 * string object, otherwise it always returns a raw string object. */
137 robj
*rdbLoadIntegerObject(rio
*rdb
, int enctype
, int encode
) {
138 unsigned char enc
[4];
141 if (enctype
== REDIS_RDB_ENC_INT8
) {
142 if (rioRead(rdb
,enc
,1) == 0) return NULL
;
143 val
= (signed char)enc
[0];
144 } else if (enctype
== REDIS_RDB_ENC_INT16
) {
146 if (rioRead(rdb
,enc
,2) == 0) return NULL
;
147 v
= enc
[0]|(enc
[1]<<8);
149 } else if (enctype
== REDIS_RDB_ENC_INT32
) {
151 if (rioRead(rdb
,enc
,4) == 0) return NULL
;
152 v
= enc
[0]|(enc
[1]<<8)|(enc
[2]<<16)|(enc
[3]<<24);
155 val
= 0; /* anti-warning */
156 redisPanic("Unknown RDB integer encoding type");
159 return createStringObjectFromLongLong(val
);
161 return createObject(REDIS_STRING
,sdsfromlonglong(val
));
164 /* String objects in the form "2391" "-100" without any space and with a
165 * range of values that can fit in an 8, 16 or 32 bit signed value can be
166 * encoded as integers to save space */
167 int rdbTryIntegerEncoding(char *s
, size_t len
, unsigned char *enc
) {
169 char *endptr
, buf
[32];
171 /* Check if it's possible to encode this value as a number */
172 value
= strtoll(s
, &endptr
, 10);
173 if (endptr
[0] != '\0') return 0;
174 ll2string(buf
,32,value
);
176 /* If the number converted back into a string is not identical
177 * then it's not possible to encode the string as integer */
178 if (strlen(buf
) != len
|| memcmp(buf
,s
,len
)) return 0;
180 return rdbEncodeInteger(value
,enc
);
183 int rdbSaveLzfStringObject(rio
*rdb
, unsigned char *s
, size_t len
) {
184 size_t comprlen
, outlen
;
189 /* We require at least four bytes compression for this to be worth it */
190 if (len
<= 4) return 0;
192 if ((out
= zmalloc(outlen
+1)) == NULL
) return 0;
193 comprlen
= lzf_compress(s
, len
, out
, outlen
);
198 /* Data compressed! Let's save it on disk */
199 byte
= (REDIS_RDB_ENCVAL
<<6)|REDIS_RDB_ENC_LZF
;
200 if ((n
= rdbWriteRaw(rdb
,&byte
,1)) == -1) goto writeerr
;
203 if ((n
= rdbSaveLen(rdb
,comprlen
)) == -1) goto writeerr
;
206 if ((n
= rdbSaveLen(rdb
,len
)) == -1) goto writeerr
;
209 if ((n
= rdbWriteRaw(rdb
,out
,comprlen
)) == -1) goto writeerr
;
220 robj
*rdbLoadLzfStringObject(rio
*rdb
) {
221 unsigned int len
, clen
;
222 unsigned char *c
= NULL
;
225 if ((clen
= rdbLoadLen(rdb
,NULL
)) == REDIS_RDB_LENERR
) return NULL
;
226 if ((len
= rdbLoadLen(rdb
,NULL
)) == REDIS_RDB_LENERR
) return NULL
;
227 if ((c
= zmalloc(clen
)) == NULL
) goto err
;
228 if ((val
= sdsnewlen(NULL
,len
)) == NULL
) goto err
;
229 if (rioRead(rdb
,c
,clen
) == 0) goto err
;
230 if (lzf_decompress(c
,clen
,val
,len
) == 0) goto err
;
232 return createObject(REDIS_STRING
,val
);
239 /* Save a string objet as [len][data] on disk. If the object is a string
240 * representation of an integer value we try to save it in a special form */
241 int rdbSaveRawString(rio
*rdb
, unsigned char *s
, size_t len
) {
245 /* Try integer encoding */
247 unsigned char buf
[5];
248 if ((enclen
= rdbTryIntegerEncoding((char*)s
,len
,buf
)) > 0) {
249 if (rdbWriteRaw(rdb
,buf
,enclen
) == -1) return -1;
254 /* Try LZF compression - under 20 bytes it's unable to compress even
255 * aaaaaaaaaaaaaaaaaa so skip it */
256 if (server
.rdbcompression
&& len
> 20) {
257 n
= rdbSaveLzfStringObject(rdb
,s
,len
);
258 if (n
== -1) return -1;
260 /* Return value of 0 means data can't be compressed, save the old way */
264 if ((n
= rdbSaveLen(rdb
,len
)) == -1) return -1;
267 if (rdbWriteRaw(rdb
,s
,len
) == -1) return -1;
273 /* Save a long long value as either an encoded string or a string. */
274 int rdbSaveLongLongAsStringObject(rio
*rdb
, long long value
) {
275 unsigned char buf
[32];
277 int enclen
= rdbEncodeInteger(value
,buf
);
279 return rdbWriteRaw(rdb
,buf
,enclen
);
281 /* Encode as string */
282 enclen
= ll2string((char*)buf
,32,value
);
283 redisAssert(enclen
< 32);
284 if ((n
= rdbSaveLen(rdb
,enclen
)) == -1) return -1;
286 if ((n
= rdbWriteRaw(rdb
,buf
,enclen
)) == -1) return -1;
292 /* Like rdbSaveStringObjectRaw() but handle encoded objects */
293 int rdbSaveStringObject(rio
*rdb
, robj
*obj
) {
294 /* Avoid to decode the object, then encode it again, if the
295 * object is alrady integer encoded. */
296 if (obj
->encoding
== REDIS_ENCODING_INT
) {
297 return rdbSaveLongLongAsStringObject(rdb
,(long)obj
->ptr
);
299 redisAssertWithInfo(NULL
,obj
,obj
->encoding
== REDIS_ENCODING_RAW
);
300 return rdbSaveRawString(rdb
,obj
->ptr
,sdslen(obj
->ptr
));
304 robj
*rdbGenericLoadStringObject(rio
*rdb
, int encode
) {
309 len
= rdbLoadLen(rdb
,&isencoded
);
312 case REDIS_RDB_ENC_INT8
:
313 case REDIS_RDB_ENC_INT16
:
314 case REDIS_RDB_ENC_INT32
:
315 return rdbLoadIntegerObject(rdb
,len
,encode
);
316 case REDIS_RDB_ENC_LZF
:
317 return rdbLoadLzfStringObject(rdb
);
319 redisPanic("Unknown RDB encoding type");
323 if (len
== REDIS_RDB_LENERR
) return NULL
;
324 val
= sdsnewlen(NULL
,len
);
325 if (len
&& rioRead(rdb
,val
,len
) == 0) {
329 return createObject(REDIS_STRING
,val
);
332 robj
*rdbLoadStringObject(rio
*rdb
) {
333 return rdbGenericLoadStringObject(rdb
,0);
336 robj
*rdbLoadEncodedStringObject(rio
*rdb
) {
337 return rdbGenericLoadStringObject(rdb
,1);
340 /* Save a double value. Doubles are saved as strings prefixed by an unsigned
341 * 8 bit integer specifing the length of the representation.
342 * This 8 bit integer has special values in order to specify the following
348 int rdbSaveDoubleValue(rio
*rdb
, double val
) {
349 unsigned char buf
[128];
355 } else if (!isfinite(val
)) {
357 buf
[0] = (val
< 0) ? 255 : 254;
359 #if (DBL_MANT_DIG >= 52) && (LLONG_MAX == 0x7fffffffffffffffLL)
360 /* Check if the float is in a safe range to be casted into a
361 * long long. We are assuming that long long is 64 bit here.
362 * Also we are assuming that there are no implementations around where
363 * double has precision < 52 bit.
365 * Under this assumptions we test if a double is inside an interval
366 * where casting to long long is safe. Then using two castings we
367 * make sure the decimal part is zero. If all this is true we use
368 * integer printing function that is much faster. */
369 double min
= -4503599627370495; /* (2^52)-1 */
370 double max
= 4503599627370496; /* -(2^52) */
371 if (val
> min
&& val
< max
&& val
== ((double)((long long)val
)))
372 ll2string((char*)buf
+1,sizeof(buf
),(long long)val
);
375 snprintf((char*)buf
+1,sizeof(buf
)-1,"%.17g",val
);
376 buf
[0] = strlen((char*)buf
+1);
379 return rdbWriteRaw(rdb
,buf
,len
);
382 /* For information about double serialization check rdbSaveDoubleValue() */
383 int rdbLoadDoubleValue(rio
*rdb
, double *val
) {
387 if (rioRead(rdb
,&len
,1) == 0) return -1;
389 case 255: *val
= R_NegInf
; return 0;
390 case 254: *val
= R_PosInf
; return 0;
391 case 253: *val
= R_Nan
; return 0;
393 if (rioRead(rdb
,buf
,len
) == 0) return -1;
395 sscanf(buf
, "%lg", val
);
400 /* Save the object type of object "o". */
401 int rdbSaveObjectType(rio
*rdb
, robj
*o
) {
404 return rdbSaveType(rdb
,REDIS_RDB_TYPE_STRING
);
406 if (o
->encoding
== REDIS_ENCODING_ZIPLIST
)
407 return rdbSaveType(rdb
,REDIS_RDB_TYPE_LIST_ZIPLIST
);
408 else if (o
->encoding
== REDIS_ENCODING_LINKEDLIST
)
409 return rdbSaveType(rdb
,REDIS_RDB_TYPE_LIST
);
411 redisPanic("Unknown list encoding");
413 if (o
->encoding
== REDIS_ENCODING_INTSET
)
414 return rdbSaveType(rdb
,REDIS_RDB_TYPE_SET_INTSET
);
415 else if (o
->encoding
== REDIS_ENCODING_HT
)
416 return rdbSaveType(rdb
,REDIS_RDB_TYPE_SET
);
418 redisPanic("Unknown set encoding");
420 if (o
->encoding
== REDIS_ENCODING_ZIPLIST
)
421 return rdbSaveType(rdb
,REDIS_RDB_TYPE_ZSET_ZIPLIST
);
422 else if (o
->encoding
== REDIS_ENCODING_SKIPLIST
)
423 return rdbSaveType(rdb
,REDIS_RDB_TYPE_ZSET
);
425 redisPanic("Unknown sorted set encoding");
427 if (o
->encoding
== REDIS_ENCODING_ZIPMAP
)
428 return rdbSaveType(rdb
,REDIS_RDB_TYPE_HASH_ZIPMAP
);
429 else if (o
->encoding
== REDIS_ENCODING_HT
)
430 return rdbSaveType(rdb
,REDIS_RDB_TYPE_HASH
);
432 redisPanic("Unknown hash encoding");
434 redisPanic("Unknown object type");
436 return -1; /* avoid warning */
439 /* Load object type. Return -1 when the byte doesn't contain an object type. */
440 int rdbLoadObjectType(rio
*rdb
) {
442 if ((type
= rdbLoadType(rdb
)) == -1) return -1;
443 if (!rdbIsObjectType(type
)) return -1;
447 /* Save a Redis object. Returns -1 on error, 0 on success. */
448 int rdbSaveObject(rio
*rdb
, robj
*o
) {
451 if (o
->type
== REDIS_STRING
) {
452 /* Save a string value */
453 if ((n
= rdbSaveStringObject(rdb
,o
)) == -1) return -1;
455 } else if (o
->type
== REDIS_LIST
) {
456 /* Save a list value */
457 if (o
->encoding
== REDIS_ENCODING_ZIPLIST
) {
458 size_t l
= ziplistBlobLen((unsigned char*)o
->ptr
);
460 if ((n
= rdbSaveRawString(rdb
,o
->ptr
,l
)) == -1) return -1;
462 } else if (o
->encoding
== REDIS_ENCODING_LINKEDLIST
) {
467 if ((n
= rdbSaveLen(rdb
,listLength(list
))) == -1) return -1;
470 listRewind(list
,&li
);
471 while((ln
= listNext(&li
))) {
472 robj
*eleobj
= listNodeValue(ln
);
473 if ((n
= rdbSaveStringObject(rdb
,eleobj
)) == -1) return -1;
477 redisPanic("Unknown list encoding");
479 } else if (o
->type
== REDIS_SET
) {
480 /* Save a set value */
481 if (o
->encoding
== REDIS_ENCODING_HT
) {
483 dictIterator
*di
= dictGetIterator(set
);
486 if ((n
= rdbSaveLen(rdb
,dictSize(set
))) == -1) return -1;
489 while((de
= dictNext(di
)) != NULL
) {
490 robj
*eleobj
= dictGetKey(de
);
491 if ((n
= rdbSaveStringObject(rdb
,eleobj
)) == -1) return -1;
494 dictReleaseIterator(di
);
495 } else if (o
->encoding
== REDIS_ENCODING_INTSET
) {
496 size_t l
= intsetBlobLen((intset
*)o
->ptr
);
498 if ((n
= rdbSaveRawString(rdb
,o
->ptr
,l
)) == -1) return -1;
501 redisPanic("Unknown set encoding");
503 } else if (o
->type
== REDIS_ZSET
) {
504 /* Save a sorted set value */
505 if (o
->encoding
== REDIS_ENCODING_ZIPLIST
) {
506 size_t l
= ziplistBlobLen((unsigned char*)o
->ptr
);
508 if ((n
= rdbSaveRawString(rdb
,o
->ptr
,l
)) == -1) return -1;
510 } else if (o
->encoding
== REDIS_ENCODING_SKIPLIST
) {
512 dictIterator
*di
= dictGetIterator(zs
->dict
);
515 if ((n
= rdbSaveLen(rdb
,dictSize(zs
->dict
))) == -1) return -1;
518 while((de
= dictNext(di
)) != NULL
) {
519 robj
*eleobj
= dictGetKey(de
);
520 double *score
= dictGetVal(de
);
522 if ((n
= rdbSaveStringObject(rdb
,eleobj
)) == -1) return -1;
524 if ((n
= rdbSaveDoubleValue(rdb
,*score
)) == -1) return -1;
527 dictReleaseIterator(di
);
529 redisPanic("Unknown sorted set encoding");
531 } else if (o
->type
== REDIS_HASH
) {
532 /* Save a hash value */
533 if (o
->encoding
== REDIS_ENCODING_ZIPMAP
) {
534 size_t l
= zipmapBlobLen((unsigned char*)o
->ptr
);
536 if ((n
= rdbSaveRawString(rdb
,o
->ptr
,l
)) == -1) return -1;
539 dictIterator
*di
= dictGetIterator(o
->ptr
);
542 if ((n
= rdbSaveLen(rdb
,dictSize((dict
*)o
->ptr
))) == -1) return -1;
545 while((de
= dictNext(di
)) != NULL
) {
546 robj
*key
= dictGetKey(de
);
547 robj
*val
= dictGetVal(de
);
549 if ((n
= rdbSaveStringObject(rdb
,key
)) == -1) return -1;
551 if ((n
= rdbSaveStringObject(rdb
,val
)) == -1) return -1;
554 dictReleaseIterator(di
);
557 redisPanic("Unknown object type");
562 /* Return the length the object will have on disk if saved with
563 * the rdbSaveObject() function. Currently we use a trick to get
564 * this length with very little changes to the code. In the future
565 * we could switch to a faster solution. */
566 off_t
rdbSavedObjectLen(robj
*o
) {
567 int len
= rdbSaveObject(NULL
,o
);
568 redisAssertWithInfo(NULL
,o
,len
!= -1);
572 /* Save a key-value pair, with expire time, type, key, value.
573 * On error -1 is returned.
574 * On success if the key was actaully saved 1 is returned, otherwise 0
575 * is returned (the key was already expired). */
576 int rdbSaveKeyValuePair(rio
*rdb
, robj
*key
, robj
*val
,
577 long long expiretime
, long long now
)
579 /* Save the expire time */
580 if (expiretime
!= -1) {
581 /* If this key is already expired skip it */
582 if (expiretime
< now
) return 0;
583 if (rdbSaveType(rdb
,REDIS_RDB_OPCODE_EXPIRETIME_MS
) == -1) return -1;
584 if (rdbSaveMillisecondTime(rdb
,expiretime
) == -1) return -1;
587 /* Save type, key, value */
588 if (rdbSaveObjectType(rdb
,val
) == -1) return -1;
589 if (rdbSaveStringObject(rdb
,key
) == -1) return -1;
590 if (rdbSaveObject(rdb
,val
) == -1) return -1;
594 /* Save the DB on disk. Return REDIS_ERR on error, REDIS_OK on success */
595 int rdbSave(char *filename
) {
596 dictIterator
*di
= NULL
;
600 time_t now
= mstime();
604 snprintf(tmpfile
,256,"temp-%d.rdb", (int) getpid());
605 fp
= fopen(tmpfile
,"w");
607 redisLog(REDIS_WARNING
, "Failed opening .rdb for saving: %s",
612 rioInitWithFile(&rdb
,fp
);
613 if (rdbWriteRaw(&rdb
,"REDIS0003",9) == -1) goto werr
;
615 for (j
= 0; j
< server
.dbnum
; j
++) {
616 redisDb
*db
= server
.db
+j
;
618 if (dictSize(d
) == 0) continue;
619 di
= dictGetSafeIterator(d
);
625 /* Write the SELECT DB opcode */
626 if (rdbSaveType(&rdb
,REDIS_RDB_OPCODE_SELECTDB
) == -1) goto werr
;
627 if (rdbSaveLen(&rdb
,j
) == -1) goto werr
;
629 /* Iterate this DB writing every entry */
630 while((de
= dictNext(di
)) != NULL
) {
631 sds keystr
= dictGetKey(de
);
632 robj key
, *o
= dictGetVal(de
);
635 initStaticStringObject(key
,keystr
);
636 expire
= getExpire(db
,&key
);
637 if (rdbSaveKeyValuePair(&rdb
,&key
,o
,expire
,now
) == -1) goto werr
;
639 dictReleaseIterator(di
);
642 if (rdbSaveType(&rdb
,REDIS_RDB_OPCODE_EOF
) == -1) goto werr
;
644 /* Make sure data will not remain on the OS's output buffers */
649 /* Use RENAME to make sure the DB file is changed atomically only
650 * if the generate DB file is ok. */
651 if (rename(tmpfile
,filename
) == -1) {
652 redisLog(REDIS_WARNING
,"Error moving temp DB file on the final destination: %s", strerror(errno
));
656 redisLog(REDIS_NOTICE
,"DB saved on disk");
658 server
.lastsave
= time(NULL
);
664 redisLog(REDIS_WARNING
,"Write error saving DB on disk: %s", strerror(errno
));
665 if (di
) dictReleaseIterator(di
);
669 int rdbSaveBackground(char *filename
) {
673 if (server
.bgsavechildpid
!= -1) return REDIS_ERR
;
675 server
.dirty_before_bgsave
= server
.dirty
;
678 if ((childpid
= fork()) == 0) {
682 if (server
.ipfd
> 0) close(server
.ipfd
);
683 if (server
.sofd
> 0) close(server
.sofd
);
684 retval
= rdbSave(filename
);
685 _exit((retval
== REDIS_OK
) ? 0 : 1);
688 server
.stat_fork_time
= ustime()-start
;
689 if (childpid
== -1) {
690 redisLog(REDIS_WARNING
,"Can't save in background: fork: %s",
694 redisLog(REDIS_NOTICE
,"Background saving started by pid %d",childpid
);
695 server
.bgsavechildpid
= childpid
;
696 updateDictResizePolicy();
699 return REDIS_OK
; /* unreached */
702 void rdbRemoveTempFile(pid_t childpid
) {
705 snprintf(tmpfile
,256,"temp-%d.rdb", (int) childpid
);
709 /* Load a Redis object of the specified type from the specified file.
710 * On success a newly allocated object is returned, otherwise NULL. */
711 robj
*rdbLoadObject(int rdbtype
, rio
*rdb
) {
716 redisLog(REDIS_DEBUG
,"LOADING OBJECT %d (at %d)\n",rdbtype
,rdb
->tell(rdb
));
717 if (rdbtype
== REDIS_RDB_TYPE_STRING
) {
718 /* Read string value */
719 if ((o
= rdbLoadEncodedStringObject(rdb
)) == NULL
) return NULL
;
720 o
= tryObjectEncoding(o
);
721 } else if (rdbtype
== REDIS_RDB_TYPE_LIST
) {
722 /* Read list value */
723 if ((len
= rdbLoadLen(rdb
,NULL
)) == REDIS_RDB_LENERR
) return NULL
;
725 /* Use a real list when there are too many entries */
726 if (len
> server
.list_max_ziplist_entries
) {
727 o
= createListObject();
729 o
= createZiplistObject();
732 /* Load every single element of the list */
734 if ((ele
= rdbLoadEncodedStringObject(rdb
)) == NULL
) return NULL
;
736 /* If we are using a ziplist and the value is too big, convert
737 * the object to a real list. */
738 if (o
->encoding
== REDIS_ENCODING_ZIPLIST
&&
739 ele
->encoding
== REDIS_ENCODING_RAW
&&
740 sdslen(ele
->ptr
) > server
.list_max_ziplist_value
)
741 listTypeConvert(o
,REDIS_ENCODING_LINKEDLIST
);
743 if (o
->encoding
== REDIS_ENCODING_ZIPLIST
) {
744 dec
= getDecodedObject(ele
);
745 o
->ptr
= ziplistPush(o
->ptr
,dec
->ptr
,sdslen(dec
->ptr
),REDIS_TAIL
);
749 ele
= tryObjectEncoding(ele
);
750 listAddNodeTail(o
->ptr
,ele
);
753 } else if (rdbtype
== REDIS_RDB_TYPE_SET
) {
754 /* Read list/set value */
755 if ((len
= rdbLoadLen(rdb
,NULL
)) == REDIS_RDB_LENERR
) return NULL
;
757 /* Use a regular set when there are too many entries. */
758 if (len
> server
.set_max_intset_entries
) {
759 o
= createSetObject();
760 /* It's faster to expand the dict to the right size asap in order
761 * to avoid rehashing */
762 if (len
> DICT_HT_INITIAL_SIZE
)
763 dictExpand(o
->ptr
,len
);
765 o
= createIntsetObject();
768 /* Load every single element of the list/set */
769 for (i
= 0; i
< len
; i
++) {
771 if ((ele
= rdbLoadEncodedStringObject(rdb
)) == NULL
) return NULL
;
772 ele
= tryObjectEncoding(ele
);
774 if (o
->encoding
== REDIS_ENCODING_INTSET
) {
775 /* Fetch integer value from element */
776 if (isObjectRepresentableAsLongLong(ele
,&llval
) == REDIS_OK
) {
777 o
->ptr
= intsetAdd(o
->ptr
,llval
,NULL
);
779 setTypeConvert(o
,REDIS_ENCODING_HT
);
780 dictExpand(o
->ptr
,len
);
784 /* This will also be called when the set was just converted
785 * to regular hashtable encoded set */
786 if (o
->encoding
== REDIS_ENCODING_HT
) {
787 dictAdd((dict
*)o
->ptr
,ele
,NULL
);
792 } else if (rdbtype
== REDIS_RDB_TYPE_ZSET
) {
793 /* Read list/set value */
795 size_t maxelelen
= 0;
798 if ((zsetlen
= rdbLoadLen(rdb
,NULL
)) == REDIS_RDB_LENERR
) return NULL
;
799 o
= createZsetObject();
802 /* Load every single element of the list/set */
806 zskiplistNode
*znode
;
808 if ((ele
= rdbLoadEncodedStringObject(rdb
)) == NULL
) return NULL
;
809 ele
= tryObjectEncoding(ele
);
810 if (rdbLoadDoubleValue(rdb
,&score
) == -1) return NULL
;
812 /* Don't care about integer-encoded strings. */
813 if (ele
->encoding
== REDIS_ENCODING_RAW
&&
814 sdslen(ele
->ptr
) > maxelelen
)
815 maxelelen
= sdslen(ele
->ptr
);
817 znode
= zslInsert(zs
->zsl
,score
,ele
);
818 dictAdd(zs
->dict
,ele
,&znode
->score
);
819 incrRefCount(ele
); /* added to skiplist */
822 /* Convert *after* loading, since sorted sets are not stored ordered. */
823 if (zsetLength(o
) <= server
.zset_max_ziplist_entries
&&
824 maxelelen
<= server
.zset_max_ziplist_value
)
825 zsetConvert(o
,REDIS_ENCODING_ZIPLIST
);
826 } else if (rdbtype
== REDIS_RDB_TYPE_HASH
) {
829 if ((hashlen
= rdbLoadLen(rdb
,NULL
)) == REDIS_RDB_LENERR
) return NULL
;
830 o
= createHashObject();
831 /* Too many entries? Use an hash table. */
832 if (hashlen
> server
.hash_max_zipmap_entries
)
833 convertToRealHash(o
);
834 /* Load every key/value, then set it into the zipmap or hash
835 * table, as needed. */
839 if ((key
= rdbLoadEncodedStringObject(rdb
)) == NULL
) return NULL
;
840 if ((val
= rdbLoadEncodedStringObject(rdb
)) == NULL
) return NULL
;
841 /* If we are using a zipmap and there are too big values
842 * the object is converted to real hash table encoding. */
843 if (o
->encoding
!= REDIS_ENCODING_HT
&&
844 ((key
->encoding
== REDIS_ENCODING_RAW
&&
845 sdslen(key
->ptr
) > server
.hash_max_zipmap_value
) ||
846 (val
->encoding
== REDIS_ENCODING_RAW
&&
847 sdslen(val
->ptr
) > server
.hash_max_zipmap_value
)))
849 convertToRealHash(o
);
852 if (o
->encoding
== REDIS_ENCODING_ZIPMAP
) {
853 unsigned char *zm
= o
->ptr
;
854 robj
*deckey
, *decval
;
856 /* We need raw string objects to add them to the zipmap */
857 deckey
= getDecodedObject(key
);
858 decval
= getDecodedObject(val
);
859 zm
= zipmapSet(zm
,deckey
->ptr
,sdslen(deckey
->ptr
),
860 decval
->ptr
,sdslen(decval
->ptr
),NULL
);
862 decrRefCount(deckey
);
863 decrRefCount(decval
);
867 key
= tryObjectEncoding(key
);
868 val
= tryObjectEncoding(val
);
869 dictAdd((dict
*)o
->ptr
,key
,val
);
872 } else if (rdbtype
== REDIS_RDB_TYPE_HASH_ZIPMAP
||
873 rdbtype
== REDIS_RDB_TYPE_LIST_ZIPLIST
||
874 rdbtype
== REDIS_RDB_TYPE_SET_INTSET
||
875 rdbtype
== REDIS_RDB_TYPE_ZSET_ZIPLIST
)
877 robj
*aux
= rdbLoadStringObject(rdb
);
879 if (aux
== NULL
) return NULL
;
880 o
= createObject(REDIS_STRING
,NULL
); /* string is just placeholder */
881 o
->ptr
= zmalloc(sdslen(aux
->ptr
));
882 memcpy(o
->ptr
,aux
->ptr
,sdslen(aux
->ptr
));
885 /* Fix the object encoding, and make sure to convert the encoded
886 * data type into the base type if accordingly to the current
887 * configuration there are too many elements in the encoded data
888 * type. Note that we only check the length and not max element
889 * size as this is an O(N) scan. Eventually everything will get
892 case REDIS_RDB_TYPE_HASH_ZIPMAP
:
893 o
->type
= REDIS_HASH
;
894 o
->encoding
= REDIS_ENCODING_ZIPMAP
;
895 if (zipmapLen(o
->ptr
) > server
.hash_max_zipmap_entries
)
896 convertToRealHash(o
);
898 case REDIS_RDB_TYPE_LIST_ZIPLIST
:
899 o
->type
= REDIS_LIST
;
900 o
->encoding
= REDIS_ENCODING_ZIPLIST
;
901 if (ziplistLen(o
->ptr
) > server
.list_max_ziplist_entries
)
902 listTypeConvert(o
,REDIS_ENCODING_LINKEDLIST
);
904 case REDIS_RDB_TYPE_SET_INTSET
:
906 o
->encoding
= REDIS_ENCODING_INTSET
;
907 if (intsetLen(o
->ptr
) > server
.set_max_intset_entries
)
908 setTypeConvert(o
,REDIS_ENCODING_HT
);
910 case REDIS_RDB_TYPE_ZSET_ZIPLIST
:
911 o
->type
= REDIS_ZSET
;
912 o
->encoding
= REDIS_ENCODING_ZIPLIST
;
913 if (zsetLength(o
) > server
.zset_max_ziplist_entries
)
914 zsetConvert(o
,REDIS_ENCODING_SKIPLIST
);
917 redisPanic("Unknown encoding");
921 redisPanic("Unknown object type");
926 /* Mark that we are loading in the global state and setup the fields
927 * needed to provide loading stats. */
928 void startLoading(FILE *fp
) {
933 server
.loading_start_time
= time(NULL
);
934 if (fstat(fileno(fp
), &sb
) == -1) {
935 server
.loading_total_bytes
= 1; /* just to avoid division by zero */
937 server
.loading_total_bytes
= sb
.st_size
;
941 /* Refresh the loading progress info */
942 void loadingProgress(off_t pos
) {
943 server
.loading_loaded_bytes
= pos
;
946 /* Loading finished */
947 void stopLoading(void) {
951 int rdbLoad(char *filename
) {
954 redisDb
*db
= server
.db
+0;
956 long long expiretime
, now
= mstime();
961 fp
= fopen(filename
,"r");
966 rioInitWithFile(&rdb
,fp
);
967 if (rioRead(&rdb
,buf
,9) == 0) goto eoferr
;
969 if (memcmp(buf
,"REDIS",5) != 0) {
971 redisLog(REDIS_WARNING
,"Wrong signature trying to load DB from file");
975 rdbver
= atoi(buf
+5);
976 if (rdbver
< 1 || rdbver
> 3) {
978 redisLog(REDIS_WARNING
,"Can't handle RDB format version %d",rdbver
);
988 /* Serve the clients from time to time */
989 if (!(loops
++ % 1000)) {
990 loadingProgress(rdb
.tell(&rdb
));
991 aeProcessEvents(server
.el
, AE_FILE_EVENTS
|AE_DONT_WAIT
);
995 if ((type
= rdbLoadType(&rdb
)) == -1) goto eoferr
;
996 if (type
== REDIS_RDB_OPCODE_EXPIRETIME
) {
997 if ((expiretime
= rdbLoadTime(&rdb
)) == -1) goto eoferr
;
998 /* We read the time so we need to read the object type again. */
999 if ((type
= rdbLoadType(&rdb
)) == -1) goto eoferr
;
1000 /* the EXPIRETIME opcode specifies time in seconds, so convert
1001 * into milliesconds. */
1003 } else if (type
== REDIS_RDB_OPCODE_EXPIRETIME_MS
) {
1004 /* Milliseconds precision expire times introduced with RDB
1006 if ((expiretime
= rdbLoadMillisecondTime(&rdb
)) == -1) goto eoferr
;
1007 /* We read the time so we need to read the object type again. */
1008 if ((type
= rdbLoadType(&rdb
)) == -1) goto eoferr
;
1011 if (type
== REDIS_RDB_OPCODE_EOF
)
1014 /* Handle SELECT DB opcode as a special case */
1015 if (type
== REDIS_RDB_OPCODE_SELECTDB
) {
1016 if ((dbid
= rdbLoadLen(&rdb
,NULL
)) == REDIS_RDB_LENERR
)
1018 if (dbid
>= (unsigned)server
.dbnum
) {
1019 redisLog(REDIS_WARNING
,"FATAL: Data file was created with a Redis server configured to handle more than %d databases. Exiting\n", server
.dbnum
);
1022 db
= server
.db
+dbid
;
1026 if ((key
= rdbLoadStringObject(&rdb
)) == NULL
) goto eoferr
;
1028 if ((val
= rdbLoadObject(type
,&rdb
)) == NULL
) goto eoferr
;
1029 /* Check if the key already expired */
1030 if (expiretime
!= -1 && expiretime
< now
) {
1035 /* Add the new object in the hash table */
1038 /* Set the expire time if needed */
1039 if (expiretime
!= -1) setExpire(db
,key
,expiretime
);
1047 eoferr
: /* unexpected end of file is handled here with a fatal exit */
1048 redisLog(REDIS_WARNING
,"Short read or OOM loading DB. Unrecoverable error, aborting now.");
1050 return REDIS_ERR
; /* Just to avoid warning */
1053 /* A background saving child (BGSAVE) terminated its work. Handle this. */
1054 void backgroundSaveDoneHandler(int exitcode
, int bysignal
) {
1055 if (!bysignal
&& exitcode
== 0) {
1056 redisLog(REDIS_NOTICE
,
1057 "Background saving terminated with success");
1058 server
.dirty
= server
.dirty
- server
.dirty_before_bgsave
;
1059 server
.lastsave
= time(NULL
);
1060 } else if (!bysignal
&& exitcode
!= 0) {
1061 redisLog(REDIS_WARNING
, "Background saving error");
1063 redisLog(REDIS_WARNING
,
1064 "Background saving terminated by signal %d", bysignal
);
1065 rdbRemoveTempFile(server
.bgsavechildpid
);
1067 server
.bgsavechildpid
= -1;
1068 /* Possibly there are slaves waiting for a BGSAVE in order to be served
1069 * (the first stage of SYNC is a bulk transfer of dump.rdb) */
1070 updateSlavesWaitingBgsave(exitcode
== 0 ? REDIS_OK
: REDIS_ERR
);
1073 void saveCommand(redisClient
*c
) {
1074 if (server
.bgsavechildpid
!= -1) {
1075 addReplyError(c
,"Background save already in progress");
1078 if (rdbSave(server
.dbfilename
) == REDIS_OK
) {
1079 addReply(c
,shared
.ok
);
1081 addReply(c
,shared
.err
);
1085 void bgsaveCommand(redisClient
*c
) {
1086 if (server
.bgsavechildpid
!= -1) {
1087 addReplyError(c
,"Background save already in progress");
1088 } else if (server
.bgrewritechildpid
!= -1) {
1089 addReplyError(c
,"Can't BGSAVE while AOF log rewriting is in progress");
1090 } else if (rdbSaveBackground(server
.dbfilename
) == REDIS_OK
) {
1091 addReplyStatus(c
,"Background saving started");
1093 addReply(c
,shared
.err
);