X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/16d778780eb865deefb2bfa024aef50926917eac..d934e1e85b1ec1c6d2cd07292cced53770f4a626:/src/rdb.c diff --git a/src/rdb.c b/src/rdb.c index e9ca111e..60d0a6ce 100644 --- a/src/rdb.c +++ b/src/rdb.c @@ -395,6 +395,31 @@ off_t rdbSavedObjectLen(robj *o) { return len; } +/* Save a key-value pair, with expire time, type, key, value. + * On error -1 is returned. + * On success if the key was actaully saved 1 is returned, otherwise 0 + * is returned (the key was already expired). */ +int rdbSaveKeyValuePair(FILE *fp, redisDb *db, robj *key, robj *val, + time_t now) +{ + time_t expiretime; + + expiretime = getExpire(db,key); + + /* Save the expire time */ + if (expiretime != -1) { + /* If this key is already expired skip it */ + if (expiretime < now) return 0; + if (rdbSaveType(fp,REDIS_EXPIRETIME) == -1) return -1; + if (rdbSaveTime(fp,expiretime) == -1) return -1; + } + /* Save type, key, value */ + if (rdbSaveType(fp,val->type) == -1) return -1; + if (rdbSaveStringObject(fp,key) == -1) return -1; + if (rdbSaveObject(fp,val) == -1) return -1; + return 1; +} + /* Save the DB on disk. Return REDIS_ERR on error, REDIS_OK on success */ int rdbSave(char *filename) { dictIterator *di = NULL; @@ -432,22 +457,9 @@ int rdbSave(char *filename) { while((de = dictNext(di)) != NULL) { sds keystr = dictGetEntryKey(de); robj key, *o = dictGetEntryVal(de); - time_t expiretime; initStaticStringObject(key,keystr); - expiretime = getExpire(db,&key); - - /* Save the expire time */ - if (expiretime != -1) { - /* If this key is already expired skip it */ - if (expiretime < now) continue; - if (rdbSaveType(fp,REDIS_EXPIRETIME) == -1) goto werr; - if (rdbSaveTime(fp,expiretime) == -1) goto werr; - } - /* Save type, key, value */ - if (rdbSaveType(fp,o->type) == -1) goto werr; - if (rdbSaveStringObject(fp,&key) == -1) goto werr; - if (rdbSaveObject(fp,o) == -1) goto werr; + if (rdbSaveKeyValuePair(fp,db,&key,o,now) == -1) goto werr; } dictReleaseIterator(di); } @@ -848,7 +860,6 @@ int rdbLoad(char *filename) { FILE *fp; uint32_t dbid; int type, retval, rdbver; - int swap_all_values = 0; redisDb *db = server.db+0; char buf[1024]; time_t expiretime, now = time(NULL); @@ -919,28 +930,6 @@ int rdbLoad(char *filename) { /* Set the expire time if needed */ if (expiretime != -1) setExpire(db,key,expiretime); - /* Handle swapping while loading big datasets when VM is on */ - - /* If we detecter we are hopeless about fitting something in memory - * we just swap every new key on disk. Directly... - * Note that's important to check for this condition before resorting - * to random sampling, otherwise we may try to swap already - * swapped keys. */ - if (swap_all_values) { - dictEntry *de = dictFind(db->dict,key->ptr); - - /* de may be NULL since the key already expired */ - if (de) { - vmpointer *vp; - val = dictGetEntryVal(de); - - if (val->refcount == 1 && - (vp = vmSwapObjectBlocking(val)) != NULL) - dictGetEntryVal(de) = vp; - } - decrRefCount(key); - continue; - } decrRefCount(key); } fclose(fp);