00001 /** 00002 * @copyright 00003 * ==================================================================== 00004 * Copyright (c) 2008 CollabNet. All rights reserved. 00005 * 00006 * This software is licensed as described in the file COPYING, which 00007 * you should have received as part of this distribution. The terms 00008 * are also available at http://subversion.tigris.org/license-1.html. 00009 * If newer versions of this license are posted there, you may use a 00010 * newer version instead, at your option. 00011 * 00012 * This software consists of voluntary contributions made by many 00013 * individuals. For exact contribution history, see the revision 00014 * history and logs, available at http://subversion.tigris.org/. 00015 * ==================================================================== 00016 * @endcopyright 00017 * 00018 * @file svn_cache.h 00019 * @brief In-memory cache implementation. 00020 */ 00021 00022 00023 #ifndef SVN_CACHE_H 00024 #define SVN_CACHE_H 00025 00026 #include <apr_pools.h> 00027 #include <apr_hash.h> 00028 00029 #include "svn_types.h" 00030 #include "svn_error.h" 00031 #include "svn_iter.h" 00032 #include "svn_config.h" 00033 00034 00035 #ifdef __cplusplus 00036 extern "C" { 00037 #endif /* __cplusplus */ 00038 00039 00040 00041 /** 00042 * @defgroup svn_cache_support In-memory caching 00043 * @{ 00044 */ 00045 00046 /** 00047 * A function type for copying an object @a in into a different pool @a pool 00048 * and returning the result in @a *out. 00049 * 00050 * @since New in 1.6. 00051 */ 00052 typedef svn_error_t *(*svn_cache_dup_func_t)(void **out, 00053 void *in, 00054 apr_pool_t *pool); 00055 00056 /** 00057 * A function type for deserializing an object @a *out from the string 00058 * @a data of length @a data_len in the pool @a pool. 00059 * 00060 * @since New in 1.6. 00061 */ 00062 typedef svn_error_t *(*svn_cache_deserialize_func_t)(void **out, 00063 const char *data, 00064 apr_size_t data_len, 00065 apr_pool_t *pool); 00066 00067 /** 00068 * A function type for serializing an object @a in into bytes. The 00069 * function should allocate the serialized value in @a pool, set @a 00070 * *data to the serialized value, and set *data_len to its length. 00071 * 00072 * @since New in 1.6. 00073 */ 00074 typedef svn_error_t *(*svn_cache_serialize_func_t)(char **data, 00075 apr_size_t *data_len, 00076 void *in, 00077 apr_pool_t *pool); 00078 00079 /** 00080 * A function type for transforming or ignoring errors. @a pool may 00081 * be used for temporary allocations. 00082 * 00083 * @since New in 1.6. 00084 */ 00085 typedef svn_error_t *(*svn_cache_error_handler_t)(svn_error_t *err, 00086 void *baton, 00087 apr_pool_t *pool); 00088 00089 /** 00090 * A wrapper around apr_memcache_t, provided essentially so that the 00091 * Subversion public API doesn't depend on whether or not you have 00092 * access to the APR memcache libraries. 00093 * 00094 * @since New in 1.6. 00095 */ 00096 typedef struct svn_memcache_t svn_memcache_t; 00097 00098 /** 00099 * Opaque type for an in-memory cache. 00100 * 00101 * @since New in 1.6. 00102 */ 00103 typedef struct svn_cache_t svn_cache_t; 00104 00105 /** 00106 * Creates a new cache in @a *cache_p. This cache will use @a pool 00107 * for all of its storage needs. The elements in the cache will be 00108 * indexed by keys of length @a klen, which may be APR_HASH_KEY_STRING 00109 * if they are strings. Cached values will be copied in and out of 00110 * the cache using @a dup_func. 00111 * 00112 * The cache stores up to @a pages * @a items_per_page items at a 00113 * time. The exact cache invalidation strategy is not defined here, 00114 * but in general, a lower value for @a items_per_page means more 00115 * memory overhead for the same number of items, but a higher value 00116 * for @a items_per_page means more items are cleared at once. Both 00117 * @a pages and @a items_per_page must be positive (though they both 00118 * may certainly be 1). 00119 * 00120 * If @a thread_safe is true, and APR is compiled with threads, all 00121 * accesses to the cache will be protected with a mutex. 00122 * 00123 * Note that NULL is a legitimate value for cache entries (and @a dup_func 00124 * will not be called on it). 00125 * 00126 * It is not safe for @a dup_func to interact with the cache itself. 00127 * 00128 * @since New in 1.6. 00129 */ 00130 svn_error_t * 00131 svn_cache_create_inprocess(svn_cache_t **cache_p, 00132 svn_cache_dup_func_t dup_func, 00133 apr_ssize_t klen, 00134 apr_int64_t pages, 00135 apr_int64_t items_per_page, 00136 svn_boolean_t thread_safe, 00137 apr_pool_t *pool); 00138 /** 00139 * Creates a new cache in @a *cache_p, communicating to a memcached 00140 * process via @a memcache. The elements in the cache will be indexed 00141 * by keys of length @a klen, which may be APR_HASH_KEY_STRING if they 00142 * are strings. Values will be serialized for memcached using @a 00143 * serialize_func and deserialized using @a deserialize_func. Because 00144 * the same memcached server may cache many different kinds of values, 00145 * @a prefix should be specified to differentiate this cache from 00146 * other caches. @a *cache_p will be allocated in @a pool. 00147 * 00148 * If @a deserialize_func is NULL, then the data is returned as an 00149 * svn_string_t; if @a serialize_func is NULL, then the data is 00150 * assumed to be an svn_stringbuf_t. 00151 * 00152 * These caches are always thread safe. 00153 * 00154 * These caches do not support svn_cache_iter. 00155 * 00156 * If Subversion was not built with apr_memcache support, always 00157 * raises SVN_ERR_NO_APR_MEMCACHE. 00158 * 00159 * @since New in 1.6. 00160 */ 00161 svn_error_t * 00162 svn_cache_create_memcache(svn_cache_t **cache_p, 00163 svn_memcache_t *memcache, 00164 svn_cache_serialize_func_t serialize_func, 00165 svn_cache_deserialize_func_t deserialize_func, 00166 apr_ssize_t klen, 00167 const char *prefix, 00168 apr_pool_t *pool); 00169 00170 /** 00171 * Given @a config, returns an APR memcached interface in @a 00172 * *memcache_p allocated in @a pool if @a config contains entries in 00173 * the SVN_CACHE_CONFIG_CATEGORY_MEMCACHED_SERVERS section describing 00174 * memcached servers; otherwise, sets @a *memcache_p to NULL. 00175 * 00176 * If Subversion was not built with apr_memcache_support, then raises 00177 * SVN_ERR_NO_APR_MEMCACHE if and only if @a config is configured to 00178 * use memcache. 00179 * 00180 * @since New in 1.6. 00181 */ 00182 svn_error_t * 00183 svn_cache_make_memcache_from_config(svn_memcache_t **memcache_p, 00184 svn_config_t *config, 00185 apr_pool_t *pool); 00186 00187 /** 00188 * Sets @a handler to be @a cache's error handling routine. If any 00189 * error is returned from a call to svn_cache_get or svn_cache_set, @a 00190 * handler will be called with @a baton and the error, and the 00191 * original function will return whatever error @a handler returns 00192 * instead (possibly SVN_NO_ERROR); @a handler will receive the pool 00193 * passed to the svn_cache_* function. @a pool is used for temporary 00194 * allocations. 00195 * 00196 * @since New in 1.6. 00197 */ 00198 svn_error_t * 00199 svn_cache_set_error_handler(svn_cache_t *cache, 00200 svn_cache_error_handler_t handler, 00201 void *baton, 00202 apr_pool_t *pool); 00203 00204 00205 #define SVN_CACHE_CONFIG_CATEGORY_MEMCACHED_SERVERS "memcached-servers" 00206 00207 /** 00208 * Fetches a value indexed by @a key from @a cache into @a *value, 00209 * setting @a *found to TRUE iff it is in the cache and FALSE if it is 00210 * not found. The value is copied into @a pool using the copy 00211 * function provided to the cache's constructor. 00212 * 00213 * @since New in 1.6. 00214 */ 00215 svn_error_t * 00216 svn_cache_get(void **value, 00217 svn_boolean_t *found, 00218 const svn_cache_t *cache, 00219 const void *key, 00220 apr_pool_t *pool); 00221 00222 /** 00223 * Stores the value @a value under the key @a key in @a cache. @a pool 00224 * is used only for temporary allocations. The cache makes copies of 00225 * @a key and @a value if necessary (that is, @a key and @a value may 00226 * have shorter lifetimes than the cache). 00227 * 00228 * If there is already a value for @a key, this will replace it. Bear 00229 * in mind that in some circumstances this may leak memory (that is, 00230 * the cache's copy of the previous value may not be immediately 00231 * cleared); it is only guaranteed to not leak for caches created with 00232 * @a items_per_page equal to 1. 00233 * 00234 * @since New in 1.6. 00235 */ 00236 svn_error_t * 00237 svn_cache_set(svn_cache_t *cache, 00238 const void *key, 00239 void *value, 00240 apr_pool_t *pool); 00241 00242 /** 00243 * Iterates over the elements currently in @a cache, calling @a func 00244 * for each one until there are no more elements or @a func returns an 00245 * error. Uses @a pool for temporary allocations. 00246 * 00247 * If @a completed is not NULL, then on return - if @a func returns no 00248 * errors - @a *completed will be set to @c TRUE. 00249 * 00250 * If @a func returns an error other than @c SVN_ERR_ITER_BREAK, that 00251 * error is returned. When @a func returns @c SVN_ERR_ITER_BREAK, 00252 * iteration is interrupted, but no error is returned and @a 00253 * *completed is set to @c FALSE. (The error handler set by 00254 * svn_cache_set_error_handler is not used for svn_cache_iter.) 00255 * 00256 * It is not legal to perform any other cache operations on @a cache 00257 * inside @a func. 00258 * 00259 * svn_cache_iter is not supported by all cache implementations; see 00260 * the svn_cache_create_* function for details. 00261 * 00262 * @since New in 1.6. 00263 */ 00264 svn_error_t * 00265 svn_cache_iter(svn_boolean_t *completed, 00266 const svn_cache_t *cache, 00267 svn_iter_apr_hash_cb_t func, 00268 void *baton, 00269 apr_pool_t *pool); 00270 /** @} */ 00271 00272 00273 #ifdef __cplusplus 00274 } 00275 #endif /* __cplusplus */ 00276 00277 #endif /* SVN_CACHE_H */
1.3.9.1