]> git.saurik.com Git - redis.git/blobdiff - zipmap.c
use function to determine length of a single entry
[redis.git] / zipmap.c
index f168d0a75fc98527999e1e8c4ac1be122afe5639..8a07b388ed5aabb6af3cd83a4bc175d1a6bf6f7b 100644 (file)
--- a/zipmap.c
+++ b/zipmap.c
 #include <assert.h>
 #include "zmalloc.h"
 
-#define ZIPMAP_BIGLEN 253
-#define ZIPMAP_EMPTY 254
+#define ZIPMAP_BIGLEN 254
 #define ZIPMAP_END 255
 
-#define ZIPMAP_STATUS_FRAGMENTED 1
-
 /* The following defines the max value for the <free> field described in the
  * comments above, that is, the max number of trailing bytes in a value. */
 #define ZIPMAP_VALUE_MAX_FREE 5
 unsigned char *zipmapNew(void) {
     unsigned char *zm = zmalloc(2);
 
-    zm[0] = 0; /* Status */
+    zm[0] = 0; /* Length */
     zm[1] = ZIPMAP_END;
     return zm;
 }
@@ -142,13 +139,7 @@ static unsigned int zipmapEncodeLength(unsigned char *p, unsigned int len) {
  *
  * If NULL is returned, and totlen is not NULL, it is set to the entire
  * size of the zimap, so that the calling function will be able to
- * reallocate the original zipmap to make room for more entries.
- *
- * If NULL is returned, and freeoff and freelen are not NULL, they are set
- * to the offset of the first empty space that can hold '*freelen' bytes
- * (freelen is an integer pointer used both to signal the required length
- * and to get the reply from the function). If there is not a suitable
- * free space block to hold the requested bytes, *freelen is set to 0. */
+ * reallocate the original zipmap to make room for more entries. */
 static unsigned char *zipmapLookupRaw(unsigned char *zm, unsigned char *key, unsigned int klen, unsigned int *totlen) {
     unsigned char *p = zm+1, *k = NULL;
     unsigned int l;
@@ -210,7 +201,6 @@ static unsigned int zipmapRawValueLength(unsigned char *p) {
  * free space if any). */
 static unsigned int zipmapRawEntryLength(unsigned char *p) {
     unsigned int l = zipmapRawKeyLength(p);
-
     return l + zipmapRawValueLength(p+l);
 }
 
@@ -237,15 +227,14 @@ unsigned char *zipmapSet(unsigned char *zm, unsigned char *key, unsigned int kle
         zm = zipmapResize(zm, zmlen+reqlen);
         p = zm+zmlen-1;
         zmlen = zmlen+reqlen;
-    } else {
-        unsigned char *b = p;
 
+        /* Increase zipmap length (this is an insert) */
+        if (zm[0] < ZIPMAP_BIGLEN) zm[0]++;
+    } else {
         /* Key found. Is there enough space for the new value? */
         /* Compute the total length: */
         if (update) *update = 1;
-        freelen = zipmapRawKeyLength(b);
-        b += freelen;
-        freelen += zipmapRawValueLength(b);
+        freelen = zipmapRawEntryLength(p);
         if (freelen < reqlen) {
             /* Move remaining entries to the current position, so this
              * pair can be appended. Note: the +1 in memmove is caused
@@ -288,12 +277,16 @@ unsigned char *zipmapSet(unsigned char *zm, unsigned char *key, unsigned int kle
 /* Remove the specified key. If 'deleted' is not NULL the pointed integer is
  * set to 0 if the key was not found, to 1 if it was found and deleted. */
 unsigned char *zipmapDel(unsigned char *zm, unsigned char *key, unsigned int klen, int *deleted) {
-    unsigned int zmlen;
+    unsigned int zmlen, freelen;
     unsigned char *p = zipmapLookupRaw(zm,key,klen,&zmlen);
     if (p) {
-        unsigned int freelen = zipmapRawEntryLength(p);
+        freelen = zipmapRawEntryLength(p);
         memmove(p, p+freelen, zmlen-((p-zm)+freelen+1));
         zm = zipmapResize(zm, zmlen-freelen);
+
+        /* Decrease zipmap length */
+        if (zm[0] < ZIPMAP_BIGLEN) zm[0]--;
+
         if (deleted) *deleted = 1;
     } else {
         if (deleted) *deleted = 0;
@@ -318,8 +311,6 @@ unsigned char *zipmapRewind(unsigned char *zm) {
  * }
  */
 unsigned char *zipmapNext(unsigned char *zm, unsigned char **key, unsigned int *klen, unsigned char **value, unsigned int *vlen) {
-    while(zm[0] == ZIPMAP_EMPTY)
-        zm += zipmapDecodeLength(zm+1);
     if (zm[0] == ZIPMAP_END) return NULL;
     if (key) {
         *key = zm;
@@ -355,10 +346,16 @@ int zipmapExists(unsigned char *zm, unsigned char *key, unsigned int klen) {
 
 /* Return the number of entries inside a zipmap */
 unsigned int zipmapLen(unsigned char *zm) {
-    unsigned char *p = zipmapRewind(zm);
     unsigned int len = 0;
+    if (zm[0] < ZIPMAP_BIGLEN) {
+        len = zm[0];
+    } else {
+        unsigned char *p = zipmapRewind(zm);
+        while((p = zipmapNext(p,NULL,NULL,NULL,NULL)) != NULL) len++;
 
-    while((p = zipmapNext(p,NULL,NULL,NULL,NULL)) != NULL) len++;
+        /* Re-store length if small enough */
+        if (len < ZIPMAP_BIGLEN) zm[0] = len;
+    }
     return len;
 }
 
@@ -370,10 +367,6 @@ void zipmapRepr(unsigned char *p) {
         if (p[0] == ZIPMAP_END) {
             printf("{end}");
             break;
-        } else if (p[0] == ZIPMAP_EMPTY) {
-            l = zipmapDecodeLength(p+1);
-            printf("{%u empty block}", l);
-            p += l;
         } else {
             unsigned char e;