]> git.saurik.com Git - redis.git/commitdiff
add function to retrieve length of ziplist
authorPieter Noordhuis <pcnoordhuis@gmail.com>
Sat, 22 May 2010 17:35:40 +0000 (19:35 +0200)
committerPieter Noordhuis <pcnoordhuis@gmail.com>
Sat, 29 May 2010 19:10:16 +0000 (21:10 +0200)
ziplist.c
ziplist.h

index 4907d07b24382a5bbadcdd1f504f1ef611583655..3dd3c1e8fd798d5189705e9572bef48d68863d58 100644 (file)
--- a/ziplist.c
+++ b/ziplist.c
@@ -388,6 +388,24 @@ unsigned int ziplistCompare(unsigned char *p, unsigned char *entry, unsigned int
     }
 }
 
+/* Return length of ziplist. */
+unsigned int ziplistLen(unsigned char *zl) {
+    unsigned int len = 0;
+    if (ZIPLIST_LENGTH(zl) < ZIP_BIGLEN) {
+        len = ZIPLIST_LENGTH(zl);
+    } else {
+        unsigned char *p = zl+ZIPLIST_HEADER_SIZE;
+        while (*p != ZIP_END) {
+            p += zipRawEntryLength(p);
+            len++;
+        }
+
+        /* Re-store length if small enough */
+        if (len < ZIP_BIGLEN) ZIPLIST_LENGTH(zl) = len;
+    }
+    return len;
+}
+
 void ziplistRepr(unsigned char *zl) {
     unsigned char *p, encoding;
     unsigned int l, lsize;
index 4b2d1c937314b85fa1e4ca2352b771a719bb5782..cea7f5dee442f2f1529fd2d280c496b2f2600dba 100644 (file)
--- a/ziplist.h
+++ b/ziplist.h
@@ -9,4 +9,5 @@ unsigned char *ziplistNext(unsigned char *p);
 unsigned int ziplistGet(unsigned char *p, unsigned char **e, unsigned int *elen, long long *v);
 unsigned char *ziplistDelete(unsigned char *zl, unsigned char **p);
 unsigned char *ziplistDeleteRange(unsigned char *zl, unsigned int index, unsigned int num);
-unsigned int ziplistCompare(unsigned char *p, unsigned char *entry, unsigned int elen);
\ No newline at end of file
+unsigned int ziplistCompare(unsigned char *p, unsigned char *entry, unsigned int elen);
+unsigned int ziplistLen(unsigned char *zl);
\ No newline at end of file