X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/2612e0521fde55db2c720092d4ad02a8f015f46e..65fd32ab0a7ff1d3d052e3b70dcceac6719de58c:/src/zipmap.c diff --git a/src/zipmap.c b/src/zipmap.c index be780a82..d9b7c8b3 100644 --- a/src/zipmap.c +++ b/src/zipmap.c @@ -52,15 +52,15 @@ * lengths are encoded in a single value or in a 5 bytes value. * If the first byte value (as an unsigned 8 bit value) is between 0 and * 252, it's a single-byte length. If it is 253 then a four bytes unsigned - * integer follows (in the host byte ordering). A value fo 255 is used to + * integer follows (in the host byte ordering). A value of 255 is used to * signal the end of the hash. The special value 254 is used to mark * empty space that can be used to add new key/value pairs. * - * is the number of free unused bytes - * after the string, resulting from modification of values associated to a - * key (for instance if "foo" is set to "bar', and later "foo" will be se to - * "hi", I'll have a free byte to use if the value will enlarge again later, - * or even in order to add a key/value pair if it fits. + * is the number of free unused bytes after the string, resulting + * from modification of values associated to a key. For instance if "foo" + * is set to "bar", and later "foo" will be set to "hi", it will have a + * free byte to use if the value will enlarge again later, or even in + * order to add a key/value pair if it fits. * * is always an unsigned 8 bit number, because if after an * update operation there are more than a few free bytes, the zipmap will be @@ -80,6 +80,7 @@ #include #include #include "zmalloc.h" +#include "endianconv.h" #define ZIPMAP_BIGLEN 254 #define ZIPMAP_END 255 @@ -108,6 +109,7 @@ static unsigned int zipmapDecodeLength(unsigned char *p) { if (len < ZIPMAP_BIGLEN) return len; memcpy(&len,p+1,sizeof(unsigned int)); + memrev32ifbe(&len); return len; } @@ -123,6 +125,7 @@ static unsigned int zipmapEncodeLength(unsigned char *p, unsigned int len) { } else { p[0] = ZIPMAP_BIGLEN; memcpy(p+1,&len,sizeof(len)); + memrev32ifbe(p+1); return 1+sizeof(len); } } @@ -144,7 +147,7 @@ static unsigned char *zipmapLookupRaw(unsigned char *zm, unsigned char *key, uns /* Match or skip the key */ l = zipmapDecodeLength(p); llen = zipmapEncodeLength(NULL,l); - if (k == NULL && l == klen && !memcmp(p+llen,key,l)) { + if (key != NULL && k == NULL && l == klen && !memcmp(p+llen,key,l)) { /* Only return when the user doesn't care * for the total length of the zipmap. */ if (totlen != NULL) { @@ -295,7 +298,7 @@ unsigned char *zipmapDel(unsigned char *zm, unsigned char *key, unsigned int kle return zm; } -/* Call it before to iterate trought elements via zipmapNext() */ +/* Call before iterating through elements via zipmapNext() */ unsigned char *zipmapRewind(unsigned char *zm) { return zm+1; } @@ -360,6 +363,16 @@ unsigned int zipmapLen(unsigned char *zm) { return len; } +/* Return the raw size in bytes of a zipmap, so that we can serialize + * the zipmap on disk (or everywhere is needed) just writing the returned + * amount of bytes of the C array starting at the zipmap pointer. */ +size_t zipmapBlobLen(unsigned char *zm) { + unsigned int totlen; + zipmapLookupRaw(zm,NULL,0,&totlen); + return totlen; +} + +#ifdef ZIPMAP_TEST_MAIN void zipmapRepr(unsigned char *p) { unsigned int l; @@ -393,7 +406,6 @@ void zipmapRepr(unsigned char *p) { printf("\n"); } -#ifdef ZIPMAP_TEST_MAIN int main(void) { unsigned char *zm; @@ -440,7 +452,7 @@ int main(void) { vlen, vlen, value); } } - printf("\nIterate trought elements:\n"); + printf("\nIterate through elements:\n"); { unsigned char *i = zipmapRewind(zm); unsigned char *key, *value;