Patch for "murmur" hash algorithm.
[awesomized/libmemcached] / lib / memcached_hash.c
index 0d83cb75a7d0b0591b9ae4f2cbb8c0ccbd960862..f365cc0ee6815b817e3304573b316a0d3f098144 100644 (file)
@@ -1,23 +1,27 @@
 #include "common.h"
 
 /* Defines */
-static uint64_t FNV_64_INIT= 0xcbf29ce484222325L;
-static uint64_t FNV_64_PRIME= 0x100000001b3L;
+static uint64_t FNV_64_INIT= 0xcbf29ce484222325LL;
+static uint64_t FNV_64_PRIME= 0x100000001b3LL;
 
-static uint32_t FNV_32_INIT= 2166136261L;
+static uint32_t FNV_32_INIT= 2166136261UL;
 static uint32_t FNV_32_PRIME= 16777619;
 
 /* Prototypes */
-static unsigned int internal_generate_hash(char *key, size_t key_length);
-static uint32_t internal_generate_md5(char *key, size_t key_length);
+static uint32_t internal_generate_hash(char *key, size_t key_length);
 static uint32_t internal_generate_md5(char *key, size_t key_length);
 static uint32_t internal_generate_ketama_md5(char *key, size_t key_length);
 
 unsigned int memcached_generate_hash(memcached_st *ptr, char *key, size_t key_length)
 {
-  uint32_t hash;
+  uint32_t hash= 1; /* Just here to remove compile warning */
   unsigned int x;
 
+  WATCHPOINT_ASSERT(ptr->number_of_hosts);
+
+  if (ptr->number_of_hosts == 1)
+    return 0;
+
   switch (ptr->hash)
   {
   case MEMCACHED_HASH_DEFAULT:
@@ -28,17 +32,22 @@ unsigned int memcached_generate_hash(memcached_st *ptr, char *key, size_t key_le
     break;
   case MEMCACHED_HASH_CRC:
     hash= ((hash_crc32(key, key_length) >> 16) & 0x7fff);
+    if (hash == 0)
+      hash= 1;
     break;
     /* FNV hash'es lifted from Dustin Sallings work */
   case MEMCACHED_HASH_FNV1_64: 
     {
       /* Thanks to pierre@demartines.com for the pointer */
-      hash = FNV_64_INIT;
+      uint64_t temp_hash;
+
+      temp_hash= FNV_64_INIT;
       for (x= 0; x < key_length; x++) 
       {
-        hash *= FNV_64_PRIME;
-        hash ^= key[x];
+        temp_hash *= FNV_64_PRIME;
+        temp_hash ^= key[x];
       }
+      hash= (uint32_t)temp_hash;
     }
     break;
   case MEMCACHED_HASH_FNV1A_64: 
@@ -76,22 +85,38 @@ unsigned int memcached_generate_hash(memcached_st *ptr, char *key, size_t key_le
       hash= internal_generate_ketama_md5(key, key_length);
       break;
     }
+    case MEMCACHED_HASH_HSIEH:
+    {
+      hash= hsieh_hash(key, key_length);
+      break;
+    }
+    case MEMCACHED_HASH_MURMUR:
+    {
+      hash= murmur_hash(key, key_length);
+      break;
+    }
   }
 
   WATCHPOINT_ASSERT(hash);
-  if (ptr->flags & MEM_USE_KETAMA)
+
+  if (ptr->distribution == MEMCACHED_DISTRIBUTION_MODULA)
   {
-    WATCHPOINT_ASSERT(0);
-    return 0;
+    return hash % ptr->number_of_hosts;
   }
   else
-    return hash % ptr->number_of_hosts;
+  {
+    unsigned int server_key;
+
+    server_key= hash % MEMCACHED_WHEEL_SIZE;
+
+    return ptr->wheel[server_key];
+  }
 }
 
 static uint32_t internal_generate_hash(char *key, size_t key_length)
 {
   char *ptr= key;
-  unsigned int value= 0;
+  uint32_t value= 0;
 
   while (--key_length) 
   {