[Groonga-commit] groonga/groonga at 0fff6ff [master] Support cache per database

Back to archive index

Kouhei Sutou null+****@clear*****
Fri Feb 16 17:25:57 JST 2018


Kouhei Sutou	2018-02-16 17:25:57 +0900 (Fri, 16 Feb 2018)

  New Revision: 0fff6ff1513f13a4f8df8b72fe32a713855b52c9
  https://github.com/groonga/groonga/commit/0fff6ff1513f13a4f8df8b72fe32a713855b52c9

  Message:
    Support cache per database
    
    New APIs:
    
      * grn_db_set_cache()
      * grn_db_get_cache()

  Modified files:
    include/groonga/db.h
    lib/cache.c
    lib/db.c
    lib/grn_db.h
    src/httpd/nginx-module/ngx_http_groonga_module.c

  Modified: include/groonga/db.h (+3 -1)
===================================================================
--- include/groonga/db.h    2018-02-15 18:19:30 +0900 (cbfbfec30)
+++ include/groonga/db.h    2018-02-16 17:25:57 +0900 (0088b5a4a)
@@ -1,5 +1,5 @@
 /*
-  Copyright(C) 2009-2016 Brazil
+  Copyright(C) 2009-2018 Brazil
 
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -40,6 +40,8 @@ GRN_API grn_rc grn_db_recover(grn_ctx *ctx, grn_obj *db);
 GRN_API grn_rc grn_db_unmap(grn_ctx *ctx, grn_obj *db);
 GRN_API uint32_t grn_db_get_last_modified(grn_ctx *ctx, grn_obj *db);
 GRN_API grn_bool grn_db_is_dirty(grn_ctx *ctx, grn_obj *db);
+GRN_API grn_rc grn_db_set_cache(grn_ctx *ctx, grn_obj *db, grn_cache *cache);
+GRN_API grn_cache *grn_db_get_cache(grn_ctx *ctx, grn_obj *db);
 
 #define GRN_DB_EACH_BEGIN_FLAGS(ctx, cursor, id, flags)                 \
   GRN_TABLE_EACH_BEGIN_FLAGS(ctx,                                       \

  Modified: lib/cache.c (+3 -0)
===================================================================
--- lib/cache.c    2018-02-15 18:19:30 +0900 (4d113a5b2)
+++ lib/cache.c    2018-02-16 17:25:57 +0900 (f26f5ddb4)
@@ -411,6 +411,9 @@ grn_cache_current_set(grn_ctx *ctx, grn_cache *cache)
 grn_cache *
 grn_cache_current_get(grn_ctx *ctx)
 {
+  if (ctx && ctx->impl && ctx->impl->db && ((grn_db *)ctx->impl->db)->cache) {
+    return ((grn_db *)ctx->impl->db)->cache;
+  }
   return grn_cache_current;
 }
 

  Modified: lib/db.c (+54 -0)
===================================================================
--- lib/db.c    2018-02-15 18:19:30 +0900 (a773f28c7)
+++ lib/db.c    2018-02-16 17:25:57 +0900 (6ec22588d)
@@ -216,6 +216,7 @@ grn_db_create(grn_ctx *ctx, const char *path, grn_db_create_optarg *optarg)
   s->keys = NULL;
   s->specs = NULL;
   s->config = NULL;
+  s->cache = NULL;
 
   {
     grn_bool use_default_db_key = GRN_TRUE;
@@ -333,6 +334,7 @@ grn_db_open(grn_ctx *ctx, const char *path)
   s->keys = NULL;
   s->specs = NULL;
   s->config = NULL;
+  s->cache = NULL;
 
   {
     uint32_t type = grn_io_detect_type(ctx, path);
@@ -501,6 +503,58 @@ grn_db_close(grn_ctx *ctx, grn_obj *db)
   GRN_API_RETURN(GRN_SUCCESS);
 }
 
+grn_rc
+grn_db_set_cache(grn_ctx *ctx, grn_obj *db, grn_cache *cache)
+{
+  GRN_API_ENTER;
+
+  if (!ctx) {
+    GRN_API_RETURN(GRN_INVALID_ARGUMENT);
+  }
+
+  if (!db) {
+    ERR(GRN_INVALID_ARGUMENT, "[db][cache][set] DB must not NULL");
+    GRN_API_RETURN(ctx->rc);
+  }
+
+  if (db->header.type != GRN_DB) {
+    ERR(GRN_INVALID_ARGUMENT, "[db][cache][set] must be DB: %d",
+        db->header.type);
+    GRN_API_RETURN(ctx->rc);
+  }
+
+  ((grn_db *)db)->cache = cache;
+
+  GRN_API_RETURN(GRN_SUCCESS);
+}
+
+grn_cache *
+grn_db_get_cache(grn_ctx *ctx, grn_obj *db)
+{
+  grn_cache *cache;
+
+  GRN_API_ENTER;
+
+  if (!ctx) {
+    GRN_API_RETURN(NULL);
+  }
+
+  if (!db) {
+    ERR(GRN_INVALID_ARGUMENT, "[db][cache][get] DB must not NULL");
+    GRN_API_RETURN(NULL);
+  }
+
+  if (db->header.type != GRN_DB) {
+    ERR(GRN_INVALID_ARGUMENT, "[db][cache][get] must be DB: %d",
+        db->header.type);
+    GRN_API_RETURN(NULL);
+  }
+
+  cache = ((grn_db *)db)->cache;
+
+  GRN_API_RETURN(cache);
+}
+
 grn_obj *
 grn_ctx_get(grn_ctx *ctx, const char *name, int name_size)
 {

  Modified: lib/grn_db.h (+2 -1)
===================================================================
--- lib/grn_db.h    2018-02-15 18:19:30 +0900 (f0fbddad8)
+++ lib/grn_db.h    2018-02-16 17:25:57 +0900 (157703cd0)
@@ -1,6 +1,6 @@
 /* -*- c-basic-offset: 2 -*- */
 /*
-  Copyright(C) 2009-2016 Brazil
+  Copyright(C) 2009-2018 Brazil
 
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -48,6 +48,7 @@ struct _grn_db {
   grn_hash *config;
   grn_tiny_array values;
   grn_critical_section lock;
+  grn_cache *cache;
 };
 
 #define GRN_SERIALIZED_SPEC_INDEX_SPEC   0

  Modified: src/httpd/nginx-module/ngx_http_groonga_module.c (+3 -3)
===================================================================
--- src/httpd/nginx-module/ngx_http_groonga_module.c    2018-02-15 18:19:30 +0900 (15836a92e)
+++ src/httpd/nginx-module/ngx_http_groonga_module.c    2018-02-16 17:25:57 +0900 (77347dedd)
@@ -333,7 +333,9 @@ ngx_http_groonga_context_init(ngx_http_groonga_loc_conf_t *location_conf,
   }
 
   grn_ctx_use(context, location_conf->database);
-  grn_cache_current_set(context, location_conf->cache);
+  grn_db_set_cache(context,
+                   location_conf->database,
+                   location_conf->cache);
 
   /* TODO: It doesn't work yet. We need to implement request timeout
    * handler. */
@@ -1497,12 +1499,10 @@ ngx_http_groonga_close_database_callback(ngx_http_groonga_loc_conf_t *location_c
   ngx_http_groonga_context_init_query_logger(location_conf,
                                              data->pool,
                                              data->log);
-  grn_cache_current_set(context, location_conf->cache);
 
   grn_obj_close(context, location_conf->database);
   ngx_http_groonga_context_log_error(data->log);
 
-  grn_cache_current_set(context, NULL);
   grn_cache_close(context, location_conf->cache);
 }
 
-------------- next part --------------
HTML����������������������������...
URL: https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20180216/9c44214e/attachment-0001.htm 



More information about the Groonga-commit mailing list
Back to archive index