You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@subversion.apache.org by st...@apache.org on 2011/04/13 01:42:14 UTC

svn commit: r1091603 - /subversion/trunk/subversion/libsvn_subr/cache-memcache.c

Author: stefan2
Date: Tue Apr 12 23:42:13 2011
New Revision: 1091603

URL: http://svn.apache.org/viewvc?rev=1091603&view=rev
Log:
Refactor cache-memcache internal functions and extract common
code sections into separate functions:

* subversion/libsvn_subr/cache-memcache.c:
  (memcache_internal_get): new function - the core part of the getters
  (memcache_get, memcache_get_partial): reduce & use new function
  (memcache_internal_set): new function - the core part of the setters
  (memcache_set): reduce & use new function

Modified:
    subversion/trunk/subversion/libsvn_subr/cache-memcache.c

Modified: subversion/trunk/subversion/libsvn_subr/cache-memcache.c
URL: http://svn.apache.org/viewvc/subversion/trunk/subversion/libsvn_subr/cache-memcache.c?rev=1091603&r1=1091602&r2=1091603&view=diff
==============================================================================
--- subversion/trunk/subversion/libsvn_subr/cache-memcache.c (original)
+++ subversion/trunk/subversion/libsvn_subr/cache-memcache.c Tue Apr 12 23:42:13 2011
@@ -126,19 +126,21 @@ build_key(const char **mc_key,
   return SVN_NO_ERROR;
 }
 
-
+/* Core functionality of our getter functions: fetch DATA from the memcached
+ * given by CACHE_VOID and identified by KEY. Indicate success in FOUND and
+ * use a tempoary sub-pool of POOL for allocations.
+ */
 static svn_error_t *
