Return number of bytes written from rdbSave* functions

This commit is contained in:
Pieter Noordhuis 2010-11-21 15:39:34 +01:00
parent c086b85afb
commit 8a623a98c3

133
src/rdb.c
View File

@ -11,36 +11,40 @@
int rdbSaveType(FILE *fp, unsigned char type) { int rdbSaveType(FILE *fp, unsigned char type) {
if (fwrite(&type,1,1,fp) == 0) return -1; if (fwrite(&type,1,1,fp) == 0) return -1;
return 0; return 1; /* bytes written */
} }
int rdbSaveTime(FILE *fp, time_t t) { int rdbSaveTime(FILE *fp, time_t t) {
int32_t t32 = (int32_t) t; int32_t t32 = (int32_t) t;
if (fwrite(&t32,4,1,fp) == 0) return -1; if (fwrite(&t32,4,1,fp) == 0) return -1;
return 0; return 4; /* bytes written */
} }
/* check rdbLoadLen() comments for more info */ /* check rdbLoadLen() comments for more info */
int rdbSaveLen(FILE *fp, uint32_t len) { int rdbSaveLen(FILE *fp, uint32_t len) {
unsigned char buf[2]; unsigned char buf[2];
int nwritten;
if (len < (1<<6)) { if (len < (1<<6)) {
/* Save a 6 bit len */ /* Save a 6 bit len */
buf[0] = (len&0xFF)|(REDIS_RDB_6BITLEN<<6); buf[0] = (len&0xFF)|(REDIS_RDB_6BITLEN<<6);
if (fwrite(buf,1,1,fp) == 0) return -1; if (fwrite(buf,1,1,fp) == 0) return -1;
nwritten = 1;
} else if (len < (1<<14)) { } else if (len < (1<<14)) {
/* Save a 14 bit len */ /* Save a 14 bit len */
buf[0] = ((len>>8)&0xFF)|(REDIS_RDB_14BITLEN<<6); buf[0] = ((len>>8)&0xFF)|(REDIS_RDB_14BITLEN<<6);
buf[1] = len&0xFF; buf[1] = len&0xFF;
if (fwrite(buf,2,1,fp) == 0) return -1; if (fwrite(buf,2,1,fp) == 0) return -1;
nwritten = 2;
} else { } else {
/* Save a 32 bit len */ /* Save a 32 bit len */
buf[0] = (REDIS_RDB_32BITLEN<<6); buf[0] = (REDIS_RDB_32BITLEN<<6);
if (fwrite(buf,1,1,fp) == 0) return -1; if (fwrite(buf,1,1,fp) == 0) return -1;
len = htonl(len); len = htonl(len);
if (fwrite(&len,4,1,fp) == 0) return -1; if (fwrite(&len,4,1,fp) == 0) return -1;
nwritten = 1+4;
} }
return 0; return nwritten;
} }
/* Encode 'value' as an integer if possible (if integer will fit the /* Encode 'value' as an integer if possible (if integer will fit the
@ -93,6 +97,7 @@ int rdbTryIntegerEncoding(char *s, size_t len, unsigned char *enc) {
int rdbSaveLzfStringObject(FILE *fp, unsigned char *s, size_t len) { int rdbSaveLzfStringObject(FILE *fp, unsigned char *s, size_t len) {
size_t comprlen, outlen; size_t comprlen, outlen;
unsigned char byte; unsigned char byte;
int n, nwritten = 0;
void *out; void *out;
/* We require at least four bytes compression for this to be worth it */ /* We require at least four bytes compression for this to be worth it */
@ -107,11 +112,19 @@ int rdbSaveLzfStringObject(FILE *fp, unsigned char *s, size_t len) {
/* Data compressed! Let's save it on disk */ /* Data compressed! Let's save it on disk */
byte = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_LZF; byte = (REDIS_RDB_ENCVAL<<6)|REDIS_RDB_ENC_LZF;
if (fwrite(&byte,1,1,fp) == 0) goto writeerr; if (fwrite(&byte,1,1,fp) == 0) goto writeerr;
if (rdbSaveLen(fp,comprlen) == -1) goto writeerr; nwritten += 1;
if (rdbSaveLen(fp,len) == -1) goto writeerr;
if ((n = rdbSaveLen(fp,comprlen)) == -1) goto writeerr;
nwritten += n;
if ((n = rdbSaveLen(fp,len)) == -1) goto writeerr;
nwritten += n;
if (fwrite(out,comprlen,1,fp) == 0) goto writeerr; if (fwrite(out,comprlen,1,fp) == 0) goto writeerr;
nwritten += comprlen;
zfree(out); zfree(out);
return comprlen; return nwritten;
writeerr: writeerr:
zfree(out); zfree(out);
@ -122,47 +135,54 @@ writeerr:
* representation of an integer value we try to safe it in a special form */ * representation of an integer value we try to safe it in a special form */
int rdbSaveRawString(FILE *fp, unsigned char *s, size_t len) { int rdbSaveRawString(FILE *fp, unsigned char *s, size_t len) {
int enclen; int enclen;
int n, nwritten = 0;
/* Try integer encoding */ /* Try integer encoding */
if (len <= 11) { if (len <= 11) {
unsigned char buf[5]; unsigned char buf[5];
if ((enclen = rdbTryIntegerEncoding((char*)s,len,buf)) > 0) { if ((enclen = rdbTryIntegerEncoding((char*)s,len,buf)) > 0) {
if (fwrite(buf,enclen,1,fp) == 0) return -1; if (fwrite(buf,enclen,1,fp) == 0) return -1;
return 0; return enclen;
} }
} }
/* Try LZF compression - under 20 bytes it's unable to compress even /* Try LZF compression - under 20 bytes it's unable to compress even
* aaaaaaaaaaaaaaaaaa so skip it */ * aaaaaaaaaaaaaaaaaa so skip it */
if (server.rdbcompression && len > 20) { if (server.rdbcompression && len > 20) {
int retval; n = rdbSaveLzfStringObject(fp,s,len);
if (n == -1) return -1;
retval = rdbSaveLzfStringObject(fp,s,len); if (n > 0) return n;
if (retval == -1) return -1; /* Return value of 0 means data can't be compressed, save the old way */
if (retval > 0) return 0;
/* retval == 0 means data can't be compressed, save the old way */
} }
/* Store verbatim */ /* Store verbatim */
if (rdbSaveLen(fp,len) == -1) return -1; if ((n = rdbSaveLen(fp,len)) == -1) return -1;
if (len && fwrite(s,len,1,fp) == 0) return -1; nwritten += n;
return 0; if (len > 0) {
if (fwrite(s,len,1,fp) == 0) return -1;
nwritten += len;
}
return nwritten;
} }
/* Save a long long value as either an encoded string or a string. */ /* Save a long long value as either an encoded string or a string. */
int rdbSaveLongLongAsStringObject(FILE *fp, long long value) { int rdbSaveLongLongAsStringObject(FILE *fp, long long value) {
unsigned char buf[32]; unsigned char buf[32];
int n, nwritten = 0;
int enclen = rdbEncodeInteger(value,buf); int enclen = rdbEncodeInteger(value,buf);
if (enclen > 0) { if (enclen > 0) {
if (fwrite(buf,enclen,1,fp) == 0) return -1; if (fwrite(buf,enclen,1,fp) == 0) return -1;
nwritten = enclen;
} else { } else {
/* Encode as string */ /* Encode as string */
enclen = ll2string((char*)buf,32,value); enclen = ll2string((char*)buf,32,value);
redisAssert(enclen < 32); redisAssert(enclen < 32);
if (rdbSaveLen(fp,enclen) == -1) return -1; if ((n = rdbSaveLen(fp,enclen)) == -1) return -1;
nwritten += n;
if (fwrite(buf,enclen,1,fp) == 0) return -1; if (fwrite(buf,enclen,1,fp) == 0) return -1;
nwritten += enclen;
} }
return 0; return nwritten;
} }
/* Like rdbSaveStringObjectRaw() but handle encoded objects */ /* Like rdbSaveStringObjectRaw() but handle encoded objects */
@ -217,14 +237,17 @@ int rdbSaveDoubleValue(FILE *fp, double val) {
len = buf[0]+1; len = buf[0]+1;
} }
if (fwrite(buf,len,1,fp) == 0) return -1; if (fwrite(buf,len,1,fp) == 0) return -1;
return 0; return len;
} }
/* Save a Redis object. */ /* Save a Redis object. */
int rdbSaveObject(FILE *fp, robj *o) { int rdbSaveObject(FILE *fp, robj *o) {
int n, nwritten = 0;
if (o->type == REDIS_STRING) { if (o->type == REDIS_STRING) {
/* Save a string value */ /* Save a string value */
if (rdbSaveStringObject(fp,o) == -1) return -1; if ((n = rdbSaveStringObject(fp,o)) == -1) return -1;
nwritten += n;
} else if (o->type == REDIS_LIST) { } else if (o->type == REDIS_LIST) {
/* Save a list value */ /* Save a list value */
if (o->encoding == REDIS_ENCODING_ZIPLIST) { if (o->encoding == REDIS_ENCODING_ZIPLIST) {
@ -233,15 +256,19 @@ int rdbSaveObject(FILE *fp, robj *o) {
unsigned int vlen; unsigned int vlen;
long long vlong; long long vlong;
if (rdbSaveLen(fp,ziplistLen(o->ptr)) == -1) return -1; if ((n = rdbSaveLen(fp,ziplistLen(o->ptr))) == -1) return -1;
nwritten += n;
p = ziplistIndex(o->ptr,0); p = ziplistIndex(o->ptr,0);
while(ziplistGet(p,&vstr,&vlen,&vlong)) { while(ziplistGet(p,&vstr,&vlen,&vlong)) {
if (vstr) { if (vstr) {
if (rdbSaveRawString(fp,vstr,vlen) == -1) if ((n = rdbSaveRawString(fp,vstr,vlen)) == -1)
return -1; return -1;
nwritten += n;
} else { } else {
if (rdbSaveLongLongAsStringObject(fp,vlong) == -1) if ((n = rdbSaveLongLongAsStringObject(fp,vlong)) == -1)
return -1; return -1;
nwritten += n;
} }
p = ziplistNext(o->ptr,p); p = ziplistNext(o->ptr,p);
} }
@ -250,11 +277,14 @@ int rdbSaveObject(FILE *fp, robj *o) {
listIter li; listIter li;
listNode *ln; listNode *ln;
if (rdbSaveLen(fp,listLength(list)) == -1) return -1; if ((n = rdbSaveLen(fp,listLength(list))) == -1) return -1;
nwritten += n;
listRewind(list,&li); listRewind(list,&li);
while((ln = listNext(&li))) { while((ln = listNext(&li))) {
robj *eleobj = listNodeValue(ln); robj *eleobj = listNodeValue(ln);
if (rdbSaveStringObject(fp,eleobj) == -1) return -1; if ((n = rdbSaveStringObject(fp,eleobj)) == -1) return -1;
nwritten += n;
} }
} else { } else {
redisPanic("Unknown list encoding"); redisPanic("Unknown list encoding");
@ -266,10 +296,13 @@ int rdbSaveObject(FILE *fp, robj *o) {
dictIterator *di = dictGetIterator(set); dictIterator *di = dictGetIterator(set);
dictEntry *de; dictEntry *de;
if (rdbSaveLen(fp,dictSize(set)) == -1) return -1; if ((n = rdbSaveLen(fp,dictSize(set))) == -1) return -1;
nwritten += n;
while((de = dictNext(di)) != NULL) { while((de = dictNext(di)) != NULL) {
robj *eleobj = dictGetEntryKey(de); robj *eleobj = dictGetEntryKey(de);
if (rdbSaveStringObject(fp,eleobj) == -1) return -1; if ((n = rdbSaveStringObject(fp,eleobj)) == -1) return -1;
nwritten += n;
} }
dictReleaseIterator(di); dictReleaseIterator(di);
} else if (o->encoding == REDIS_ENCODING_INTSET) { } else if (o->encoding == REDIS_ENCODING_INTSET) {
@ -277,9 +310,12 @@ int rdbSaveObject(FILE *fp, robj *o) {
int64_t llval; int64_t llval;
int i = 0; int i = 0;
if (rdbSaveLen(fp,intsetLen(is)) == -1) return -1; if ((n = rdbSaveLen(fp,intsetLen(is))) == -1) return -1;
nwritten += n;
while(intsetGet(is,i++,&llval)) { while(intsetGet(is,i++,&llval)) {
if (rdbSaveLongLongAsStringObject(fp,llval) == -1) return -1; if ((n = rdbSaveLongLongAsStringObject(fp,llval)) == -1) return -1;
nwritten += n;
} }
} else { } else {
redisPanic("Unknown set encoding"); redisPanic("Unknown set encoding");
@ -290,13 +326,17 @@ int rdbSaveObject(FILE *fp, robj *o) {
dictIterator *di = dictGetIterator(zs->dict); dictIterator *di = dictGetIterator(zs->dict);
dictEntry *de; dictEntry *de;
if (rdbSaveLen(fp,dictSize(zs->dict)) == -1) return -1; if ((n = rdbSaveLen(fp,dictSize(zs->dict))) == -1) return -1;
nwritten += n;
while((de = dictNext(di)) != NULL) { while((de = dictNext(di)) != NULL) {
robj *eleobj = dictGetEntryKey(de); robj *eleobj = dictGetEntryKey(de);
double *score = dictGetEntryVal(de); double *score = dictGetEntryVal(de);
if (rdbSaveStringObject(fp,eleobj) == -1) return -1; if ((n = rdbSaveStringObject(fp,eleobj)) == -1) return -1;
if (rdbSaveDoubleValue(fp,*score) == -1) return -1; nwritten += n;
if ((n = rdbSaveDoubleValue(fp,*score)) == -1) return -1;
nwritten += n;
} }
dictReleaseIterator(di); dictReleaseIterator(di);
} else if (o->type == REDIS_HASH) { } else if (o->type == REDIS_HASH) {
@ -307,29 +347,37 @@ int rdbSaveObject(FILE *fp, robj *o) {
unsigned char *key, *val; unsigned char *key, *val;
unsigned int klen, vlen; unsigned int klen, vlen;
if (rdbSaveLen(fp,count) == -1) return -1; if ((n = rdbSaveLen(fp,count)) == -1) return -1;
nwritten += n;
while((p = zipmapNext(p,&key,&klen,&val,&vlen)) != NULL) { while((p = zipmapNext(p,&key,&klen,&val,&vlen)) != NULL) {
if (rdbSaveRawString(fp,key,klen) == -1) return -1; if ((n = rdbSaveRawString(fp,key,klen)) == -1) return -1;
if (rdbSaveRawString(fp,val,vlen) == -1) return -1; nwritten += n;
if ((n = rdbSaveRawString(fp,val,vlen)) == -1) return -1;
nwritten += n;
} }
} else { } else {
dictIterator *di = dictGetIterator(o->ptr); dictIterator *di = dictGetIterator(o->ptr);
dictEntry *de; dictEntry *de;
if (rdbSaveLen(fp,dictSize((dict*)o->ptr)) == -1) return -1; if ((n = rdbSaveLen(fp,dictSize((dict*)o->ptr))) == -1) return -1;
nwritten += n;
while((de = dictNext(di)) != NULL) { while((de = dictNext(di)) != NULL) {
robj *key = dictGetEntryKey(de); robj *key = dictGetEntryKey(de);
robj *val = dictGetEntryVal(de); robj *val = dictGetEntryVal(de);
if (rdbSaveStringObject(fp,key) == -1) return -1; if ((n = rdbSaveStringObject(fp,key)) == -1) return -1;
if (rdbSaveStringObject(fp,val) == -1) return -1; nwritten += n;
if ((n = rdbSaveStringObject(fp,val)) == -1) return -1;
nwritten += n;
} }
dictReleaseIterator(di); dictReleaseIterator(di);
} }
} else { } else {
redisPanic("Unknown object type"); redisPanic("Unknown object type");
} }
return 0; return nwritten;
} }
/* Return the length the object will have on disk if saved with /* Return the length the object will have on disk if saved with
@ -337,10 +385,13 @@ int rdbSaveObject(FILE *fp, robj *o) {
* this length with very little changes to the code. In the future * this length with very little changes to the code. In the future
* we could switch to a faster solution. */ * we could switch to a faster solution. */
off_t rdbSavedObjectLen(robj *o, FILE *fp) { off_t rdbSavedObjectLen(robj *o, FILE *fp) {
int nwritten;
if (fp == NULL) fp = server.devnull; if (fp == NULL) fp = server.devnull;
rewind(fp); rewind(fp);
redisAssert(rdbSaveObject(fp,o) != 1);
return ftello(fp); /* Determining the saved length of an object should never return -1 */
redisAssert((nwritten = rdbSaveObject(fp,o)) != -1);
return nwritten;
} }
/* Return the number of pages required to save this object in the swap file */ /* Return the number of pages required to save this object in the swap file */