Skip to content

Commit

Permalink
Add ruby/load plugin that provides ruby_load command
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Oct 8, 2013
1 parent f09804c commit 7c71e15
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 6 deletions.
19 changes: 15 additions & 4 deletions plugins/ruby/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
7 changes: 5 additions & 2 deletions plugins/ruby/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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
File renamed without changes.
109 changes: 109 additions & 0 deletions plugins/ruby/load.c
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 2 additions & 0 deletions plugins/ruby/load_sources.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
load_la_SOURCES = \
load.c
4 changes: 4 additions & 0 deletions test/command/suite/ruby/load/existent.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
register ruby/load
[[0,0.0,0.0],true]
ruby_load "expr.rb"
[[0,0.0,0.0],{"value":null}]
5 changes: 5 additions & 0 deletions test/command/suite/ruby/load/existent.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#@on-error omit
register ruby/load
#@on-error default

ruby_load "expr.rb"

0 comments on commit 7c71e15

Please sign in to comment.