Navigation Menu

Skip to content

Commit

Permalink
Support cache per database
Browse files Browse the repository at this point in the history
New APIs:

  * grn_db_set_cache()
  * grn_db_get_cache()
  • Loading branch information
kou committed Feb 16, 2018
1 parent 8294d71 commit 0fff6ff
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 5 deletions.
4 changes: 3 additions & 1 deletion include/groonga/db.h
@@ -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
Expand Down Expand Up @@ -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, \
Expand Down
3 changes: 3 additions & 0 deletions lib/cache.c
Expand Up @@ -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;
}

Expand Down
54 changes: 54 additions & 0 deletions lib/db.c
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
{
Expand Down
3 changes: 2 additions & 1 deletion lib/grn_db.h
@@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/httpd/nginx-module/ngx_http_groonga_module.c
Expand Up @@ -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. */
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 0fff6ff

Please sign in to comment.