]> git.saurik.com Git - redis.git/blobdiff - redis.c
Changes to TODO list, commented a function in redis.c
[redis.git] / redis.c
diff --git a/redis.c b/redis.c
index ca88add3d8bc34c6b1ea096009ff7b6271b43050..dff33754fb4109d4267a4a2412d736815550a62c 100644 (file)
--- a/redis.c
+++ b/redis.c
@@ -924,6 +924,7 @@ void backgroundRewriteDoneHandler(int statloc) {
             close(server.appendfd);
             server.appendfd = fd;
             fsync(fd);
+            server.appendseldb = -1; /* Make sure it will issue SELECT */
             redisLog(REDIS_NOTICE,"The new append only file was selected for future appends.");
         } else {
             /* If append only is disabled we just generate a dump in this
@@ -2308,7 +2309,11 @@ static robj *getDecodedObject(robj *o) {
 /* Compare two string objects via strcmp() or alike.
  * Note that the objects may be integer-encoded. In such a case we
  * use snprintf() to get a string representation of the numbers on the stack
- * and compare the strings, it's much faster than calling getDecodedObject(). */
+ * and compare the strings, it's much faster than calling getDecodedObject().
+ *
+ * Important note: if objects are not integer encoded, but binary-safe strings,
+ * sdscmp() from sds.c will apply memcmp() so this function ca be considered
+ * binary safe. */
 static int compareStringObjects(robj *a, robj *b) {
     assert(a->type == REDIS_STRING && b->type == REDIS_STRING);
     char bufa[128], bufb[128], *astr, *bstr;
@@ -5656,6 +5661,14 @@ static void feedAppendOnlyFile(struct redisCommand *cmd, int dictid, robj **argv
          }
          exit(1);
     }
+    /* If a background append only file rewriting is in progress we want to
+     * accumulate the differences between the child DB and the current one
+     * in a buffer, so that when the child process will do its work we
+     * can append the differences to the new append only file. */
+    if (server.bgrewritechildpid != -1)
+        server.bgrewritebuf = sdscatlen(server.bgrewritebuf,buf,sdslen(buf));
+
+    sdsfree(buf);
     now = time(NULL);
     if (server.appendfsync == APPENDFSYNC_ALWAYS ||
         (server.appendfsync == APPENDFSYNC_EVERYSEC &&
@@ -5731,7 +5744,7 @@ int loadAppendOnlyFile(char *filename) {
             if (buf[0] != '$') goto fmterr;
             len = strtol(buf+1,NULL,10);
             argsds = sdsnewlen(NULL,len);
-            if (fread(argsds,len,1,fp) == 0) goto fmterr;
+            if (len && fread(argsds,len,1,fp) == 0) goto fmterr;
             argv[j] = createObject(REDIS_STRING,argsds);
             if (fread(buf,2,1,fp) == 0) goto fmterr; /* discard CRLF */
         }
@@ -5845,7 +5858,7 @@ static int rewriteAppendOnlyFile(char *filename) {
 
         /* SELECT the new DB */
         if (fwrite(selectcmd,sizeof(selectcmd)-1,1,fp) == 0) goto werr;
-        if (fwriteBulkLong(fp,j+1) == 0) goto werr;
+        if (fwriteBulkLong(fp,j) == 0) goto werr;
 
         /* Iterate this DB writing every entry */
         while((de = dictNext(di)) != NULL) {
@@ -5854,7 +5867,6 @@ static int rewriteAppendOnlyFile(char *filename) {
             time_t expiretime = getExpire(db,key);
 
             /* Save the key and associated value */
-            if (rdbSaveStringObject(fp,key) == -1) goto werr;
             if (o->type == REDIS_STRING) {
                 /* Emit a SET command */
                 char cmd[]="*3\r\n$3\r\nSET\r\n";
@@ -5985,6 +5997,11 @@ static int rewriteAppendOnlyFileBackground(void) {
         redisLog(REDIS_NOTICE,
             "Background append only file rewriting started by pid %d",childpid);
         server.bgrewritechildpid = childpid;
+        /* We set appendseldb to -1 in order to force the next call to the
+         * feedAppendOnlyFile() to issue a SELECT command, so the differences
+         * accumulated by the parent into server.bgrewritebuf will start
+         * with a SELECT statement and it will be safe to merge. */
+        server.appendseldb = -1;
         return REDIS_OK;
     }
     return REDIS_OK; /* unreached */