+#include "redis.h"
+#include "lzf.h" /* LZF compression library */
+
#include <math.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <arpa/inet.h>
#include <sys/stat.h>
-#include "rdb.h"
-#include "lzf.h" /* LZF compression library */
static int rdbWriteRaw(rio *rdb, void *p, size_t len) {
- if (rioWrite(rdb,p,len) == 0)
+ if (rdb && rioWrite(rdb,p,len) == 0)
return -1;
- return 1;
+ return len;
}
int rdbSaveType(rio *rdb, unsigned char type) {
if (obj->encoding == REDIS_ENCODING_INT) {
return rdbSaveLongLongAsStringObject(rdb,(long)obj->ptr);
} else {
- redisAssert(obj->encoding == REDIS_ENCODING_RAW);
+ redisAssertWithInfo(NULL,obj,obj->encoding == REDIS_ENCODING_RAW);
return rdbSaveRawString(rdb,obj->ptr,sdslen(obj->ptr));
}
}
nwritten += n;
while((de = dictNext(di)) != NULL) {
- robj *eleobj = dictGetEntryKey(de);
+ robj *eleobj = dictGetKey(de);
if ((n = rdbSaveStringObject(rdb,eleobj)) == -1) return -1;
nwritten += n;
}
nwritten += n;
while((de = dictNext(di)) != NULL) {
- robj *eleobj = dictGetEntryKey(de);
- double *score = dictGetEntryVal(de);
+ robj *eleobj = dictGetKey(de);
+ double *score = dictGetVal(de);
if ((n = rdbSaveStringObject(rdb,eleobj)) == -1) return -1;
nwritten += n;
nwritten += n;
while((de = dictNext(di)) != NULL) {
- robj *key = dictGetEntryKey(de);
- robj *val = dictGetEntryVal(de);
+ robj *key = dictGetKey(de);
+ robj *val = dictGetVal(de);
if ((n = rdbSaveStringObject(rdb,key)) == -1) return -1;
nwritten += n;
* we could switch to a faster solution. */
off_t rdbSavedObjectLen(robj *o) {
int len = rdbSaveObject(NULL,o);
- redisAssert(len != -1);
+ redisAssertWithInfo(NULL,o,len != -1);
return len;
}
FILE *fp;
rio rdb;
- if (server.ds_enabled) {
- cacheForcePointInTime();
- return dsRdbSave(filename);
- }
-
snprintf(tmpfile,256,"temp-%d.rdb", (int) getpid());
fp = fopen(tmpfile,"w");
if (!fp) {
return REDIS_ERR;
}
- rdb = rioInitWithFile(fp);
+ rioInitWithFile(&rdb,fp);
if (rdbWriteRaw(&rdb,"REDIS0002",9) == -1) goto werr;
for (j = 0; j < server.dbnum; j++) {
redisDb *db = server.db+j;
dict *d = db->dict;
if (dictSize(d) == 0) continue;
- di = dictGetIterator(d);
+ di = dictGetSafeIterator(d);
if (!di) {
fclose(fp);
return REDIS_ERR;
/* Iterate this DB writing every entry */
while((de = dictNext(di)) != NULL) {
- sds keystr = dictGetEntryKey(de);
- robj key, *o = dictGetEntryVal(de);
+ sds keystr = dictGetKey(de);
+ robj key, *o = dictGetVal(de);
time_t expire;
initStaticStringObject(key,keystr);
int rdbSaveBackground(char *filename) {
pid_t childpid;
+ long long start;
- if (server.bgsavechildpid != -1 ||
- server.bgsavethread != (pthread_t) -1) return REDIS_ERR;
+ if (server.bgsavechildpid != -1) return REDIS_ERR;
server.dirty_before_bgsave = server.dirty;
- if (server.ds_enabled) {
- cacheForcePointInTime();
- return dsRdbSaveBackground(filename);
- }
-
+ start = ustime();
if ((childpid = fork()) == 0) {
int retval;
_exit((retval == REDIS_OK) ? 0 : 1);
} else {
/* Parent */
+ server.stat_fork_time = ustime()-start;
if (childpid == -1) {
redisLog(REDIS_WARNING,"Can't save in background: fork: %s",
strerror(errno));
int rdbLoad(char *filename) {
uint32_t dbid;
- int type, retval, rdbver;
+ int type, rdbver;
redisDb *db = server.db+0;
char buf[1024];
time_t expiretime, now = time(NULL);
rio rdb;
fp = fopen(filename,"r");
- if (!fp) return REDIS_ERR;
- if (fread(buf,9,1,fp) == 0) goto eoferr;
+ if (!fp) {
+ errno = ENOENT;
+ return REDIS_ERR;
+ }
+ rioInitWithFile(&rdb,fp);
+ if (rioRead(&rdb,buf,9) == 0) goto eoferr;
buf[9] = '\0';
if (memcmp(buf,"REDIS",5) != 0) {
fclose(fp);
redisLog(REDIS_WARNING,"Wrong signature trying to load DB from file");
+ errno = EINVAL;
return REDIS_ERR;
}
rdbver = atoi(buf+5);
if (rdbver < 1 || rdbver > 2) {
fclose(fp);
redisLog(REDIS_WARNING,"Can't handle RDB format version %d",rdbver);
+ errno = EINVAL;
return REDIS_ERR;
}
startLoading(fp);
- rdb = rioInitWithFile(fp);
while(1) {
robj *key, *val;
expiretime = -1;
continue;
}
/* Add the new object in the hash table */
- retval = dbAdd(db,key,val);
- if (retval == REDIS_ERR) {
- redisLog(REDIS_WARNING,"Loading DB, duplicated key (%s) found! Unrecoverable error, exiting now.", key->ptr);
- exit(1);
- }
+ dbAdd(db,key,val);
+
/* Set the expire time if needed */
if (expiretime != -1) setExpire(db,key,expiretime);
rdbRemoveTempFile(server.bgsavechildpid);
}
server.bgsavechildpid = -1;
- server.bgsavethread = (pthread_t) -1;
- server.bgsavethread_state = REDIS_BGSAVE_THREAD_UNACTIVE;
/* Possibly there are slaves waiting for a BGSAVE in order to be served
* (the first stage of SYNC is a bulk transfer of dump.rdb) */
updateSlavesWaitingBgsave(exitcode == 0 ? REDIS_OK : REDIS_ERR);
}
void saveCommand(redisClient *c) {
- if (server.bgsavechildpid != -1 || server.bgsavethread != (pthread_t)-1) {
+ if (server.bgsavechildpid != -1) {
addReplyError(c,"Background save already in progress");
return;
}
}
void bgsaveCommand(redisClient *c) {
- if (server.bgsavechildpid != -1 || server.bgsavethread != (pthread_t)-1) {
+ if (server.bgsavechildpid != -1) {
addReplyError(c,"Background save already in progress");
- return;
- }
- if (rdbSaveBackground(server.dbfilename) == REDIS_OK) {
+ } else if (server.bgrewritechildpid != -1) {
+ addReplyError(c,"Can't BGSAVE while AOF log rewriting is in progress");
+ } else if (rdbSaveBackground(server.dbfilename) == REDIS_OK) {
addReplyStatus(c,"Background saving started");
} else {
addReply(c,shared.err);