Kouhei Sutou
null+****@clear*****
Wed Apr 26 10:42:58 JST 2017
Kouhei Sutou 2017-04-26 10:42:58 +0900 (Wed, 26 Apr 2017) New Revision: ff2dd28f2f36d4ba4afa18afd1060b23ba5402e6 https://github.com/ranguba/rroonga/commit/ff2dd28f2f36d4ba4afa18afd1060b23ba5402e6 Message: Add Groonga::DefaultCache Added files: ext/groonga/rb-grn-default-cache.c test/test-default-cache.rb Modified files: ext/groonga/rb-grn.h ext/groonga/rb-groonga.c Added: ext/groonga/rb-grn-default-cache.c (+104 -0) 100644 =================================================================== --- /dev/null +++ ext/groonga/rb-grn-default-cache.c 2017-04-26 10:42:58 +0900 (5cf1f6c) @@ -0,0 +1,104 @@ +/* -*- mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + Copyright (C) 2017 Kouhei Sutou <kou �� clear-code.com> + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License version 2.1 as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "rb-grn.h" + +/* + * Document-module: Groonga::DefaultCache + * + * This module provides the default cache related features. + * + * @since 7.0.2 + */ + +/* + * @overload base_path + * @return [String, nil] the default cache base path or `nil`. + * + * @since 7.0.2 + */ +static VALUE +rb_grn_default_cache_s_get_base_path (VALUE klass) +{ + const char *base_path; + + base_path = grn_get_default_cache_base_path(); + if (base_path) { + return rb_str_new_cstr(base_path); + } else { + return Qnil; + } +} + +/* + * @overload base_path=(base_path) + * @param base_path [String, nil] The new default cache base path. + * If you specify `String`, the default cache will be persistent. + * If you specify `nil`, the default cache will be just on memory. + * You need to call {.reopen} to apply this change. + * @return [String, nil] The `base_path` itself. + * + * @since 7.0.2 + */ +static VALUE +rb_grn_default_cache_s_set_base_path (VALUE klass, VALUE rb_base_path) +{ + if (NIL_P(rb_base_path)) { + grn_set_default_cache_base_path(NULL); + } else { + const char *base_path; + base_path = StringValueCStr(rb_base_path); + grn_set_default_cache_base_path(base_path); + } + + return rb_base_path; +} + +/* + * @overload reopen + * Reopens the default cache. + * + * @return [void] + * + * @since 7.0.2 + */ +static VALUE +rb_grn_default_cache_s_reopen (VALUE klass) +{ + grn_rc rc; + + rc = grn_cache_default_reopen(); + rb_grn_rc_check(rc, klass); + + return Qnil; +} + +void +rb_grn_init_default_cache (VALUE mGrn) +{ + VALUE mGrnDefaultCache; + + mGrnDefaultCache = rb_define_module_under(mGrn, "DefaultCache"); + + rb_define_singleton_method(mGrnDefaultCache, "base_path", + rb_grn_default_cache_s_get_base_path, 0); + rb_define_singleton_method(mGrnDefaultCache, "base_path=", + rb_grn_default_cache_s_set_base_path, 1); + rb_define_singleton_method(mGrnDefaultCache, "reopen", + rb_grn_default_cache_s_reopen, 0); +} Modified: ext/groonga/rb-grn.h (+1 -0) =================================================================== --- ext/groonga/rb-grn.h 2017-04-25 16:25:15 +0900 (9be30b3) +++ ext/groonga/rb-grn.h 2017-04-26 10:42:58 +0900 (1dd50ee) @@ -368,6 +368,7 @@ void rb_grn_init_request_timer (VALUE mGrn); void rb_grn_init_request_timer_id (VALUE mGrn); void rb_grn_init_id (VALUE mGrn); void rb_grn_init_name (VALUE mGrn); +void rb_grn_init_default_cache (VALUE mGrn); VALUE rb_grn_rc_to_exception (grn_rc rc); void rb_grn_rc_check (grn_rc rc, Modified: ext/groonga/rb-groonga.c (+1 -0) =================================================================== --- ext/groonga/rb-groonga.c 2017-04-25 16:25:15 +0900 (1601c86) +++ ext/groonga/rb-groonga.c 2017-04-26 10:42:58 +0900 (211e67f) @@ -256,4 +256,5 @@ Init_groonga (void) rb_grn_init_request_timer_id(mGrn); rb_grn_init_id(mGrn); rb_grn_init_name(mGrn); + rb_grn_init_default_cache(mGrn); } Added: test/test-default-cache.rb (+43 -0) 100644 =================================================================== --- /dev/null +++ test/test-default-cache.rb 2017-04-26 10:42:58 +0900 (f95628c) @@ -0,0 +1,43 @@ +# Copyright (C) 2017 Kouhei Sutou <kou �� clear-code.com> +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License version 2.1 as published by the Free Software Foundation. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +class DefaultCacheTest < Test::Unit::TestCase + include GroongaTestUtils + + setup :setup_database + def setup + begin + yield + ensure + Groonga::DefaultCache.base_path = nil + Groonga::DefaultCache.reopen + end + end + + test "set base_path" do + base_path = "#{@database_path}.cache" + keys_path = "#{base_path}.keys" + Groonga::DefaultCache.base_path = base_path + assert do + not File.exist?(keys_path) + end + + Groonga::DefaultCache.reopen + + assert do + File.exist?(keys_path) + end + end +end -------------- next part -------------- HTML����������������������������...下载