Kouhei Sutou
null+****@clear*****
Thu Apr 6 11:29:28 JST 2017
Kouhei Sutou 2017-04-06 11:29:28 +0900 (Thu, 06 Apr 2017) New Revision: da33942ce4674e3f84791b82a8210758a95594cd https://github.com/groonga/groonga/commit/da33942ce4674e3f84791b82a8210758a95594cd Message: cache: support using persistent cache New APIs: * grn_set_default_cache_base_path(): sets the default base path for persistent cache * grn_get_default_cache_base_path(): gets the default base path for persistent cache * grn_persistent_cache_open(): creates a new persistent cache Modified files: include/groonga/cache.h lib/cache.c Modified: include/groonga/cache.h (+5 -0) =================================================================== --- include/groonga/cache.h 2017-04-05 15:20:06 +0900 (967c9e8) +++ include/groonga/cache.h 2017-04-06 11:29:28 +0900 (a923404) @@ -25,7 +25,12 @@ extern "C" { #define GRN_CACHE_DEFAULT_MAX_N_ENTRIES 100 typedef struct _grn_cache grn_cache; +GRN_API void grn_set_default_cache_base_path(const char *base_path); +GRN_API const char *grn_get_default_cache_base_path(void); + GRN_API grn_cache *grn_cache_open(grn_ctx *ctx); +GRN_API grn_cache *grn_persistent_cache_open(grn_ctx *ctx, + const char *base_path); GRN_API grn_rc grn_cache_close(grn_ctx *ctx, grn_cache *cache); GRN_API grn_rc grn_cache_current_set(grn_ctx *ctx, grn_cache *cache); Modified: lib/cache.c (+62 -13) =================================================================== --- lib/cache.c 2017-04-05 15:20:06 +0900 (470ec2a) +++ lib/cache.c 2017-04-06 11:29:28 +0900 (3348a21) @@ -71,6 +71,29 @@ struct _grn_cache { static grn_ctx grn_cache_ctx; static grn_cache *grn_cache_current = NULL; static grn_cache *grn_cache_default = NULL; +static char grn_cache_default_base_path[PATH_MAX]; + +void +grn_set_default_cache_base_path(const char *base_path) +{ + if (base_path) { + grn_strcpy(grn_cache_default_base_path, + PATH_MAX, + base_path); + } else { + grn_cache_default_base_path[0] = '\0'; + } +} + +const char * +grn_get_default_cache_base_path(void) +{ + if (grn_cache_default_base_path[0] == '\0') { + return NULL; + } else { + return grn_cache_default_base_path; + } +} inline static void grn_cache_open_memory(grn_ctx *ctx, grn_cache *cache) @@ -201,8 +224,10 @@ grn_cache_open_persistent(grn_ctx *ctx, cache->impl.persistent.timeout = 1000; } -grn_cache * -grn_cache_open(grn_ctx *ctx) +static grn_cache * +grn_cache_open_raw(grn_ctx *ctx, + grn_bool is_memory, + const char *base_path) { grn_cache *cache = NULL; @@ -213,21 +238,12 @@ grn_cache_open(grn_ctx *ctx) goto exit; } - { - char grn_cache_type_env[GRN_ENV_BUFFER_SIZE]; - grn_getenv("GRN_CACHE_TYPE", grn_cache_type_env, GRN_ENV_BUFFER_SIZE); - if (strcmp(grn_cache_type_env, "persistent") == 0) { - cache->is_memory = GRN_FALSE; - } else { - cache->is_memory = GRN_TRUE; - } - } - cache->ctx = ctx; + cache->is_memory = is_memory; if (cache->is_memory) { grn_cache_open_memory(ctx, cache); } else { - grn_cache_open_persistent(ctx, cache, NULL); + grn_cache_open_persistent(ctx, cache, base_path); } if (ctx->rc != GRN_SUCCESS) { GRN_FREE(cache); @@ -243,6 +259,39 @@ exit : GRN_API_RETURN(cache); } +grn_cache * +grn_cache_open(grn_ctx *ctx) +{ + const char *base_path = NULL; + grn_bool is_memory; + + if (grn_cache_default_base_path[0] != '\0') { + base_path = grn_cache_default_base_path; + } + + if (base_path) { + is_memory = GRN_FALSE; + } else { + char grn_cache_type_env[GRN_ENV_BUFFER_SIZE]; + grn_getenv("GRN_CACHE_TYPE", grn_cache_type_env, GRN_ENV_BUFFER_SIZE); + if (strcmp(grn_cache_type_env, "persistent") == 0) { + is_memory = GRN_FALSE; + } else { + is_memory = GRN_TRUE; + } + } + + return grn_cache_open_raw(ctx, is_memory, base_path); +} + +grn_cache * +grn_persistent_cache_open(grn_ctx *ctx, const char *base_path) +{ + grn_bool is_memory = GRN_FALSE; + return grn_cache_open_raw(ctx, is_memory, base_path); +} + + inline static void grn_cache_close_memory(grn_ctx *ctx, grn_cache *cache) { -------------- next part -------------- HTML����������������������������... Télécharger