Kouhei Sutou
null+****@clear*****
Tue Nov 10 16:24:01 JST 2015
Kouhei Sutou 2015-11-10 16:24:01 +0900 (Tue, 10 Nov 2015) New Revision: 31605a7d7f45ceb18aca252153fc7da93d6d5115 https://github.com/ranguba/rroonga/commit/31605a7d7f45ceb18aca252153fc7da93d6d5115 Message: Bind grn_conf_* Added files: ext/groonga/rb-grn-conf.c Modified files: ext/groonga/rb-grn-utils.c ext/groonga/rb-grn.h ext/groonga/rb-groonga.c lib/groonga/context.rb Added: ext/groonga/rb-grn-conf.c (+145 -0) 100644 =================================================================== --- /dev/null +++ ext/groonga/rb-grn-conf.c 2015-11-10 16:24:01 +0900 (5958b9f) @@ -0,0 +1,145 @@ +/* -*- coding: utf-8; mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + Copyright (C) 2015 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" + +#define SELF(object) (RVAL2GRNCONTEXT(object)) + +/* + * Document-class: Groonga::Conf + * + * This class manages database global configurations. Each + * configuration is key and value pair. + */ + +/* + * Creates a new configuration set for `context`. + * + * @overload initialize(context=nil) + * @param [Groonga::Context, nil] context The context to be configured. + * If `context` is `nil`, {Groonga::Context.default} is used. + * + * @since 5.0.9 + */ +static VALUE +rb_grn_conf_initialize (VALUE self, VALUE rb_context) +{ + rb_iv_set(self, "@context", rb_context); + + return Qnil; +} + +/* + * Gets a configuration value for key. + * + * @overload conf[](key) + * @param [String] key The key. + * @return [String, nil] The value associated with `key`. + * + * @since 5.0.9 + */ +static VALUE +rb_grn_conf_get (VALUE self, VALUE rb_key) +{ + VALUE rb_context; + VALUE rb_value; + grn_ctx *context; + const char *key; + int key_size; + const char *value; + uint32_t value_size; + + rb_context = rb_iv_get(self, "@context"); + context = rb_grn_context_ensure(&rb_context); + + rb_key = rb_grn_convert_to_string(rb_key); + key = RSTRING_PTR(rb_key); + key_size = RSTRING_LEN(rb_key); + + { + grn_rc rc; + rc = grn_conf_get(context, + key, key_size, + &value, &value_size); + rb_grn_context_check(context, self); + rb_grn_rc_check(rc, self); + } + + if (value_size == 0) { + rb_value = Qnil; + } else { + rb_value = rb_grn_context_rb_string_new(context, value, value_size); + } + + return rb_value; +} + +/* + * Sets a configuration key and value pair. + * + * @overload conf[]=(key, value) + * @param [String] key The key. + * @param [String] value The value to be assigned. + * @return [String] `value`. + */ +static VALUE +rb_grn_conf_set (VALUE self, VALUE rb_key, VALUE rb_value) +{ + VALUE rb_value_original = rb_value; + VALUE rb_context; + grn_ctx *context; + const char *key; + int key_size; + const char *value; + int value_size; + + rb_context = rb_iv_get(self, "@context"); + context = rb_grn_context_ensure(&rb_context); + + rb_key = rb_grn_convert_to_string(rb_key); + key = RSTRING_PTR(rb_key); + key_size = RSTRING_LEN(rb_key); + + rb_value = rb_grn_convert_to_string(rb_value); + value = RSTRING_PTR(rb_value); + value_size = RSTRING_LEN(rb_value); + + { + grn_rc rc; + rc = grn_conf_set(context, + key, key_size, + value, value_size); + rb_grn_context_check(context, self); + rb_grn_rc_check(rc, self); + } + + return rb_value_original; +} + +void +rb_grn_init_conf (VALUE mGrn) +{ + VALUE cGrnConf; + + cGrnConf = rb_define_class_under(mGrn, "Conf", rb_cObject); + + rb_define_method(cGrnConf, "initialize", rb_grn_conf_initialize, 1); + + rb_define_method(cGrnConf, "[]", rb_grn_conf_get, 1); + rb_define_method(cGrnConf, "[]=", rb_grn_conf_set, 2); +} Modified: ext/groonga/rb-grn-utils.c (+5 -1) =================================================================== --- ext/groonga/rb-grn-utils.c 2015-10-07 18:55:27 +0900 (dbcf5cc) +++ ext/groonga/rb-grn-utils.c 2015-11-10 16:24:01 +0900 (28945cd) @@ -174,7 +174,11 @@ rb_grn_equal_string (const char *string1, const char *string2) VALUE rb_grn_convert_to_string (VALUE object) { - return rb_convert_type(object, RUBY_T_STRING, "String", "to_str"); + if (rb_type(object) == T_SYMBOL) { + return rb_sym2str(object); + } else { + return rb_convert_type(object, RUBY_T_STRING, "String", "to_str"); + } } VALUE Modified: ext/groonga/rb-grn.h (+1 -0) =================================================================== --- ext/groonga/rb-grn.h 2015-10-07 18:55:27 +0900 (0568524) +++ ext/groonga/rb-grn.h 2015-11-10 16:24:01 +0900 (6cc8ebb) @@ -354,6 +354,7 @@ void rb_grn_init_snippet (VALUE mGrn); void rb_grn_init_plugin (VALUE mGrn); void rb_grn_init_normalizer (VALUE mGrn); void rb_grn_init_thread (VALUE mGrn); +void rb_grn_init_conf (VALUE mGrn); VALUE rb_grn_rc_to_exception (grn_rc rc); const char *rb_grn_rc_to_message (grn_rc rc); Modified: ext/groonga/rb-groonga.c (+1 -0) =================================================================== --- ext/groonga/rb-groonga.c 2015-10-07 18:55:27 +0900 (e9177fc) +++ ext/groonga/rb-groonga.c 2015-11-10 16:24:01 +0900 (d93f570) @@ -192,4 +192,5 @@ Init_groonga (void) rb_grn_init_plugin(mGrn); rb_grn_init_normalizer(mGrn); rb_grn_init_thread(mGrn); + rb_grn_init_conf(mGrn); } Modified: lib/groonga/context.rb (+9 -1) =================================================================== --- lib/groonga/context.rb 2015-10-07 18:55:27 +0900 (fc99ddd) +++ lib/groonga/context.rb 2015-11-10 16:24:01 +0900 (fea51bb) @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# Copyright (C) 2010-2013 Kouhei Sutou <kou �� clear-code.com> +# Copyright (C) 2010-2015 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 @@ -346,5 +346,13 @@ module Groonga memory_pool = @memory_pools.last memory_pool.register(object) end + + # @return [Groonga::Conf] The database level configuration sets of + # this context. + # + # @since 5.0.9 + def conf + @conf ||= Conf.new(self) + end end end -------------- next part -------------- HTML����������������������������...Download