-memcache_get(void **value_p,
-             svn_boolean_t *found,
-             void *cache_void,
-             const void *key,
-             apr_pool_t *pool)
+memcache_internal_get(char **data,
+                      apr_size_t *size
+                      svn_boolean_t *found,
+                      void *cache_void,
+                      const void *key,
+                      apr_pool_t *pool)
 {
   memcache_t *cache = cache_void;
   apr_status_t apr_err;
-  char *data;
   const char *mc_key;
-  apr_size_t data_len;
   apr_pool_t *subpool = svn_pool_create(pool);
 
   SVN_ERR(build_key(&mc_key, cache, key, subpool));
@@ -146,8 +148,8 @@ memcache_get(void **value_p,
   apr_err = apr_memcache_getp(cache->memcache,
                               pool,
                               mc_key,
-                              &data,
-                              &data_len,
+                              data,
+                              data_len,
                               NULL /* ignore flags */);
   if (apr_err == APR_NOTFOUND)
     {
@@ -155,22 +157,10 @@ memcache_get(void **value_p,
       svn_pool_destroy(subpool);
       return SVN_NO_ERROR;
     }
-  else if (apr_err != APR_SUCCESS || !data)
+  else if (apr_err != APR_SUCCESS || !*data)
     return svn_error_wrap_apr(apr_err,
                               _("Unknown memcached error while reading"));
 
-  /* We found it! */
-  if (cache->deserialize_func)
-    {
-      SVN_ERR((cache->deserialize_func)(value_p, data, data_len, pool));
-    }
-  else
-    {
-      svn_string_t *value = apr_pcalloc(pool, sizeof(*value));
-      value->data = data;
-      value->len = data_len;
-      *value_p = value;
-    }
   *found = TRUE;
 
   svn_pool_destroy(subpool);
@@ -179,19 +169,75 @@ memcache_get(void **value_p,
 
 
 static svn_error_t *
+memcache_get(void **value_p,
+             svn_boolean_t *found,
+             void *cache_void,
+             const void *key,
+             apr_pool_t *pool)
+{
+  char *data;
+  apr_size_t data_len;
+  SVN_ERR(memcache_internal_get(&data,
+                                &size,
+                                found,
+                                cache_void,
+                                key,
+                                pool));
+
+  /* If we found it, de-serialize it. */
+  if (*found)
+    if (cache->deserialize_func)
+      {
+        SVN_ERR((cache->deserialize_func)(value_p, data, data_len, pool));
+      }
+    else
+      {
+        svn_string_t *value = apr_pcalloc(pool, sizeof(*value));
+        value->data = data;
+        value->len = data_len;
+        *value_p = value;
+      }
+
+  return SVN_NO_ERROR;
+}
+
+/* Core functionality of our setter functions: store LENGH bytes of DATA 
+ * to be identified by KEY in the memcached given by CACHE_VOID. Use POOL
+ * for temporary allocations.
+ */
+static svn_error_t *
+memcache_internal_set(void *cache_void,
+                      const void *key,
+                      const char *data,
+                      apr_size_t len,
+                      apr_pool_t *pool)
+{
+  memcache_t *cache = cache_void;
+  const char *mc_key;
+  apr_status_t apr_err;
+
+  SVN_ERR(build_key(&mc_key, cache, key, pool));
+  apr_err = apr_memcache_set(cache->memcache, mc_key, data, data_len, 0, 0);
+
+  /* ### Maybe write failures should be ignored (but logged)? */
+  if (apr_err != APR_SUCCESS)
+    return svn_error_wrap_apr(apr_err,
+                              _("Unknown memcached error while writing"));
+
+  return SVN_NO_ERROR;
+}
+
+
+static svn_error_t *
 memcache_set(void *cache_void,
              const void *key,
              void *value,
              apr_pool_t *pool)
 {
-  memcache_t *cache = cache_void;
   apr_pool_t *subpool = svn_pool_create(pool);
   char *data;
-  const char *mc_key;
   apr_size_t data_len;
-  apr_status_t apr_err;
-
-  SVN_ERR(build_key(&mc_key, cache, key, subpool));
+  svn_error_t *err;
 
   if (cache->serialize_func)
     {
@@ -204,15 +250,10 @@ memcache_set(void *cache_void,
       data_len = value_str->len;
     }
 
-  apr_err = apr_memcache_set(cache->memcache, mc_key, data, data_len, 0, 0);
-
-  /* ### Maybe write failures should be ignored (but logged)? */
-  if (apr_err != APR_SUCCESS)
-    return svn_error_wrap_apr(apr_err,
-                              _("Unknown memcached error while writing"));
+  err = memcache_internal_set(cache_void, key, data, data_len, subpool);
 
   svn_pool_destroy(subpool);
-  return SVN_NO_ERROR;
+  return err;
 }
 
 static svn_error_t *
@@ -224,38 +265,21 @@ memcache_get_partial(void **value_p,
                      void *baton,
                      apr_pool_t *pool)
 {
-  memcache_t *cache = cache_void;
-  svn_error_t *err;
-  apr_status_t apr_err;
-  char *data;
-  const char *mc_key;
-  apr_size_t data_len;
-  apr_pool_t *subpool = svn_pool_create(pool);
-
-  SVN_ERR(build_key(&mc_key, cache, key, subpool));
-
-  apr_err = apr_memcache_getp(cache->memcache,
-                              subpool,
-                              mc_key,
-                              &data,
-                              &data_len,
-                              NULL /* ignore flags */);
-  if (apr_err == APR_NOTFOUND)
-    {
-      *found = FALSE;
-      svn_pool_destroy(subpool);
-      return SVN_NO_ERROR;
-    }
-  else if (apr_err != APR_SUCCESS || !data)
-    return svn_error_wrap_apr(apr_err,
-                              _("Unknown memcached error while reading"));
+  svn_error_t *err = SVN_NO_ERROR;
 
-  /* We found it! */
-  *found = TRUE;
-  err = func(value_p, data, data_len, baton, pool);
-
-  svn_pool_destroy(subpool);
-  return err;
+  char *data;
+  apr_size_t size;
+  SVN_ERR(memcache_internal_get(&data,
+                                &size,
+                                found,
+                                cache_void,
+                                key,
+                                pool));
+
+  /* If we found it, de-serialize it. */
+  return *found
+    ? func(value_p, data, data_len, baton, pool)
+    : err;
 }