]> git.saurik.com Git - redis.git/commitdiff
code to compare strings with entries in ziplist, regardless of their encoding
authorPieter Noordhuis <pcnoordhuis@gmail.com>
Sat, 22 May 2010 14:25:06 +0000 (16:25 +0200)
committerPieter Noordhuis <pcnoordhuis@gmail.com>
Sat, 29 May 2010 19:10:16 +0000 (21:10 +0200)
ziplist.c

index fe2bfaf6ccebafa8f0d218dc9e107562a5fc2701..55fcc80cdce415ee9e71846100266244e955e093 100644 (file)
--- a/ziplist.c
+++ b/ziplist.c
@@ -365,6 +365,28 @@ unsigned char *ziplistDelete(unsigned char *zl, unsigned char **p) {
     return zl;
 }
 
+/* Compare entry pointer to by 'p' with 'entry'. Return 1 if equal. */
+unsigned int ziplistCompare(unsigned char *p, unsigned char *entry, unsigned int elen) {
+    unsigned int zlen, lensize;
+    char encoding;
+    long long zval, eval;
+    if (*p == ZIP_END) return 0;
+
+    zlen = zipDecodeLength(p,&lensize);
+    if (zipTryEncoding(entry,&eval,&encoding)) {
+        /* Do integer compare */
+        zval = zipLoadInteger(p+lensize,ZIP_ENCODING(p));
+        return zval == eval;
+    } else {
+        /* Raw compare */
+        if (zlen == elen) {
+            return memcmp(p+lensize,entry,elen) == 0;
+        } else {
+            return 0;
+        }
+    }
+}
+
 void ziplistRepr(unsigned char *zl) {
     unsigned char *p, encoding;
     unsigned int l, lsize;
@@ -557,7 +579,31 @@ int main(int argc, char **argv) {
         }
         printf("\n");
         ziplistRepr(zl);
-        printf("\n");
+    }
+
+    printf("Compare strings with ziplist entries:\n");
+    {
+        zl = createList();
+        p = ziplistIndex(zl, 0);
+        if (!ziplistCompare(p,"hello",5)) {
+            printf("ERROR\n");
+            return;
+        }
+        if (ziplistCompare(p,"hella",5)) {
+            printf("ERROR\n");
+            return;
+        }
+
+        p = ziplistIndex(zl, 3);
+        if (!ziplistCompare(p,"1024",4)) {
+            printf("ERROR\n");
+            return;
+        }
+        if (ziplistCompare(p,"1025",4)) {
+            printf("ERROR\n");
+            return;
+        }
+        printf("SUCCESS\n");
     }
 
     return 0;