]> git.saurik.com Git - redis.git/blobdiff - zipmap.c
moved code to delete a single node from a zset to a separate function
[redis.git] / zipmap.c
index ba5ea0e3ea20337e52d57d7f5c1e86088141f20a..cfdfd56a62c6974e7e368e35826aa742ae710ecc 100644 (file)
--- a/zipmap.c
+++ b/zipmap.c
  *
  * The most compact representation of the above two elements hash is actually:
  *
- * "\x00\x03\x00foo\x03\x00bar\x05\x00hello\x05\x00world\xff"
+ * "\x00\x03foo\x03\x00bar\x05hello\x05\x00world\xff"
  *
  * Empty space is marked using a 254 bytes + a <len> (coded as already
  * specified). The length includes the 254 bytes in the count and the
  * space taken by the <len> field. So for instance removing the "foo" key
  * from the zipmap above will lead to the following representation:
  *
- * "\xfd\x10........\x05\x00hello\x05\x00world\xff"
+ * "\x00\xfd\x10........\x05hello\x05\x00world\xff"
  *
  * Note that because empty space, keys, values, are all prefixed length
  * "objects", the lookup will take O(N) where N is the numeber of elements
@@ -147,7 +147,7 @@ static unsigned int zipmapEncodeLength(unsigned char *p, unsigned int len) {
 static unsigned char *zipmapLookupRaw(unsigned char *zm, unsigned char *key, unsigned int klen, unsigned int *totlen, unsigned int *freeoff, unsigned int *freelen) {
     unsigned char *p = zm+1;
     unsigned int l;
-    unsigned int reqfreelen;
+    unsigned int reqfreelen = 0; /* initialized just to prevent warning */
 
     if (freelen) {
         reqfreelen = *freelen;
@@ -192,14 +192,14 @@ static unsigned long zipmapRequiredLength(unsigned int klen, unsigned int vlen)
     return l;
 }
 
-/* Return the total amonut used by a key (encoded length + payload) */
+/* Return the total amount used by a key (encoded length + payload) */
 static unsigned int zipmapRawKeyLength(unsigned char *p) {
     unsigned int l = zipmapDecodeLength(p);
     
     return zipmapEncodeLength(NULL,l) + l;
 }
 
-/* Return the total amonut used by a value
+/* Return the total amount used by a value
  * (encoded length + single byte free count + payload) */
 static unsigned int zipmapRawValueLength(unsigned char *p) {
     unsigned int l = zipmapDecodeLength(p);
@@ -210,6 +210,15 @@ static unsigned int zipmapRawValueLength(unsigned char *p) {
     return used;
 }
 
+/* If 'p' points to a key, this function returns the total amount of
+ * bytes used to store this entry (entry = key + associated value + trailing
+ * free space if any). */
+static unsigned int zipmapRawEntryLength(unsigned char *p) {
+    unsigned int l = zipmapRawKeyLength(p);
+
+    return l + zipmapRawValueLength(p+l);
+}
+
 /* Set key to value, creating the key if it does not already exist. */
 unsigned char *zipmapSet(unsigned char *zm, unsigned char *key, unsigned int klen, unsigned char *val, unsigned int vlen) {
     unsigned int oldlen = 0, freeoff = 0, freelen;
@@ -240,9 +249,10 @@ unsigned char *zipmapSet(unsigned char *zm, unsigned char *key, unsigned int kle
         b += freelen;
         freelen += zipmapRawValueLength(b);
         if (freelen < reqlen) {
-            /* Mark this blog as free and recurse */
+            /* Mark this entry as free and recurse */
             p[0] = ZIPMAP_EMPTY;
             zipmapEncodeLength(p+1,freelen);
+            zm[0] |= ZIPMAP_STATUS_FRAGMENTED;
             return zipmapSet(zm,key,klen,val,vlen);
         }
     }
@@ -277,6 +287,23 @@ unsigned char *zipmapSet(unsigned char *zm, unsigned char *key, unsigned int kle
     return zm;
 }
 
+/* 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 char *p = zipmapLookupRaw(zm,key,klen,NULL,NULL,NULL);
+    if (p) {
+        unsigned int freelen = zipmapRawEntryLength(p);
+
+        p[0] = ZIPMAP_EMPTY;
+        zipmapEncodeLength(p+1,freelen);
+        zm[0] |= ZIPMAP_STATUS_FRAGMENTED;
+        if (deleted) *deleted = 1;
+    } else {
+        if (deleted) *deleted = 0;
+    }
+    return zm;
+}
+
 void zipmapRepr(unsigned char *p) {
     unsigned int l;
 
@@ -303,7 +330,7 @@ void zipmapRepr(unsigned char *p) {
             p += zipmapEncodeLength(NULL,l);
             e = *p++;
             fwrite(p,l,1,stdout);
-            p += l;
+            p += l+e;
             if (e) {
                 printf("[");
                 while(e--) printf(".");
@@ -320,6 +347,13 @@ int main(void) {
     zm = zipmapNew();
     zm = zipmapSet(zm,(unsigned char*) "hello",5, (unsigned char*) "world!",6);
     zm = zipmapSet(zm,(unsigned char*) "foo",3, (unsigned char*) "bar",3);
+    zm = zipmapSet(zm,(unsigned char*) "foo",3, (unsigned char*) "!",1);
+    zipmapRepr(zm);
+    zm = zipmapSet(zm,(unsigned char*) "foo",3, (unsigned char*) "12345",5);
+    zipmapRepr(zm);
+    zm = zipmapSet(zm,(unsigned char*) "new",3, (unsigned char*) "xx",2);
+    zipmapRepr(zm);
+    zm = zipmapDel(zm,(unsigned char*) "new",3,NULL);
     zipmapRepr(zm);
     return 0;
 }