Kouhei Sutou
null+****@clear*****
Tue Oct 8 22:15:52 JST 2013
Kouhei Sutou 2013-10-08 22:15:52 +0900 (Tue, 08 Oct 2013) New Revision: 7c71e15d9cc336e0c641e5208a024e53665f78f1 https://github.com/groonga/groonga/commit/7c71e15d9cc336e0c641e5208a024e53665f78f1 Message: Add ruby/load plugin that provides ruby_load command Added files: plugins/ruby/load.c plugins/ruby/load_sources.am test/command/suite/ruby/load/existent.expected test/command/suite/ruby/load/existent.test Modified files: plugins/ruby/CMakeLists.txt plugins/ruby/Makefile.am Renamed files: plugins/ruby/eval_sources.am (from plugins/ruby/sources.am) Modified: plugins/ruby/CMakeLists.txt (+15 -4) =================================================================== --- plugins/ruby/CMakeLists.txt 2013-10-08 22:10:26 +0900 (b42e153) +++ plugins/ruby/CMakeLists.txt 2013-10-08 22:15:52 +0900 (ba7deaf) @@ -18,12 +18,23 @@ include_directories( ${MRUBY_INCLUDE_DIRS}) if(GRN_WITH_MRUBY) - read_file_list(${CMAKE_CURRENT_SOURCE_DIR}/sources.am RUBY_SOURCES) - add_library(eval MODULE ${RUBY_SOURCES}) - set_source_files_properties(${RUBY_SOURCES} + set(GRN_RELATIVE_RUBY_PLUGINS_DIR "${GRN_RELATIVE_PLUGINS_DIR}/ruby") + + read_file_list(${CMAKE_CURRENT_SOURCE_DIR}/eval_sources.am RUBY_EVAL_SOURCES) + add_library(eval MODULE ${RUBY_EVAL_SOURCES}) + set_source_files_properties(${RUBY_EVAL_SOURCES} PROPERTIES COMPILE_FLAGS "${GRN_C_COMPILE_FLAGS}") set_target_properties(eval PROPERTIES PREFIX "") target_link_libraries(eval libgroonga) - install(TARGETS eval DESTINATION "${GRN_RELATIVE_PLUGINS_DIR}/ruby") + install(TARGETS eval DESTINATION "${GRN_RELATIVE_RUBY_PLUGINS_DIR}") + + read_file_list(${CMAKE_CURRENT_SOURCE_DIR}/load_sources.am RUBY_LOAD_SOURCES) + add_library(load MODULE ${RUBY_LOAD_SOURCES}) + set_source_files_properties(${RUBY_LOAD_SOURCES} + PROPERTIES + COMPILE_FLAGS "${GRN_C_COMPILE_FLAGS}") + set_target_properties(load PROPERTIES PREFIX "") + target_link_libraries(load libgroonga) + install(TARGETS load DESTINATION "${GRN_RELATIVE_RUBY_PLUGINS_DIR}") endif() Modified: plugins/ruby/Makefile.am (+5 -2) =================================================================== --- plugins/ruby/Makefile.am 2013-10-08 22:10:26 +0900 (e0bf2ba) +++ plugins/ruby/Makefile.am 2013-10-08 22:15:52 +0900 (381fb47) @@ -20,7 +20,10 @@ LIBS = \ $(MESSAGE_PACK_LIBS) if WITH_MRUBY -ruby_plugins_LTLIBRARIES = eval.la +ruby_plugins_LTLIBRARIES = \ + eval.la \ + load.la endif -include sources.am +include eval_sources.am +include load_sources.am Renamed: plugins/ruby/eval_sources.am (+0 -0) 100% =================================================================== Added: plugins/ruby/load.c (+109 -0) 100644 =================================================================== --- /dev/null +++ plugins/ruby/load.c 2013-10-08 22:15:52 +0900 (71d43c2) @@ -0,0 +1,109 @@ +/* -*- c-basic-offset: 2; indent-tabs-mode: nil -*- */ +/* + Copyright(C) 2013 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 <mrb.h> +#include <output.h> +#include <db.h> +#include <util.h> + +#include <groonga/plugin.h> + +#define VAR GRN_PROC_GET_VAR_BY_OFFSET + +static void +output_result(grn_ctx *ctx, mrb_value result) +{ + grn_obj grn_result; + + GRN_OUTPUT_MAP_OPEN("result", 1); + GRN_OUTPUT_CSTR("value"); + GRN_VOID_INIT(&grn_result); + if (grn_mrb_to_grn(ctx, result, &grn_result) == GRN_SUCCESS) { + GRN_OUTPUT_OBJ(&grn_result, NULL); + } else { + GRN_OUTPUT_CSTR("unsupported return value"); + } + grn_obj_unlink(ctx, &grn_result); + GRN_OUTPUT_MAP_CLOSE(); +} + +static grn_obj * +command_ruby_load(grn_ctx *ctx, int nargs, grn_obj **args, + grn_user_data *user_data) +{ + grn_obj *path; + mrb_value result; + + path = VAR(0); + switch (path->header.domain) { + case GRN_DB_SHORT_TEXT : + case GRN_DB_TEXT : + case GRN_DB_LONG_TEXT : + break; + default : + { + grn_obj inspected; + GRN_TEXT_INIT(&inspected, 0); + grn_inspect(ctx, &inspected, path); + ERR(GRN_INVALID_ARGUMENT, "path must be a string: <%.*s>", + (int)GRN_TEXT_LEN(&inspected), GRN_TEXT_VALUE(&inspected)); + GRN_OBJ_FIN(ctx, &inspected); + return NULL; + } + break; + } + + GRN_TEXT_PUTC(ctx, path, '\0'); + result = grn_mrb_load(ctx, GRN_TEXT_VALUE(path)); + output_result(ctx, result); + + return NULL; +} + +grn_rc +GRN_PLUGIN_INIT(grn_ctx *ctx) +{ + return GRN_SUCCESS; +} + +#define DEF_VAR(v,x) do {\ + (v).name = (x);\ + (v).name_size = (x) ? sizeof(x) - 1 : 0;\ + GRN_TEXT_INIT(&(v).value, 0);\ +} while (0) + +#define DEF_COMMAND(name, func, nvars, vars)\ + (grn_proc_create(ctx, (name), (sizeof(name) - 1),\ + GRN_PROC_COMMAND, (func), NULL, NULL, (nvars), (vars))) + +grn_rc +GRN_PLUGIN_REGISTER(grn_ctx *ctx) +{ + grn_expr_var vars[1]; + + DEF_VAR(vars[0], "path"); + DEF_COMMAND("ruby_load", command_ruby_load, 1, vars); + + return ctx->rc; +} + +grn_rc +GRN_PLUGIN_FIN(grn_ctx *ctx) +{ + return GRN_SUCCESS; +} Added: plugins/ruby/load_sources.am (+2 -0) 100644 =================================================================== --- /dev/null +++ plugins/ruby/load_sources.am 2013-10-08 22:15:52 +0900 (2484dcc) @@ -0,0 +1,2 @@ +load_la_SOURCES = \ + load.c Added: test/command/suite/ruby/load/existent.expected (+4 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/ruby/load/existent.expected 2013-10-08 22:15:52 +0900 (526ebe0) @@ -0,0 +1,4 @@ +register ruby/load +[[0,0.0,0.0],true] +ruby_load "expr.rb" +[[0,0.0,0.0],{"value":null}] Added: test/command/suite/ruby/load/existent.test (+5 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/ruby/load/existent.test 2013-10-08 22:15:52 +0900 (4f79d38) @@ -0,0 +1,5 @@ +#@on-error omit +register ruby/load +#@on-error default + +ruby_load "expr.rb" -------------- next part -------------- HTML����������������������������... 다운로드