lib/cdb++/cdb_get.cc
changeset 2 b3afb9f1e801
parent 0 6f7a81934006
--- a/lib/cdb++/cdb_get.cc	Sun Jan 20 00:12:17 2008 +0100
+++ b/lib/cdb++/cdb_get.cc	Sun Jan 20 00:22:09 2008 +0100
@@ -17,54 +17,32 @@
 #include "cdb++.h"
 #include "internal.h"
 
-static int match(fdibuf& in, const mystring& key)
-{
-  unsigned len = key.length();
-  const char* keyptr = key.c_str();
-  
-  while (len > 0) {
-    char buf[32];
-    unsigned n = sizeof buf;
-    if(n > len) n = len;
-    if(!in.read(buf, n)) return -1;
-    for(unsigned i = 0;i < n;++i)
-      if(buf[i] != keyptr[i]) return 0;
-    keyptr += n;
-    len -= n;
-  }
-  return 1;
-}
-
-int cdb_reader::seek(const mystring& key, uint32& dlen)
+char* cdb_reader::seek(const mystring& key, uint32& dlen)
 {
   if(failed)
-    return -1;
+    return 0;
   uint32 h = hash(key);
   uint32 pos = 8 * (h & 255);
 
-  uint32 poshash = unpack(header+pos); 
-  uint32 lenhash = unpack(header+pos+4);
+  uint32 poshash = unpack(map+pos); 
+  uint32 lenhash = unpack(map+pos+4);
   
-  if(!lenhash) return 0;
+  if(!lenhash)
+    return 0;
   uint32 h2 = (h >> 8) % lenhash;
 
   for (uint32 loop = 0; loop < lenhash; ++loop) {
-    if(!in.seek(poshash+8*h2)) return -1;
-    unsigned char packbuf[8];
-    if(!in.read(packbuf, 8)) return -1;
-    uint32 poskd = unpack(packbuf + 4);
-    if(!poskd) return 0;
-    if(unpack(packbuf) == h) {
-      if(!in.seek(poskd)) return -1;
-      if(!in.read(packbuf, 8)) return -1;
-      if(unpack(packbuf) == key.length())
-	switch(match(in, key)) {
-	case -1:
-	  return -1;
-	case 1:
-	  dlen = unpack(packbuf + 4);
-	  return 1;
-	}
+    unsigned char* ptrhash = map + poshash + 8*h2;
+    uint32 poskd = unpack(ptrhash + 4);
+    if(!poskd)
+      return 0;
+    if(unpack(ptrhash) == h) {
+      unsigned char* ptrmatch = map + poskd;
+      if(unpack(ptrmatch) == key.length() &&
+	 !memcmp(ptrmatch+8, key.c_str(), key.length())) {
+	dlen = unpack(ptrmatch + 4);
+	return (char*)ptrmatch + 8 + key.length();
+      }
     }
     if(++h2 == lenhash) h2 = 0;
   }
@@ -75,13 +53,9 @@
 {
   if(failed)
     return 0;
-  uint32 len;
-  switch(seek(key, len)) {
-  case -1: return 0;
-  case 0:  return 0;
-  }
-  // Big assume: data is relatively small (will fit on stack)
-  char buf[len];
-  datum* d = !in.read(buf, len) ? 0 : new datum(key, mystring(buf, len));
-  return d;
+  uint32 datalen;
+  char* dataptr = seek(key, datalen);
+  if(!dataptr)
+    return 0;
+  return new datum(key, mystring(dataptr, datalen));
 }