You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@apr.apache.org by pq...@apache.org on 2007/06/08 07:37:05 UTC

svn commit: r545425 - in /apr/apr-util/trunk: include/apr_memcache.h memcache/apr_memcache.c

Author: pquerna
Date: Thu Jun  7 22:37:04 2007
New Revision: 545425

URL: http://svn.apache.org/viewvc?view=rev&rev=545425
Log:
Make the hash calculation function used for server selection a plugable part of the API, allowing end users to use something other than our non-standard CRC.

Modified:
    apr/apr-util/trunk/include/apr_memcache.h
    apr/apr-util/trunk/memcache/apr_memcache.c

Modified: apr/apr-util/trunk/include/apr_memcache.h
URL: http://svn.apache.org/viewvc/apr/apr-util/trunk/include/apr_memcache.h?view=diff&rev=545425&r1=545424&r2=545425
==============================================================================
--- apr/apr-util/trunk/include/apr_memcache.h (original)
+++ apr/apr-util/trunk/include/apr_memcache.h Thu Jun  7 22:37:04 2007
@@ -74,6 +74,15 @@
     apr_time_t btime;
 };
 
+/* Custom hash callback function prototype, user for server selection.
+* @param baton user selected baton
+* @param data data to hash
+* @param data_len length of data
+*/
+typedef apr_uint32_t (*apr_memcahce_hash_func)(void *baton,
+                                               const char *data,
+                                               apr_size_t data_len);
+
 /** Container for a set of memcached servers */
 typedef struct
 {
@@ -82,6 +91,8 @@
     apr_uint16_t ntotal; /**< Number of Servers Added */
     apr_memcache_server_t **live_servers; /**< Array of Servers */
     apr_pool_t *p; /** Pool to use for allocations */
+    void *hash_baton;
+    apr_memcahce_hash_func hash_func;
 } apr_memcache_t;
 
 /** Returned Data from a multiple get */
@@ -101,7 +112,13 @@
  * @return crc32 hash of data
  * @remark The crc32 hash is not compatible with old memcached clients.
  */
-APR_DECLARE(apr_uint32_t) apr_memcache_hash(const char *data, apr_size_t data_len);
+APR_DECLARE(apr_uint32_t) apr_memcache_hash(apr_memcache_t *mc,
+                                            const char *data,
+                                            apr_size_t data_len);
+
+APR_DECLARE(apr_uint32_t) apr_memcache_hash_default(void *baton,
+                                                    const char *data,
+                                                    apr_size_t data_len);
 
 /**
  * Picks a server based on a hash

Modified: apr/apr-util/trunk/memcache/apr_memcache.c
URL: http://svn.apache.org/viewvc/apr/apr-util/trunk/memcache/apr_memcache.c?view=diff&rev=545425&r1=545424&r2=545425
==============================================================================
--- apr/apr-util/trunk/memcache/apr_memcache.c (original)
+++ apr/apr-util/trunk/memcache/apr_memcache.c Thu Jun  7 22:37:04 2007
@@ -486,18 +486,32 @@
   0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d,
 };
 
-APR_DECLARE(apr_uint32_t) apr_memcache_hash(const char *data, const apr_size_t data_len)
+APR_DECLARE(apr_uint32_t) apr_memcache_hash_default(void *baton, 
+                                                    const char *data,
+                                                    const apr_size_t data_len)
 {
     apr_uint32_t i;
     apr_uint32_t crc;
     crc = ~0;
-
+    
     for (i = 0; i < data_len; i++)
         crc = (crc >> 8) ^ crc32tab[(crc ^ (data[i])) & 0xff];
-  
+    
     return ((~crc >> 16) & 0x7fff);
 }
 
+APR_DECLARE(apr_uint32_t) apr_memcache_hash(apr_memcache_t *mc,
+                                            const char *data,
+                                            const apr_size_t data_len)
+{
+    if (mc->hash_func) {
+        return mc->hash_func(mc->hash_baton, data, data_len);
+    }
+    else {
+        return apr_memcache_hash_default(NULL, data, data_len);
+    }
+}
+
 static apr_status_t get_server_line(apr_memcache_conn_t *conn)
 {
     apr_size_t bsize = BUFFER_SIZE;
@@ -540,7 +554,7 @@
 
     apr_size_t key_size = strlen(key);
 
-    hash = apr_memcache_hash(key, key_size);
+    hash = apr_memcache_hash(mc, key, key_size);
 
     ms = apr_memcache_find_server_hash(mc, hash);
 
@@ -667,7 +681,7 @@
     int klen = strlen(key);
     struct iovec vec[3];
 
-    hash = apr_memcache_hash(key, klen);
+    hash = apr_memcache_hash(mc, key, klen);
     ms = apr_memcache_find_server_hash(mc, hash);
     if (ms == NULL)
         return APR_NOTFOUND;
@@ -797,7 +811,7 @@
     struct iovec vec[3];
     int klen = strlen(key);
 
-    hash = apr_memcache_hash(key, klen);
+    hash = apr_memcache_hash(mc, key, klen);
     ms = apr_memcache_find_server_hash(mc, hash);
     if (ms == NULL)
         return APR_NOTFOUND;
@@ -866,7 +880,7 @@
     struct iovec vec[3];
     int klen = strlen(key);
 
-    hash = apr_memcache_hash(key, klen);
+    hash = apr_memcache_hash(mc, key, klen);
     ms = apr_memcache_find_server_hash(mc, hash);
     if (ms == NULL)
         return APR_NOTFOUND;
@@ -1134,7 +1148,7 @@
         value_hash_index = apr_hash_next(value_hash_index);
         klen = strlen(value->key);
 
-        hash = apr_memcache_hash(value->key, klen);
+        hash = apr_memcache_hash(mc, value->key, klen);
         ms = apr_memcache_find_server_hash(mc, hash);
         if (ms == NULL) {
             continue;