Kouhei Sutou
null+****@clear*****
Thu Feb 19 12:05:15 JST 2015
Kouhei Sutou 2015-02-19 12:05:15 +0900 (Thu, 19 Feb 2015) New Revision: 962fa25dd6ae6a407124a8ef22c8bbbf9fee2f57 https://github.com/groonga/groonga/commit/962fa25dd6ae6a407124a8ef22c8bbbf9fee2f57 Message: mrb: extract a common code for getting option value as API Added files: lib/mrb/mrb_options.c lib/mrb/mrb_options.h Modified files: lib/mrb/mrb_expr.c lib/mrb/mrb_table.c lib/mrb/mrb_table_cursor.c lib/mrb/mrb_writer.c lib/mrb/sources.am Modified: lib/mrb/mrb_expr.c (+2 -2) =================================================================== --- lib/mrb/mrb_expr.c 2015-02-19 04:31:36 +0900 (f726075) +++ lib/mrb/mrb_expr.c 2015-02-19 12:05:15 +0900 (b249eb9) @@ -34,6 +34,7 @@ #include "mrb_ctx.h" #include "mrb_expr.h" #include "mrb_converter.h" +#include "mrb_options.h" static struct mrb_data_type mrb_grn_scan_info_type = { "Groonga::ScanInfo", @@ -541,8 +542,7 @@ mrb_grn_expression_parse(mrb_state *mrb, mrb_value self) if (!mrb_nil_p(mrb_options)) { mrb_value mrb_flags; - mrb_flags = mrb_hash_get(mrb, mrb_options, - mrb_symbol_value(mrb_intern_lit(mrb, "flags"))); + mrb_flags = grn_mrb_options_get_lit(mrb, mrb_options, "flags"); if (!mrb_nil_p(mrb_flags)) { flags = mrb_fixnum(mrb_flags); } Added: lib/mrb/mrb_options.c (+39 -0) 100644 =================================================================== --- /dev/null +++ lib/mrb/mrb_options.c 2015-02-19 12:05:15 +0900 (ff3a1c0) @@ -0,0 +1,39 @@ +/* -*- c-basic-offset: 2 -*- */ +/* + Copyright(C) 2015 Brazil + + 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 "../grn_ctx_impl.h" +#include "../grn_db.h" + +#ifdef GRN_WITH_MRUBY +#include <mruby.h> +#include <mruby/hash.h> + +#include "mrb_options.h" + +mrb_value +grn_mrb_options_get_static(mrb_state *mrb, + mrb_value mrb_options, + const char *key, + size_t key_size) +{ + mrb_sym mrb_key; + + mrb_key = mrb_intern_static(mrb, key, key_size); + return mrb_hash_get(mrb, mrb_options, mrb_symbol_value(mrb_key)); +} +#endif Added: lib/mrb/mrb_options.h (+40 -0) 100644 =================================================================== --- /dev/null +++ lib/mrb/mrb_options.h 2015-02-19 12:05:15 +0900 (1aa547d) @@ -0,0 +1,40 @@ +/* -*- c-basic-offset: 2 -*- */ +/* + Copyright(C) 2015 Brazil + + 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 +*/ + +#ifndef GRN_MRB_OPTIONS_H +#define GRN_MRB_OPTIONS_H + +#include "../grn_ctx.h" + +#ifdef __cplusplus +extern "C" { +#endif + +mrb_value grn_mrb_options_get_static(mrb_state *mrb, + mrb_value mrb_options, + const char *key, + size_t key_size); +#define grn_mrb_options_get_lit(mrb, mrb_options, literal) \ + grn_mrb_options_get_static(mrb, mrb_options, \ + (literal), mrb_strlen_lit(literal)) + +#ifdef __cplusplus +} +#endif + +#endif /* GRN_MRB_OPTIONS_H */ Modified: lib/mrb/mrb_table.c (+7 -13) =================================================================== --- lib/mrb/mrb_table.c 2015-02-19 04:31:36 +0900 (2b86fdb) +++ lib/mrb/mrb_table.c 2015-02-19 12:05:15 +0900 (46f8508) @@ -29,6 +29,7 @@ #include "mrb_ctx.h" #include "mrb_table.h" #include "mrb_converter.h" +#include "mrb_options.h" static mrb_value mrb_grn_table_is_locked(mrb_state *mrb, mrb_value self) @@ -86,14 +87,12 @@ mrb_grn_table_select(mrb_state *mrb, mrb_value self) mrb_value mrb_result; mrb_value mrb_operator; - mrb_result = mrb_hash_get(mrb, mrb_options, - mrb_symbol_value(mrb_intern_lit(mrb, "result"))); + mrb_result = grn_mrb_options_get_lit(mrb, mrb_options, "result"); if (!mrb_nil_p(mrb_result)) { result = DATA_PTR(mrb_result); } - mrb_operator = mrb_hash_get(mrb, mrb_options, - mrb_symbol_value(mrb_intern_lit(mrb, "operator"))); + mrb_operator = grn_mrb_options_get_lit(mrb, mrb_options, "operator"); if (!mrb_nil_p(mrb_operator)) { operator = mrb_fixnum(mrb_operator); } @@ -133,8 +132,7 @@ mrb_grn_table_sort(mrb_state *mrb, mrb_value self) mrb_value mrb_sort_order; mrb_sort_options = RARRAY_PTR(mrb_keys)[i]; - mrb_sort_key = mrb_hash_get(mrb, mrb_sort_options, - mrb_symbol_value(mrb_intern_lit(mrb, "key"))); + mrb_sort_key = grn_mrb_options_get_lit(mrb, mrb_sort_options, "key"); switch (mrb_type(mrb_sort_key)) { case MRB_TT_STRING : keys[i].key = grn_obj_column(ctx, table, @@ -158,9 +156,7 @@ mrb_grn_table_sort(mrb_state *mrb, mrb_value self) } keys[i].flags = 0; - mrb_sort_order = - mrb_hash_get(mrb, mrb_sort_options, - mrb_symbol_value(mrb_intern_lit(mrb, "order"))); + mrb_sort_order = grn_mrb_options_get_lit(mrb, mrb_sort_options, "order"); if (mrb_nil_p(mrb_sort_order) || (mrb_symbol(mrb_sort_order) == mrb_intern_lit(mrb, "ascending"))) { keys[i].flags |= GRN_TABLE_SORT_ASC; @@ -173,14 +169,12 @@ mrb_grn_table_sort(mrb_state *mrb, mrb_value self) mrb_value mrb_offset; mrb_value mrb_limit; - mrb_offset = mrb_hash_get(mrb, mrb_options, - mrb_symbol_value(mrb_intern_lit(mrb, "offset"))); + mrb_offset = grn_mrb_options_get_lit(mrb, mrb_options, "offset"); if (!mrb_nil_p(mrb_offset)) { offset = mrb_fixnum(mrb_offset); } - mrb_limit = mrb_hash_get(mrb, mrb_options, - mrb_symbol_value(mrb_intern_lit(mrb, "limit"))); + mrb_limit = grn_mrb_options_get_lit(mrb, mrb_options, "limit"); if (!mrb_nil_p(mrb_limit)) { limit = mrb_fixnum(mrb_limit); } Modified: lib/mrb/mrb_table_cursor.c (+4 -6) =================================================================== --- lib/mrb/mrb_table_cursor.c 2015-02-19 04:31:36 +0900 (6605960) +++ lib/mrb/mrb_table_cursor.c 2015-02-19 12:05:15 +0900 (fadd4b3) @@ -27,6 +27,7 @@ #include "mrb_ctx.h" #include "mrb_table_cursor.h" +#include "mrb_options.h" static struct mrb_data_type mrb_grn_table_cursor_type = { "Groonga::TableCursor", @@ -108,16 +109,13 @@ mrb_grn_table_cursor_singleton_open_raw(mrb_state *mrb, mrb_value klass) mrb_value mrb_max; mrb_value mrb_flags; - mrb_min = mrb_hash_get(mrb, mrb_options, - mrb_symbol_value(mrb_intern_lit(mrb, "min"))); + mrb_min = grn_mrb_options_get_lit(mrb, mrb_options, "min"); mrb_value_to_border_value(mrb, "min", mrb_min, &min_buffer, &min, &min_size); - mrb_max = mrb_hash_get(mrb, mrb_options, - mrb_symbol_value(mrb_intern_lit(mrb, "max"))); + mrb_max = grn_mrb_options_get_lit(mrb, mrb_options, "max"); mrb_value_to_border_value(mrb, "max", mrb_max, &max_buffer, &max, &max_size); - mrb_flags = mrb_hash_get(mrb, mrb_options, - mrb_symbol_value(mrb_intern_lit(mrb, "flags"))); + mrb_flags = grn_mrb_options_get_lit(mrb, mrb_options, "flags"); if (!mrb_nil_p(mrb_flags)) { flags = mrb_fixnum(mrb_flags); } Modified: lib/mrb/mrb_writer.c (+3 -4) =================================================================== --- lib/mrb/mrb_writer.c 2015-02-19 04:31:36 +0900 (e14073e) +++ lib/mrb/mrb_writer.c 2015-02-19 12:05:15 +0900 (70e1b6d) @@ -28,6 +28,7 @@ #include "../grn_output.h" #include "mrb_ctx.h" #include "mrb_writer.h" +#include "mrb_options.h" static mrb_value writer_write(mrb_state *mrb, mrb_value self) @@ -164,14 +165,12 @@ writer_write_table_records(mrb_state *mrb, mrb_value self) mrb_value mrb_offset; mrb_value mrb_limit; - mrb_offset = mrb_hash_get(mrb, mrb_options, - mrb_symbol_value(mrb_intern_lit(mrb, "offset"))); + mrb_offset = grn_mrb_options_get_lit(mrb, mrb_options, "offset"); if (!mrb_nil_p(mrb_offset)) { offset = mrb_fixnum(mrb_offset); } - mrb_limit = mrb_hash_get(mrb, mrb_options, - mrb_symbol_value(mrb_intern_lit(mrb, "limit"))); + mrb_limit = grn_mrb_options_get_lit(mrb, mrb_options, "limit"); if (!mrb_nil_p(mrb_limit)) { limit = mrb_fixnum(mrb_limit); } Modified: lib/mrb/sources.am (+2 -0) =================================================================== --- lib/mrb/sources.am 2015-02-19 04:31:36 +0900 (c4f02fc) +++ lib/mrb/sources.am 2015-02-19 12:05:15 +0900 (27c6d01) @@ -39,6 +39,8 @@ libgrnmrb_la_SOURCES = \ mrb_object.h \ mrb_operator.c \ mrb_operator.h \ + mrb_options.c \ + mrb_options.h \ mrb_patricia_trie.c \ mrb_patricia_trie.h \ mrb_procedure.c \ -------------- next part -------------- HTML����������������������������...Download