Skip to content

Commit

Permalink
Add groonga-mruby command
Browse files Browse the repository at this point in the history
You can evaluate Ruby script in the specified database context with
groonga-mruby.

Groonga-mruby is added just for testing.
  • Loading branch information
kou committed Nov 3, 2014
1 parent 8ff4ef9 commit c9ea214
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ aclocal.m4
/src/groonga
/src/groonga-benchmark
/src/grnslap
/src/groonga-mruby
Makefile
Makefile.in
version.sh
Expand Down
1 change: 1 addition & 0 deletions config.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export GROONGA="@GROONGA@"
export GROONGA_HTTPD="@GROONGA_HTTPD@"
export GROONGA_SUGGEST_CREATE_DATASET="@GROONGA_SUGGEST_CREATE_DATASET@"
export GROONGA_BENCHMARK="@GROONGA_BENCHMARK@"
export GROONGA_MRUBY="@GROONGA_MRUBY@"
4 changes: 4 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,10 @@ AC_SUBST(GROONGA_BENCHMARK)
GROONGA_SUGGEST_CREATE_DATASET="${ac_pwd}/src/suggest/groonga-suggest-create-dataset"
AC_SUBST(GROONGA_SUGGEST_CREATE_DATASET)

# groonga-mruby binary path
GROONGA_MRUBY="${ac_pwd}/src/groonga-mruby"
AC_SUBST(GROONGA_MRUBY)

# check Cutter with GLib support if available
REQUIRED_MINIMUM_CUTTER_VERSION=1.1.6
REQUIRED_MINIMUM_CPPCUTTER_VERSION=1.2.0
Expand Down
9 changes: 9 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ SUBDIRS = \
NONEXISTENT_CXX_SOURCE = nonexistent.cpp

bin_PROGRAMS = groonga groonga-benchmark
if WITH_MRUBY
bin_PROGRAMS += groonga-mruby
endif
noinst_PROGRAMS = grnslap

EXTRA_DIST = \
Expand Down Expand Up @@ -46,3 +49,9 @@ nodist_EXTRA_groonga_benchmark_SOURCES = $(NONEXISTENT_CXX_SOURCE)
groonga_benchmark_LDADD = \
$(top_builddir)/lib/libgroonga.la \
$(MESSAGE_PACK_LIBS)

include groonga_mruby_sources.am
nodist_EXTRA_groonga_mruby_SOURCES = $(NONEXISTENT_CXX_SOURCE)
groonga_mruby_LDADD = \
$(top_builddir)/lib/libgroonga.la \
$(MESSAGE_PACK_LIBS)
84 changes: 84 additions & 0 deletions src/groonga_mruby.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/* -*- c-basic-offset: 2 -*- */
/*
Copyright(C) 2014 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 "../lib/grn_mrb.h"

#include <stdio.h>
#include <stdlib.h>

static int
run(grn_ctx *ctx, const char *db_path, const char *ruby_script_path)
{
grn_obj *db;

db = grn_db_open(ctx, db_path);
if (!db) {
if (ctx->rc == GRN_NO_SUCH_FILE_OR_DIRECTORY) {
db = grn_db_create(ctx, db_path, NULL);
if (!db) {
fprintf(stderr, "Failed to create database: <%s>: %s",
db_path, ctx->errbuf);
return EXIT_FAILURE;
}
} else {
fprintf(stderr, "Failed to open database: <%s>: %s",
db_path, ctx->errbuf);
return EXIT_FAILURE;
}
}

grn_mrb_load(ctx, ruby_script_path);
if (ctx->rc != GRN_SUCCESS) {
fprintf(stderr, "Failed to load Ruby script: <%s>: %s",
ruby_script_path, ctx->errbuf);
}

grn_obj_close(ctx, db);

if (ctx->rc == GRN_SUCCESS) {
return EXIT_SUCCESS;
} else {
return EXIT_FAILURE;
}
}

int
main(int argc, char **argv)
{
int exit_code = EXIT_SUCCESS;

if (argc != 3) {
fprintf(stderr, "Usage: %s DB_PATH RUBY_SCRIPT_PATH\n", argv[0]);
return EXIT_FAILURE;
}

if (grn_init() != GRN_SUCCESS) {
return EXIT_FAILURE;
}

{
grn_ctx ctx;
grn_ctx_init(&ctx, 0);
exit_code = run(&ctx, argv[1], argv[2]);
grn_ctx_fin(&ctx);
}

grn_fin();

return exit_code;
}
2 changes: 2 additions & 0 deletions src/groonga_mruby_sources.am
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
groonga_mruby_SOURCES = \
groonga_mruby.c

0 comments on commit c9ea214

Please sign in to comment.