diff options
Diffstat (limited to 'storage/mroonga/vendor/groonga/lib/mrb')
100 files changed, 5335 insertions, 868 deletions
diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_accessor.c b/storage/mroonga/vendor/groonga/lib/mrb/mrb_accessor.c index a350c6b04f3..daa0caada06 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/mrb_accessor.c +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_accessor.c @@ -1,6 +1,6 @@ /* -*- c-basic-offset: 2 -*- */ /* - Copyright(C) 2013-2014 Brazil + Copyright(C) 2013-2015 Brazil This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -16,7 +16,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "../ctx_impl.h" +#include "../grn_ctx_impl.h" #ifdef GRN_WITH_MRUBY #include <mruby.h> @@ -24,8 +24,9 @@ #include <mruby/variable.h> #include <mruby/data.h> -#include "../db.h" +#include "../grn_db.h" #include "mrb_accessor.h" +#include "mrb_converter.h" static struct mrb_data_type mrb_grn_accessor_type = { "Groonga::Accessor", @@ -49,8 +50,25 @@ mrb_grn_accessor_next(mrb_state *mrb, mrb_value self) grn_accessor *accessor; accessor = DATA_PTR(self); - if (!accessor->next) { return mrb_nil_value(); } - return mrb_cptr_value(mrb, accessor->next); + return grn_mrb_value_from_grn_obj(mrb, (grn_obj *)(accessor->next)); +} + +static mrb_value +mrb_grn_accessor_have_next_p(mrb_state *mrb, mrb_value self) +{ + grn_accessor *accessor; + + accessor = DATA_PTR(self); + return mrb_bool_value(accessor->next != NULL); +} + +static mrb_value +mrb_grn_accessor_object(mrb_state *mrb, mrb_value self) +{ + grn_accessor *accessor; + + accessor = DATA_PTR(self); + return grn_mrb_value_from_grn_obj(mrb, accessor->obj); } void @@ -67,5 +85,9 @@ grn_mrb_accessor_init(grn_ctx *ctx) mrb_grn_accessor_initialize, MRB_ARGS_REQ(1)); mrb_define_method(mrb, klass, "next", mrb_grn_accessor_next, MRB_ARGS_NONE()); + mrb_define_method(mrb, klass, "have_next?", + mrb_grn_accessor_have_next_p, MRB_ARGS_NONE()); + mrb_define_method(mrb, klass, "object", + mrb_grn_accessor_object, MRB_ARGS_NONE()); } #endif diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_accessor.h b/storage/mroonga/vendor/groonga/lib/mrb/mrb_accessor.h index 2aaf32f48f9..7b27e6375ec 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/mrb_accessor.h +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_accessor.h @@ -19,8 +19,8 @@ #ifndef GRN_MRB_ACCESSOR_H #define GRN_MRB_ACCESSOR_H -#include "../ctx.h" -#include "../db.h" +#include "../grn_ctx.h" +#include "../grn_db.h" #ifdef __cplusplus extern "C" { diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_array.c b/storage/mroonga/vendor/groonga/lib/mrb/mrb_array.c new file mode 100644 index 00000000000..f19bcfa9563 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_array.c @@ -0,0 +1,60 @@ +/* -*- 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 "../grn_ctx_impl.h" + +#ifdef GRN_WITH_MRUBY +#include <mruby.h> +#include <mruby/class.h> +#include <mruby/data.h> + +#include "mrb_array.h" + +static struct mrb_data_type mrb_grn_array_type = { + "Groonga::Array", + NULL +}; + +static mrb_value +mrb_grn_array_initialize(mrb_state *mrb, mrb_value self) +{ + mrb_value mrb_array_ptr; + + mrb_get_args(mrb, "o", &mrb_array_ptr); + DATA_TYPE(self) = &mrb_grn_array_type; + DATA_PTR(self) = mrb_cptr(mrb_array_ptr); + return self; +} + +void +grn_mrb_array_init(grn_ctx *ctx) +{ + grn_mrb_data *data = &(ctx->impl->mrb); + mrb_state *mrb = data->state; + struct RClass *module = data->module; + struct RClass *table_class; + struct RClass *klass; + + table_class = mrb_class_get_under(mrb, module, "Table"); + klass = mrb_define_class_under(mrb, module, "Array", table_class); + MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA); + + mrb_define_method(mrb, klass, "initialize", + mrb_grn_array_initialize, MRB_ARGS_REQ(1)); +} +#endif diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_array.h b/storage/mroonga/vendor/groonga/lib/mrb/mrb_array.h new file mode 100644 index 00000000000..062281684ee --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_array.h @@ -0,0 +1,34 @@ +/* -*- 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 +*/ + +#ifndef GRN_MRB_ARRAY_H +#define GRN_MRB_ARRAY_H + +#include "../grn_ctx.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void grn_mrb_array_init(grn_ctx *ctx); + +#ifdef __cplusplus +} +#endif + +#endif /* GRN_MRB_ARRAY_H */ diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_bulk.c b/storage/mroonga/vendor/groonga/lib/mrb/mrb_bulk.c index ad428ea5bde..eb57a016148 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/mrb_bulk.c +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_bulk.c @@ -1,6 +1,6 @@ /* -*- c-basic-offset: 2 -*- */ /* - Copyright(C) 2014 Brazil + Copyright(C) 2014-2015 Brazil This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -16,7 +16,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "../ctx_impl.h" +#include "../grn_ctx_impl.h" #ifdef GRN_WITH_MRUBY #include <mruby.h> @@ -24,8 +24,9 @@ #include <mruby/variable.h> #include <mruby/data.h> #include <mruby/numeric.h> +#include <mruby/string.h> -#include "../db.h" +#include "../grn_db.h" #include "mrb_bulk.h" static struct mrb_data_type mrb_grn_bulk_type = { @@ -33,33 +34,62 @@ static struct mrb_data_type mrb_grn_bulk_type = { NULL }; -static mrb_value -mrb_grn_bulk_initialize(mrb_state *mrb, mrb_value self) +grn_obj * +grn_mrb_value_to_bulk(mrb_state *mrb, mrb_value mrb_value_, grn_obj *bulk) { - mrb_value mrb_bulk_ptr; - - mrb_get_args(mrb, "o", &mrb_bulk_ptr); - DATA_TYPE(self) = &mrb_grn_bulk_type; - DATA_PTR(self) = mrb_cptr(mrb_bulk_ptr); - return self; -} + grn_ctx *ctx = (grn_ctx *)mrb->ud; + + switch (mrb_type(mrb_value_)) { + case MRB_TT_FALSE : + if (mrb_nil_p(mrb_value_)) { + grn_obj_reinit(ctx, bulk, GRN_DB_VOID, 0); + } else { + grn_obj_reinit(ctx, bulk, GRN_DB_BOOL, 0); + GRN_BOOL_SET(ctx, bulk, GRN_FALSE); + } + break; + case MRB_TT_TRUE : + grn_obj_reinit(ctx, bulk, GRN_DB_BOOL, 0); + GRN_BOOL_SET(ctx, bulk, GRN_TRUE); + break; + case MRB_TT_FIXNUM : + grn_obj_reinit(ctx, bulk, GRN_DB_INT64, 0); + GRN_INT64_SET(ctx, bulk, mrb_fixnum(mrb_value_)); + break; + case MRB_TT_SYMBOL : + { + const char *name; + mrb_int name_length; -static mrb_value -mrb_grn_bulk_get_domain(mrb_state *mrb, mrb_value self) -{ - grn_obj *bulk; + grn_obj_reinit(ctx, bulk, GRN_DB_TEXT, 0); + name = mrb_sym2name_len(mrb, mrb_symbol(mrb_value_), &name_length); + GRN_TEXT_SET(ctx, bulk, name, name_length); + } + break; + case MRB_TT_FLOAT : + grn_obj_reinit(ctx, bulk, GRN_DB_FLOAT, 0); + GRN_FLOAT_SET(ctx, bulk, mrb_float(mrb_value_)); + break; + case MRB_TT_STRING : + grn_obj_reinit(ctx, bulk, GRN_DB_TEXT, 0); + GRN_TEXT_SET(ctx, bulk, RSTRING_PTR(mrb_value_), RSTRING_LEN(mrb_value_)); + break; + default : + mrb_raisef(mrb, E_ARGUMENT_ERROR, + "unsupported object to convert to bulk: %S", + mrb_value_); + break; + } - bulk = DATA_PTR(self); - return mrb_fixnum_value(bulk->header.domain); + return bulk; } -static mrb_value -mrb_grn_bulk_get_value(mrb_state *mrb, mrb_value self) +mrb_value +grn_mrb_value_from_bulk(mrb_state *mrb, grn_obj *bulk) { - grn_obj *bulk; mrb_value mrb_value_; + grn_ctx *ctx = (grn_ctx *)mrb->ud; - bulk = DATA_PTR(self); switch (bulk->header.domain) { case GRN_DB_INT32 : { @@ -81,11 +111,33 @@ mrb_grn_bulk_get_value(mrb_state *mrb, mrb_value self) mrb_value_ = mrb_fixnum_value(value); } break; + case GRN_DB_TIME : + { + int64_t value; + int32_t sec; + int32_t usec; + + value = GRN_TIME_VALUE(bulk); + GRN_TIME_UNPACK(value, sec, usec); + mrb_value_ = mrb_funcall(mrb, + mrb_obj_value(ctx->impl->mrb.builtin.time_class), + "at", + 2, + mrb_fixnum_value(sec), + mrb_fixnum_value(usec)); + } + break; + case GRN_DB_SHORT_TEXT : + case GRN_DB_TEXT : + case GRN_DB_LONG_TEXT : + mrb_value_ = mrb_str_new_static(mrb, + GRN_TEXT_VALUE(bulk), + GRN_TEXT_LEN(bulk)); + break; default : { #define MESSAGE_SIZE 4096 char message[MESSAGE_SIZE]; - grn_ctx *ctx = (grn_ctx *)mrb->ud; grn_obj *domain; char domain_name[GRN_TABLE_MAX_KEY_SIZE]; int domain_name_size; @@ -96,14 +148,14 @@ mrb_grn_bulk_get_value(mrb_state *mrb, mrb_value self) domain_name, GRN_TABLE_MAX_KEY_SIZE); grn_obj_unlink(ctx, domain); } else { - strcpy(domain_name, "unknown"); + grn_strcpy(domain_name, GRN_TABLE_MAX_KEY_SIZE, "unknown"); domain_name_size = strlen(domain_name); } - snprintf(message, MESSAGE_SIZE, - "unsupported bulk value type: <%d>(%.*s)", - bulk->header.domain, - domain_name_size, - domain_name); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "unsupported bulk value type: <%d>(%.*s)", + bulk->header.domain, + domain_name_size, + domain_name); mrb_raise(mrb, E_RANGE_ERROR, message); #undef MESSAGE_SIZE } @@ -113,6 +165,43 @@ mrb_grn_bulk_get_value(mrb_state *mrb, mrb_value self) return mrb_value_; } +grn_bool +grn_mrb_bulk_cast(mrb_state *mrb, grn_obj *from, grn_obj *to, grn_id domain_id) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + grn_rc rc; + + grn_obj_reinit(ctx, to, domain_id, 0); + rc = grn_obj_cast(ctx, from, to, GRN_FALSE); + return rc == GRN_SUCCESS; +} + +static mrb_value +mrb_grn_bulk_initialize(mrb_state *mrb, mrb_value self) +{ + mrb_value mrb_bulk_ptr; + + mrb_get_args(mrb, "o", &mrb_bulk_ptr); + DATA_TYPE(self) = &mrb_grn_bulk_type; + DATA_PTR(self) = mrb_cptr(mrb_bulk_ptr); + return self; +} + +static mrb_value +mrb_grn_bulk_get_domain(mrb_state *mrb, mrb_value self) +{ + grn_obj *bulk; + + bulk = DATA_PTR(self); + return mrb_fixnum_value(bulk->header.domain); +} + +static mrb_value +mrb_grn_bulk_get_value(mrb_state *mrb, mrb_value self) +{ + return grn_mrb_value_from_bulk(mrb, DATA_PTR(self)); +} + static mrb_value mrb_grn_bulk_equal(mrb_state *mrb, mrb_value self) { diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_bulk.h b/storage/mroonga/vendor/groonga/lib/mrb/mrb_bulk.h index 3be86584dc6..dd1f46fb84e 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/mrb_bulk.h +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_bulk.h @@ -1,6 +1,6 @@ /* -*- c-basic-offset: 2 -*- */ /* - Copyright(C) 2014 Brazil + Copyright(C) 2014-2015 Brazil This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -19,8 +19,8 @@ #ifndef GRN_MRB_BULK_H #define GRN_MRB_BULK_H -#include "../ctx.h" -#include "../db.h" +#include "../grn_ctx.h" +#include "../grn_db.h" #ifdef __cplusplus extern "C" { @@ -28,6 +28,15 @@ extern "C" { void grn_mrb_bulk_init(grn_ctx *ctx); +mrb_value grn_mrb_value_from_bulk(mrb_state *mrb, grn_obj *bulk); +grn_obj *grn_mrb_value_to_bulk(mrb_state *mrb, + mrb_value mrb_value_, + grn_obj *bulk); +grn_bool grn_mrb_bulk_cast(mrb_state *mrb, + grn_obj *from, + grn_obj *to, + grn_id domain_id); + #ifdef __cplusplus } #endif diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_column.c b/storage/mroonga/vendor/groonga/lib/mrb/mrb_column.c index 545f070d91c..1d3bbb69422 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/mrb_column.c +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_column.c @@ -1,6 +1,6 @@ /* -*- c-basic-offset: 2 -*- */ /* - Copyright(C) 2013 Brazil + Copyright(C) 2013-2014 Brazil This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -16,13 +16,42 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "../ctx_impl.h" +#include "../grn_ctx_impl.h" #ifdef GRN_WITH_MRUBY #include <mruby.h> #include <mruby/class.h> +#include <mruby/data.h> +#include "mrb_ctx.h" #include "mrb_column.h" +#include "mrb_converter.h" + +static mrb_value +mrb_grn_column_is_locked(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + unsigned int is_locked; + + is_locked = grn_obj_is_locked(ctx, DATA_PTR(self)); + grn_mrb_ctx_check(mrb); + + return mrb_bool_value(is_locked != 0); +} + +static mrb_value +mrb_grn_column_get_table(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + grn_obj *table; + + table = grn_column_table(ctx, DATA_PTR(self)); + if (!table) { + return mrb_nil_value(); + } + + return grn_mrb_value_from_grn_obj(mrb, table); +} void grn_mrb_column_init(grn_ctx *ctx) @@ -35,5 +64,11 @@ grn_mrb_column_init(grn_ctx *ctx) klass = mrb_define_class_under(mrb, module, "Column", object_class); MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA); + + mrb_define_method(mrb, klass, "locked?", + mrb_grn_column_is_locked, MRB_ARGS_NONE()); + + mrb_define_method(mrb, klass, "table", + mrb_grn_column_get_table, MRB_ARGS_NONE()); } #endif diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_column.h b/storage/mroonga/vendor/groonga/lib/mrb/mrb_column.h index 77e0dab82fd..abb99915fa5 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/mrb_column.h +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_column.h @@ -19,7 +19,7 @@ #ifndef GRN_MRB_COLUMN_H #define GRN_MRB_COLUMN_H -#include "../ctx.h" +#include "../grn_ctx.h" #ifdef __cplusplus extern "C" { diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_command.c b/storage/mroonga/vendor/groonga/lib/mrb/mrb_command.c new file mode 100644 index 00000000000..0ff3a0d5b69 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_command.c @@ -0,0 +1,180 @@ +/* -*- 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 <groonga/command.h> + +#ifdef GRN_WITH_MRUBY +#include <mruby.h> +#include <mruby/class.h> +#include <mruby/data.h> +#include <mruby/value.h> +#include <mruby/string.h> + +#include "mrb_ctx.h" +#include "mrb_command.h" + +static struct mrb_data_type mrb_grn_command_type = { + "Groonga::Command", + NULL +}; + +mrb_value +mrb_grn_command_instantiate(grn_ctx *ctx, grn_obj *command) +{ + grn_mrb_data *data = &(ctx->impl->mrb); + mrb_state *mrb = data->state; + struct RClass *module = data->module; + char name[GRN_TABLE_MAX_KEY_SIZE]; + int name_size; + mrb_value mrb_name; + struct RClass *command_class; + struct RClass *target_command_class; + mrb_value mrb_target_command_class; + mrb_value mrb_arguments[1]; + + name_size = grn_obj_name(ctx, command, name, GRN_TABLE_MAX_KEY_SIZE); + mrb_name = mrb_str_new(mrb, name, name_size); + + command_class = mrb_class_get_under(mrb, module, "Command"); + mrb_target_command_class = mrb_funcall(mrb, + mrb_obj_value(command_class), + "find_class", 1, mrb_name); + if (mrb_nil_p(mrb_target_command_class)) { + target_command_class = command_class; + } else { + target_command_class = mrb_class_ptr(mrb_target_command_class); + } + mrb_arguments[0] = mrb_cptr_value(mrb, command); + return mrb_obj_new(mrb, target_command_class, 1, mrb_arguments); +} + +static void +mrb_grn_command_run_wrapper(grn_ctx *ctx, + grn_obj *command, + grn_command_input *input, + void *user_data) +{ + grn_mrb_data *data = &(ctx->impl->mrb); + mrb_state *mrb = data->state; + struct RClass *module = data->module; + int arena_index; + mrb_value mrb_command; + mrb_value mrb_input; + + arena_index = mrb_gc_arena_save(mrb); + mrb_command = mrb_grn_command_instantiate(ctx, command); + { + struct RClass *command_input_class; + mrb_value mrb_arguments[1]; + command_input_class = mrb_class_get_under(mrb, module, "CommandInput"); + mrb_arguments[0] = mrb_cptr_value(mrb, input); + mrb_input = mrb_obj_new(mrb, command_input_class, 1, mrb_arguments); + } + mrb_funcall(mrb, mrb_command, "run_internal", 1, mrb_input); + mrb_gc_arena_restore(mrb, arena_index); +} + +static mrb_value +mrb_grn_command_singleton_register(mrb_state *mrb, mrb_value klass) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + mrb_value mrb_name; + mrb_value *mrb_arguments; + mrb_int n_arguments; + + mrb_get_args(mrb, "Sa", &mrb_name, &mrb_arguments, &n_arguments); + + { + grn_expr_var *vars; + mrb_int i; + + for (i = 0; i < n_arguments; i++) { + mrb_arguments[i] = mrb_convert_type(mrb, mrb_arguments[i], + MRB_TT_STRING, "String", "to_str"); + } + vars = GRN_MALLOCN(grn_expr_var, n_arguments); + for (i = 0; i < n_arguments; i++) { + mrb_value mrb_argument = mrb_arguments[i]; + grn_expr_var *var = &vars[i]; + var->name = RSTRING_PTR(mrb_argument); + var->name_size = RSTRING_LEN(mrb_argument); + GRN_TEXT_INIT(&(var->value), 0); + } + + grn_command_register(ctx, + RSTRING_PTR(mrb_name), + RSTRING_LEN(mrb_name), + mrb_grn_command_run_wrapper, + vars, + n_arguments, + NULL); + + for (i = 0; i < n_arguments; i++) { + grn_expr_var *var = &vars[i]; + GRN_OBJ_FIN(ctx, &(var->value)); + } + GRN_FREE(vars); + } + + grn_mrb_ctx_check(mrb); + + { + grn_mrb_data *data = &(ctx->impl->mrb); + struct RClass *command_class; + command_class = mrb_class_get_under(mrb, data->module, "Command"); + mrb_funcall(mrb, + mrb_obj_value(command_class), + "register_class", 2, mrb_name, klass); + } + + return mrb_nil_value(); +} + +static mrb_value +mrb_grn_command_initialize(mrb_state *mrb, mrb_value self) +{ + mrb_value mrb_command_ptr; + + mrb_get_args(mrb, "o", &mrb_command_ptr); + DATA_TYPE(self) = &mrb_grn_command_type; + DATA_PTR(self) = mrb_cptr(mrb_command_ptr); + return self; +} + +void +grn_mrb_command_init(grn_ctx *ctx) +{ + grn_mrb_data *data = &(ctx->impl->mrb); + mrb_state *mrb = data->state; + struct RClass *module = data->module; + struct RClass *procedure_class; + struct RClass *klass; + + procedure_class = mrb_class_get_under(mrb, module, "Procedure"); + klass = mrb_define_class_under(mrb, module, "Command", procedure_class); + MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA); + + mrb_define_singleton_method(mrb, (struct RObject *)klass, "register", + mrb_grn_command_singleton_register, + MRB_ARGS_REQ(2)); + + mrb_define_method(mrb, klass, "initialize", + mrb_grn_command_initialize, MRB_ARGS_REQ(1)); +} +#endif diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_command.h b/storage/mroonga/vendor/groonga/lib/mrb/mrb_command.h new file mode 100644 index 00000000000..2b899df3a6d --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_command.h @@ -0,0 +1,36 @@ +/* -*- 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_COMMAND_H +#define GRN_MRB_COMMAND_H + +#include "../grn_ctx.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void grn_mrb_command_init(grn_ctx *ctx); + +mrb_value grn_mrb_command_instantiate(grn_ctx *ctx, grn_obj *command); + +#ifdef __cplusplus +} +#endif + +#endif /* GRN_MRB_COMMAND_H */ diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_command_input.c b/storage/mroonga/vendor/groonga/lib/mrb/mrb_command_input.c new file mode 100644 index 00000000000..f39202398d1 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_command_input.c @@ -0,0 +1,122 @@ +/* -*- 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 <groonga/command.h> + +#ifdef GRN_WITH_MRUBY +#include <mruby.h> +#include <mruby/class.h> +#include <mruby/data.h> +#include <mruby/value.h> +#include <mruby/string.h> + +#include "mrb_ctx.h" +#include "mrb_command_input.h" + +static struct mrb_data_type mrb_grn_command_input_type = { + "Groonga::CommandInput", + NULL +}; + +static mrb_value +mrb_grn_command_input_initialize(mrb_state *mrb, mrb_value self) +{ + mrb_value mrb_command_input_ptr; + + mrb_get_args(mrb, "o", &mrb_command_input_ptr); + DATA_TYPE(self) = &mrb_grn_command_input_type; + DATA_PTR(self) = mrb_cptr(mrb_command_input_ptr); + return self; +} + +static mrb_value +mrb_grn_command_input_array_reference(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + grn_command_input *input; + mrb_value mrb_key_or_offset; + grn_obj *argument; + + input = DATA_PTR(self); + mrb_get_args(mrb, "o", &mrb_key_or_offset); + + switch (mrb_type(mrb_key_or_offset)) { + case MRB_TT_FIXNUM : + { + mrb_int offset = mrb_fixnum(mrb_key_or_offset); + argument = grn_command_input_at(ctx, input, offset); + } + break; + case MRB_TT_SYMBOL : + { + mrb_sym mrb_key_symbol; + const char *key; + mrb_int key_length; + + mrb_key_symbol = mrb_symbol(mrb_key_or_offset); + key = mrb_sym2name_len(mrb, mrb_key_symbol, &key_length); + argument = grn_command_input_get(ctx, input, key, key_length); + } + break; + case MRB_TT_STRING : + { + mrb_value mrb_key = mrb_key_or_offset; + argument = grn_command_input_get(ctx, input, + RSTRING_PTR(mrb_key), + RSTRING_LEN(mrb_key)); + } + break; + default : + mrb_raisef(mrb, E_ARGUMENT_ERROR, + "must be offset (as integer) or key (as symbol or string): %S", + mrb_key_or_offset); + break; + } + + if (!argument) { + return mrb_nil_value(); + } + + if (GRN_TEXT_LEN(argument) == 0) { + return mrb_nil_value(); + } + + return mrb_str_new_static(mrb, + GRN_TEXT_VALUE(argument), + GRN_TEXT_LEN(argument)); +} + +void +grn_mrb_command_input_init(grn_ctx *ctx) +{ + grn_mrb_data *data = &(ctx->impl->mrb); + mrb_state *mrb = data->state; + struct RClass *module = data->module; + struct RClass *klass; + + klass = mrb_define_class_under(mrb, module, "CommandInput", mrb->object_class); + MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA); + + mrb_define_method(mrb, klass, "initialize", + mrb_grn_command_input_initialize, MRB_ARGS_REQ(1)); + + mrb_define_method(mrb, klass, "[]", + mrb_grn_command_input_array_reference, MRB_ARGS_REQ(1)); +} +#endif diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_command_input.h b/storage/mroonga/vendor/groonga/lib/mrb/mrb_command_input.h new file mode 100644 index 00000000000..a4eb09779d1 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_command_input.h @@ -0,0 +1,34 @@ +/* -*- 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_COMMAND_INPUT_H +#define GRN_MRB_COMMAND_INPUT_H + +#include "../grn_ctx.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void grn_mrb_command_input_init(grn_ctx *ctx); + +#ifdef __cplusplus +} +#endif + +#endif /* GRN_MRB_COMMAND_INPUT_H */ diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_content_type.c b/storage/mroonga/vendor/groonga/lib/mrb/mrb_content_type.c new file mode 100644 index 00000000000..7f4ff139de1 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_content_type.c @@ -0,0 +1,49 @@ +/* -*- 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" + +#ifdef GRN_WITH_MRUBY +#include <mruby.h> + +#include "mrb_ctx.h" +#include "mrb_content_type.h" + +void +grn_mrb_content_type_init(grn_ctx *ctx) +{ + grn_mrb_data *data = &(ctx->impl->mrb); + mrb_state *mrb = data->state; + struct RClass *module; + + module = mrb_define_module_under(mrb, data->module, "ContentType"); + + mrb_define_const(mrb, module, "NONE", + mrb_fixnum_value(GRN_CONTENT_NONE)); + mrb_define_const(mrb, module, "TSV", + mrb_fixnum_value(GRN_CONTENT_TSV)); + mrb_define_const(mrb, module, "JSON", + mrb_fixnum_value(GRN_CONTENT_JSON)); + mrb_define_const(mrb, module, "XML", + mrb_fixnum_value(GRN_CONTENT_XML)); + mrb_define_const(mrb, module, "MSGPACK", + mrb_fixnum_value(GRN_CONTENT_MSGPACK)); + mrb_define_const(mrb, module, "GROONGA_COMMAND_LIST", + mrb_fixnum_value(GRN_CONTENT_GROONGA_COMMAND_LIST)); +} +#endif diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_content_type.h b/storage/mroonga/vendor/groonga/lib/mrb/mrb_content_type.h new file mode 100644 index 00000000000..46103ff1a62 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_content_type.h @@ -0,0 +1,34 @@ +/* -*- 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_CONTENT_TYPE_H +#define GRN_MRB_CONTENT_TYPE_H + +#include "../grn_ctx.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void grn_mrb_content_type_init(grn_ctx *ctx); + +#ifdef __cplusplus +} +#endif + +#endif /* GRN_MRB_CONTENT_TYPE_H */ diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_converter.c b/storage/mroonga/vendor/groonga/lib/mrb/mrb_converter.c index 2e7c41edbdf..e4c3aceb435 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/mrb_converter.c +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_converter.c @@ -1,6 +1,6 @@ /* -*- c-basic-offset: 2 -*- */ /* - Copyright(C) 2013-2014 Brazil + Copyright(C) 2013-2015 Brazil This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -16,14 +16,125 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "../ctx_impl.h" +#include "../grn_ctx_impl.h" +#include "../grn_db.h" #ifdef GRN_WITH_MRUBY #include <mruby.h> #include <mruby/class.h> #include <mruby/data.h> +#include <mruby/string.h> #include "mrb_converter.h" +#include "mrb_bulk.h" + +void +grn_mrb_value_to_raw_data_buffer_init(mrb_state *mrb, + grn_mrb_value_to_raw_data_buffer *buffer) +{ + GRN_VOID_INIT(&(buffer->from)); + GRN_VOID_INIT(&(buffer->to)); +} + +void +grn_mrb_value_to_raw_data_buffer_fin(mrb_state *mrb, + grn_mrb_value_to_raw_data_buffer *buffer) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + + GRN_OBJ_FIN(ctx, &(buffer->from)); + GRN_OBJ_FIN(ctx, &(buffer->to)); +} + +void +grn_mrb_value_to_raw_data(mrb_state *mrb, + const char *context, + mrb_value mrb_value_, + grn_id domain_id, + grn_mrb_value_to_raw_data_buffer *buffer, + void **raw_value, + unsigned int *raw_value_size) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + enum mrb_vtype mrb_value_type; + grn_bool try_cast = GRN_FALSE; + grn_obj *from_bulk = NULL; + + if (mrb_nil_p(mrb_value_)) { + *raw_value = NULL; + *raw_value_size = 0; + return; + } + + mrb_value_type = mrb_type(mrb_value_); + + switch (mrb_value_type) { + case MRB_TT_STRING : + switch (domain_id) { + case GRN_DB_SHORT_TEXT : + case GRN_DB_TEXT : + case GRN_DB_LONG_TEXT : + *raw_value = RSTRING_PTR(mrb_value_); + *raw_value_size = RSTRING_LEN(mrb_value_); + break; + default : + try_cast = GRN_TRUE; + break; + } + break; + default : + { + struct RClass *klass; + grn_mrb_data *data = &(ctx->impl->mrb); + + klass = mrb_class(mrb, mrb_value_); + if (domain_id == GRN_DB_TIME && + klass == data->builtin.time_class) { + mrb_value mrb_sec; + mrb_value mrb_usec; + + mrb_sec = mrb_funcall(mrb, mrb_value_, "to_i", 0); + mrb_usec = mrb_funcall(mrb, mrb_value_, "usec", 0); + buffer->value.time_value = GRN_TIME_PACK(mrb_fixnum(mrb_sec), + mrb_fixnum(mrb_usec)); + *raw_value = &(buffer->value.time_value); + *raw_value_size = sizeof(buffer->value.time_value); + } else { + try_cast = GRN_TRUE; + if (mrb_value_type == MRB_TT_DATA && + klass == mrb_class_get_under(mrb, data->module, "Bulk")) { + from_bulk = DATA_PTR(mrb_value_); + } + } + } + break; + } + + if (!try_cast) { + return; + } + + if (!from_bulk) { + from_bulk = &(buffer->from); + grn_mrb_value_to_bulk(mrb, mrb_value_, from_bulk); + } + if (!grn_mrb_bulk_cast(mrb, from_bulk, &(buffer->to), domain_id)) { + grn_obj *domain; + char domain_name[GRN_TABLE_MAX_KEY_SIZE]; + int domain_name_size; + + domain = grn_ctx_at(ctx, domain_id); + domain_name_size = grn_obj_name(ctx, domain, domain_name, + GRN_TABLE_MAX_KEY_SIZE); + mrb_raisef(mrb, E_ARGUMENT_ERROR, + "%S: failed to convert to %S: %S", + mrb_str_new_static(mrb, context, strlen(context)), + mrb_str_new_static(mrb, domain_name, domain_name_size), + mrb_value_); + } + *raw_value = GRN_BULK_HEAD(&(buffer->to)); + *raw_value_size = GRN_BULK_VSIZE(&(buffer->to)); +} struct RClass * grn_mrb_class_from_grn_obj(mrb_state *mrb, grn_obj *object) @@ -34,12 +145,12 @@ grn_mrb_class_from_grn_obj(mrb_state *mrb, grn_obj *object) data = &(ctx->impl->mrb); switch (object->header.type) { - case GRN_ACCESSOR : - klass = mrb_class_get_under(mrb, data->module, "Accessor"); - break; case GRN_BULK : klass = mrb_class_get_under(mrb, data->module, "Bulk"); break; + case GRN_ACCESSOR : + klass = mrb_class_get_under(mrb, data->module, "Accessor"); + break; case GRN_COLUMN_FIX_SIZE : klass = mrb_class_get_under(mrb, data->module, "FixedSizeColumn"); break; @@ -49,12 +160,30 @@ grn_mrb_class_from_grn_obj(mrb_state *mrb, grn_obj *object) case GRN_COLUMN_INDEX : klass = mrb_class_get_under(mrb, data->module, "IndexColumn"); break; - case GRN_EXPR : - klass = mrb_class_get_under(mrb, data->module, "Expression"); + case GRN_TYPE : + klass = mrb_class_get_under(mrb, data->module, "Type"); break; case GRN_PROC : klass = mrb_class_get_under(mrb, data->module, "Procedure"); break; + case GRN_EXPR : + klass = mrb_class_get_under(mrb, data->module, "Expression"); + break; + case GRN_TABLE_NO_KEY : + klass = mrb_class_get_under(mrb, data->module, "Array"); + break; + case GRN_TABLE_HASH_KEY : + klass = mrb_class_get_under(mrb, data->module, "HashTable"); + break; + case GRN_TABLE_PAT_KEY : + klass = mrb_class_get_under(mrb, data->module, "PatriciaTrie"); + break; + case GRN_TABLE_DAT_KEY : + klass = mrb_class_get_under(mrb, data->module, "DoubleArrayTrie"); + break; + case GRN_DB : + klass = mrb_class_get_under(mrb, data->module, "Database"); + break; case GRN_VOID : klass = mrb_class_get_under(mrb, data->module, "Void"); break; @@ -65,8 +194,8 @@ grn_mrb_class_from_grn_obj(mrb_state *mrb, grn_obj *object) if (!klass) { #define BUFFER_SIZE 1024 char buffer[BUFFER_SIZE]; - snprintf(buffer, BUFFER_SIZE - 1, - "can't find class for object type: %#x", object->header.type); + grn_snprintf(buffer, BUFFER_SIZE, BUFFER_SIZE, + "can't find class for object type: %#x", object->header.type); mrb_raise(mrb, E_ARGUMENT_ERROR, buffer); #undef BUFFER_SIZE } @@ -90,4 +219,71 @@ grn_mrb_value_from_grn_obj(mrb_state *mrb, grn_obj *object) mrb_object = mrb_obj_new(mrb, mrb_class, 1, mrb_new_arguments); return mrb_object; } + +grn_id +grn_mrb_class_to_type(mrb_state *mrb, struct RClass *klass) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + grn_id type = GRN_DB_VOID; + + if (klass == mrb->nil_class) { + type = GRN_DB_VOID; + } else if (klass == mrb->true_class || + klass == mrb->false_class) { + type = GRN_DB_BOOL; + } else if (klass == mrb->symbol_class) { + type = GRN_DB_TEXT; + } else if (klass == mrb->fixnum_class) { + type = GRN_DB_INT64; + } else if (klass == mrb->float_class) { + type = GRN_DB_FLOAT; + } else if (klass == mrb->string_class) { + type = GRN_DB_TEXT; + } else if (klass == ctx->impl->mrb.builtin.time_class) { + type = GRN_DB_TIME; + } else { + mrb_raisef(mrb, E_ARGUMENT_ERROR, + "unsupported class: %S", mrb_obj_value(klass)); + } + + return type; +} + +static mrb_value +mrb_grn_converter_singleton_convert(mrb_state *mrb, mrb_value klass) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + grn_obj *from = &(ctx->impl->mrb.buffer.from); + grn_obj *to = &(ctx->impl->mrb.buffer.to); + mrb_value mrb_from; + mrb_value mrb_to_class; + grn_id to_type; + + mrb_get_args(mrb, "oC", &mrb_from, &mrb_to_class); + + grn_mrb_value_to_bulk(mrb, mrb_from, from); + to_type = grn_mrb_class_to_type(mrb, mrb_class_ptr(mrb_to_class)); + if (!grn_mrb_bulk_cast(mrb, from, to, to_type)) { + mrb_raisef(mrb, E_ARGUMENT_ERROR, + "failed to convert to %S: %S", + mrb_to_class, + mrb_from); + } + + return grn_mrb_value_from_bulk(mrb, to); +} + +void +grn_mrb_converter_init(grn_ctx *ctx) +{ + grn_mrb_data *data = &(ctx->impl->mrb); + mrb_state *mrb = data->state; + struct RClass *module; + + module = mrb_define_module_under(mrb, data->module, "Converter"); + + mrb_define_singleton_method(mrb, (struct RObject *)module, "convert", + mrb_grn_converter_singleton_convert, + MRB_ARGS_REQ(2)); +} #endif diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_converter.h b/storage/mroonga/vendor/groonga/lib/mrb/mrb_converter.h index 27223a8c0bf..c2cb58c0e6d 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/mrb_converter.h +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_converter.h @@ -19,16 +19,39 @@ #ifndef GRN_MRB_CONVERTER_H #define GRN_MRB_CONVERTER_H -#include "../ctx.h" +#include "../grn_ctx.h" #ifdef __cplusplus extern "C" { #endif -#ifdef GRN_WITH_MRUBY +void grn_mrb_converter_init(grn_ctx *ctx); + +typedef struct { + grn_obj from; + grn_obj to; + union { + int64_t time_value; + } value; +} grn_mrb_value_to_raw_data_buffer; + +void grn_mrb_value_to_raw_data_buffer_init(mrb_state *mrb, + grn_mrb_value_to_raw_data_buffer *buffer); +void grn_mrb_value_to_raw_data_buffer_fin(mrb_state *mrb, + grn_mrb_value_to_raw_data_buffer *buffer); +void grn_mrb_value_to_raw_data(mrb_state *mrb, + const char *context, + mrb_value mrb_value_, + grn_id domain_id, + grn_mrb_value_to_raw_data_buffer *buffer, + void **raw_value, + unsigned int *raw_value_size); + struct RClass *grn_mrb_class_from_grn_obj(mrb_state *mrb, grn_obj *object); mrb_value grn_mrb_value_from_grn_obj(mrb_state *mrb, grn_obj *object); -#endif + +grn_id grn_mrb_class_to_grn_type(mrb_state *mrb, struct RClass *klass); +grn_id grn_mrb_value_to_grn_type(mrb_state *mrb, mrb_value value); #ifdef __cplusplus } diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_ctx.c b/storage/mroonga/vendor/groonga/lib/mrb/mrb_ctx.c index 3f7bc36202d..3814313f3d0 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/mrb_ctx.c +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_ctx.c @@ -1,6 +1,6 @@ /* -*- c-basic-offset: 2 -*- */ /* - Copyright(C) 2013-2014 Brazil + Copyright(C) 2013-2015 Brazil This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -16,7 +16,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "../ctx_impl.h" +#include "../grn_ctx_impl.h" #ifdef GRN_WITH_MRUBY #include <mruby.h> @@ -25,7 +25,7 @@ #include <mruby/variable.h> #include <mruby/string.h> -#include "../mrb.h" +#include "../grn_mrb.h" #include "mrb_ctx.h" #include "mrb_converter.h" @@ -53,12 +53,26 @@ static mrb_value ctx_array_reference(mrb_state *mrb, mrb_value self) { grn_ctx *ctx = (grn_ctx *)mrb->ud; + mrb_value mrb_id_or_name; grn_obj *object; - char *name; - int name_length; - mrb_get_args(mrb, "s", &name, &name_length); - object = grn_ctx_get(ctx, name, name_length); + mrb_get_args(mrb, "o", &mrb_id_or_name); + + if (mrb_nil_p(mrb_id_or_name)) { + return mrb_nil_value(); + } + + if (mrb_fixnum_p(mrb_id_or_name)) { + grn_id id = mrb_fixnum(mrb_id_or_name); + object = grn_ctx_at(ctx, id); + } else { + mrb_value mrb_name; + mrb_name = mrb_convert_type(mrb, mrb_id_or_name, + MRB_TT_STRING, "String", "to_str"); + object = grn_ctx_get(ctx, + RSTRING_PTR(mrb_name), + RSTRING_LEN(mrb_name)); + } return grn_mrb_value_from_grn_obj(mrb, object); } @@ -187,6 +201,14 @@ ctx_set_error_message(mrb_state *mrb, mrb_value self) return error_message; } +static mrb_value +ctx_get_database(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + + return grn_mrb_value_from_grn_obj(mrb, grn_ctx_db(ctx)); +} + void grn_mrb_ctx_check(mrb_state *mrb) { @@ -202,449 +224,449 @@ grn_mrb_ctx_check(mrb_state *mrb) return; case GRN_END_OF_DATA: error_class = mrb_class_get_under(mrb, module, "EndOfData"); - snprintf(message, MESSAGE_SIZE, - "end of data: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "end of data: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_UNKNOWN_ERROR: error_class = mrb_class_get_under(mrb, module, "UnknownError"); - snprintf(message, MESSAGE_SIZE, - "unknown error: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "unknown error: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_OPERATION_NOT_PERMITTED: error_class = mrb_class_get_under(mrb, module, "OperationNotPermitted"); - snprintf(message, MESSAGE_SIZE, - "operation not permitted: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "operation not permitted: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_NO_SUCH_FILE_OR_DIRECTORY: error_class = mrb_class_get_under(mrb, module, "NoSuchFileOrDirectory"); - snprintf(message, MESSAGE_SIZE, - "no such file or directory: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "no such file or directory: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_NO_SUCH_PROCESS: error_class = mrb_class_get_under(mrb, module, "NoSuchProcess"); - snprintf(message, MESSAGE_SIZE, - "no such process: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "no such process: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_INTERRUPTED_FUNCTION_CALL: error_class = mrb_class_get_under(mrb, module, "InterruptedFunctionCall"); - snprintf(message, MESSAGE_SIZE, - "interrupted function call: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "interrupted function call: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_INPUT_OUTPUT_ERROR: error_class = mrb_class_get_under(mrb, module, "InputOutputError"); - snprintf(message, MESSAGE_SIZE, - "input output error: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "input output error: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_NO_SUCH_DEVICE_OR_ADDRESS: error_class = mrb_class_get_under(mrb, module, "NoSuchDeviceOrAddress"); - snprintf(message, MESSAGE_SIZE, - "no such device or address: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "no such device or address: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_ARG_LIST_TOO_LONG: error_class = mrb_class_get_under(mrb, module, "ArgListTooLong"); - snprintf(message, MESSAGE_SIZE, - "arg list too long: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "arg list too long: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_EXEC_FORMAT_ERROR: error_class = mrb_class_get_under(mrb, module, "ExecFormatError"); - snprintf(message, MESSAGE_SIZE, - "exec format error: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "exec format error: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_BAD_FILE_DESCRIPTOR: error_class = mrb_class_get_under(mrb, module, "BadFileDescriptor"); - snprintf(message, MESSAGE_SIZE, - "bad file descriptor: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "bad file descriptor: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_NO_CHILD_PROCESSES: error_class = mrb_class_get_under(mrb, module, "NoChildProcesses"); - snprintf(message, MESSAGE_SIZE, - "no child processes: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "no child processes: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_RESOURCE_TEMPORARILY_UNAVAILABLE: error_class = mrb_class_get_under(mrb, module, "ResourceTemporarilyUnavailable"); - snprintf(message, MESSAGE_SIZE, - "resource temporarily unavailable: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "resource temporarily unavailable: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_NOT_ENOUGH_SPACE: error_class = mrb_class_get_under(mrb, module, "NotEnoughSpace"); - snprintf(message, MESSAGE_SIZE, - "not enough space: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "not enough space: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_PERMISSION_DENIED: error_class = mrb_class_get_under(mrb, module, "PermissionDenied"); - snprintf(message, MESSAGE_SIZE, - "permission denied: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "permission denied: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_BAD_ADDRESS: error_class = mrb_class_get_under(mrb, module, "BadAddress"); - snprintf(message, MESSAGE_SIZE, - "bad address: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "bad address: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_RESOURCE_BUSY: error_class = mrb_class_get_under(mrb, module, "ResourceBusy"); - snprintf(message, MESSAGE_SIZE, - "resource busy: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "resource busy: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_FILE_EXISTS: error_class = mrb_class_get_under(mrb, module, "FileExists"); - snprintf(message, MESSAGE_SIZE, - "file exists: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "file exists: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_IMPROPER_LINK: error_class = mrb_class_get_under(mrb, module, "ImproperLink"); - snprintf(message, MESSAGE_SIZE, - "improper link: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "improper link: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_NO_SUCH_DEVICE: error_class = mrb_class_get_under(mrb, module, "NoSuchDevice"); - snprintf(message, MESSAGE_SIZE, - "no such device: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "no such device: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_NOT_A_DIRECTORY: error_class = mrb_class_get_under(mrb, module, "NotDirectory"); - snprintf(message, MESSAGE_SIZE, - "not directory: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "not directory: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_IS_A_DIRECTORY: error_class = mrb_class_get_under(mrb, module, "IsDirectory"); - snprintf(message, MESSAGE_SIZE, - "is directory: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "is directory: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_INVALID_ARGUMENT: error_class = mrb_class_get_under(mrb, module, "InvalidArgument"); - snprintf(message, MESSAGE_SIZE, - "invalid argument: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "invalid argument: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_TOO_MANY_OPEN_FILES_IN_SYSTEM: error_class = mrb_class_get_under(mrb, module, "TooManyOpenFilesInSystem"); - snprintf(message, MESSAGE_SIZE, - "too many open files in system: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "too many open files in system: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_TOO_MANY_OPEN_FILES: error_class = mrb_class_get_under(mrb, module, "TooManyOpenFiles"); - snprintf(message, MESSAGE_SIZE, - "too many open files: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "too many open files: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_INAPPROPRIATE_I_O_CONTROL_OPERATION: error_class = mrb_class_get_under(mrb, module, "InappropriateIOControlOperation"); - snprintf(message, MESSAGE_SIZE, - "inappropriate IO control operation: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "inappropriate IO control operation: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_FILE_TOO_LARGE: error_class = mrb_class_get_under(mrb, module, "FileTooLarge"); - snprintf(message, MESSAGE_SIZE, - "file too large: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "file too large: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_NO_SPACE_LEFT_ON_DEVICE: error_class = mrb_class_get_under(mrb, module, "NoSpaceLeftOnDevice"); - snprintf(message, MESSAGE_SIZE, - "no space left on device: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "no space left on device: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_INVALID_SEEK: error_class = mrb_class_get_under(mrb, module, "InvalidSeek"); - snprintf(message, MESSAGE_SIZE, - "invalid seek: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "invalid seek: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_READ_ONLY_FILE_SYSTEM: error_class = mrb_class_get_under(mrb, module, "ReadOnlyFileSystem"); - snprintf(message, MESSAGE_SIZE, - "read only file system: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "read only file system: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_TOO_MANY_LINKS: error_class = mrb_class_get_under(mrb, module, "TooManyLinks"); - snprintf(message, MESSAGE_SIZE, - "too many links: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "too many links: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_BROKEN_PIPE: error_class = mrb_class_get_under(mrb, module, "BrokenPipe"); - snprintf(message, MESSAGE_SIZE, - "broken pipe: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "broken pipe: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_DOMAIN_ERROR: error_class = mrb_class_get_under(mrb, module, "DomainError"); - snprintf(message, MESSAGE_SIZE, - "domain error: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "domain error: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_RESULT_TOO_LARGE: error_class = mrb_class_get_under(mrb, module, "ResultTooLarge"); - snprintf(message, MESSAGE_SIZE, - "result too large: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "result too large: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_RESOURCE_DEADLOCK_AVOIDED: error_class = mrb_class_get_under(mrb, module, "ResourceDeadlockAvoided"); - snprintf(message, MESSAGE_SIZE, - "resource deadlock avoided: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "resource deadlock avoided: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_NO_MEMORY_AVAILABLE: error_class = mrb_class_get_under(mrb, module, "NoMemoryAvailable"); - snprintf(message, MESSAGE_SIZE, - "no memory available: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "no memory available: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_FILENAME_TOO_LONG: error_class = mrb_class_get_under(mrb, module, "FilenameTooLong"); - snprintf(message, MESSAGE_SIZE, - "filename too long: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "filename too long: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_NO_LOCKS_AVAILABLE: error_class = mrb_class_get_under(mrb, module, "NoLocksAvailable"); - snprintf(message, MESSAGE_SIZE, - "no locks available: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "no locks available: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_FUNCTION_NOT_IMPLEMENTED: error_class = mrb_class_get_under(mrb, module, "FunctionNotImplemented"); - snprintf(message, MESSAGE_SIZE, - "function not implemented: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "function not implemented: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_DIRECTORY_NOT_EMPTY: error_class = mrb_class_get_under(mrb, module, "DirectoryNotEmpty"); - snprintf(message, MESSAGE_SIZE, - "directory not empty: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "directory not empty: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_ILLEGAL_BYTE_SEQUENCE: error_class = mrb_class_get_under(mrb, module, "IllegalByteSequence"); - snprintf(message, MESSAGE_SIZE, - "illegal byte sequence: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "illegal byte sequence: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_SOCKET_NOT_INITIALIZED: error_class = mrb_class_get_under(mrb, module, "SocketNotInitialized"); - snprintf(message, MESSAGE_SIZE, - "socket not initialized: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "socket not initialized: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_OPERATION_WOULD_BLOCK: error_class = mrb_class_get_under(mrb, module, "OperationWouldBlock"); - snprintf(message, MESSAGE_SIZE, - "operation would block: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "operation would block: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_ADDRESS_IS_NOT_AVAILABLE: error_class = mrb_class_get_under(mrb, module, "AddressIsNotAvailable"); - snprintf(message, MESSAGE_SIZE, - "address is not available: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "address is not available: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_NETWORK_IS_DOWN: error_class = mrb_class_get_under(mrb, module, "NetworkIsDown"); - snprintf(message, MESSAGE_SIZE, - "network is down: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "network is down: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_NO_BUFFER: error_class = mrb_class_get_under(mrb, module, "NoBuffer"); - snprintf(message, MESSAGE_SIZE, - "no buffer: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "no buffer: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_SOCKET_IS_ALREADY_CONNECTED: error_class = mrb_class_get_under(mrb, module, "SocketIsAlreadyConnected"); - snprintf(message, MESSAGE_SIZE, - "socket is already connected: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "socket is already connected: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_SOCKET_IS_NOT_CONNECTED: error_class = mrb_class_get_under(mrb, module, "SocketIsNotConnected"); - snprintf(message, MESSAGE_SIZE, - "socket is not connected: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "socket is not connected: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_SOCKET_IS_ALREADY_SHUTDOWNED: error_class = mrb_class_get_under(mrb, module, "SocketIsAlreadyShutdowned"); - snprintf(message, MESSAGE_SIZE, - "socket is already shutdowned: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "socket is already shutdowned: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_OPERATION_TIMEOUT: error_class = mrb_class_get_under(mrb, module, "OperationTimeout"); - snprintf(message, MESSAGE_SIZE, - "operation timeout: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "operation timeout: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_CONNECTION_REFUSED: error_class = mrb_class_get_under(mrb, module, "ConnectionRefused"); - snprintf(message, MESSAGE_SIZE, - "connection refused: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "connection refused: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_RANGE_ERROR: error_class = mrb_class_get_under(mrb, module, "RangeError"); - snprintf(message, MESSAGE_SIZE, - "range error: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "range error: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_TOKENIZER_ERROR: error_class = mrb_class_get_under(mrb, module, "TokenizerError"); - snprintf(message, MESSAGE_SIZE, - "tokenizer error: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "tokenizer error: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_FILE_CORRUPT: error_class = mrb_class_get_under(mrb, module, "FileCorrupt"); - snprintf(message, MESSAGE_SIZE, - "file corrupt: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "file corrupt: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_INVALID_FORMAT: error_class = mrb_class_get_under(mrb, module, "InvalidFormat"); - snprintf(message, MESSAGE_SIZE, - "invalid format: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "invalid format: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_OBJECT_CORRUPT: error_class = mrb_class_get_under(mrb, module, "ObjectCorrupt"); - snprintf(message, MESSAGE_SIZE, - "object corrupt: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "object corrupt: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_TOO_MANY_SYMBOLIC_LINKS: error_class = mrb_class_get_under(mrb, module, "TooManySymbolicLinks"); - snprintf(message, MESSAGE_SIZE, - "too many symbolic links: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "too many symbolic links: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_NOT_SOCKET: error_class = mrb_class_get_under(mrb, module, "NotSocket"); - snprintf(message, MESSAGE_SIZE, - "not socket: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "not socket: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_OPERATION_NOT_SUPPORTED: error_class = mrb_class_get_under(mrb, module, "OperationNotSupported"); - snprintf(message, MESSAGE_SIZE, - "operation not supported: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "operation not supported: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_ADDRESS_IS_IN_USE: error_class = mrb_class_get_under(mrb, module, "AddressIsInUse"); - snprintf(message, MESSAGE_SIZE, - "address is in use: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "address is in use: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_ZLIB_ERROR: error_class = mrb_class_get_under(mrb, module, "ZlibError"); - snprintf(message, MESSAGE_SIZE, - "zlib error: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "zlib error: <%s>(%d)", + ctx->errbuf, ctx->rc); break; - case GRN_LZO_ERROR: - error_class = mrb_class_get_under(mrb, module, "LzoError"); - snprintf(message, MESSAGE_SIZE, - "LZO error: <%s>(%d)", - ctx->errbuf, ctx->rc); + case GRN_LZ4_ERROR: + error_class = mrb_class_get_under(mrb, module, "LZ4Error"); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "LZ4 error: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_STACK_OVER_FLOW: error_class = mrb_class_get_under(mrb, module, "StackOverFlow"); - snprintf(message, MESSAGE_SIZE, - "stack over flow: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "stack over flow: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_SYNTAX_ERROR: error_class = mrb_class_get_under(mrb, module, "SyntaxError"); - snprintf(message, MESSAGE_SIZE, - "syntax error: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "syntax error: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_RETRY_MAX: error_class = mrb_class_get_under(mrb, module, "RetryMax"); - snprintf(message, MESSAGE_SIZE, - "retry max: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "retry max: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_INCOMPATIBLE_FILE_FORMAT: error_class = mrb_class_get_under(mrb, module, "IncompatibleFileFormat"); - snprintf(message, MESSAGE_SIZE, - "incompatible file format: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "incompatible file format: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_UPDATE_NOT_ALLOWED: error_class = mrb_class_get_under(mrb, module, "UpdateNotAllowed"); - snprintf(message, MESSAGE_SIZE, - "update not allowed: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "update not allowed: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_TOO_SMALL_OFFSET: error_class = mrb_class_get_under(mrb, module, "TooSmallOffset"); - snprintf(message, MESSAGE_SIZE, - "too small offset: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "too small offset: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_TOO_LARGE_OFFSET: error_class = mrb_class_get_under(mrb, module, "TooLargeOffset"); - snprintf(message, MESSAGE_SIZE, - "too large offset: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "too large offset: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_TOO_SMALL_LIMIT: error_class = mrb_class_get_under(mrb, module, "TooSmallLimit"); - snprintf(message, MESSAGE_SIZE, - "too small limit: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "too small limit: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_CAS_ERROR: error_class = mrb_class_get_under(mrb, module, "CASError"); - snprintf(message, MESSAGE_SIZE, - "CAS error: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "CAS error: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_UNSUPPORTED_COMMAND_VERSION: error_class = mrb_class_get_under(mrb, module, "UnsupportedCommandVersion"); - snprintf(message, MESSAGE_SIZE, - "unsupported command version: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "unsupported command version: <%s>(%d)", + ctx->errbuf, ctx->rc); break; case GRN_NORMALIZER_ERROR: error_class = mrb_class_get_under(mrb, module, "NormalizerError"); - snprintf(message, MESSAGE_SIZE, - "normalizer error: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "normalizer error: <%s>(%d)", + ctx->errbuf, ctx->rc); break; default: error_class = mrb_class_get_under(mrb, module, "Error"); - snprintf(message, MESSAGE_SIZE, - "unsupported error: <%s>(%d)", - ctx->errbuf, ctx->rc); + grn_snprintf(message, MESSAGE_SIZE, MESSAGE_SIZE, + "unsupported error: <%s>(%d)", + ctx->errbuf, ctx->rc); break; } #undef MESSAGE_SIZE @@ -690,8 +712,7 @@ grn_mrb_ctx_init(grn_ctx *ctx) mrb_define_method(mrb, klass, "error_message=", ctx_set_error_message, MRB_ARGS_REQ(1)); - grn_mrb_load(ctx, "context/error_level.rb"); - grn_mrb_load(ctx, "context/rc.rb"); - grn_mrb_load(ctx, "context.rb"); + mrb_define_method(mrb, klass, "database", ctx_get_database, + MRB_ARGS_NONE()); } #endif diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_ctx.h b/storage/mroonga/vendor/groonga/lib/mrb/mrb_ctx.h index 5068d0d75e2..aa527bfec51 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/mrb_ctx.h +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_ctx.h @@ -19,16 +19,14 @@ #ifndef GRN_MRB_CTX_H #define GRN_MRB_CTX_H -#include "../ctx.h" +#include "../grn_ctx.h" #ifdef __cplusplus extern "C" { #endif void grn_mrb_ctx_init(grn_ctx *ctx); -#ifdef GRN_WITH_MRUBY void grn_mrb_ctx_check(mrb_state *mrb); -#endif #ifdef __cplusplus } diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_database.c b/storage/mroonga/vendor/groonga/lib/mrb/mrb_database.c new file mode 100644 index 00000000000..22994104add --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_database.c @@ -0,0 +1,124 @@ +/* -*- 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 "../grn_ctx_impl.h" + +#ifdef GRN_WITH_MRUBY +#include <mruby.h> +#include <mruby/class.h> +#include <mruby/data.h> + +#include "mrb_ctx.h" +#include "mrb_database.h" + +static struct mrb_data_type mrb_grn_database_type = { + "Groonga::Database", + NULL +}; + +static mrb_value +mrb_grn_database_initialize(mrb_state *mrb, mrb_value self) +{ + mrb_value mrb_database_ptr; + + mrb_get_args(mrb, "o", &mrb_database_ptr); + DATA_TYPE(self) = &mrb_grn_database_type; + DATA_PTR(self) = mrb_cptr(mrb_database_ptr); + return self; +} + +static mrb_value +mrb_grn_database_singleton_open(mrb_state *mrb, mrb_value klass) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + grn_obj *database; + char *path; + + mrb_get_args(mrb, "z", &path); + + database = grn_db_open(ctx, path); + grn_mrb_ctx_check(mrb); + + return mrb_funcall(mrb, klass, "new", 1, mrb_cptr_value(mrb, database)); +} + +static mrb_value +mrb_grn_database_singleton_create(mrb_state *mrb, mrb_value klass) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + grn_obj *database; + char *path; + + mrb_get_args(mrb, "z", &path); + + database = grn_db_create(ctx, path, NULL); + grn_mrb_ctx_check(mrb); + + return mrb_funcall(mrb, klass, "new", 1, mrb_cptr_value(mrb, database)); +} + +static mrb_value +mrb_grn_database_recover(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + + grn_db_recover(ctx, DATA_PTR(self)); + grn_mrb_ctx_check(mrb); + + return mrb_nil_value(); +} + +static mrb_value +mrb_grn_database_is_locked(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + unsigned int is_locked; + + is_locked = grn_obj_is_locked(ctx, DATA_PTR(self)); + grn_mrb_ctx_check(mrb); + + return mrb_bool_value(is_locked != 0); +} + +void +grn_mrb_database_init(grn_ctx *ctx) +{ + grn_mrb_data *data = &(ctx->impl->mrb); + mrb_state *mrb = data->state; + struct RClass *module = data->module; + struct RClass *object_class = data->object_class; + struct RClass *klass; + + klass = mrb_define_class_under(mrb, module, "Database", object_class); + MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA); + + mrb_define_singleton_method(mrb, (struct RObject *)klass, "open", + mrb_grn_database_singleton_open, + MRB_ARGS_REQ(1)); + mrb_define_singleton_method(mrb, (struct RObject *)klass, "create", + mrb_grn_database_singleton_create, + MRB_ARGS_REQ(1)); + + mrb_define_method(mrb, klass, "initialize", + mrb_grn_database_initialize, MRB_ARGS_REQ(1)); + mrb_define_method(mrb, klass, "recover", + mrb_grn_database_recover, MRB_ARGS_NONE()); + mrb_define_method(mrb, klass, "locked?", + mrb_grn_database_is_locked, MRB_ARGS_NONE()); +} +#endif diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_database.h b/storage/mroonga/vendor/groonga/lib/mrb/mrb_database.h new file mode 100644 index 00000000000..fd319c55fd1 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_database.h @@ -0,0 +1,34 @@ +/* -*- 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 +*/ + +#ifndef GRN_MRB_DATABASE_H +#define GRN_MRB_DATABASE_H + +#include "../grn_ctx.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void grn_mrb_database_init(grn_ctx *ctx); + +#ifdef __cplusplus +} +#endif + +#endif /* GRN_MRB_DATABASE_H */ diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_double_array_trie.c b/storage/mroonga/vendor/groonga/lib/mrb/mrb_double_array_trie.c new file mode 100644 index 00000000000..5c034090b29 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_double_array_trie.c @@ -0,0 +1,60 @@ +/* -*- 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 "../grn_ctx_impl.h" + +#ifdef GRN_WITH_MRUBY +#include <mruby.h> +#include <mruby/class.h> +#include <mruby/data.h> + +#include "mrb_double_array_trie.h" + +static struct mrb_data_type mrb_grn_double_array_trie_type = { + "Groonga::DoubleArrayTrie", + NULL +}; + +static mrb_value +mrb_grn_double_array_trie_initialize(mrb_state *mrb, mrb_value self) +{ + mrb_value mrb_double_array_trie_ptr; + + mrb_get_args(mrb, "o", &mrb_double_array_trie_ptr); + DATA_TYPE(self) = &mrb_grn_double_array_trie_type; + DATA_PTR(self) = mrb_cptr(mrb_double_array_trie_ptr); + return self; +} + +void +grn_mrb_double_array_trie_init(grn_ctx *ctx) +{ + grn_mrb_data *data = &(ctx->impl->mrb); + mrb_state *mrb = data->state; + struct RClass *module = data->module; + struct RClass *table_class; + struct RClass *klass; + + table_class = mrb_class_get_under(mrb, module, "Table"); + klass = mrb_define_class_under(mrb, module, "DoubleArrayTrie", table_class); + MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA); + + mrb_define_method(mrb, klass, "initialize", + mrb_grn_double_array_trie_initialize, MRB_ARGS_REQ(1)); +} +#endif diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_double_array_trie.h b/storage/mroonga/vendor/groonga/lib/mrb/mrb_double_array_trie.h new file mode 100644 index 00000000000..a69a94e556e --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_double_array_trie.h @@ -0,0 +1,34 @@ +/* -*- 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 +*/ + +#ifndef GRN_MRB_DOUBLE_ARRAY_TRIE_H +#define GRN_MRB_DOUBLE_ARRAY_TRIE_H + +#include "../grn_ctx.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void grn_mrb_double_array_trie_init(grn_ctx *ctx); + +#ifdef __cplusplus +} +#endif + +#endif /* GRN_MRB_DOUBLE_ARRAY_TRIE_H */ diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_error.c b/storage/mroonga/vendor/groonga/lib/mrb/mrb_error.c index 2f45cfc736e..a3513be1118 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/mrb_error.c +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_error.c @@ -16,12 +16,12 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "../ctx_impl.h" +#include "../grn_ctx_impl.h" #ifdef GRN_WITH_MRUBY #include <mruby.h> -#include "../mrb.h" +#include "../grn_mrb.h" #include "mrb_error.h" void @@ -31,155 +31,164 @@ grn_mrb_error_init(grn_ctx *ctx) mrb_state *mrb = data->state; struct RClass *module = data->module; struct RClass *error_class; + struct RClass *groonga_error_class; error_class = mrb_define_class_under(mrb, module, "Error", mrb->eStandardError_class); + groonga_error_class = mrb_define_class_under(mrb, module, "GroongaError", + error_class); mrb_define_class_under(mrb, module, "EndOfData", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "UnknownError", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "OperationNotPermitted", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "NoSuchFileOrDirectory", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "NoSuchProcess", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "InterruptedFunctionCall", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "InputOutputError", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "NoSuchDeviceOrAddress", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "ArgListTooLong", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "ExecFormatError", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "BadFileDescriptor", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "NoChildProcesses", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "ResourceTemporarilyUnavailable", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "NotEnoughSpace", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "PermissionDenied", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "BadAddress", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "ResourceBusy", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "FileExists", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "ImproperLink", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "NoSuchDevice", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "NotDirectory", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "IsDirectory", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "InvalidArgument", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "TooManyOpenFilesInSystem", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "TooManyOpenFiles", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "InappropriateIOControlOperation", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "FileTooLarge", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "NoSpaceLeftOnDevice", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "InvalidSeek", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "ReadOnlyFileSystem", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "TooManyLinks", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "BrokenPipe", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "DomainError", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "ResultTooLarge", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "ResourceDeadlockAvoided", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "NoMemoryAvailable", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "FilenameTooLong", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "NoLocksAvailable", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "FunctionNotImplemented", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "DirectoryNotEmpty", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "IllegalByteSequence", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "SocketNotInitialized", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "OperationWouldBlock", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "AddressIsNotAvailable", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "NetworkIsDown", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "NoBuffer", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "SocketIsAlreadyConnected", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "SocketIsNotConnected", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "SocketIsAlreadyShutdowned", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "OperationTimeout", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "ConnectionRefused", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "RangeError", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "TokenizerError", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "FileCorrupt", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "InvalidFormat", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "ObjectCorrupt", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "TooManySymbolicLinks", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "NotSocket", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "OperationNotSupported", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "AddressIsInUse", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "ZlibError", - error_class); - mrb_define_class_under(mrb, module, "LzoError", - error_class); + groonga_error_class); + mrb_define_class_under(mrb, module, "LZ4Error", + groonga_error_class); mrb_define_class_under(mrb, module, "StackOverFlow", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "SyntaxError", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "RetryMax", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "IncompatibleFileFormat", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "UpdateNotAllowed", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "TooSmallOffset", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "TooLargeOffset", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "TooSmallLimit", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "CASError", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "UnsupportedCommandVersion", - error_class); + groonga_error_class); mrb_define_class_under(mrb, module, "NormalizerError", - error_class); + groonga_error_class); + mrb_define_class_under(mrb, module, "TokenFilterError", + groonga_error_class); + mrb_define_class_under(mrb, module, "CommandError", + groonga_error_class); + mrb_define_class_under(mrb, module, "PluginError", + groonga_error_class); } #endif diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_error.h b/storage/mroonga/vendor/groonga/lib/mrb/mrb_error.h index c59fabc12af..c68c58fa273 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/mrb_error.h +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_error.h @@ -19,7 +19,7 @@ #ifndef GRN_MRB_ERROR_H #define GRN_MRB_ERROR_H -#include "../ctx.h" +#include "../grn_ctx.h" #ifdef __cplusplus extern "C" { diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_expr.c b/storage/mroonga/vendor/groonga/lib/mrb/mrb_expr.c index 3d0499c6b5c..dcae12a131a 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/mrb_expr.c +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_expr.c @@ -1,6 +1,6 @@ /* -*- c-basic-offset: 2 -*- */ /* - Copyright(C) 2013-2014 Brazil + Copyright(C) 2013-2015 Brazil This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -16,7 +16,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "../ctx_impl.h" +#include "../grn_ctx_impl.h" #ifdef GRN_WITH_MRUBY #include <mruby.h> @@ -25,14 +25,17 @@ #include <mruby/data.h> #include <mruby/string.h> #include <mruby/array.h> +#include <mruby/hash.h> -#include "../expr.h" -#include "../util.h" -#include "../mrb.h" +#include "../grn_expr.h" +#include "../grn_util.h" +#include "../grn_mrb.h" #include "mrb_accessor.h" #include "mrb_ctx.h" #include "mrb_expr.h" +#include "mrb_operator.h" #include "mrb_converter.h" +#include "mrb_options.h" static struct mrb_data_type mrb_grn_scan_info_type = { "Groonga::ScanInfo", @@ -98,17 +101,35 @@ mrb_grn_expr_code_initialize(mrb_state *mrb, mrb_value self) static mrb_value mrb_grn_scan_info_put_index(mrb_state *mrb, mrb_value self) { + grn_ctx *ctx = (grn_ctx *)mrb->ud; + scan_info *si; + mrb_value mrb_index; int sid; int32_t weight; - scan_info *si; - grn_ctx *ctx = (grn_ctx *)mrb->ud; + mrb_value mrb_scorer; + mrb_value mrb_scorer_args_expr; + int32_t scorer_args_expr_offset; grn_obj *index; - mrb_value mrb_index; - - mrb_get_args(mrb, "oii", &mrb_index, &sid, &weight); + grn_obj *scorer = NULL; + grn_obj *scorer_args_expr = NULL; + + mrb_get_args(mrb, "oiiooi", + &mrb_index, &sid, &weight, + &mrb_scorer, + &mrb_scorer_args_expr, + &scorer_args_expr_offset); si = DATA_PTR(self); index = DATA_PTR(mrb_index); - grn_scan_info_put_index(ctx, si, index, sid, weight); + if (!mrb_nil_p(mrb_scorer)) { + scorer = DATA_PTR(mrb_scorer); + } + if (!mrb_nil_p(mrb_scorer_args_expr)) { + scorer_args_expr = DATA_PTR(mrb_scorer_args_expr); + } + grn_scan_info_put_index(ctx, si, index, sid, weight, + scorer, + scorer_args_expr, + scorer_args_expr_offset); return self; } @@ -120,17 +141,19 @@ mrb_grn_scan_info_get_op(mrb_state *mrb, mrb_value self) si = DATA_PTR(self); op = grn_scan_info_get_op(si); - return mrb_fixnum_value(op); + return grn_mrb_value_from_operator(mrb, op); } static mrb_value mrb_grn_scan_info_set_op(mrb_state *mrb, mrb_value self) { scan_info *si; + mrb_value mrb_op; grn_operator op; - mrb_get_args(mrb, "i", &op); + mrb_get_args(mrb, "o", &mrb_op, &op); si = DATA_PTR(self); + op = grn_mrb_value_to_operator(mrb, mrb_op); grn_scan_info_set_op(si, op); return self; } @@ -190,10 +213,12 @@ static mrb_value mrb_grn_scan_info_set_logical_op(mrb_state *mrb, mrb_value self) { scan_info *si; + mrb_value mrb_logical_op; grn_operator logical_op; - mrb_get_args(mrb, "i", &logical_op); + mrb_get_args(mrb, "o", &mrb_logical_op); si = DATA_PTR(self); + logical_op = grn_mrb_value_to_operator(mrb, mrb_logical_op); grn_scan_info_set_logical_op(si, logical_op); return self; } @@ -206,7 +231,7 @@ mrb_grn_scan_info_get_logical_op(mrb_state *mrb, mrb_value self) si = DATA_PTR(self); logical_op = grn_scan_info_get_logical_op(si); - return mrb_fixnum_value(logical_op); + return grn_mrb_value_from_operator(mrb, logical_op); } static mrb_value @@ -287,11 +312,86 @@ mrb_grn_scan_info_push_arg(mrb_state *mrb, mrb_value self) } static mrb_value +mrb_grn_expr_code_inspect(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + grn_expr_code *code; + mrb_value inspected; + + code = DATA_PTR(self); + + inspected = mrb_str_buf_new(mrb, 48); + + mrb_str_cat_lit(mrb, inspected, "#<"); + mrb_str_cat_cstr(mrb, inspected, mrb_obj_classname(mrb, self)); + mrb_str_cat_lit(mrb, inspected, ":"); + mrb_str_concat(mrb, inspected, mrb_ptr_to_str(mrb, mrb_cptr(self))); + + { + int32_t weight; + uint32_t offset; + + weight = grn_expr_code_get_weight(ctx, DATA_PTR(self), &offset); + + mrb_str_cat_lit(mrb, inspected, " weight="); + mrb_str_concat(mrb, inspected, + mrb_funcall(mrb, + mrb_fixnum_value(weight), + "inspect", + 0)); + mrb_str_cat_lit(mrb, inspected, ", offset="); + mrb_str_concat(mrb, inspected, + mrb_funcall(mrb, + mrb_fixnum_value(offset), + "inspect", + 0)); + } + + mrb_str_cat_lit(mrb, inspected, ", modify="); + mrb_str_concat(mrb, inspected, + mrb_funcall(mrb, + mrb_fixnum_value(code->modify), + "inspect", + 0)); + + mrb_str_cat_lit(mrb, inspected, ", op="); + mrb_str_concat(mrb, inspected, + mrb_funcall(mrb, + grn_mrb_value_from_operator(mrb, code->op), + "inspect", + 0)); + + mrb_str_cat_lit(mrb, inspected, ", flags="); + mrb_str_concat(mrb, inspected, + mrb_funcall(mrb, + mrb_fixnum_value(code->flags), + "inspect", + 0)); + + mrb_str_cat_lit(mrb, inspected, ", value="); + mrb_str_concat(mrb, inspected, + mrb_funcall(mrb, + grn_mrb_value_from_grn_obj(mrb, code->value), + "inspect", + 0)); + + mrb_str_cat_lit(mrb, inspected, ">"); + + return inspected; +} + +static mrb_value mrb_grn_expr_code_get_weight(mrb_state *mrb, mrb_value self) { grn_ctx *ctx = (grn_ctx *)mrb->ud; + int32_t weight; + uint32_t offset; + mrb_value mrb_values[2]; - return mrb_fixnum_value(grn_expr_code_get_weight(ctx, DATA_PTR(self))); + weight = grn_expr_code_get_weight(ctx, DATA_PTR(self), &offset); + mrb_values[0] = mrb_fixnum_value(weight); + mrb_values[1] = mrb_fixnum_value(offset); + return mrb_ary_new_from_values(mrb, 2, mrb_values); } static mrb_value @@ -309,7 +409,7 @@ mrb_grn_expr_code_get_op(mrb_state *mrb, mrb_value self) grn_expr_code *expr_code; expr_code = DATA_PTR(self); - return mrb_fixnum_value(expr_code->op); + return grn_mrb_value_from_operator(mrb, expr_code->op); } static mrb_value @@ -322,6 +422,41 @@ mrb_grn_expr_code_get_flags(mrb_state *mrb, mrb_value self) } static mrb_value +mrb_grn_expression_singleton_create(mrb_state *mrb, mrb_value klass) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + mrb_value mrb_expr; + mrb_value mrb_table; + mrb_value mrb_new_arguments[1]; + grn_obj *expr, *variable = NULL; + + mrb_get_args(mrb, "o", &mrb_table); + if (mrb_nil_p(mrb_table)) { + expr = grn_expr_create(ctx, NULL, 0); + } else { + grn_obj *table = DATA_PTR(mrb_table); + GRN_EXPR_CREATE_FOR_QUERY(ctx, table, expr, variable); + } + + if (!expr) { + grn_mrb_ctx_check(mrb); + return mrb_nil_value(); + } + + mrb_new_arguments[0] = mrb_cptr_value(mrb, expr); + mrb_expr = mrb_obj_new(mrb, mrb_class_ptr(klass), 1, mrb_new_arguments); + { + mrb_value mrb_variable = mrb_nil_value(); + if (variable) { + mrb_variable = grn_mrb_value_from_grn_obj(mrb, variable); + } + mrb_iv_set(mrb, mrb_expr, mrb_intern_lit(mrb, "@variable"), mrb_variable); + } + + return mrb_expr; +} + +static mrb_value mrb_grn_expression_initialize(mrb_state *mrb, mrb_value self) { mrb_value mrb_expression_ptr; @@ -409,6 +544,175 @@ mrb_grn_expression_allocate_constant(mrb_state *mrb, mrb_value self) return grn_mrb_value_from_grn_obj(mrb, grn_object); } +static mrb_value +mrb_grn_expression_parse(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + grn_obj *expr; + char *query; + mrb_int query_size; + grn_obj *default_column = NULL; + grn_operator default_mode = GRN_OP_MATCH; + grn_operator default_operator = GRN_OP_AND; + grn_expr_flags flags = GRN_EXPR_SYNTAX_SCRIPT; + mrb_value mrb_options = mrb_nil_value(); + + expr = DATA_PTR(self); + mrb_get_args(mrb, "s|H", &query, &query_size, &mrb_options); + + if (!mrb_nil_p(mrb_options)) { + mrb_value mrb_flags; + + mrb_flags = grn_mrb_options_get_lit(mrb, mrb_options, "flags"); + if (!mrb_nil_p(mrb_flags)) { + flags = mrb_fixnum(mrb_flags); + } + } + + grn_expr_parse(ctx, expr, query, query_size, default_column, + default_mode, default_operator, flags); + grn_mrb_ctx_check(mrb); + + return mrb_nil_value(); +} + +static mrb_value +mrb_grn_expression_append_object(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + grn_obj *expr; + mrb_value mrb_object; + grn_obj *object; + mrb_value mrb_op; + grn_operator op; + int n_args; + + expr = DATA_PTR(self); + mrb_get_args(mrb, "ooi", &mrb_object, &mrb_op, &n_args); + + object = DATA_PTR(mrb_object); + op = grn_mrb_value_to_operator(mrb, mrb_op); + grn_expr_append_obj(ctx, expr, object, op, n_args); + grn_mrb_ctx_check(mrb); + + return mrb_nil_value(); +} + +static mrb_value +mrb_grn_expression_append_constant(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + grn_obj *expr; + mrb_value mrb_constant; + mrb_value mrb_op; + grn_operator op; + int n_args; + + expr = DATA_PTR(self); + mrb_get_args(mrb, "ooi", &mrb_constant, &mrb_op, &n_args); + + op = grn_mrb_value_to_operator(mrb, mrb_op); + switch (mrb_type(mrb_constant)) { + case MRB_TT_FALSE : + if (mrb_nil_p(mrb_constant)) { + grn_obj constant; + GRN_VOID_INIT(&constant); + grn_expr_append_const(ctx, expr, &constant, op, n_args); + GRN_OBJ_FIN(ctx, &constant); + } else { + grn_obj constant; + GRN_BOOL_INIT(&constant, 0); + GRN_BOOL_SET(ctx, &constant, GRN_FALSE); + grn_expr_append_const(ctx, expr, &constant, op, n_args); + GRN_OBJ_FIN(ctx, &constant); + } + break; + case MRB_TT_TRUE : + { + grn_obj constant; + GRN_BOOL_INIT(&constant, 0); + GRN_BOOL_SET(ctx, &constant, GRN_TRUE); + grn_expr_append_const(ctx, expr, &constant, op, n_args); + GRN_OBJ_FIN(ctx, &constant); + } + break; + case MRB_TT_FIXNUM : + grn_expr_append_const_int(ctx, expr, mrb_fixnum(mrb_constant), op, n_args); + break; + case MRB_TT_SYMBOL : + { + const char *value; + mrb_int value_length; + + value = mrb_sym2name_len(mrb, mrb_symbol(mrb_constant), &value_length); + grn_expr_append_const_str(ctx, expr, value, value_length, op, n_args); + } + break; + case MRB_TT_FLOAT : + { + grn_obj constant; + GRN_FLOAT_INIT(&constant, 0); + GRN_FLOAT_SET(ctx, &constant, mrb_float(mrb_constant)); + grn_expr_append_const(ctx, expr, &constant, op, n_args); + GRN_OBJ_FIN(ctx, &constant); + } + break; + case MRB_TT_STRING : + grn_expr_append_const_str(ctx, expr, + RSTRING_PTR(mrb_constant), + RSTRING_LEN(mrb_constant), + op, n_args); + break; + default : + { + struct RClass *klass; + + klass = mrb_class(mrb, mrb_constant); + if (klass == ctx->impl->mrb.builtin.time_class) { + grn_obj constant; + mrb_value mrb_sec; + mrb_value mrb_usec; + + mrb_sec = mrb_funcall(mrb, mrb_constant, "to_i", 0); + mrb_usec = mrb_funcall(mrb, mrb_constant, "usec", 0); + GRN_TIME_INIT(&constant, 0); + GRN_TIME_SET(ctx, &constant, + GRN_TIME_PACK(mrb_fixnum(mrb_sec), mrb_fixnum(mrb_usec))); + grn_expr_append_const(ctx, expr, &constant, op, n_args); + GRN_OBJ_FIN(ctx, &constant); + } else { + mrb_raisef(mrb, E_ARGUMENT_ERROR, + "unsupported constant to append to expression: %S", + mrb_constant); + } + } + break; + } + + grn_mrb_ctx_check(mrb); + + return mrb_nil_value(); +} + +static mrb_value +mrb_grn_expression_append_operator(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + grn_obj *expr; + mrb_value mrb_op; + int n_args; + grn_operator op; + + expr = DATA_PTR(self); + mrb_get_args(mrb, "oi", &mrb_op, &n_args); + + op = grn_mrb_value_to_operator(mrb, mrb_op); + grn_expr_append_op(ctx, expr, op, n_args); + grn_mrb_ctx_check(mrb); + + return mrb_nil_value(); +} + void grn_mrb_expr_init(grn_ctx *ctx) { @@ -422,7 +726,7 @@ grn_mrb_expr_init(grn_ctx *ctx) mrb_define_method(mrb, klass, "initialize", mrb_grn_scan_info_initialize, MRB_ARGS_REQ(1)); mrb_define_method(mrb, klass, "put_index", - mrb_grn_scan_info_put_index, MRB_ARGS_REQ(3)); + mrb_grn_scan_info_put_index, MRB_ARGS_REQ(6)); mrb_define_method(mrb, klass, "op", mrb_grn_scan_info_get_op, MRB_ARGS_NONE()); mrb_define_method(mrb, klass, "op=", @@ -457,6 +761,8 @@ grn_mrb_expr_init(grn_ctx *ctx) MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA); mrb_define_method(mrb, klass, "initialize", mrb_grn_expr_code_initialize, MRB_ARGS_REQ(1)); + mrb_define_method(mrb, klass, "inspect", + mrb_grn_expr_code_inspect, MRB_ARGS_NONE()); mrb_define_method(mrb, klass, "weight", mrb_grn_expr_code_get_weight, MRB_ARGS_NONE()); mrb_define_method(mrb, klass, "value", @@ -466,8 +772,21 @@ grn_mrb_expr_init(grn_ctx *ctx) mrb_define_method(mrb, klass, "flags", mrb_grn_expr_code_get_flags, MRB_ARGS_NONE()); + { + struct RClass *expression_code_class = klass; + struct RClass *flags_module; + flags_module = mrb_define_module_under(mrb, expression_code_class, "Flags"); + mrb_define_const(mrb, flags_module, "RELATIONAL_EXPRESSION", + mrb_fixnum_value(GRN_EXPR_CODE_RELATIONAL_EXPRESSION)); + } + klass = mrb_define_class_under(mrb, module, "Expression", object_class); MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA); + + mrb_define_singleton_method(mrb, (struct RObject *)klass, "create", + mrb_grn_expression_singleton_create, + MRB_ARGS_REQ(1)); + mrb_define_method(mrb, klass, "initialize", mrb_grn_expression_initialize, MRB_ARGS_REQ(1)); mrb_define_method(mrb, klass, "codes", @@ -479,10 +798,15 @@ grn_mrb_expr_init(grn_ctx *ctx) mrb_define_method(mrb, klass, "allocate_constant", mrb_grn_expression_allocate_constant, MRB_ARGS_REQ(1)); - grn_mrb_load(ctx, "expression.rb"); - grn_mrb_load(ctx, "scan_info.rb"); - grn_mrb_load(ctx, "scan_info_data.rb"); - grn_mrb_load(ctx, "scan_info_builder.rb"); + mrb_define_method(mrb, klass, "parse", + mrb_grn_expression_parse, MRB_ARGS_ARG(1, 1)); + + mrb_define_method(mrb, klass, "append_object", + mrb_grn_expression_append_object, MRB_ARGS_REQ(2)); + mrb_define_method(mrb, klass, "append_constant", + mrb_grn_expression_append_constant, MRB_ARGS_REQ(3)); + mrb_define_method(mrb, klass, "append_operator", + mrb_grn_expression_append_operator, MRB_ARGS_REQ(2)); } scan_info ** @@ -501,7 +825,7 @@ grn_mrb_scan_info_build(grn_ctx *ctx, grn_obj *expr, int *n, mrb_expression = grn_mrb_value_from_grn_obj(mrb, expr); mrb_sis = mrb_funcall(mrb, mrb_expression, "build_scan_info", 2, - mrb_fixnum_value(op), + grn_mrb_value_from_operator(mrb, op), mrb_fixnum_value(size)); if (mrb_nil_p(mrb_sis)) { @@ -535,4 +859,31 @@ exit: return sis; } + +unsigned int +grn_mrb_expr_estimate_size(grn_ctx *ctx, grn_obj *expr, grn_obj *table) +{ + grn_mrb_data *data = &(ctx->impl->mrb); + mrb_state *mrb = data->state; + mrb_value mrb_expression; + mrb_value mrb_table; + mrb_value mrb_size; + unsigned int size; + int arena_index; + + arena_index = mrb_gc_arena_save(mrb); + + mrb_expression = grn_mrb_value_from_grn_obj(mrb, expr); + mrb_table = grn_mrb_value_from_grn_obj(mrb, table); + mrb_size = mrb_funcall(mrb, mrb_expression, "estimate_size", 1, mrb_table); + if (mrb->exc) { + size = grn_table_size(ctx, table); + } else { + size = mrb_fixnum(mrb_size); + } + + mrb_gc_arena_restore(mrb, arena_index); + + return size; +} #endif diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_expr.h b/storage/mroonga/vendor/groonga/lib/mrb/mrb_expr.h index 7fd5c66e9c5..0564401dac9 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/mrb_expr.h +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_expr.h @@ -1,6 +1,6 @@ /* -*- c-basic-offset: 2 -*- */ /* - Copyright(C) 2013 Brazil + Copyright(C) 2013-2015 Brazil This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -19,8 +19,8 @@ #ifndef GRN_MRB_EXPR_H #define GRN_MRB_EXPR_H -#include "../ctx.h" -#include "../expr.h" +#include "../grn_ctx.h" +#include "../grn_expr.h" #ifdef __cplusplus extern "C" { @@ -28,6 +28,9 @@ extern "C" { void grn_mrb_expr_init(grn_ctx *ctx); scan_info **grn_mrb_scan_info_build(grn_ctx *ctx, grn_obj *expr, int *n, grn_operator op, uint32_t size); +unsigned int grn_mrb_expr_estimate_size(grn_ctx *ctx, + grn_obj *expr, + grn_obj *table); #ifdef __cplusplus } diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_fixed_size_column.c b/storage/mroonga/vendor/groonga/lib/mrb/mrb_fixed_size_column.c index d8df913f77d..130225029a2 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/mrb_fixed_size_column.c +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_fixed_size_column.c @@ -16,7 +16,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "../ctx_impl.h" +#include "../grn_ctx_impl.h" #ifdef GRN_WITH_MRUBY #include <mruby.h> diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_fixed_size_column.h b/storage/mroonga/vendor/groonga/lib/mrb/mrb_fixed_size_column.h index 938dcd76c7a..1037716c9f7 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/mrb_fixed_size_column.h +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_fixed_size_column.h @@ -19,7 +19,7 @@ #ifndef GRN_MRB_FIXED_SIZE_COLUMN_H #define GRN_MRB_FIXED_SIZE_COLUMN_H -#include "../ctx.h" +#include "../grn_ctx.h" #ifdef __cplusplus extern "C" { diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_hash_table.c b/storage/mroonga/vendor/groonga/lib/mrb/mrb_hash_table.c new file mode 100644 index 00000000000..31d00956137 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_hash_table.c @@ -0,0 +1,117 @@ +/* -*- c-basic-offset: 2 -*- */ +/* + Copyright(C) 2014-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" + +#ifdef GRN_WITH_MRUBY +#include <mruby.h> +#include <mruby/class.h> +#include <mruby/data.h> +#include <mruby/string.h> + +#include "mrb_ctx.h" +#include "mrb_hash_table.h" +#include "mrb_options.h" + +static struct mrb_data_type mrb_grn_hash_table_type = { + "Groonga::HashTable", + NULL +}; + +static mrb_value +mrb_grn_hash_table_singleton_create(mrb_state *mrb, mrb_value klass) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + mrb_value mrb_options = mrb_nil_value(); + const char *name = NULL; + unsigned int name_size = 0; + const char *path = NULL; + grn_obj_flags flags = GRN_OBJ_TABLE_HASH_KEY; + grn_obj *key_type = NULL; + grn_obj *value_type = NULL; + grn_obj *table; + + mrb_get_args(mrb, "|H", &mrb_options); + + if (!mrb_nil_p(mrb_options)) { + mrb_value mrb_name; + mrb_value mrb_flags; + mrb_value mrb_key_type; + mrb_value mrb_value_type; + + mrb_name = grn_mrb_options_get_lit(mrb, mrb_options, "name"); + if (!mrb_nil_p(mrb_name)) { + name = RSTRING_PTR(mrb_name); + name_size = RSTRING_LEN(mrb_name); + } + + mrb_flags = grn_mrb_options_get_lit(mrb, mrb_options, "flags"); + if (!mrb_nil_p(mrb_flags)) { + flags |= mrb_fixnum(mrb_flags); + } + + mrb_key_type = grn_mrb_options_get_lit(mrb, mrb_options, "key_type"); + if (!mrb_nil_p(mrb_key_type)) { + key_type = DATA_PTR(mrb_key_type); + } + + mrb_value_type = grn_mrb_options_get_lit(mrb, mrb_options, "value_type"); + if (!mrb_nil_p(mrb_value_type)) { + key_type = DATA_PTR(mrb_value_type); + } + } + + table = grn_table_create(ctx, name, name_size, path, flags, + key_type, value_type); + grn_mrb_ctx_check(mrb); + + return mrb_funcall(mrb, klass, "new", 1, mrb_cptr_value(mrb, table)); +} + +static mrb_value +mrb_grn_hash_table_initialize(mrb_state *mrb, mrb_value self) +{ + mrb_value mrb_hash_table_ptr; + + mrb_get_args(mrb, "o", &mrb_hash_table_ptr); + DATA_TYPE(self) = &mrb_grn_hash_table_type; + DATA_PTR(self) = mrb_cptr(mrb_hash_table_ptr); + return self; +} + +void +grn_mrb_hash_table_init(grn_ctx *ctx) +{ + grn_mrb_data *data = &(ctx->impl->mrb); + mrb_state *mrb = data->state; + struct RClass *module = data->module; + struct RClass *table_class; + struct RClass *klass; + + table_class = mrb_class_get_under(mrb, module, "Table"); + klass = mrb_define_class_under(mrb, module, "HashTable", table_class); + MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA); + + mrb_define_singleton_method(mrb, (struct RObject *)klass, "create", + mrb_grn_hash_table_singleton_create, + MRB_ARGS_OPT(1)); + + mrb_define_method(mrb, klass, "initialize", + mrb_grn_hash_table_initialize, MRB_ARGS_REQ(1)); +} +#endif diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_hash_table.h b/storage/mroonga/vendor/groonga/lib/mrb/mrb_hash_table.h new file mode 100644 index 00000000000..223267b47d9 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_hash_table.h @@ -0,0 +1,34 @@ +/* -*- 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 +*/ + +#ifndef GRN_MRB_HASH_TABLE_H +#define GRN_MRB_HASH_TABLE_H + +#include "../grn_ctx.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void grn_mrb_hash_table_init(grn_ctx *ctx); + +#ifdef __cplusplus +} +#endif + +#endif /* GRN_MRB_HASH_TABLE_H */ diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_id.c b/storage/mroonga/vendor/groonga/lib/mrb/mrb_id.c index aa907b84a43..f55960716e9 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/mrb_id.c +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_id.c @@ -16,7 +16,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "../ctx_impl.h" +#include "../grn_ctx_impl.h" #ifdef GRN_WITH_MRUBY #include <mruby.h> @@ -32,6 +32,11 @@ grn_mrb_id_init(grn_ctx *ctx) id_module = mrb_define_module_under(mrb, module, "ID"); + mrb_define_const(mrb, id_module, "NIL", + mrb_fixnum_value(GRN_ID_NIL)); + mrb_define_const(mrb, id_module, "MAX", + mrb_fixnum_value(GRN_ID_MAX)); + mrb_define_const(mrb, id_module, "VOID", mrb_fixnum_value(GRN_DB_VOID)); mrb_define_const(mrb, id_module, "DB", diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_id.h b/storage/mroonga/vendor/groonga/lib/mrb/mrb_id.h index f5b44358143..fcfd80384b5 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/mrb_id.h +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_id.h @@ -19,7 +19,7 @@ #ifndef GRN_MRB_ID_H #define GRN_MRB_ID_H -#include "../ctx.h" +#include "../grn_ctx.h" #ifdef __cplusplus extern "C" { diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_index_column.c b/storage/mroonga/vendor/groonga/lib/mrb/mrb_index_column.c index 7bdf7528fc7..eb85ff955fa 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/mrb_index_column.c +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_index_column.c @@ -1,6 +1,6 @@ /* -*- c-basic-offset: 2 -*- */ /* - Copyright(C) 2014 Brazil + Copyright(C) 2014-2015 Brazil This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -16,14 +16,18 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "../ctx_impl.h" +#include "../grn_ctx_impl.h" +#include "../grn_ii.h" #ifdef GRN_WITH_MRUBY #include <mruby.h> #include <mruby/class.h> #include <mruby/data.h> +#include "mrb_converter.h" #include "mrb_index_column.h" +#include "mrb_operator.h" +#include "mrb_options.h" static struct mrb_data_type mrb_grn_index_column_type = { "Groonga::IndexColumn", @@ -41,6 +45,84 @@ mrb_grn_index_column_initialize(mrb_state *mrb, mrb_value self) return self; } +static mrb_value +mrb_grn_index_column_get_lexicon(mrb_state *mrb, mrb_value self) +{ + grn_obj *index_column; + grn_obj *lexicon; + + index_column = DATA_PTR(self); + lexicon = ((grn_ii *)index_column)->lexicon; + + return grn_mrb_value_from_grn_obj(mrb, lexicon); +} + +static mrb_value +mrb_grn_index_column_estimate_size_for_term_id(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + grn_obj *index_column; + mrb_int term_id; + unsigned int size; + + index_column = DATA_PTR(self); + mrb_get_args(mrb, "i", &term_id); + + size = grn_ii_estimate_size(ctx, (grn_ii *)index_column, term_id); + return mrb_fixnum_value(size); +} + +static mrb_value +mrb_grn_index_column_estimate_size_for_query(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + grn_obj *index_column; + char *query; + mrb_int query_len; + mrb_value mrb_options = mrb_nil_value(); + grn_search_optarg optarg; + unsigned int size; + + index_column = DATA_PTR(self); + mrb_get_args(mrb, "s|H", &query, &query_len, &mrb_options); + + memset(&optarg, 0, sizeof(grn_search_optarg)); + optarg.mode = GRN_OP_EXACT; + + if (!mrb_nil_p(mrb_options)) { + mrb_value mrb_mode; + + mrb_mode = grn_mrb_options_get_lit(mrb, mrb_options, "mode"); + if (!mrb_nil_p(mrb_mode)) { + optarg.mode = grn_mrb_value_to_operator(mrb, mrb_mode); + } + } + + size = grn_ii_estimate_size_for_query(ctx, (grn_ii *)index_column, + query, query_len, &optarg); + return mrb_fixnum_value(size); +} + +static mrb_value +mrb_grn_index_column_estimate_size_for_lexicon_cursor(mrb_state *mrb, + mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + grn_obj *index_column; + mrb_value mrb_lexicon_cursor; + grn_table_cursor *lexicon_cursor; + unsigned int size; + + index_column = DATA_PTR(self); + mrb_get_args(mrb, "o", &mrb_lexicon_cursor); + + lexicon_cursor = DATA_PTR(mrb_lexicon_cursor); + size = grn_ii_estimate_size_for_lexicon_cursor(ctx, + (grn_ii *)index_column, + lexicon_cursor); + return mrb_fixnum_value(size); +} + void grn_mrb_index_column_init(grn_ctx *ctx) { @@ -55,5 +137,19 @@ grn_mrb_index_column_init(grn_ctx *ctx) MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA); mrb_define_method(mrb, klass, "initialize", mrb_grn_index_column_initialize, MRB_ARGS_REQ(1)); + + mrb_define_method(mrb, klass, "lexicon", + mrb_grn_index_column_get_lexicon, + MRB_ARGS_NONE()); + + mrb_define_method(mrb, klass, "estimate_size_for_term_id", + mrb_grn_index_column_estimate_size_for_term_id, + MRB_ARGS_REQ(1)); + mrb_define_method(mrb, klass, "estimate_size_for_query", + mrb_grn_index_column_estimate_size_for_query, + MRB_ARGS_ARG(1, 1)); + mrb_define_method(mrb, klass, "estimate_size_for_lexicon_cursor", + mrb_grn_index_column_estimate_size_for_lexicon_cursor, + MRB_ARGS_REQ(1)); } #endif diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_index_column.h b/storage/mroonga/vendor/groonga/lib/mrb/mrb_index_column.h index 10e0589bf93..307448fd4c3 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/mrb_index_column.h +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_index_column.h @@ -19,7 +19,7 @@ #ifndef GRN_MRB_INDEX_COLUMN_H #define GRN_MRB_INDEX_COLUMN_H -#include "../ctx.h" +#include "../grn_ctx.h" #ifdef __cplusplus extern "C" { diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_index_cursor.c b/storage/mroonga/vendor/groonga/lib/mrb/mrb_index_cursor.c new file mode 100644 index 00000000000..34133ff7ae0 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_index_cursor.c @@ -0,0 +1,224 @@ +/* -*- 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_ii.h" +#include "../grn_db.h" + +#ifdef GRN_WITH_MRUBY +#include <mruby.h> +#include <mruby/class.h> +#include <mruby/data.h> +#include <mruby/string.h> +#include <mruby/hash.h> +#include <mruby/variable.h> + +#include "mrb_ctx.h" +#include "mrb_index_cursor.h" +#include "mrb_converter.h" +#include "mrb_options.h" + +static struct mrb_data_type mrb_grn_index_cursor_type = { + "Groonga::IndexCursor", + NULL +}; + +static mrb_value +mrb_grn_index_cursor_singleton_open_raw(mrb_state *mrb, mrb_value klass) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + mrb_value mrb_table_cursor; + mrb_value mrb_index; + mrb_value mrb_options = mrb_nil_value(); + grn_obj *index_cursor; + grn_table_cursor *table_cursor; + grn_obj *index; + grn_id rid_min = GRN_ID_NIL; + grn_id rid_max = GRN_ID_MAX; + int flags = 0; + mrb_value mrb_index_cursor; + + mrb_get_args(mrb, "oo|H", &mrb_table_cursor, &mrb_index, &mrb_options); + + table_cursor = DATA_PTR(mrb_table_cursor); + index = DATA_PTR(mrb_index); + if (!mrb_nil_p(mrb_options)) { + /* TODO */ + } + index_cursor = grn_index_cursor_open(ctx, table_cursor, index, + rid_min, rid_max, flags); + grn_mrb_ctx_check(mrb); + + mrb_index_cursor = mrb_funcall(mrb, klass, "new", 1, + mrb_cptr_value(mrb, index_cursor)); + mrb_iv_set(mrb, mrb_index_cursor, mrb_intern_lit(mrb, "@index"), mrb_index); + return mrb_index_cursor; +} + +static mrb_value +mrb_grn_index_cursor_initialize(mrb_state *mrb, mrb_value self) +{ + mrb_value mrb_index_cursor_ptr; + + mrb_get_args(mrb, "o", &mrb_index_cursor_ptr); + DATA_TYPE(self) = &mrb_grn_index_cursor_type; + DATA_PTR(self) = mrb_cptr(mrb_index_cursor_ptr); + + return self; +} + +static mrb_value +mrb_grn_index_cursor_close(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + grn_obj *index_cursor; + + index_cursor = DATA_PTR(self); + if (index_cursor) { + DATA_PTR(self) = NULL; + grn_obj_close(ctx, index_cursor); + grn_mrb_ctx_check(mrb); + } + + return mrb_nil_value(); +} + +static mrb_value +mrb_grn_index_cursor_count(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + grn_id term_id; + int n_records = 0; + + while (grn_index_cursor_next(ctx, DATA_PTR(self), &term_id)) { + n_records++; + } + + return mrb_fixnum_value(n_records); +} + +static mrb_value +mrb_grn_index_cursor_select(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + mrb_value mrb_result_set; + mrb_value mrb_options; + grn_obj *index_cursor; + grn_obj *expr = NULL; + grn_obj *expr_variable = NULL; + int offset = 0; + int limit = 10; + int n_matched_records = 0; + mrb_value mrb_index; + grn_obj *index; + grn_obj *lexicon; + grn_obj *data_table; + grn_hash *result_set; + grn_posting *posting; + grn_id term_id; + grn_operator op = GRN_OP_OR; + + mrb_get_args(mrb, "o|H", &mrb_result_set, &mrb_options); + + index_cursor = DATA_PTR(self); + result_set = DATA_PTR(mrb_result_set); + + if (!mrb_nil_p(mrb_options)) { + mrb_value mrb_expr; + mrb_value mrb_offset; + mrb_value mrb_limit; + + mrb_expr = grn_mrb_options_get_lit(mrb, mrb_options, "expression"); + if (!mrb_nil_p(mrb_expr)) { + expr = DATA_PTR(mrb_expr); + expr_variable = grn_expr_get_var_by_offset(ctx, expr, 0); + } + + mrb_offset = grn_mrb_options_get_lit(mrb, mrb_options, "offset"); + if (!mrb_nil_p(mrb_offset)) { + offset = mrb_fixnum(mrb_offset); + } + + mrb_limit = grn_mrb_options_get_lit(mrb, mrb_options, "limit"); + if (!mrb_nil_p(mrb_limit)) { + limit = mrb_fixnum(mrb_limit); + } + } + + if (limit <= 0) { + return mrb_fixnum_value(n_matched_records); + } + + mrb_index = mrb_iv_get(mrb, self, mrb_intern_lit(mrb, "@index")); + index = DATA_PTR(mrb_index); + lexicon = ((grn_ii *)index)->lexicon; + data_table = grn_ctx_at(ctx, grn_obj_get_range(ctx, index)); + + while ((posting = grn_index_cursor_next(ctx, index_cursor, &term_id))) { + if (expr) { + grn_bool matched_raw; + grn_obj *matched; + + GRN_RECORD_SET(ctx, expr_variable, posting->rid); + matched = grn_expr_exec(ctx, expr, 0); + GRN_TRUEP(ctx, matched, matched_raw); + if (!matched_raw) { + continue; + } + } + n_matched_records++; + if (offset > 0) { + offset--; + continue; + } + grn_ii_posting_add(ctx, (grn_ii_posting *)posting, result_set, op); + limit--; + if (limit == 0) { + break; + } + } + grn_ii_resolve_sel_and(ctx, result_set, op); + + return mrb_fixnum_value(n_matched_records); +} + +void +grn_mrb_index_cursor_init(grn_ctx *ctx) +{ + grn_mrb_data *data = &(ctx->impl->mrb); + mrb_state *mrb = data->state; + struct RClass *module = data->module; + struct RClass *klass; + + klass = mrb_define_class_under(mrb, module, "IndexCursor", mrb->object_class); + MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA); + + mrb_define_singleton_method(mrb, (struct RObject *)klass, "open_raw", + mrb_grn_index_cursor_singleton_open_raw, + MRB_ARGS_ARG(2, 1)); + + mrb_define_method(mrb, klass, "initialize", + mrb_grn_index_cursor_initialize, MRB_ARGS_REQ(1)); + mrb_define_method(mrb, klass, "close", + mrb_grn_index_cursor_close, MRB_ARGS_NONE()); + mrb_define_method(mrb, klass, "count", + mrb_grn_index_cursor_count, MRB_ARGS_NONE()); + mrb_define_method(mrb, klass, "select", + mrb_grn_index_cursor_select, MRB_ARGS_ARG(1, 1)); +} +#endif diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_index_cursor.h b/storage/mroonga/vendor/groonga/lib/mrb/mrb_index_cursor.h new file mode 100644 index 00000000000..afbad53db9f --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_index_cursor.h @@ -0,0 +1,34 @@ +/* -*- 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_INDEX_CURSOR_H +#define GRN_MRB_INDEX_CURSOR_H + +#include "../grn_ctx.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void grn_mrb_index_cursor_init(grn_ctx *ctx); + +#ifdef __cplusplus +} +#endif + +#endif /* GRN_MRB_INDEX_CURSOR_H */ diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_logger.c b/storage/mroonga/vendor/groonga/lib/mrb/mrb_logger.c index 4917694fa8a..884ca761e83 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/mrb_logger.c +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_logger.c @@ -16,7 +16,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "../ctx_impl.h" +#include "../grn_ctx_impl.h" #ifdef GRN_WITH_MRUBY #include <mruby.h> @@ -25,7 +25,7 @@ #include <mruby/variable.h> #include <mruby/string.h> -#include "../mrb.h" +#include "../grn_mrb.h" #include "mrb_logger.h" static mrb_value diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_logger.h b/storage/mroonga/vendor/groonga/lib/mrb/mrb_logger.h index 5bfb62e98a2..358313b7ff9 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/mrb_logger.h +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_logger.h @@ -19,7 +19,7 @@ #ifndef GRN_MRB_LOGGER_H #define GRN_MRB_LOGGER_H -#include "../ctx.h" +#include "../grn_ctx.h" #ifdef __cplusplus extern "C" { diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_obj.c b/storage/mroonga/vendor/groonga/lib/mrb/mrb_obj.c deleted file mode 100644 index 142a242b980..00000000000 --- a/storage/mroonga/vendor/groonga/lib/mrb/mrb_obj.c +++ /dev/null @@ -1,135 +0,0 @@ -/* -*- c-basic-offset: 2 -*- */ -/* - Copyright(C) 2013-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 "../ctx_impl.h" -#include "../util.h" - -#ifdef GRN_WITH_MRUBY -#include <mruby.h> -#include <mruby/string.h> -#include <mruby/class.h> -#include <mruby/data.h> - -#include "../mrb.h" -#include "mrb_obj.h" -#include "mrb_converter.h" - -static mrb_value -object_get_name(mrb_state *mrb, mrb_value self) -{ - grn_ctx *ctx = (grn_ctx *)mrb->ud; - grn_obj *object; - char name[GRN_TABLE_MAX_KEY_SIZE]; - int name_length; - - object = DATA_PTR(self); - name_length = grn_obj_name(ctx, object, name, GRN_TABLE_MAX_KEY_SIZE); - - return mrb_str_new(mrb, name, name_length); -} - -static mrb_value -object_find_index(mrb_state *mrb, mrb_value self) -{ - grn_ctx *ctx = (grn_ctx *)mrb->ud; - grn_obj *object; - mrb_value mrb_operator; - grn_obj *index; - int n_indexes; - int section_id; - - mrb_get_args(mrb, "o", &mrb_operator); - object = DATA_PTR(self); - n_indexes = grn_column_index(ctx, - object, - mrb_fixnum(mrb_operator), - &index, - 1, - §ion_id); - if (n_indexes == 0) { - return mrb_nil_value(); - } else { - grn_mrb_data *data; - struct RClass *klass; - mrb_value args[2]; - - data = &(ctx->impl->mrb); - klass = mrb_class_get_under(mrb, data->module, "IndexInfo"); - args[0] = grn_mrb_value_from_grn_obj(mrb, index); - args[1] = mrb_fixnum_value(section_id); - return mrb_obj_new(mrb, klass, 2, args); - } -} - -static mrb_value -object_grn_inspect(mrb_state *mrb, mrb_value self) -{ - grn_ctx *ctx = (grn_ctx *)mrb->ud; - grn_obj buffer; - mrb_value inspected; - - GRN_TEXT_INIT(&buffer, 0); - grn_inspect(ctx, &buffer, DATA_PTR(self)); - inspected = mrb_str_new(mrb, GRN_TEXT_VALUE(&buffer), GRN_TEXT_LEN(&buffer)); - GRN_OBJ_FIN(ctx, &buffer); - - return inspected; -} - -static mrb_value -object_equal(mrb_state *mrb, mrb_value self) -{ - grn_obj *object, *other_object; - mrb_value mrb_other; - - mrb_get_args(mrb, "o", &mrb_other); - if (!mrb_obj_is_kind_of(mrb, mrb_other, mrb_obj_class(mrb, self))) { - return mrb_false_value(); - } - - object = DATA_PTR(self); - other_object = DATA_PTR(mrb_other); - if (object == other_object) { - return mrb_true_value(); - } else { - return mrb_false_value(); - } -} - -void -grn_mrb_obj_init(grn_ctx *ctx) -{ - grn_mrb_data *data = &(ctx->impl->mrb); - mrb_state *mrb = data->state; - struct RClass *module = data->module; - struct RClass *klass; - - klass = mrb_define_class_under(mrb, module, "Object", mrb->object_class); - MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA); - data->object_class = klass; - - mrb_define_method(mrb, klass, "name", object_get_name, MRB_ARGS_NONE()); - mrb_define_method(mrb, klass, "find_index", - object_find_index, MRB_ARGS_REQ(1)); - mrb_define_method(mrb, klass, "grn_inspect", - object_grn_inspect, MRB_ARGS_NONE()); - mrb_define_method(mrb, klass, "==", object_equal, MRB_ARGS_REQ(1)); - - grn_mrb_load(ctx, "index_info.rb"); -} -#endif diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_object.c b/storage/mroonga/vendor/groonga/lib/mrb/mrb_object.c new file mode 100644 index 00000000000..8efaa41e50d --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_object.c @@ -0,0 +1,263 @@ +/* -*- c-basic-offset: 2 -*- */ +/* + Copyright(C) 2013-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_util.h" + +#ifdef GRN_WITH_MRUBY +#include <mruby.h> +#include <mruby/string.h> +#include <mruby/class.h> +#include <mruby/data.h> + +#include "../grn_mrb.h" +#include "mrb_object.h" +#include "mrb_operator.h" +#include "mrb_converter.h" + +static mrb_value +object_inspect(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + grn_obj *object; + mrb_value inspected; + + object = DATA_PTR(self); + inspected = mrb_str_buf_new(mrb, 48); + + mrb_str_cat_lit(mrb, inspected, "#<"); + mrb_str_cat_cstr(mrb, inspected, mrb_obj_classname(mrb, self)); + mrb_str_cat_lit(mrb, inspected, ":"); + mrb_str_concat(mrb, inspected, mrb_ptr_to_str(mrb, mrb_cptr(self))); + if (object) { + grn_obj buffer; + GRN_TEXT_INIT(&buffer, 0); + grn_inspect(ctx, &buffer, object); + mrb_str_cat_lit(mrb, inspected, " "); + mrb_str_cat(mrb, inspected, GRN_TEXT_VALUE(&buffer), GRN_TEXT_LEN(&buffer)); + GRN_OBJ_FIN(ctx, &buffer); + } else { + mrb_str_cat_lit(mrb, inspected, " (closed)"); + } + mrb_str_cat_lit(mrb, inspected, ">"); + + return inspected; +} + +static mrb_value +object_get_id(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + grn_id id; + + id = grn_obj_id(ctx, DATA_PTR(self)); + + return mrb_fixnum_value(id); +} + +static mrb_value +object_get_name(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + grn_obj *object; + char name[GRN_TABLE_MAX_KEY_SIZE]; + int name_length; + + object = DATA_PTR(self); + name_length = grn_obj_name(ctx, object, name, GRN_TABLE_MAX_KEY_SIZE); + + return mrb_str_new(mrb, name, name_length); +} + +static mrb_value +object_find_index(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + grn_obj *object; + mrb_value mrb_operator; + grn_operator operator; + grn_index_datum index_datum; + int n_index_data; + + mrb_get_args(mrb, "o", &mrb_operator); + object = DATA_PTR(self); + operator = grn_mrb_value_to_operator(mrb, mrb_operator); + n_index_data = grn_column_find_index_data(ctx, + object, + operator, + &index_datum, + 1); + if (n_index_data == 0) { + return mrb_nil_value(); + } else { + grn_mrb_data *data; + struct RClass *klass; + mrb_value args[2]; + + data = &(ctx->impl->mrb); + klass = mrb_class_get_under(mrb, data->module, "IndexInfo"); + args[0] = grn_mrb_value_from_grn_obj(mrb, index_datum.index); + args[1] = mrb_fixnum_value(index_datum.section); + return mrb_obj_new(mrb, klass, 2, args); + } +} + +static mrb_value +object_grn_inspect(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + grn_obj buffer; + mrb_value inspected; + + GRN_TEXT_INIT(&buffer, 0); + grn_inspect(ctx, &buffer, DATA_PTR(self)); + inspected = mrb_str_new(mrb, GRN_TEXT_VALUE(&buffer), GRN_TEXT_LEN(&buffer)); + GRN_OBJ_FIN(ctx, &buffer); + + return inspected; +} + +static mrb_value +object_equal(mrb_state *mrb, mrb_value self) +{ + grn_obj *object, *other_object; + mrb_value mrb_other; + + mrb_get_args(mrb, "o", &mrb_other); + if (!mrb_obj_is_kind_of(mrb, mrb_other, mrb_obj_class(mrb, self))) { + return mrb_false_value(); + } + + object = DATA_PTR(self); + other_object = DATA_PTR(mrb_other); + if (object == other_object) { + return mrb_true_value(); + } else { + return mrb_false_value(); + } +} + +static mrb_value +object_close(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + grn_obj *object; + + object = DATA_PTR(self); + if (!object) { + mrb_raise(mrb, E_ARGUMENT_ERROR, "already closed object"); + } + + grn_obj_close(ctx, object); + DATA_PTR(self) = NULL; + + return mrb_nil_value(); +} + +static mrb_value +object_get_domain_id(mrb_state *mrb, mrb_value self) +{ + grn_obj *object; + grn_id domain_id; + + object = DATA_PTR(self); + domain_id = object->header.domain; + + if (domain_id == GRN_ID_NIL) { + return mrb_nil_value(); + } else { + return mrb_fixnum_value(domain_id); + } +} + +static mrb_value +object_get_range_id(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + grn_obj *object; + grn_id range_id; + + object = DATA_PTR(self); + range_id = grn_obj_get_range(ctx, object); + + if (range_id == GRN_ID_NIL) { + return mrb_nil_value(); + } else { + return mrb_fixnum_value(range_id); + } +} + +static mrb_value +object_is_temporary(mrb_state *mrb, mrb_value self) +{ + grn_obj *object; + grn_obj_flags flags; + + object = DATA_PTR(self); + flags = object->header.flags; + return mrb_bool_value((flags & GRN_OBJ_PERSISTENT) != GRN_OBJ_PERSISTENT); +} + +static mrb_value +object_is_persistent(mrb_state *mrb, mrb_value self) +{ + grn_obj *object; + grn_obj_flags flags; + + object = DATA_PTR(self); + flags = object->header.flags; + return mrb_bool_value((flags & GRN_OBJ_PERSISTENT) == GRN_OBJ_PERSISTENT); +} + +void +grn_mrb_object_init(grn_ctx *ctx) +{ + grn_mrb_data *data = &(ctx->impl->mrb); + mrb_state *mrb = data->state; + struct RClass *module = data->module; + struct RClass *klass; + + klass = mrb_define_class_under(mrb, module, "Object", mrb->object_class); + MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA); + data->object_class = klass; + + mrb_define_method(mrb, klass, "inspect", + object_inspect, MRB_ARGS_NONE()); + + mrb_define_method(mrb, klass, "id", object_get_id, MRB_ARGS_NONE()); + mrb_define_method(mrb, klass, "name", object_get_name, MRB_ARGS_NONE()); + mrb_define_method(mrb, klass, "find_index", + object_find_index, MRB_ARGS_REQ(1)); + mrb_define_method(mrb, klass, "grn_inspect", + object_grn_inspect, MRB_ARGS_NONE()); + mrb_define_method(mrb, klass, "==", object_equal, MRB_ARGS_REQ(1)); + mrb_define_method(mrb, klass, "close", object_close, MRB_ARGS_NONE()); + + mrb_define_method(mrb, klass, "domain_id", object_get_domain_id, + MRB_ARGS_NONE()); + mrb_define_method(mrb, klass, "range_id", object_get_range_id, + MRB_ARGS_NONE()); + + mrb_define_method(mrb, klass, "temporary?", object_is_temporary, + MRB_ARGS_NONE()); + mrb_define_method(mrb, klass, "persistent?", object_is_persistent, + MRB_ARGS_NONE()); + + grn_mrb_load(ctx, "index_info.rb"); +} +#endif diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_object.h b/storage/mroonga/vendor/groonga/lib/mrb/mrb_object.h new file mode 100644 index 00000000000..97a586c0f11 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_object.h @@ -0,0 +1,34 @@ +/* -*- c-basic-offset: 2 -*- */ +/* + Copyright(C) 2013-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 +*/ + +#ifndef GRN_MRB_OBJECT_H +#define GRN_MRB_OBJECT_H + +#include "../grn_ctx.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void grn_mrb_object_init(grn_ctx *ctx); + +#ifdef __cplusplus +} +#endif + +#endif /* GRN_MRB_OBJECT_H */ diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_object_flags.c b/storage/mroonga/vendor/groonga/lib/mrb/mrb_object_flags.c new file mode 100644 index 00000000000..c8accada692 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_object_flags.c @@ -0,0 +1,95 @@ +/* -*- 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" + +#ifdef GRN_WITH_MRUBY +# include <mruby.h> +# include <mruby/string.h> +# include <mruby/class.h> +# include <mruby/data.h> + +# include "../grn_mrb.h" +# include "mrb_object.h" +# include "mrb_operator.h" +# include "mrb_converter.h" + +void +grn_mrb_object_flags_init(grn_ctx *ctx) +{ + grn_mrb_data *data = &(ctx->impl->mrb); + mrb_state *mrb = data->state; + struct RClass *module = data->module; + struct RClass *flags_module; + + flags_module = mrb_define_module_under(mrb, module, "ObjectFlags"); + +#define MRB_DEFINE_FLAG(name) \ + mrb_define_const(mrb, flags_module, #name, \ + mrb_fixnum_value(GRN_OBJ_ ## name)) + + MRB_DEFINE_FLAG(TABLE_TYPE_MASK); + MRB_DEFINE_FLAG(TABLE_HASH_KEY); + MRB_DEFINE_FLAG(TABLE_PAT_KEY); + MRB_DEFINE_FLAG(TABLE_DAT_KEY); + MRB_DEFINE_FLAG(TABLE_NO_KEY); + + MRB_DEFINE_FLAG(KEY_MASK); + MRB_DEFINE_FLAG(KEY_UINT); + MRB_DEFINE_FLAG(KEY_INT); + MRB_DEFINE_FLAG(KEY_FLOAT); + MRB_DEFINE_FLAG(KEY_GEO_POINT); + + MRB_DEFINE_FLAG(KEY_WITH_SIS); + MRB_DEFINE_FLAG(KEY_NORMALIZE); + + MRB_DEFINE_FLAG(COLUMN_TYPE_MASK); + MRB_DEFINE_FLAG(COLUMN_SCALAR); + MRB_DEFINE_FLAG(COLUMN_VECTOR); + MRB_DEFINE_FLAG(COLUMN_INDEX); + + MRB_DEFINE_FLAG(COMPRESS_MASK); + MRB_DEFINE_FLAG(COMPRESS_NONE); + MRB_DEFINE_FLAG(COMPRESS_ZLIB); + MRB_DEFINE_FLAG(COMPRESS_LZ4); + + MRB_DEFINE_FLAG(WITH_SECTION); + MRB_DEFINE_FLAG(WITH_WEIGHT); + MRB_DEFINE_FLAG(WITH_POSITION); + MRB_DEFINE_FLAG(RING_BUFFER); + + MRB_DEFINE_FLAG(UNIT_MASK); + MRB_DEFINE_FLAG(UNIT_DOCUMENT_NONE); + MRB_DEFINE_FLAG(UNIT_DOCUMENT_SECTION); + MRB_DEFINE_FLAG(UNIT_DOCUMENT_POSITION); + MRB_DEFINE_FLAG(UNIT_SECTION_NONE); + MRB_DEFINE_FLAG(UNIT_SECTION_POSITION); + MRB_DEFINE_FLAG(UNIT_POSITION_NONE); + MRB_DEFINE_FLAG(UNIT_USERDEF_DOCUMENT); + MRB_DEFINE_FLAG(UNIT_USERDEF_SECTION); + MRB_DEFINE_FLAG(UNIT_USERDEF_POSITION); + + MRB_DEFINE_FLAG(NO_SUBREC); + MRB_DEFINE_FLAG(WITH_SUBREC); + + MRB_DEFINE_FLAG(KEY_VAR_SIZE); + + MRB_DEFINE_FLAG(TEMPORARY); + MRB_DEFINE_FLAG(PERSISTENT); +} +#endif /* GRN_WITH_MRUBY */ diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_object_flags.h b/storage/mroonga/vendor/groonga/lib/mrb/mrb_object_flags.h new file mode 100644 index 00000000000..60c82227078 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_object_flags.h @@ -0,0 +1,34 @@ +/* -*- 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_OBJECT_FLAGS_H +#define GRN_MRB_OBJECT_FLAGS_H + +#include "../grn_ctx.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void grn_mrb_object_flags_init(grn_ctx *ctx); + +#ifdef __cplusplus +} +#endif + +#endif /* GRN_MRB_OBJECT_FLAGS_H */ diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_operator.c b/storage/mroonga/vendor/groonga/lib/mrb/mrb_operator.c index 5a18d2e7023..2e0cb481722 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/mrb_operator.c +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_operator.c @@ -1,6 +1,6 @@ /* -*- c-basic-offset: 2 -*- */ /* - Copyright(C) 2014 Brazil + Copyright(C) 2014-2015 Brazil This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -16,175 +16,139 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "../ctx_impl.h" +#include "../grn_ctx_impl.h" #ifdef GRN_WITH_MRUBY #include <mruby.h> #include "mrb_operator.h" +mrb_value +grn_mrb_value_from_operator(mrb_state *mrb, grn_operator op) +{ + grn_ctx *ctx = (grn_ctx *)(mrb->ud); + grn_mrb_data *data = &(ctx->impl->mrb); + mrb_value mrb_op_raw; + mrb_value mrb_op; + + mrb_op_raw = mrb_fixnum_value(op); + mrb_op = mrb_funcall(mrb, mrb_obj_value(data->groonga.operator_class), + "find", 1, mrb_op_raw); + if (mrb_nil_p(mrb_op)) { + return mrb_op_raw; + } else { + return mrb_op; + } +} + +grn_operator +grn_mrb_value_to_operator(mrb_state *mrb, mrb_value mrb_op) +{ + if (!mrb_fixnum_p(mrb_op)) { + mrb_op = mrb_funcall(mrb, mrb_op, "value", 0); + } + + return mrb_fixnum(mrb_op); +} + void grn_mrb_operator_init(grn_ctx *ctx) { - mrb_state *mrb = ctx->impl->mrb.state; + grn_mrb_data *data = &(ctx->impl->mrb); + mrb_state *mrb = data->state; struct RClass *module = ctx->impl->mrb.module; - struct RClass *operator_module; + struct RClass *klass; + mrb_value klass_obj; + + klass = mrb_class_get_under(mrb, module, "Operator"); + data->groonga.operator_class = klass; + + klass_obj = mrb_obj_value(klass); +#define DEFINE_OPERATOR(name) \ + mrb_funcall(mrb, klass_obj, "register", 1, \ + mrb_funcall(mrb, klass_obj, "new", 2, \ + mrb_str_new_lit(mrb, #name), \ + mrb_fixnum_value(GRN_OP_ ## name))) - operator_module = mrb_define_module_under(mrb, module, "Operator"); + DEFINE_OPERATOR(PUSH); + DEFINE_OPERATOR(POP); + DEFINE_OPERATOR(NOP); + DEFINE_OPERATOR(CALL); + DEFINE_OPERATOR(INTERN); + DEFINE_OPERATOR(GET_REF); + DEFINE_OPERATOR(GET_VALUE); + DEFINE_OPERATOR(AND); + DEFINE_OPERATOR(AND_NOT); + DEFINE_OPERATOR(OR); + DEFINE_OPERATOR(ASSIGN); + DEFINE_OPERATOR(STAR_ASSIGN); + DEFINE_OPERATOR(SLASH_ASSIGN); + DEFINE_OPERATOR(MOD_ASSIGN); + DEFINE_OPERATOR(PLUS_ASSIGN); + DEFINE_OPERATOR(MINUS_ASSIGN); + DEFINE_OPERATOR(SHIFTL_ASSIGN); + DEFINE_OPERATOR(SHIFTR_ASSIGN); + DEFINE_OPERATOR(SHIFTRR_ASSIGN); + DEFINE_OPERATOR(AND_ASSIGN); + DEFINE_OPERATOR(XOR_ASSIGN); + DEFINE_OPERATOR(OR_ASSIGN); + DEFINE_OPERATOR(JUMP); + DEFINE_OPERATOR(CJUMP); + DEFINE_OPERATOR(COMMA); + DEFINE_OPERATOR(BITWISE_OR); + DEFINE_OPERATOR(BITWISE_XOR); + DEFINE_OPERATOR(BITWISE_AND); + DEFINE_OPERATOR(BITWISE_NOT); + DEFINE_OPERATOR(EQUAL); + DEFINE_OPERATOR(NOT_EQUAL); + DEFINE_OPERATOR(LESS); + DEFINE_OPERATOR(GREATER); + DEFINE_OPERATOR(LESS_EQUAL); + DEFINE_OPERATOR(GREATER_EQUAL); + DEFINE_OPERATOR(IN); + DEFINE_OPERATOR(MATCH); + DEFINE_OPERATOR(NEAR); + DEFINE_OPERATOR(NEAR2); + DEFINE_OPERATOR(SIMILAR); + DEFINE_OPERATOR(TERM_EXTRACT); + DEFINE_OPERATOR(SHIFTL); + DEFINE_OPERATOR(SHIFTR); + DEFINE_OPERATOR(SHIFTRR); + DEFINE_OPERATOR(PLUS); + DEFINE_OPERATOR(MINUS); + DEFINE_OPERATOR(STAR); + DEFINE_OPERATOR(SLASH); + DEFINE_OPERATOR(MOD); + DEFINE_OPERATOR(DELETE); + DEFINE_OPERATOR(INCR); + DEFINE_OPERATOR(DECR); + DEFINE_OPERATOR(INCR_POST); + DEFINE_OPERATOR(DECR_POST); + DEFINE_OPERATOR(NOT); + DEFINE_OPERATOR(ADJUST); + DEFINE_OPERATOR(EXACT); + DEFINE_OPERATOR(LCP); + DEFINE_OPERATOR(PARTIAL); + DEFINE_OPERATOR(UNSPLIT); + DEFINE_OPERATOR(PREFIX); + DEFINE_OPERATOR(SUFFIX); + DEFINE_OPERATOR(GEO_DISTANCE1); + DEFINE_OPERATOR(GEO_DISTANCE2); + DEFINE_OPERATOR(GEO_DISTANCE3); + DEFINE_OPERATOR(GEO_DISTANCE4); + DEFINE_OPERATOR(GEO_WITHINP5); + DEFINE_OPERATOR(GEO_WITHINP6); + DEFINE_OPERATOR(GEO_WITHINP8); + DEFINE_OPERATOR(OBJ_SEARCH); + DEFINE_OPERATOR(EXPR_GET_VAR); + DEFINE_OPERATOR(TABLE_CREATE); + DEFINE_OPERATOR(TABLE_SELECT); + DEFINE_OPERATOR(TABLE_SORT); + DEFINE_OPERATOR(TABLE_GROUP); + DEFINE_OPERATOR(JSON_PUT); + DEFINE_OPERATOR(GET_MEMBER); + DEFINE_OPERATOR(REGEXP); - mrb_define_const(mrb, operator_module, "PUSH", - mrb_fixnum_value(GRN_OP_PUSH)); - mrb_define_const(mrb, operator_module, "POP", - mrb_fixnum_value(GRN_OP_POP)); - mrb_define_const(mrb, operator_module, "NOP", - mrb_fixnum_value(GRN_OP_NOP)); - mrb_define_const(mrb, operator_module, "CALL", - mrb_fixnum_value(GRN_OP_CALL)); - mrb_define_const(mrb, operator_module, "INTERN", - mrb_fixnum_value(GRN_OP_INTERN)); - mrb_define_const(mrb, operator_module, "GET_REF", - mrb_fixnum_value(GRN_OP_GET_REF)); - mrb_define_const(mrb, operator_module, "GET_VALUE", - mrb_fixnum_value(GRN_OP_GET_VALUE)); - mrb_define_const(mrb, operator_module, "AND", - mrb_fixnum_value(GRN_OP_AND)); - mrb_define_const(mrb, operator_module, "AND_NOT", - mrb_fixnum_value(GRN_OP_AND_NOT)); - mrb_define_const(mrb, operator_module, "OR", - mrb_fixnum_value(GRN_OP_OR)); - mrb_define_const(mrb, operator_module, "ASSIGN", - mrb_fixnum_value(GRN_OP_ASSIGN)); - mrb_define_const(mrb, operator_module, "STAR_ASSIGN", - mrb_fixnum_value(GRN_OP_STAR_ASSIGN)); - mrb_define_const(mrb, operator_module, "SLASH_ASSIGN", - mrb_fixnum_value(GRN_OP_SLASH_ASSIGN)); - mrb_define_const(mrb, operator_module, "MOD_ASSIGN", - mrb_fixnum_value(GRN_OP_MOD_ASSIGN)); - mrb_define_const(mrb, operator_module, "PLUS_ASSIGN", - mrb_fixnum_value(GRN_OP_PLUS_ASSIGN)); - mrb_define_const(mrb, operator_module, "MINUS_ASSIGN", - mrb_fixnum_value(GRN_OP_MINUS_ASSIGN)); - mrb_define_const(mrb, operator_module, "SHIFTL_ASSIGN", - mrb_fixnum_value(GRN_OP_SHIFTL_ASSIGN)); - mrb_define_const(mrb, operator_module, "SHIFTR_ASSIGN", - mrb_fixnum_value(GRN_OP_SHIFTR_ASSIGN)); - mrb_define_const(mrb, operator_module, "SHIFTRR_ASSIGN", - mrb_fixnum_value(GRN_OP_SHIFTRR_ASSIGN)); - mrb_define_const(mrb, operator_module, "AND_ASSIGN", - mrb_fixnum_value(GRN_OP_AND_ASSIGN)); - mrb_define_const(mrb, operator_module, "XOR_ASSIGN", - mrb_fixnum_value(GRN_OP_XOR_ASSIGN)); - mrb_define_const(mrb, operator_module, "OR_ASSIGN", - mrb_fixnum_value(GRN_OP_OR_ASSIGN)); - mrb_define_const(mrb, operator_module, "JUMP", - mrb_fixnum_value(GRN_OP_JUMP)); - mrb_define_const(mrb, operator_module, "CJUMP", - mrb_fixnum_value(GRN_OP_CJUMP)); - mrb_define_const(mrb, operator_module, "COMMA", - mrb_fixnum_value(GRN_OP_COMMA)); - mrb_define_const(mrb, operator_module, "BITWISE_OR", - mrb_fixnum_value(GRN_OP_BITWISE_OR)); - mrb_define_const(mrb, operator_module, "BITWISE_XOR", - mrb_fixnum_value(GRN_OP_BITWISE_XOR)); - mrb_define_const(mrb, operator_module, "BITWISE_AND", - mrb_fixnum_value(GRN_OP_BITWISE_AND)); - mrb_define_const(mrb, operator_module, "BITWISE_NOT", - mrb_fixnum_value(GRN_OP_BITWISE_NOT)); - mrb_define_const(mrb, operator_module, "EQUAL", - mrb_fixnum_value(GRN_OP_EQUAL)); - mrb_define_const(mrb, operator_module, "NOT_EQUAL", - mrb_fixnum_value(GRN_OP_NOT_EQUAL)); - mrb_define_const(mrb, operator_module, "LESS", - mrb_fixnum_value(GRN_OP_LESS)); - mrb_define_const(mrb, operator_module, "GREATER", - mrb_fixnum_value(GRN_OP_GREATER)); - mrb_define_const(mrb, operator_module, "LESS_EQUAL", - mrb_fixnum_value(GRN_OP_LESS_EQUAL)); - mrb_define_const(mrb, operator_module, "GREATER_EQUAL", - mrb_fixnum_value(GRN_OP_GREATER_EQUAL)); - mrb_define_const(mrb, operator_module, "IN", - mrb_fixnum_value(GRN_OP_IN)); - mrb_define_const(mrb, operator_module, "MATCH", - mrb_fixnum_value(GRN_OP_MATCH)); - mrb_define_const(mrb, operator_module, "NEAR", - mrb_fixnum_value(GRN_OP_NEAR)); - mrb_define_const(mrb, operator_module, "NEAR2", - mrb_fixnum_value(GRN_OP_NEAR2)); - mrb_define_const(mrb, operator_module, "SIMILAR", - mrb_fixnum_value(GRN_OP_SIMILAR)); - mrb_define_const(mrb, operator_module, "TERM_EXTRACT", - mrb_fixnum_value(GRN_OP_TERM_EXTRACT)); - mrb_define_const(mrb, operator_module, "SHIFTL", - mrb_fixnum_value(GRN_OP_SHIFTL)); - mrb_define_const(mrb, operator_module, "SHIFTR", - mrb_fixnum_value(GRN_OP_SHIFTR)); - mrb_define_const(mrb, operator_module, "SHIFTRR", - mrb_fixnum_value(GRN_OP_SHIFTRR)); - mrb_define_const(mrb, operator_module, "PLUS", - mrb_fixnum_value(GRN_OP_PLUS)); - mrb_define_const(mrb, operator_module, "MINUS", - mrb_fixnum_value(GRN_OP_MINUS)); - mrb_define_const(mrb, operator_module, "STAR", - mrb_fixnum_value(GRN_OP_STAR)); - mrb_define_const(mrb, operator_module, "SLASH", - mrb_fixnum_value(GRN_OP_SLASH)); - mrb_define_const(mrb, operator_module, "MOD", - mrb_fixnum_value(GRN_OP_MOD)); - mrb_define_const(mrb, operator_module, "DELETE", - mrb_fixnum_value(GRN_OP_DELETE)); - mrb_define_const(mrb, operator_module, "INCR", - mrb_fixnum_value(GRN_OP_INCR)); - mrb_define_const(mrb, operator_module, "DECR", - mrb_fixnum_value(GRN_OP_DECR)); - mrb_define_const(mrb, operator_module, "INCR_POST", - mrb_fixnum_value(GRN_OP_INCR_POST)); - mrb_define_const(mrb, operator_module, "DECR_POST", - mrb_fixnum_value(GRN_OP_DECR_POST)); - mrb_define_const(mrb, operator_module, "NOT", - mrb_fixnum_value(GRN_OP_NOT)); - mrb_define_const(mrb, operator_module, "ADJUST", - mrb_fixnum_value(GRN_OP_ADJUST)); - mrb_define_const(mrb, operator_module, "EXACT", - mrb_fixnum_value(GRN_OP_EXACT)); - mrb_define_const(mrb, operator_module, "LCP", - mrb_fixnum_value(GRN_OP_LCP)); - mrb_define_const(mrb, operator_module, "PARTIAL", - mrb_fixnum_value(GRN_OP_PARTIAL)); - mrb_define_const(mrb, operator_module, "UNSPLIT", - mrb_fixnum_value(GRN_OP_UNSPLIT)); - mrb_define_const(mrb, operator_module, "PREFIX", - mrb_fixnum_value(GRN_OP_PREFIX)); - mrb_define_const(mrb, operator_module, "SUFFIX", - mrb_fixnum_value(GRN_OP_SUFFIX)); - mrb_define_const(mrb, operator_module, "GEO_DISTANCE1", - mrb_fixnum_value(GRN_OP_GEO_DISTANCE1)); - mrb_define_const(mrb, operator_module, "GEO_DISTANCE2", - mrb_fixnum_value(GRN_OP_GEO_DISTANCE2)); - mrb_define_const(mrb, operator_module, "GEO_DISTANCE3", - mrb_fixnum_value(GRN_OP_GEO_DISTANCE3)); - mrb_define_const(mrb, operator_module, "GEO_DISTANCE4", - mrb_fixnum_value(GRN_OP_GEO_DISTANCE4)); - mrb_define_const(mrb, operator_module, "GEO_WITHINP5", - mrb_fixnum_value(GRN_OP_GEO_WITHINP5)); - mrb_define_const(mrb, operator_module, "GEO_WITHINP6", - mrb_fixnum_value(GRN_OP_GEO_WITHINP6)); - mrb_define_const(mrb, operator_module, "GEO_WITHINP8", - mrb_fixnum_value(GRN_OP_GEO_WITHINP8)); - mrb_define_const(mrb, operator_module, "OBJ_SEARCH", - mrb_fixnum_value(GRN_OP_OBJ_SEARCH)); - mrb_define_const(mrb, operator_module, "EXPR_GET_VAR", - mrb_fixnum_value(GRN_OP_EXPR_GET_VAR)); - mrb_define_const(mrb, operator_module, "TABLE_CREATE", - mrb_fixnum_value(GRN_OP_TABLE_CREATE)); - mrb_define_const(mrb, operator_module, "TABLE_SELECT", - mrb_fixnum_value(GRN_OP_TABLE_SELECT)); - mrb_define_const(mrb, operator_module, "TABLE_SORT", - mrb_fixnum_value(GRN_OP_TABLE_SORT)); - mrb_define_const(mrb, operator_module, "TABLE_GROUP", - mrb_fixnum_value(GRN_OP_TABLE_GROUP)); - mrb_define_const(mrb, operator_module, "JSON_PUT", - mrb_fixnum_value(GRN_OP_JSON_PUT)); - mrb_define_const(mrb, operator_module, "GET_MEMBER", - mrb_fixnum_value(GRN_OP_GET_MEMBER)); +#undef DEFINE_OPERATOR } #endif diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_operator.h b/storage/mroonga/vendor/groonga/lib/mrb/mrb_operator.h index 2650cc9421d..b76c4983446 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/mrb_operator.h +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_operator.h @@ -1,6 +1,6 @@ /* -*- c-basic-offset: 2 -*- */ /* - Copyright(C) 2014 Brazil + Copyright(C) 2014-2015 Brazil This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -19,13 +19,15 @@ #ifndef GRN_MRB_OPERATOR_H #define GRN_MRB_OPERATOR_H -#include "../ctx.h" +#include "../grn_ctx.h" #ifdef __cplusplus extern "C" { #endif void grn_mrb_operator_init(grn_ctx *ctx); +mrb_value grn_mrb_value_from_operator(mrb_state *mrb, grn_operator op); +grn_operator grn_mrb_value_to_operator(mrb_state *mrb, mrb_value mrb_op); #ifdef __cplusplus } diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_options.c b/storage/mroonga/vendor/groonga/lib/mrb/mrb_options.c new file mode 100644 index 00000000000..ff3a1c0e731 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_options.c @@ -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 diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_options.h b/storage/mroonga/vendor/groonga/lib/mrb/mrb_options.h new file mode 100644 index 00000000000..1aa547d3be9 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_options.h @@ -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 */ diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_patricia_trie.c b/storage/mroonga/vendor/groonga/lib/mrb/mrb_patricia_trie.c new file mode 100644 index 00000000000..d2486f21c37 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_patricia_trie.c @@ -0,0 +1,59 @@ +/* -*- 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 "../grn_ctx_impl.h" + +#ifdef GRN_WITH_MRUBY +#include <mruby.h> +#include <mruby/class.h> +#include <mruby/data.h> + +#include "mrb_patricia_trie.h" + +static struct mrb_data_type mrb_grn_patricia_trie_type = { + "Groonga::PatriciaTrie", + NULL +}; + +static mrb_value +mrb_grn_patricia_trie_initialize(mrb_state *mrb, mrb_value self) +{ + mrb_value mrb_patricia_trie_ptr; + + mrb_get_args(mrb, "o", &mrb_patricia_trie_ptr); + DATA_TYPE(self) = &mrb_grn_patricia_trie_type; + DATA_PTR(self) = mrb_cptr(mrb_patricia_trie_ptr); + return self; +} + +void +grn_mrb_patricia_trie_init(grn_ctx *ctx) +{ + grn_mrb_data *data = &(ctx->impl->mrb); + mrb_state *mrb = data->state; + struct RClass *module = data->module; + struct RClass *table_class; + struct RClass *klass; + + table_class = mrb_class_get_under(mrb, module, "Table"); + klass = mrb_define_class_under(mrb, module, "PatriciaTrie", table_class); + MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA); + mrb_define_method(mrb, klass, "initialize", + mrb_grn_patricia_trie_initialize, MRB_ARGS_REQ(1)); +} +#endif diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_patricia_trie.h b/storage/mroonga/vendor/groonga/lib/mrb/mrb_patricia_trie.h new file mode 100644 index 00000000000..f281b25969b --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_patricia_trie.h @@ -0,0 +1,34 @@ +/* -*- 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 +*/ + +#ifndef GRN_MRB_PATRICIA_TRIE_H +#define GRN_MRB_PATRICIA_TRIE_H + +#include "../grn_ctx.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void grn_mrb_patricia_trie_init(grn_ctx *ctx); + +#ifdef __cplusplus +} +#endif + +#endif /* GRN_MRB_PATRICIA_TRIE_H */ diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_procedure.c b/storage/mroonga/vendor/groonga/lib/mrb/mrb_procedure.c index fa9c677f2e6..3dfe78488df 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/mrb_procedure.c +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_procedure.c @@ -16,7 +16,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "../ctx_impl.h" +#include "../grn_ctx_impl.h" #ifdef GRN_WITH_MRUBY #include <mruby.h> @@ -41,6 +41,15 @@ mrb_grn_procedure_initialize(mrb_state *mrb, mrb_value self) return self; } +static mrb_value +mrb_grn_procedure_scorer_p(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + grn_obj *proc = DATA_PTR(self); + + return mrb_bool_value(grn_obj_is_scorer_proc(ctx, proc)); +} + void grn_mrb_procedure_init(grn_ctx *ctx) { @@ -54,5 +63,8 @@ grn_mrb_procedure_init(grn_ctx *ctx) MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA); mrb_define_method(mrb, klass, "initialize", mrb_grn_procedure_initialize, MRB_ARGS_REQ(1)); + + mrb_define_method(mrb, klass, "scorer?", + mrb_grn_procedure_scorer_p, MRB_ARGS_NONE()); } #endif diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_procedure.h b/storage/mroonga/vendor/groonga/lib/mrb/mrb_procedure.h index 16a4b32895c..36bd10fdab9 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/mrb_procedure.h +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_procedure.h @@ -19,7 +19,7 @@ #ifndef GRN_MRB_PROCEDURE_H #define GRN_MRB_PROCEDURE_H -#include "../ctx.h" +#include "../grn_ctx.h" #ifdef __cplusplus extern "C" { diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_table.c b/storage/mroonga/vendor/groonga/lib/mrb/mrb_table.c new file mode 100644 index 00000000000..651a77e4549 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_table.c @@ -0,0 +1,257 @@ +/* -*- c-basic-offset: 2 -*- */ +/* + Copyright(C) 2014-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" + +#ifdef GRN_WITH_MRUBY +#include <mruby.h> +#include <mruby/class.h> +#include <mruby/data.h> +#include <mruby/hash.h> +#include <mruby/array.h> +#include <mruby/string.h> + +#include "mrb_ctx.h" +#include "mrb_table.h" +#include "mrb_converter.h" +#include "mrb_options.h" + +static mrb_value +mrb_grn_table_array_reference(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + grn_obj *table; + grn_id key_domain_id; + mrb_value mrb_key; + grn_id record_id; + grn_mrb_value_to_raw_data_buffer buffer; + void *key; + unsigned int key_size; + + mrb_get_args(mrb, "o", &mrb_key); + + table = DATA_PTR(self); + if (table->header.type == GRN_DB) { + key_domain_id = GRN_DB_SHORT_TEXT; + } else { + key_domain_id = table->header.domain; + } + + grn_mrb_value_to_raw_data_buffer_init(mrb, &buffer); + grn_mrb_value_to_raw_data(mrb, "key", mrb_key, key_domain_id, + &buffer, &key, &key_size); + record_id = grn_table_get(ctx, table, key, key_size); + grn_mrb_value_to_raw_data_buffer_fin(mrb, &buffer); + + if (record_id == GRN_ID_NIL) { + return mrb_nil_value(); + } else { + return mrb_fixnum_value(record_id); + } +} + +static mrb_value +mrb_grn_table_is_locked(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + unsigned int is_locked; + + is_locked = grn_obj_is_locked(ctx, DATA_PTR(self)); + grn_mrb_ctx_check(mrb); + + return mrb_bool_value(is_locked != 0); +} + +static mrb_value +mrb_grn_table_get_size(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + unsigned int size; + + size = grn_table_size(ctx, DATA_PTR(self)); + grn_mrb_ctx_check(mrb); + + return mrb_fixnum_value(size); +} + +static mrb_value +mrb_grn_table_is_empty(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + unsigned int size; + + size = grn_table_size(ctx, DATA_PTR(self)); + grn_mrb_ctx_check(mrb); + + return mrb_bool_value(size == 0); +} + +static mrb_value +mrb_grn_table_select(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + grn_obj *table; + grn_obj *expr; + grn_obj *result = NULL; + grn_operator operator = GRN_OP_OR; + mrb_value mrb_expr; + mrb_value mrb_options = mrb_nil_value(); + + table = DATA_PTR(self); + mrb_get_args(mrb, "o|H", &mrb_expr, &mrb_options); + + expr = DATA_PTR(mrb_expr); + + if (!mrb_nil_p(mrb_options)) { + mrb_value mrb_result; + mrb_value mrb_operator; + + mrb_result = grn_mrb_options_get_lit(mrb, mrb_options, "result"); + if (!mrb_nil_p(mrb_result)) { + result = DATA_PTR(mrb_result); + } + + mrb_operator = grn_mrb_options_get_lit(mrb, mrb_options, "operator"); + if (!mrb_nil_p(mrb_operator)) { + operator = mrb_fixnum(mrb_operator); + } + } + + result = grn_table_select(ctx, table, expr, result, operator); + grn_mrb_ctx_check(mrb); + + return grn_mrb_value_from_grn_obj(mrb, result); +} + +/* TODO: Fix memory leak on error */ +static mrb_value +mrb_grn_table_sort(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + grn_obj *table; + grn_obj *result = NULL; + grn_table_sort_key *keys; + int i, n_keys; + int offset = 0; + int limit = -1; + mrb_value mrb_keys; + mrb_value mrb_options = mrb_nil_value(); + + table = DATA_PTR(self); + mrb_get_args(mrb, "o|H", &mrb_keys, &mrb_options); + + mrb_keys = mrb_convert_type(mrb, mrb_keys, + MRB_TT_ARRAY, "Array", "to_ary"); + + n_keys = RARRAY_LEN(mrb_keys); + keys = GRN_MALLOCN(grn_table_sort_key, n_keys); + for (i = 0; i < n_keys; i++) { + mrb_value mrb_sort_options; + mrb_value mrb_sort_key; + mrb_value mrb_sort_order; + + mrb_sort_options = RARRAY_PTR(mrb_keys)[i]; + 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, + RSTRING_PTR(mrb_sort_key), + RSTRING_LEN(mrb_sort_key)); + break; + case MRB_TT_SYMBOL : + { + const char *name; + mrb_int name_length; + name = mrb_sym2name_len(mrb, mrb_symbol(mrb_sort_key), &name_length); + keys[i].key = grn_obj_column(ctx, table, name, name_length); + } + break; + default : + /* TODO: free */ + mrb_raisef(mrb, E_ARGUMENT_ERROR, + "sort key must be string or symbol: %S", + mrb_sort_key); + break; + } + + keys[i].flags = 0; + 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; + } else { + keys[i].flags |= GRN_TABLE_SORT_DESC; + } + } + + if (!mrb_nil_p(mrb_options)) { + mrb_value mrb_offset; + mrb_value mrb_limit; + + mrb_offset = grn_mrb_options_get_lit(mrb, mrb_options, "offset"); + if (!mrb_nil_p(mrb_offset)) { + offset = mrb_fixnum(mrb_offset); + } + + mrb_limit = grn_mrb_options_get_lit(mrb, mrb_options, "limit"); + if (!mrb_nil_p(mrb_limit)) { + limit = mrb_fixnum(mrb_limit); + } + } + + result = grn_table_create(ctx, NULL, 0, NULL, GRN_TABLE_NO_KEY, + NULL, table); + grn_table_sort(ctx, table, offset, limit, result, keys, n_keys); + for (i = 0; i < n_keys; i++) { + grn_obj_unlink(ctx, keys[i].key); + } + GRN_FREE(keys); + grn_mrb_ctx_check(mrb); + + return grn_mrb_value_from_grn_obj(mrb, result); +} + +void +grn_mrb_table_init(grn_ctx *ctx) +{ + grn_mrb_data *data = &(ctx->impl->mrb); + mrb_state *mrb = data->state; + struct RClass *module = data->module; + struct RClass *object_class = data->object_class; + struct RClass *klass; + + klass = mrb_define_class_under(mrb, module, "Table", object_class); + MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA); + + mrb_define_method(mrb, klass, "[]", + mrb_grn_table_array_reference, MRB_ARGS_REQ(1)); + + mrb_define_method(mrb, klass, "locked?", + mrb_grn_table_is_locked, MRB_ARGS_NONE()); + + mrb_define_method(mrb, klass, "size", + mrb_grn_table_get_size, MRB_ARGS_NONE()); + mrb_define_method(mrb, klass, "empty?", + mrb_grn_table_is_empty, MRB_ARGS_NONE()); + + mrb_define_method(mrb, klass, "select", + mrb_grn_table_select, MRB_ARGS_ARG(1, 1)); + mrb_define_method(mrb, klass, "sort", + mrb_grn_table_sort, MRB_ARGS_ARG(1, 1)); +} +#endif diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_table.h b/storage/mroonga/vendor/groonga/lib/mrb/mrb_table.h new file mode 100644 index 00000000000..000088fcf40 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_table.h @@ -0,0 +1,34 @@ +/* -*- 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 +*/ + +#ifndef GRN_MRB_TABLE_H +#define GRN_MRB_TABLE_H + +#include "../grn_ctx.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void grn_mrb_table_init(grn_ctx *ctx); + +#ifdef __cplusplus +} +#endif + +#endif /* GRN_MRB_TABLE_H */ diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_table_cursor.c b/storage/mroonga/vendor/groonga/lib/mrb/mrb_table_cursor.c new file mode 100644 index 00000000000..76d4429d24f --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_table_cursor.c @@ -0,0 +1,176 @@ +/* -*- c-basic-offset: 2 -*- */ +/* + Copyright(C) 2014-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" + +#ifdef GRN_WITH_MRUBY +#include <mruby.h> +#include <mruby/class.h> +#include <mruby/data.h> +#include <mruby/string.h> +#include <mruby/hash.h> + +#include "mrb_ctx.h" +#include "mrb_bulk.h" +#include "mrb_table_cursor.h" + +#include "mrb_converter.h" +#include "mrb_options.h" + +static struct mrb_data_type mrb_grn_table_cursor_type = { + "Groonga::TableCursor", + NULL +}; + +static mrb_value +mrb_grn_table_cursor_singleton_open_raw(mrb_state *mrb, mrb_value klass) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + mrb_value mrb_table; + mrb_value mrb_options = mrb_nil_value(); + grn_table_cursor *table_cursor; + grn_obj *table; + void *min = NULL; + unsigned int min_size = 0; + grn_mrb_value_to_raw_data_buffer min_buffer; + void *max = NULL; + unsigned int max_size = 0; + grn_mrb_value_to_raw_data_buffer max_buffer; + int offset = 0; + int limit = -1; + int flags = 0; + + mrb_get_args(mrb, "o|H", &mrb_table, &mrb_options); + + table = DATA_PTR(mrb_table); + grn_mrb_value_to_raw_data_buffer_init(mrb, &min_buffer); + grn_mrb_value_to_raw_data_buffer_init(mrb, &max_buffer); + if (!mrb_nil_p(mrb_options)) { + grn_id key_domain_id; + mrb_value mrb_min; + mrb_value mrb_max; + mrb_value mrb_flags; + + if (table->header.type == GRN_DB) { + key_domain_id = GRN_DB_SHORT_TEXT; + } else { + key_domain_id = table->header.domain; + } + + mrb_min = grn_mrb_options_get_lit(mrb, mrb_options, "min"); + grn_mrb_value_to_raw_data(mrb, "min", mrb_min, + key_domain_id, &min_buffer, &min, &min_size); + + mrb_max = grn_mrb_options_get_lit(mrb, mrb_options, "max"); + grn_mrb_value_to_raw_data(mrb, "max", mrb_max, + key_domain_id, &max_buffer, &max, &max_size); + + mrb_flags = grn_mrb_options_get_lit(mrb, mrb_options, "flags"); + if (!mrb_nil_p(mrb_flags)) { + flags = mrb_fixnum(mrb_flags); + } + } + table_cursor = grn_table_cursor_open(ctx, table, + min, min_size, + max, max_size, + offset, limit, flags); + grn_mrb_value_to_raw_data_buffer_fin(mrb, &min_buffer); + grn_mrb_value_to_raw_data_buffer_fin(mrb, &max_buffer); + grn_mrb_ctx_check(mrb); + + return mrb_funcall(mrb, klass, "new", 1, mrb_cptr_value(mrb, table_cursor)); +} + +static mrb_value +mrb_grn_table_cursor_initialize(mrb_state *mrb, mrb_value self) +{ + mrb_value mrb_table_cursor_ptr; + + mrb_get_args(mrb, "o", &mrb_table_cursor_ptr); + DATA_TYPE(self) = &mrb_grn_table_cursor_type; + DATA_PTR(self) = mrb_cptr(mrb_table_cursor_ptr); + + return self; +} + +static mrb_value +mrb_grn_table_cursor_close(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + grn_table_cursor *table_cursor; + + table_cursor = DATA_PTR(self); + if (table_cursor) { + DATA_PTR(self) = NULL; + grn_table_cursor_close(ctx, table_cursor); + grn_mrb_ctx_check(mrb); + } + + return mrb_nil_value(); +} + +static mrb_value +mrb_grn_table_cursor_next(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + grn_id id; + + id = grn_table_cursor_next(ctx, DATA_PTR(self)); + grn_mrb_ctx_check(mrb); + + return mrb_fixnum_value(id); +} + +static mrb_value +mrb_grn_table_cursor_count(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + int n_records = 0; + + while (grn_table_cursor_next(ctx, DATA_PTR(self)) != GRN_ID_NIL) { + n_records++; + } + + return mrb_fixnum_value(n_records); +} + +void +grn_mrb_table_cursor_init(grn_ctx *ctx) +{ + grn_mrb_data *data = &(ctx->impl->mrb); + mrb_state *mrb = data->state; + struct RClass *module = data->module; + struct RClass *klass; + + klass = mrb_define_class_under(mrb, module, "TableCursor", mrb->object_class); + MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA); + + mrb_define_singleton_method(mrb, (struct RObject *)klass, "open_raw", + mrb_grn_table_cursor_singleton_open_raw, + MRB_ARGS_ARG(1, 1)); + + mrb_define_method(mrb, klass, "initialize", + mrb_grn_table_cursor_initialize, MRB_ARGS_REQ(1)); + mrb_define_method(mrb, klass, "close", + mrb_grn_table_cursor_close, MRB_ARGS_NONE()); + mrb_define_method(mrb, klass, "next", + mrb_grn_table_cursor_next, MRB_ARGS_NONE()); + mrb_define_method(mrb, klass, "count", + mrb_grn_table_cursor_count, MRB_ARGS_NONE()); +} +#endif diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_table_cursor.h b/storage/mroonga/vendor/groonga/lib/mrb/mrb_table_cursor.h new file mode 100644 index 00000000000..f05ab0977a1 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_table_cursor.h @@ -0,0 +1,34 @@ +/* -*- 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 +*/ + +#ifndef GRN_MRB_TABLE_CURSOR_H +#define GRN_MRB_TABLE_CURSOR_H + +#include "../grn_ctx.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void grn_mrb_table_cursor_init(grn_ctx *ctx); + +#ifdef __cplusplus +} +#endif + +#endif /* GRN_MRB_TABLE_CURSOR_H */ diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_table_cursor_flags.c b/storage/mroonga/vendor/groonga/lib/mrb/mrb_table_cursor_flags.c new file mode 100644 index 00000000000..8b889fc5b47 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_table_cursor_flags.c @@ -0,0 +1,60 @@ +/* -*- 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 "../grn_ctx_impl.h" + +#ifdef GRN_WITH_MRUBY +#include <mruby.h> +#include <mruby/class.h> + +#include "mrb_table_cursor_flags.h" + +void +grn_mrb_table_cursor_flags_init(grn_ctx *ctx) +{ + grn_mrb_data *data = &(ctx->impl->mrb); + mrb_state *mrb = data->state; + struct RClass *module = data->module; + struct RClass *flags_module; + + flags_module = mrb_define_module_under(mrb, module, "TableCursorFlags"); + + mrb_define_const(mrb, flags_module, "ASCENDING", + mrb_fixnum_value(GRN_CURSOR_ASCENDING)); + mrb_define_const(mrb, flags_module, "DESCENDING", + mrb_fixnum_value(GRN_CURSOR_DESCENDING)); + mrb_define_const(mrb, flags_module, "GE", + mrb_fixnum_value(GRN_CURSOR_GE)); + mrb_define_const(mrb, flags_module, "GT", + mrb_fixnum_value(GRN_CURSOR_GT)); + mrb_define_const(mrb, flags_module, "LE", + mrb_fixnum_value(GRN_CURSOR_LE)); + mrb_define_const(mrb, flags_module, "LT", + mrb_fixnum_value(GRN_CURSOR_LT)); + mrb_define_const(mrb, flags_module, "BY_KEY", + mrb_fixnum_value(GRN_CURSOR_BY_KEY)); + mrb_define_const(mrb, flags_module, "BY_ID", + mrb_fixnum_value(GRN_CURSOR_BY_ID)); + mrb_define_const(mrb, flags_module, "PREFIX", + mrb_fixnum_value(GRN_CURSOR_PREFIX)); + mrb_define_const(mrb, flags_module, "SIZE_BY_BIT", + mrb_fixnum_value(GRN_CURSOR_SIZE_BY_BIT)); + mrb_define_const(mrb, flags_module, "RK", + mrb_fixnum_value(GRN_CURSOR_RK)); +} +#endif diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_table_cursor_flags.h b/storage/mroonga/vendor/groonga/lib/mrb/mrb_table_cursor_flags.h new file mode 100644 index 00000000000..f336cde9325 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_table_cursor_flags.h @@ -0,0 +1,34 @@ +/* -*- 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 +*/ + +#ifndef GRN_MRB_TABLE_CURSOR_FLAGS_H +#define GRN_MRB_TABLE_CURSOR_FLAGS_H + +#include "../grn_ctx.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void grn_mrb_table_cursor_flags_init(grn_ctx *ctx); + +#ifdef __cplusplus +} +#endif + +#endif /* GRN_MRB_TABLE_CURSOR_FLAGS_H */ diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_type.c b/storage/mroonga/vendor/groonga/lib/mrb/mrb_type.c new file mode 100644 index 00000000000..9fe602e6c40 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_type.c @@ -0,0 +1,60 @@ +/* -*- 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 "../grn_ctx_impl.h" + +#ifdef GRN_WITH_MRUBY +#include <mruby.h> +#include <mruby/class.h> +#include <mruby/data.h> + +#include "mrb_ctx.h" +#include "mrb_type.h" + +static struct mrb_data_type mrb_grn_type_type = { + "Groonga::Type", + NULL +}; + +static mrb_value +mrb_grn_type_initialize(mrb_state *mrb, mrb_value self) +{ + mrb_value mrb_type_ptr; + + mrb_get_args(mrb, "o", &mrb_type_ptr); + DATA_TYPE(self) = &mrb_grn_type_type; + DATA_PTR(self) = mrb_cptr(mrb_type_ptr); + return self; +} + +void +grn_mrb_type_init(grn_ctx *ctx) +{ + grn_mrb_data *data = &(ctx->impl->mrb); + mrb_state *mrb = data->state; + struct RClass *module = data->module; + struct RClass *object_class = data->object_class; + struct RClass *klass; + + klass = mrb_define_class_under(mrb, module, "Type", object_class); + MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA); + + mrb_define_method(mrb, klass, "initialize", + mrb_grn_type_initialize, MRB_ARGS_REQ(1)); +} +#endif diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_obj.h b/storage/mroonga/vendor/groonga/lib/mrb/mrb_type.h index 31d53240f9e..f86167443a6 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/mrb_obj.h +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_type.h @@ -1,6 +1,6 @@ /* -*- c-basic-offset: 2 -*- */ /* - Copyright(C) 2013 Brazil + 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 @@ -16,19 +16,19 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#ifndef GRN_MRB_OBJ_H -#define GRN_MRB_OBJ_H +#ifndef GRN_MRB_TYPE_H +#define GRN_MRB_TYPE_H -#include "../ctx.h" +#include "../grn_ctx.h" #ifdef __cplusplus extern "C" { #endif -void grn_mrb_obj_init(grn_ctx *ctx); +void grn_mrb_type_init(grn_ctx *ctx); #ifdef __cplusplus } #endif -#endif /* GRN_MRB_OBJ_H */ +#endif /* GRN_MRB_TYPE_H */ diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_variable_size_column.c b/storage/mroonga/vendor/groonga/lib/mrb/mrb_variable_size_column.c index 9073b326f85..153b28066d8 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/mrb_variable_size_column.c +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_variable_size_column.c @@ -16,7 +16,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "../ctx_impl.h" +#include "../grn_ctx_impl.h" #ifdef GRN_WITH_MRUBY #include <mruby.h> diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_variable_size_column.h b/storage/mroonga/vendor/groonga/lib/mrb/mrb_variable_size_column.h index d120f737671..c904e7023b1 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/mrb_variable_size_column.h +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_variable_size_column.h @@ -19,7 +19,7 @@ #ifndef GRN_MRB_VARIABLE_SIZE_COLUMN_H #define GRN_MRB_VARIABLE_SIZE_COLUMN_H -#include "../ctx.h" +#include "../grn_ctx.h" #ifdef __cplusplus extern "C" { diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_void.c b/storage/mroonga/vendor/groonga/lib/mrb/mrb_void.c index 976c472be92..bdc451930f6 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/mrb_void.c +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_void.c @@ -16,7 +16,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "../ctx_impl.h" +#include "../grn_ctx_impl.h" #ifdef GRN_WITH_MRUBY #include <mruby.h> @@ -24,7 +24,7 @@ #include <mruby/variable.h> #include <mruby/data.h> -#include "../db.h" +#include "../grn_db.h" #include "mrb_void.h" static struct mrb_data_type mrb_grn_void_type = { diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_void.h b/storage/mroonga/vendor/groonga/lib/mrb/mrb_void.h index a766cecb0b7..474d7804c73 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/mrb_void.h +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_void.h @@ -19,7 +19,7 @@ #ifndef GRN_MRB_VOID_H #define GRN_MRB_VOID_H -#include "../ctx.h" +#include "../grn_ctx.h" #ifdef __cplusplus extern "C" { diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_writer.c b/storage/mroonga/vendor/groonga/lib/mrb/mrb_writer.c new file mode 100644 index 00000000000..e35a066e560 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_writer.c @@ -0,0 +1,238 @@ +/* -*- 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" + +#ifdef GRN_WITH_MRUBY +#include <mruby.h> +#include <mruby/data.h> +#include <mruby/hash.h> +#include <mruby/string.h> + +#include "../grn_mrb.h" +#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) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + mrb_value target; + + mrb_get_args(mrb, "o", &target); + + switch (mrb_type(target)) { + case MRB_TT_FALSE : + GRN_OUTPUT_BOOL(GRN_FALSE); + break; + case MRB_TT_TRUE : + GRN_OUTPUT_BOOL(GRN_TRUE); + break; + case MRB_TT_FIXNUM : + GRN_OUTPUT_INT32(mrb_fixnum(target)); + break; + case MRB_TT_FLOAT : + GRN_OUTPUT_FLOAT(mrb_float(target)); + break; + case MRB_TT_STRING : + GRN_OUTPUT_STR(RSTRING_PTR(target), RSTRING_LEN(target)); + break; + default : + mrb_raisef(mrb, E_ARGUMENT_ERROR, + "must be true, false, number, float or string: %S", target); + break; + } + + return mrb_nil_value(); +} + +static mrb_value +writer_open_array(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + char *name; + mrb_int n_elements; + + mrb_get_args(mrb, "zi", &name, &n_elements); + GRN_OUTPUT_ARRAY_OPEN(name, n_elements); + + return mrb_nil_value(); +} + +static mrb_value +writer_close_array(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + + GRN_OUTPUT_ARRAY_CLOSE(); + + return mrb_nil_value(); +} + +static mrb_value +writer_open_map(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + char *name; + mrb_int n_elements; + + mrb_get_args(mrb, "zi", &name, &n_elements); + GRN_OUTPUT_MAP_OPEN(name, n_elements); + + return mrb_nil_value(); +} + +static mrb_value +writer_close_map(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + + GRN_OUTPUT_MAP_CLOSE(); + + return mrb_nil_value(); +} + +static mrb_value +writer_write_table_columns(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + mrb_value mrb_table; + char *columns; + mrb_int columns_size; + grn_obj *table; + grn_obj_format format; + int n_hits = 0; + int offset = 0; + int limit = 0; + int hits_offset = 0; + + mrb_get_args(mrb, "os", &mrb_table, &columns, &columns_size); + + table = DATA_PTR(mrb_table); + GRN_OBJ_FORMAT_INIT(&format, n_hits, offset, limit, hits_offset); + format.flags |= GRN_OBJ_FORMAT_WITH_COLUMN_NAMES; + { + grn_rc rc; + rc = grn_output_format_set_columns(ctx, &format, + table, columns, columns_size); + if (rc != GRN_SUCCESS) { + GRN_OBJ_FORMAT_FIN(ctx, &format); + grn_mrb_ctx_check(mrb); + } + } + GRN_OUTPUT_TABLE_COLUMNS(table, &format); + GRN_OBJ_FORMAT_FIN(ctx, &format); + + return mrb_nil_value(); +} + +static mrb_value +writer_write_table_records(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + mrb_value mrb_table; + mrb_value mrb_options = mrb_nil_value(); + char *columns; + mrb_int columns_size; + grn_obj *table; + grn_obj_format format; + int n_hits = 0; + int offset = 0; + int limit = -1; + int hits_offset = 0; + + mrb_get_args(mrb, "os|H", &mrb_table, &columns, &columns_size, &mrb_options); + + table = DATA_PTR(mrb_table); + if (!mrb_nil_p(mrb_options)) { + mrb_value mrb_offset; + mrb_value mrb_limit; + + mrb_offset = grn_mrb_options_get_lit(mrb, mrb_options, "offset"); + if (!mrb_nil_p(mrb_offset)) { + offset = mrb_fixnum(mrb_offset); + } + + mrb_limit = grn_mrb_options_get_lit(mrb, mrb_options, "limit"); + if (!mrb_nil_p(mrb_limit)) { + limit = mrb_fixnum(mrb_limit); + } + } + if (limit < 0) { + limit = grn_table_size(ctx, table) + limit + 1; + } + GRN_OBJ_FORMAT_INIT(&format, n_hits, offset, limit, hits_offset); + { + grn_rc rc; + rc = grn_output_format_set_columns(ctx, &format, + table, columns, columns_size); + if (rc != GRN_SUCCESS) { + GRN_OBJ_FORMAT_FIN(ctx, &format); + grn_mrb_ctx_check(mrb); + } + } + GRN_OUTPUT_TABLE_RECORDS(table, &format); + GRN_OBJ_FORMAT_FIN(ctx, &format); + + return mrb_nil_value(); +} + +static mrb_value +writer_set_content_type(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + grn_content_type content_type; + + mrb_get_args(mrb, "i", &content_type); + + grn_ctx_set_output_type(ctx, content_type); + + return mrb_nil_value(); +} + +void +grn_mrb_writer_init(grn_ctx *ctx) +{ + grn_mrb_data *data = &(ctx->impl->mrb); + mrb_state *mrb = data->state; + struct RClass *module = data->module; + struct RClass *klass; + + klass = mrb_define_class_under(mrb, module, "Writer", mrb->object_class); + + mrb_define_method(mrb, klass, "write", writer_write, MRB_ARGS_REQ(1)); + mrb_define_method(mrb, klass, "open_array", + writer_open_array, MRB_ARGS_REQ(2)); + mrb_define_method(mrb, klass, "close_array", + writer_close_array, MRB_ARGS_NONE()); + mrb_define_method(mrb, klass, "open_map", + writer_open_map, MRB_ARGS_REQ(2)); + mrb_define_method(mrb, klass, "close_map", + writer_close_map, MRB_ARGS_NONE()); + + mrb_define_method(mrb, klass, "write_table_columns", + writer_write_table_columns, MRB_ARGS_REQ(2)); + mrb_define_method(mrb, klass, "write_table_records", + writer_write_table_records, MRB_ARGS_ARG(2, 1)); + + mrb_define_method(mrb, klass, "content_type=", + writer_set_content_type, MRB_ARGS_REQ(1)); +} +#endif diff --git a/storage/mroonga/vendor/groonga/lib/mrb/mrb_writer.h b/storage/mroonga/vendor/groonga/lib/mrb/mrb_writer.h new file mode 100644 index 00000000000..a6b4a6b3a75 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/mrb_writer.h @@ -0,0 +1,34 @@ +/* -*- 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_WRITER_H +#define GRN_MRB_WRITER_H + +#include "../grn_ctx.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void grn_mrb_writer_init(grn_ctx *ctx); + +#ifdef __cplusplus +} +#endif + +#endif /* GRN_MRB_WRITER_H */ diff --git a/storage/mroonga/vendor/groonga/lib/mrb/scripts/Makefile.am b/storage/mroonga/vendor/groonga/lib/mrb/scripts/Makefile.am index 631923e2e06..9b6acf8bbda 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/scripts/Makefile.am +++ b/storage/mroonga/vendor/groonga/lib/mrb/scripts/Makefile.am @@ -1,3 +1,9 @@ +SUBDIRS = \ + command_line \ + context \ + initialize \ + logger + include sources.am EXTRA_DIST = \ @@ -5,5 +11,5 @@ EXTRA_DIST = \ $(RUBY_SCRIPT_FILES) if WITH_MRUBY -nobase_ruby_scripts_DATA = $(RUBY_SCRIPT_FILES) +ruby_scripts_DATA = $(RUBY_SCRIPT_FILES) endif diff --git a/storage/mroonga/vendor/groonga/lib/mrb/scripts/backtrace_entry.rb b/storage/mroonga/vendor/groonga/lib/mrb/scripts/backtrace_entry.rb index 68ea9e4b6f2..34f95e968f5 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/scripts/backtrace_entry.rb +++ b/storage/mroonga/vendor/groonga/lib/mrb/scripts/backtrace_entry.rb @@ -5,16 +5,24 @@ module Groonga match_data = /:(\d+):?/.match(entry) file = match_data.pre_match line = match_data[1].to_i - method = match_data.post_match.gsub(/\Ain /, "") - new(file, line, method) + detail_match_data = /\A(in )?(\S+)\s*/.match(match_data.post_match) + if detail_match_data[1] + method = detail_match_data[2] + message = detail_match_data.post_match + else + method = "" + message = match_data.post_match + end + new(file, line, method, message) end end - attr_reader :file, :line, :method - def initialize(file, line, method) + attr_reader :file, :line, :method, :message + def initialize(file, line, method, message) @file = file @line = line @method = method + @message = message end end end diff --git a/storage/mroonga/vendor/groonga/lib/mrb/scripts/command.rb b/storage/mroonga/vendor/groonga/lib/mrb/scripts/command.rb new file mode 100644 index 00000000000..c4e3e94bc4e --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/scripts/command.rb @@ -0,0 +1,35 @@ +module Groonga + class Command + @@classes = {} + class << self + def register_class(name, klass) + @@classes[name] = klass + end + + def find_class(name) + @@classes[name] + end + end + + private + def context + @context ||= Context.instance + end + + def writer + @writer ||= context.writer + end + + def run_internal(input) + begin + run_body(input) + rescue GroongaError => groonga_error + context.set_groonga_error(groonga_error) + nil + rescue => error + context.record_error(:command_error, error) + nil + end + end + end +end diff --git a/storage/mroonga/vendor/groonga/lib/mrb/scripts/command_line/Makefile.am b/storage/mroonga/vendor/groonga/lib/mrb/scripts/command_line/Makefile.am new file mode 100644 index 00000000000..8d580810674 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/scripts/command_line/Makefile.am @@ -0,0 +1,9 @@ +include sources.am + +EXTRA_DIST = \ + $(RUBY_SCRIPT_FILES) + +if WITH_MRUBY +ruby_scripts_command_linedir = $(ruby_scriptsdir)/command_line +ruby_scripts_command_line_DATA = $(RUBY_SCRIPT_FILES) +endif diff --git a/storage/mroonga/vendor/groonga/lib/mrb/scripts/command_line/grndb.rb b/storage/mroonga/vendor/groonga/lib/mrb/scripts/command_line/grndb.rb new file mode 100644 index 00000000000..96a99c80223 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/scripts/command_line/grndb.rb @@ -0,0 +1,169 @@ +module Groonga + module CommandLine + class Grndb + def initialize(argv) + @command, *@arguments = argv + @succeeded = true + @executed = false + @database_path = nil + end + + def run + slop = create_slop + rest = nil + begin + rest = slop.parse(@arguments) + rescue Slop::Error + $stderr.puts($!.message) + return false + end + + if slop.help? + $stdout.puts(slop.help) + return true + end + + unless @executed + if rest.empty? + $stderr.puts("No command is specified.") + else + $stderr.puts("Unknown command: <#{rest.first}>") + end + return false + end + + @succeeded + end + + private + def create_slop + slop = Slop.new + command_name = File.basename(@command) + slop.banner = "Usage: #{command_name} COMMAND [OPTIONS] DB_PATH" + slop_enable_help(slop) + + slop.command "check" do |command| + command.description "Check database" + slop_enable_help(command) + + command.run do |options, arguments| + run_command(options, arguments) do |database, new_arguments| + check(database, options, new_arguments) + end + end + end + + slop.command "recover" do |command| + command.description "Recover database" + slop_enable_help(command) + + command.run do |options, arguments| + run_command(options, arguments) do |database, new_arguments| + recover(database, options, new_arguments) + end + end + end + + slop + end + + def slop_enable_help(slop) + slop.on("-h", "--help", "Display this help message.", :tail => true) + end + + def open_database(arguments) + if arguments.empty? + $stderr.puts("Database path is missing") + @succeesed = false + return + end + + database = nil + @database_path, *rest_arguments = arguments + begin + database = Database.open(@database_path) + rescue Error => error + $stderr.puts("Failed to open database: <#{@database_path}>") + $stderr.puts(error.message) + @succeeded = false + return + end + + begin + yield(database, rest_arguments) + ensure + database.close + end + end + + def run_command(options, arguments) + @executed = true + + if options.help? + $stdout.puts(options.help) + return + end + + open_database(arguments) do |database| + yield(database) + end + end + + def recover(database, options, arguments) + begin + database.recover + rescue Error => error + $stderr.puts("Failed to recover database: <#{@database_path}>") + $stderr.puts(error.message) + @succeeded = false + end + end + + def check(database, options, arguments) + if database.locked? + message = + "Database is locked. " + + "It may be broken. " + + "Re-create the database." + $stdout.puts(message) + @succeeded = false + end + + database.each do |object| + case object + when IndexColumn + next unless object.locked? + message = + "[#{object.name}] Index column is locked. " + + "It may be broken. " + + "Re-create index by '#{@command} recover #{@database_path}'." + $stdout.puts(message) + @succeeded = false + when Column + next unless object.locked? + name = object.name + message = + "[#{name}] Data column is locked. " + + "It may be broken. " + + "(1) Truncate the column (truncate #{name}) or " + + "clear lock of the column (lock_clear #{name}) " + + "and (2) load data again." + $stdout.puts(message) + @succeeded = false + when Table + next unless object.locked? + name = object.name + message = + "[#{name}] Table is locked. " + + "It may be broken. " + + "(1) Truncate the table (truncate #{name}) or " + + "clear lock of the table (lock_clear #{name}) " + + "and (2) load data again." + $stdout.puts(message) + @succeeded = false + end + end + end + end + end +end diff --git a/storage/mroonga/vendor/groonga/lib/mrb/scripts/command_line/sources.am b/storage/mroonga/vendor/groonga/lib/mrb/scripts/command_line/sources.am new file mode 100644 index 00000000000..759948eecd6 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/scripts/command_line/sources.am @@ -0,0 +1,2 @@ +RUBY_SCRIPT_FILES = \ + grndb.rb diff --git a/storage/mroonga/vendor/groonga/lib/mrb/scripts/context.rb b/storage/mroonga/vendor/groonga/lib/mrb/scripts/context.rb index 8ebe2c913b4..e65d6653110 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/scripts/context.rb +++ b/storage/mroonga/vendor/groonga/lib/mrb/scripts/context.rb @@ -1,3 +1,6 @@ +require "context/error_level" +require "context/rc" + module Groonga class Context def guard(fallback=nil) @@ -13,19 +16,37 @@ module Groonga @logger ||= Logger.new end + def writer + @writer ||= Writer.new + end + + def set_groonga_error(groonga_error) + set_error_raw(groonga_error.class.rc, + ErrorLevel::ERROR, + groonga_error.message, + groonga_error.backtrace) + end + def record_error(rc, error) rc = RC.find(rc) if rc.is_a?(Symbol) + set_error_raw(rc, ErrorLevel::ERROR, error.message, error.backtrace) + + logger.log_error(error) + end + + private + def set_error_raw(rc, error_level, message, backtrace) self.rc = rc.to_i - self.error_level = ErrorLevel.find(:error).to_i + self.error_level = error_level.to_i - backtrace = error.backtrace - entry = BacktraceEntry.parse(backtrace.first) - self.error_file = entry.file - self.error_line = entry.line - self.error_method = entry.method - self.error_message = error.message + self.error_message = message - logger.log_error(error) + if backtrace + entry = BacktraceEntry.parse(backtrace.first) + self.error_file = entry.file + self.error_line = entry.line + self.error_method = entry.method + end end end end diff --git a/storage/mroonga/vendor/groonga/lib/mrb/scripts/context/Makefile.am b/storage/mroonga/vendor/groonga/lib/mrb/scripts/context/Makefile.am new file mode 100644 index 00000000000..8d862082cce --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/scripts/context/Makefile.am @@ -0,0 +1,9 @@ +include sources.am + +EXTRA_DIST = \ + $(RUBY_SCRIPT_FILES) + +if WITH_MRUBY +ruby_scripts_contextdir = $(ruby_scriptsdir)/context +ruby_scripts_context_DATA = $(RUBY_SCRIPT_FILES) +endif diff --git a/storage/mroonga/vendor/groonga/lib/mrb/scripts/context/rc.rb b/storage/mroonga/vendor/groonga/lib/mrb/scripts/context/rc.rb index 2863c7f6195..f06b32d78b6 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/scripts/context/rc.rb +++ b/storage/mroonga/vendor/groonga/lib/mrb/scripts/context/rc.rb @@ -5,13 +5,19 @@ module Groonga class << self def find(name) - @@names[name] + @@names[name] || UNKNOWN_ERROR + end + + def register(name, code, error_class) + rc = new(name, code) + @@names[name] = rc + error_class.rc = rc if error_class + rc end end attr_reader :name def initialize(name, code) - @@names[name] = self @name = name @code = code end @@ -20,80 +26,164 @@ module Groonga @code end - SUCCESS = new(:success, 0) - END_OF_DATA = new(:end_of_data, 1) - UNKNOWN_ERROR = new(:unknown_error, -1) - OPERATION_NOT_PERMITTED = new(:operation_not_permitted, -2) - NO_SUCH_FILE_OR_DIRECTORY = new(:no_such_file_or_directory, -3) - NO_SUCH_PROCESS = new(:no_such_process, -4) - INTERRUPTED_FUNCTION_CALL = new(:interrupted_function_call, -5) - INPUT_OUTPUT_ERROR = new(:input_output_error, -6) - NO_SUCH_DEVICE_OR_ADDRESS = new(:no_such_device_or_address, -7) - ARG_LIST_TOO_LONG = new(:arg_list_too_long, -8) - EXEC_FORMAT_ERROR = new(:exec_format_error, -9) - BAD_FILE_DESCRIPTOR = new(:bad_file_descriptor, -10) - NO_CHILD_PROCESSES = new(:no_child_processes, -11) - RESOURCE_TEMPORARILY_UNAVAILABLE = new(:resource_temporarily_unavailable, -12) - NOT_ENOUGH_SPACE = new(:not_enough_space, -13) - PERMISSION_DENIED = new(:permission_denied, -14) - BAD_ADDRESS = new(:bad_address, -15) - RESOURCE_BUSY = new(:resource_busy, -16) - FILE_EXISTS = new(:file_exists, -17) - IMPROPER_LINK = new(:improper_link, -18) - NO_SUCH_DEVICE = new(:no_such_device, -19) - NOT_A_DIRECTORY = new(:not_a_directory, -20) - IS_A_DIRECTORY = new(:is_a_directory, -21) - INVALID_ARGUMENT = new(:invalid_argument, -22) - TOO_MANY_OPEN_FILES_IN_SYSTEM = new(:too_many_open_files_in_system, -23) - TOO_MANY_OPEN_FILES = new(:too_many_open_files, -24) - INAPPROPRIATE_IO_CONTROL_OPERATION = new(:inappropriate_io_control_operation, -25) - FILE_TOO_LARGE = new(:file_too_large, -26) - NO_SPACE_LEFT_ON_DEVICE = new(:no_space_left_on_device, -27) - INVALID_SEEK = new(:invalid_seek, -28) - READ_ONLY_FILE_SYSTEM = new(:read_only_file_system, -29) - TOO_MANY_LINKS = new(:too_many_links, -30) - BROKEN_PIPE = new(:broken_pipe, -31) - DOMAIN_ERROR = new(:domain_error, -32) - RESULT_TOO_LARGE = new(:result_too_large, -33) - RESOURCE_DEADLOCK_AVOIDED = new(:resource_deadlock_avoided, -34) - NO_MEMORY_AVAILABLE = new(:no_memory_available, -35) - FILENAME_TOO_LONG = new(:filename_too_long, -36) - NO_LOCKS_AVAILABLE = new(:no_locks_available, -37) - FUNCTION_NOT_IMPLEMENTED = new(:function_not_implemented, -38) - DIRECTORY_NOT_EMPTY = new(:directory_not_empty, -39) - ILLEGAL_BYTE_SEQUENCE = new(:illegal_byte_sequence, -40) - SOCKET_NOT_INITIALIZED = new(:socket_not_initialized, -41) - OPERATION_WOULD_BLOCK = new(:operation_would_block, -42) - ADDRESS_IS_NOT_AVAILABLE = new(:address_is_not_available, -43) - NETWORK_IS_DOWN = new(:network_is_down, -44) - NO_BUFFER = new(:no_buffer, -45) - SOCKET_IS_ALREADY_CONNECTED = new(:socket_is_already_connected, -46) - SOCKET_IS_NOT_CONNECTED = new(:socket_is_not_connected, -47) - SOCKET_IS_ALREADY_SHUTDOWNED = new(:socket_is_already_shutdowned, -48) - OPERATION_TIMEOUT = new(:operation_timeout, -49) - CONNECTION_REFUSED = new(:connection_refused, -50) - RANGE_ERROR = new(:range_error, -51) - TOKENIZER_ERROR = new(:tokenizer_error, -52) - FILE_CORRUPT = new(:file_corrupt, -53) - INVALID_FORMAT = new(:invalid_format, -54) - OBJECT_CORRUPT = new(:object_corrupt, -55) - TOO_MANY_SYMBOLIC_LINKS = new(:too_many_symbolic_links, -56) - NOT_SOCKET = new(:not_socket, -57) - OPERATION_NOT_SUPPORTED = new(:operation_not_supported, -58) - ADDRESS_IS_IN_USE = new(:address_is_in_use, -59) - ZLIB_ERROR = new(:zlib_error, -60) - LZO_ERROR = new(:lzo_error, -61) - STACK_OVER_FLOW = new(:stack_over_flow, -62) - SYNTAX_ERROR = new(:syntax_error, -63) - RETRY_MAX = new(:retry_max, -64) - INCOMPATIBLE_FILE_FORMAT = new(:incompatible_file_format, -65) - UPDATE_NOT_ALLOWED = new(:update_not_allowed, -66) - TOO_SMALL_OFFSET = new(:too_small_offset, -67) - TOO_LARGE_OFFSET = new(:too_large_offset, -68) - TOO_SMALL_LIMIT = new(:too_small_limit, -69) - CAS_ERROR = new(:cas_error, -70) - UNSUPPORTED_COMMAND_VERSION = new(:unsupported_command_version, -71) - NORMALIZER_ERROR = new(:normalizer_error, -72) + SUCCESS = + register(:success, 0, nil) + END_OF_DATA = + register(:end_of_data, 1, EndOfData) + UNKNOWN_ERROR = + register(:unknown_error, -1, UnknownError) + OPERATION_NOT_PERMITTED = + register(:operation_not_permitted, -2, OperationNotPermitted) + NO_SUCH_FILE_OR_DIRECTORY = + register(:no_such_file_or_directory, -3, NoSuchFileOrDirectory) + NO_SUCH_PROCESS = + register(:no_such_process, -4, NoSuchProcess) + INTERRUPTED_FUNCTION_CALL = + register(:interrupted_function_call, -5, InterruptedFunctionCall) + INPUT_OUTPUT_ERROR = + register(:input_output_error, -6, InputOutputError) + NO_SUCH_DEVICE_OR_ADDRESS = + register(:no_such_device_or_address, -7, NoSuchDeviceOrAddress) + ARG_LIST_TOO_LONG = + register(:arg_list_too_long, -8, ArgListTooLong) + EXEC_FORMAT_ERROR = + register(:exec_format_error, -9, ExecFormatError) + BAD_FILE_DESCRIPTOR = + register(:bad_file_descriptor, -10, BadFileDescriptor) + NO_CHILD_PROCESSES = + register(:no_child_processes, -11, NoChildProcesses) + RESOURCE_TEMPORARILY_UNAVAILABLE = + register(:resource_temporarily_unavailable, -12, + ResourceTemporarilyUnavailable) + NOT_ENOUGH_SPACE = + register(:not_enough_space, -13, NotEnoughSpace) + PERMISSION_DENIED = + register(:permission_denied, -14, PermissionDenied) + BAD_ADDRESS = + register(:bad_address, -15, BadAddress) + RESOURCE_BUSY = + register(:resource_busy, -16, ResourceBusy) + FILE_EXISTS = + register(:file_exists, -17, FileExists) + IMPROPER_LINK = + register(:improper_link, -18, ImproperLink) + NO_SUCH_DEVICE = + register(:no_such_device, -19, NoSuchDevice) + NOT_DIRECTORY = + register(:not_directory, -20, NotDirectory) + IS_DIRECTORY = + register(:is_directory, -21, IsDirectory) + INVALID_ARGUMENT = + register(:invalid_argument, -22, InvalidArgument) + TOO_MANY_OPEN_FILES_IN_SYSTEM = + register(:too_many_open_files_in_system, -23, TooManyOpenFilesInSystem) + TOO_MANY_OPEN_FILES = + register(:too_many_open_files, -24, TooManyOpenFiles) + INAPPROPRIATE_IO_CONTROL_OPERATION = + register(:inappropriate_io_control_operation, -25, + InappropriateIOControlOperation) + FILE_TOO_LARGE = + register(:file_too_large, -26, FileTooLarge) + NO_SPACE_LEFT_ON_DEVICE = + register(:no_space_left_on_device, -27, NoSpaceLeftOnDevice) + INVALID_SEEK = + register(:invalid_seek, -28, InvalidSeek) + READ_ONLY_FILE_SYSTEM = + register(:read_only_file_system, -29, ReadOnlyFileSystem) + TOO_MANY_LINKS = + register(:too_many_links, -30, TooManyLinks) + BROKEN_PIPE = + register(:broken_pipe, -31, BrokenPipe) + DOMAIN_ERROR = + register(:domain_error, -32, DomainError) + RESULT_TOO_LARGE = + register(:result_too_large, -33, ResultTooLarge) + RESOURCE_DEADLOCK_AVOIDED = + register(:resource_deadlock_avoided, -34, ResourceDeadlockAvoided) + NO_MEMORY_AVAILABLE = + register(:no_memory_available, -35, NoMemoryAvailable) + FILENAME_TOO_LONG = + register(:filename_too_long, -36, FilenameTooLong) + NO_LOCKS_AVAILABLE = + register(:no_locks_available, -37, NoLocksAvailable) + FUNCTION_NOT_IMPLEMENTED = + register(:function_not_implemented, -38, FunctionNotImplemented) + DIRECTORY_NOT_EMPTY = + register(:directory_not_empty, -39, DirectoryNotEmpty) + ILLEGAL_BYTE_SEQUENCE = + register(:illegal_byte_sequence, -40, IllegalByteSequence) + SOCKET_NOT_INITIALIZED = + register(:socket_not_initialized, -41, SocketNotInitialized) + OPERATION_WOULD_BLOCK = + register(:operation_would_block, -42, OperationWouldBlock) + ADDRESS_IS_NOT_AVAILABLE = + register(:address_is_not_available, -43, AddressIsNotAvailable) + NETWORK_IS_DOWN = + register(:network_is_down, -44, NetworkIsDown) + NO_BUFFER = + register(:no_buffer, -45, NoBuffer) + SOCKET_IS_ALREADY_CONNECTED = + register(:socket_is_already_connected, -46, SocketIsAlreadyConnected) + SOCKET_IS_NOT_CONNECTED = + register(:socket_is_not_connected, -47, SocketIsNotConnected) + SOCKET_IS_ALREADY_SHUTDOWNED = + register(:socket_is_already_shutdowned, -48, SocketIsAlreadyShutdowned) + OPERATION_TIMEOUT = + register(:operation_timeout, -49, OperationTimeout) + CONNECTION_REFUSED = + register(:connection_refused, -50, ConnectionRefused) + RANGE_ERROR = + register(:range_error, -51, RangeError) + TOKENIZER_ERROR = + register(:tokenizer_error, -52, TokenizerError) + FILE_CORRUPT = + register(:file_corrupt, -53, FileCorrupt) + INVALID_FORMAT = + register(:invalid_format, -54, InvalidFormat) + OBJECT_CORRUPT = + register(:object_corrupt, -55, ObjectCorrupt) + TOO_MANY_SYMBOLIC_LINKS = + register(:too_many_symbolic_links, -56, TooManySymbolicLinks) + NOT_SOCKET = + register(:not_socket, -57, NotSocket) + OPERATION_NOT_SUPPORTED = + register(:operation_not_supported, -58, OperationNotSupported) + ADDRESS_IS_IN_USE = + register(:address_is_in_use, -59, AddressIsInUse) + ZLIB_ERROR = + register(:zlib_error, -60, ZlibError) + LZ4_ERROR = + register(:lz4_error, -61, LZ4Error) + STACK_OVER_FLOW = + register(:stack_over_flow, -62, StackOverFlow) + SYNTAX_ERROR = + register(:syntax_error, -63, SyntaxError) + RETRY_MAX = + register(:retry_max, -64, RetryMax) + INCOMPATIBLE_FILE_FORMAT = + register(:incompatible_file_format, -65, IncompatibleFileFormat) + UPDATE_NOT_ALLOWED = + register(:update_not_allowed, -66, UpdateNotAllowed) + TOO_SMALL_OFFSET = + register(:too_small_offset, -67, TooSmallOffset) + TOO_LARGE_OFFSET = + register(:too_large_offset, -68, TooLargeOffset) + TOO_SMALL_LIMIT = + register(:too_small_limit, -69, TooSmallLimit) + CAS_ERROR = + register(:cas_error, -70, CASError) + UNSUPPORTED_COMMAND_VERSION = + register(:unsupported_command_version, -71, UnsupportedCommandVersion) + NORMALIZER_ERROR = + register(:normalizer_error, -72, NormalizerError) + TOKEN_FILTER_ERROR = + register(:token_filter_error, -73, TokenFilterError) + COMMAND_ERROR = + register(:command_error, -74, CommandError) + PLUGIN_ERROR = + register(:plugin_error, -75, PluginError) + + GroongaError.rc = UNKNOWN_ERROR end end end diff --git a/storage/mroonga/vendor/groonga/lib/mrb/scripts/context/sources.am b/storage/mroonga/vendor/groonga/lib/mrb/scripts/context/sources.am new file mode 100644 index 00000000000..1f0d1624ce5 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/scripts/context/sources.am @@ -0,0 +1,3 @@ +RUBY_SCRIPT_FILES = \ + error_level.rb \ + rc.rb diff --git a/storage/mroonga/vendor/groonga/lib/mrb/scripts/database.rb b/storage/mroonga/vendor/groonga/lib/mrb/scripts/database.rb new file mode 100644 index 00000000000..54d9dc1a631 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/scripts/database.rb @@ -0,0 +1,39 @@ +module Groonga + class Database + def each + context = Context.instance + flags = + TableCursorFlags::ASCENDING | + TableCursorFlags::BY_ID + TableCursor.open(self, :flags => flags) do |cursor| + cursor.each do |id| + object = context[id] + yield(object) if object + end + end + end + + def each_table(options={}) + context = Context.instance + min = options[:prefix] + flags = 0 + if options[:order] == :descending + flags |= TableCursorFlags::DESCENDING + else + flags |= TableCursorFlags::ASCENDING + end + if options[:order_by] == :id + flags |= TableCursorFlags::BY_ID + else + flags |= TableCursorFlags::BY_KEY + end + flags |= TableCursorFlags::PREFIX if min + TableCursor.open(self, :min => min, :flags => flags) do |cursor| + cursor.each do |id| + object = context[id] + yield(object) if object.is_a?(Table) + end + end + end + end +end diff --git a/storage/mroonga/vendor/groonga/lib/mrb/scripts/error.rb b/storage/mroonga/vendor/groonga/lib/mrb/scripts/error.rb new file mode 100644 index 00000000000..e39c904534a --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/scripts/error.rb @@ -0,0 +1,16 @@ +module Groonga + class GroongaError + class << self + def rc + @rc + end + + def rc=(rc) + @rc = rc + end + end + end + + class ErrorMessage < Error + end +end diff --git a/storage/mroonga/vendor/groonga/lib/mrb/scripts/expression.rb b/storage/mroonga/vendor/groonga/lib/mrb/scripts/expression.rb index 29290e207f2..7b1199b7795 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/scripts/expression.rb +++ b/storage/mroonga/vendor/groonga/lib/mrb/scripts/expression.rb @@ -1,3 +1,8 @@ +require "scan_info" +require "scan_info_builder" + +require "expression_size_estimator" + module Groonga class Expression def build_scan_info(op, size) @@ -9,5 +14,23 @@ module Groonga nil end end + + def estimate_size(table) + begin + estimator = ExpressionSizeEstimator.new(self, table) + estimator.estimate + rescue GroongaError => groonga_error + context.set_groonga_error(groonga_error) + table.size + rescue => error + context.record_error(:unknown_error, error) + table.size + end + end + + private + def context + @context ||= Context.instance + end end end diff --git a/storage/mroonga/vendor/groonga/lib/mrb/scripts/expression_size_estimator.rb b/storage/mroonga/vendor/groonga/lib/mrb/scripts/expression_size_estimator.rb new file mode 100644 index 00000000000..73b44cae3dd --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/scripts/expression_size_estimator.rb @@ -0,0 +1,155 @@ +module Groonga + class ExpressionSizeEstimator + def initialize(expression, table) + @expression = expression + @table = table + @table_size = @table.size + end + + def estimate + builder = ScanInfoBuilder.new(@expression, Operator::OR, 0) + data_list = builder.build + return @table_size if data_list.nil? + + or_data_list = group_data_list(data_list) + or_sizes = or_data_list.collect do |and_data_list| + and_sizes = and_data_list.collect do |data| + size = estimate_data(data) + if data.logical_op == Operator::AND_NOT + size = @table_size - size + size = 0 if size < 0 + end + size + end + and_sizes.min + end + or_sizes.max + end + + private + def group_data_list(data_list) + or_data_list = [[]] + data_list.each do |data| + next if data.op == Operator::NOP + + and_data_list = or_data_list.last + if and_data_list.empty? + and_data_list << data + else + case data.logical_op + when Operator::AND, Operator::AND_NOT + and_data_list << data + else + and_data_list = [data] + or_data_list << and_data_list + end + end + end + or_data_list + end + + def estimate_data(data) + search_index = data.search_indexes.first + return @table_size if search_index.nil? + + index_column = resolve_index_column(search_index.index_column, + data.op) + return @table_size if index_column.nil? + + size = nil + case data.op + when Operator::MATCH + size = estimate_match(data, index_column) + when Operator::REGEXP + size = estimate_regexp(data, index_column) + when Operator::EQUAL + size = estimate_equal(data, index_column) + when Operator::LESS, + Operator::LESS_EQUAL, + Operator::GREATER, + Operator::GREATER_EQUAL + size = estimate_range(data, index_column) + when Operator::CALL + procedure = data.args.first + if procedure.is_a?(Procedure) and procedure.name == "between" + size = estimate_between(data, index_column) + end + end + size || @table_size + end + + def resolve_index_column(index_column, operator) + while index_column.is_a?(Accessor) + index_info = index_column.find_index(operator) + return nil if index_info.nil? + index_column = index_info.index + end + + index_column + end + + def estimate_match(data, index_column) + index_column.estimate_size(:query => data.query.value) + end + + def estimate_regexp(data, index_column) + index_column.estimate_size(:query => data.query.value, + :mode => data.op) + end + + def estimate_equal(data, index_column) + lexicon = index_column.lexicon + term_id = lexicon[data.query] + return 0 if term_id.nil? + + index_column.estimate_size(:term_id => term_id) + end + + def estimate_range(data, index_column) + lexicon = index_column.lexicon + value = data.query.value + options = {} + case data.op + when Operator::LESS + options[:max] = value + options[:flags] = TableCursorFlags::LT + when Operator::LESS_EQUAL + options[:max] = value + options[:flags] = TableCursorFlags::LE + when Operator::GREATER + options[:min] = value + options[:flags] = TableCursorFlags::GT + when Operator::GREATER_EQUAL + options[:min] = value + options[:flags] = TableCursorFlags::GE + end + TableCursor.open(lexicon, options) do |cursor| + index_column.estimate_size(:lexicon_cursor => cursor) + end + end + + def estimate_between(data, index_column) + lexicon = index_column.lexicon + _, _, min, min_border, max, max_border = data.args + options = { + :min => min, + :max => max, + :flags => 0, + } + if min_border == "include" + options[:flags] |= TableCursorFlags::LT + else + options[:flags] |= TableCursorFlags::LE + end + if max_border == "include" + options[:flags] |= TableCursorFlags::GT + else + options[:flags] |= TableCursorFlags::GE + end + + TableCursor.open(lexicon, options) do |cursor| + index_column.estimate_size(:lexicon_cursor => cursor) + end + end + end +end diff --git a/storage/mroonga/vendor/groonga/lib/mrb/scripts/index_column.rb b/storage/mroonga/vendor/groonga/lib/mrb/scripts/index_column.rb new file mode 100644 index 00000000000..25ebc149367 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/scripts/index_column.rb @@ -0,0 +1,39 @@ +module Groonga + class IndexColumn + private :estimate_size_for_term_id + private :estimate_size_for_query + private :estimate_size_for_lexicon_cursor + + # Estimate the number of matched records for term ID or query. + # + # @overload estimate_size(:term_id => term_id) + # @return [Integer] the number of matched records for the term ID. + # + # @overload estimate_size(:query => query) + # @return [Integer] the number of matched records for the query. + # + # @overload estimate_size(:lexicon_cursor => lexicon_cursor) + # @return [Integer] the number of matched records for the lexicon cursor. + # + def estimate_size(parameters) + term_id = parameters[:term_id] + if term_id + return estimate_size_for_term_id(term_id) + end + + query = parameters[:query] + if query + return estimate_size_for_query(query, parameters) + end + + lexicon_cursor = parameters[:lexicon_cursor] + if lexicon_cursor + return estimate_size_for_lexicon_cursor(lexicon_cursor) + end + + message = + "must specify :term_id, :query, :lexicon_cursor: #{parameters.inspect}" + raise InvalidArgument, message + end + end +end diff --git a/storage/mroonga/vendor/groonga/lib/mrb/scripts/index_cursor.rb b/storage/mroonga/vendor/groonga/lib/mrb/scripts/index_cursor.rb new file mode 100644 index 00000000000..8044066930a --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/scripts/index_cursor.rb @@ -0,0 +1,18 @@ +module Groonga + class IndexCursor + class << self + def open(*arguments) + cursor = open_raw(*arguments) + if block_given? + begin + yield(cursor) + ensure + cursor.close + end + else + cursor + end + end + end + end +end diff --git a/storage/mroonga/vendor/groonga/lib/mrb/scripts/initialize/Makefile.am b/storage/mroonga/vendor/groonga/lib/mrb/scripts/initialize/Makefile.am new file mode 100644 index 00000000000..e7531fdc029 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/scripts/initialize/Makefile.am @@ -0,0 +1,9 @@ +include sources.am + +EXTRA_DIST = \ + $(RUBY_SCRIPT_FILES) + +if WITH_MRUBY +ruby_scripts_initializedir = $(ruby_scriptsdir)/initialize +ruby_scripts_initialize_DATA = $(RUBY_SCRIPT_FILES) +endif diff --git a/storage/mroonga/vendor/groonga/lib/mrb/scripts/initialize/post.rb b/storage/mroonga/vendor/groonga/lib/mrb/scripts/initialize/post.rb new file mode 100644 index 00000000000..ea26a031e0a --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/scripts/initialize/post.rb @@ -0,0 +1,18 @@ +require "error" + +require "context" + +require "writer" + +require "object" +require "database" +require "index_column" +require "command" +require "table_cursor" +require "index_cursor" + +require "expression" + +require "plugin_loader" + +require "eval_context" diff --git a/storage/mroonga/vendor/groonga/lib/mrb/scripts/initialize/pre.rb b/storage/mroonga/vendor/groonga/lib/mrb/scripts/initialize/pre.rb new file mode 100644 index 00000000000..99300d11a92 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/scripts/initialize/pre.rb @@ -0,0 +1,3 @@ +require "backtrace_entry" + +require "operator" diff --git a/storage/mroonga/vendor/groonga/lib/mrb/scripts/initialize/sources.am b/storage/mroonga/vendor/groonga/lib/mrb/scripts/initialize/sources.am new file mode 100644 index 00000000000..3c26e19b2ae --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/scripts/initialize/sources.am @@ -0,0 +1,3 @@ +RUBY_SCRIPT_FILES = \ + pre.rb \ + post.rb diff --git a/storage/mroonga/vendor/groonga/lib/mrb/scripts/logger.rb b/storage/mroonga/vendor/groonga/lib/mrb/scripts/logger.rb index 06f99c14fc3..cb747a418d6 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/scripts/logger.rb +++ b/storage/mroonga/vendor/groonga/lib/mrb/scripts/logger.rb @@ -3,14 +3,19 @@ module Groonga def log_error(error) log_level = Level::ERROR.to_i - message = "#{error.class}: #{error.message}" + if error.is_a?(Error) + message = error.message + else + message = "#{error.class}: #{error.message}" + end backtrace = error.backtrace - first_raw_entry = backtrace.first - if first_raw_entry - first_entry = BacktraceEntry.parse(first_raw_entry) - file = first_entry.file - line = first_entry.line - method = first_entry.method + last_raw_entry = backtrace.last + if last_raw_entry + last_entry = BacktraceEntry.parse(last_raw_entry) + file = last_entry.file + line = last_entry.line + method = last_entry.method + # message = "#{file}:#{line}:#{method}: #{message}" else file = "" line = 0 @@ -18,8 +23,11 @@ module Groonga end log(log_level, file, line, method, message) - backtrace.each do |raw_entry| + backtrace.reverse_each.with_index do |raw_entry, i| + next if i == 0 entry = BacktraceEntry.parse(raw_entry) + message = entry.message + message = raw_entry if message.empty? log(log_level, entry.file, entry.line, entry.method, raw_entry) end end diff --git a/storage/mroonga/vendor/groonga/lib/mrb/scripts/logger/Makefile.am b/storage/mroonga/vendor/groonga/lib/mrb/scripts/logger/Makefile.am new file mode 100644 index 00000000000..448e72ca5bc --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/scripts/logger/Makefile.am @@ -0,0 +1,9 @@ +include sources.am + +EXTRA_DIST = \ + $(RUBY_SCRIPT_FILES) + +if WITH_MRUBY +ruby_scripts_loggerdir = $(ruby_scriptsdir)/logger +ruby_scripts_logger_DATA = $(RUBY_SCRIPT_FILES) +endif diff --git a/storage/mroonga/vendor/groonga/lib/mrb/scripts/logger/sources.am b/storage/mroonga/vendor/groonga/lib/mrb/scripts/logger/sources.am new file mode 100644 index 00000000000..7231ee4eecb --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/scripts/logger/sources.am @@ -0,0 +1,2 @@ +RUBY_SCRIPT_FILES = \ + level.rb diff --git a/storage/mroonga/vendor/groonga/lib/mrb/scripts/object.rb b/storage/mroonga/vendor/groonga/lib/mrb/scripts/object.rb index f1cc1a6aa79..d98b5069e7f 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/scripts/object.rb +++ b/storage/mroonga/vendor/groonga/lib/mrb/scripts/object.rb @@ -1,7 +1,11 @@ module Groonga class Object - def inspect - super[0..-2] + ": #{grn_inspect}>" + def domain + Context.instance[domain_id] + end + + def range + Context.instance[range_id] end end end diff --git a/storage/mroonga/vendor/groonga/lib/mrb/scripts/operator.rb b/storage/mroonga/vendor/groonga/lib/mrb/scripts/operator.rb new file mode 100644 index 00000000000..349695e1a53 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/scripts/operator.rb @@ -0,0 +1,22 @@ +module Groonga + class Operator + @values = {} + class << self + def register(operator) + const_set(operator.name, operator) + @values[operator.value] = operator + end + + def find(value) + @values[value] + end + end + + attr_reader :name + attr_reader :value + def initialize(name, value) + @name = name + @value = value + end + end +end diff --git a/storage/mroonga/vendor/groonga/lib/mrb/scripts/plugin_loader.rb b/storage/mroonga/vendor/groonga/lib/mrb/scripts/plugin_loader.rb new file mode 100644 index 00000000000..09b972f120d --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/scripts/plugin_loader.rb @@ -0,0 +1,14 @@ +module Groonga + class PluginLoader + class << self + def load_file(path) + begin + load(path) + rescue => error + Context.instance.record_error(:plugin_error, error) + nil + end + end + end + end +end diff --git a/storage/mroonga/vendor/groonga/lib/mrb/scripts/require.rb b/storage/mroonga/vendor/groonga/lib/mrb/scripts/require.rb new file mode 100644 index 00000000000..a824e92fa51 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/scripts/require.rb @@ -0,0 +1,68 @@ +$" = [__FILE__] + +class ScriptLoader + @@loading_paths = {} + + def initialize(path) + @base_path = path + end + + def load_once + if absolete_path?(@base_path) + loaded = load_once_path(@base_path) + if loaded.nil? + raise LoadError, error_message + else + loaded + end + else + $LOAD_PATH.each do |load_path| + unless absolete_path?(load_path) + load_path = File.expand_path(load_path) + end + loaded = load_once_path(File.join(load_path, @base_path)) + return loaded unless loaded.nil? + end + raise LoadError, error_message + end + end + + private + def error_message + "cannot load such file -- #{@base_path}" + end + + def absolete_path?(path) + path.start_with?("/") + end + + def load_once_path(path) + loaded = load_once_absolete_path(path) + return loaded unless loaded.nil? + + return nil unless File.extname(path).empty? + + load_once_absolete_path("#{path}.rb") + end + + def load_once_absolete_path(path) + return false if $".include?(path) + return false if @@loading_paths.key?(path) + + return nil unless File.file?(path) + + @@loading_paths[path] = true + load(path) + $" << path + @@loading_paths.delete(path) + + true + end +end + +module Kernel + def require(path) + loader = ScriptLoader.new(path) + loader.load_once + end +end diff --git a/storage/mroonga/vendor/groonga/lib/mrb/scripts/scan_info.rb b/storage/mroonga/vendor/groonga/lib/mrb/scripts/scan_info.rb index cf0056d7fd3..a98cf792a9c 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/scripts/scan_info.rb +++ b/storage/mroonga/vendor/groonga/lib/mrb/scripts/scan_info.rb @@ -22,8 +22,13 @@ module Groonga data.args.each do |arg| push_arg(arg) end - data.indexes.each do |index, section_id, weight| - put_index(index, section_id, weight) + data.search_indexes.each do |search_index| + put_index(search_index.index_column, + search_index.section_id, + search_index.weight, + search_index.scorer, + search_index.scorer_args_expr, + search_index.scorer_args_expr_offset || 0) end end end diff --git a/storage/mroonga/vendor/groonga/lib/mrb/scripts/scan_info_builder.rb b/storage/mroonga/vendor/groonga/lib/mrb/scripts/scan_info_builder.rb index 5e258e90e5a..dc003f88948 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/scripts/scan_info_builder.rb +++ b/storage/mroonga/vendor/groonga/lib/mrb/scripts/scan_info_builder.rb @@ -1,11 +1,6 @@ -module Groonga - # TODO: Move me - class ExpressionCode - module Flags - RELATIONAL_EXPRESSION = 0x01 - end - end +require "scan_info_data" +module Groonga class ScanInfoBuilder module Status START = 0 @@ -39,6 +34,21 @@ module Groonga Operator::GEO_WITHINP6, Operator::GEO_WITHINP8, Operator::TERM_EXTRACT, + Operator::REGEXP, + ] + + ARITHMETIC_OPERATORS = [ + Operator::BITWISE_OR, + Operator::BITWISE_XOR, + Operator::BITWISE_AND, + Operator::BITWISE_NOT, + Operator::SHIFTL, + Operator::SHIFTR, + Operator::SHIFTRR, + Operator::PLUS, + Operator::MINUS, + Operator::STAR, + Operator::MOD, ] LOGICAL_OPERATORS = [ @@ -89,7 +99,7 @@ module Groonga status = Status::COL1 data.args << code.value when Status::COL1 - raise "invalid expression: can't use column as a value: <#{code.value.name}>: <#{@expression.grn_inspect}>" + raise ErrorMessage, "invalid expression: can't use column as a value: <#{code.value.name}>: <#{@expression.grn_inspect}>" status = Status::COL2 when Status::COL2 # Do nothing @@ -114,7 +124,7 @@ module Groonga first_data = @data_list.first if (first_data.flags & ScanInfo::Flags::PUSH) == 0 or first_data.logical_op != @operator - raise "invalid expr" + raise ErrorMessage, "invalid expr" else first_data.flags &= ~ScanInfo::Flags::PUSH first_data.logical_op = @operator @@ -140,6 +150,11 @@ module Groonga return false if status > Status::CONST status = Status::START n_relation_expressions += 1 + when *ARITHMETIC_OPERATORS + return false if status < Status::COL1 + return false if status > Status::CONST + status = Status::START + return false if n_relation_expressions != (n_logical_expressions + 1) when *LOGICAL_OPERATORS return false if status != Status::START n_logical_expressions += 1 @@ -206,10 +221,12 @@ module Groonga new_data.flags = ScanInfo::Flags::POP new_data.logical_op = operator @data_list << new_data + break end else data.flags &= ~ScanInfo::Flags::PUSH data.logical_op = operator + break end else if n_dif_ops > 0 @@ -225,6 +242,7 @@ module Groonga @data_list[r..-1] + @data_list[j...r] end + break end end else @@ -235,7 +253,7 @@ module Groonga end if j < 0 - raise GRN_INVALID_ARGUMENT.new("unmatched nesting level") + raise ErrorMessage, "unmatched nesting level" end end end @@ -272,10 +290,10 @@ module Groonga return false if data.args[0] != next_data.args[0] - data_indexes = data.indexes - return false if data_indexes.empty? + data_search_indexes = data.search_indexes + return false if data_search_indexes.empty? - data_indexes == next_data.indexes + data_search_indexes == next_data.search_indexes end def lower_condition?(operator) @@ -303,7 +321,7 @@ module Groonga between_data.op = Operator::CALL between_data.logical_op = data.logical_op between_data.args = create_between_data_args(data, next_data) - between_data.indexes = data.indexes + between_data.search_indexes = data.search_indexes between_data end diff --git a/storage/mroonga/vendor/groonga/lib/mrb/scripts/scan_info_data.rb b/storage/mroonga/vendor/groonga/lib/mrb/scripts/scan_info_data.rb index 67d0bc3aef7..4a6e58a951a 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/scripts/scan_info_data.rb +++ b/storage/mroonga/vendor/groonga/lib/mrb/scripts/scan_info_data.rb @@ -1,3 +1,5 @@ +require "scan_info_search_index" + module Groonga class ScanInfoData attr_accessor :start @@ -6,18 +8,18 @@ module Groonga attr_accessor :logical_op attr_accessor :query attr_accessor :args - attr_accessor :indexes + attr_accessor :search_indexes attr_accessor :flags attr_accessor :max_interval attr_accessor :similarity_threshold def initialize(start) @start = start @end = 0 - @op = 0 + @op = Operator::NOP @logical_op = Operator::OR @query = nil @args = [] - @indexes = [] + @search_indexes = [] @flags = ScanInfo::Flags::PUSH @max_interval = nil @similarity_threshold = nil @@ -57,7 +59,7 @@ module Groonga else message = "The first argument of NEAR/NEAR2 must be Expression, Accessor or Object: #{arg.class}" - raise message + raise ErrorMessage, message end self.query = @args[1] @@ -80,7 +82,7 @@ module Groonga else message = "The first argument of SIMILAR must be Expression, Accessor or Object: #{arg.class}" - raise message + raise ErrorMesesage, message end self.query = @args[1] @@ -100,6 +102,41 @@ module Groonga self.query = arg end end + if @op == Operator::REGEXP and not index_searchable_regexp?(@query) + @search_indexes.clear + end + end + + def index_searchable_regexp?(pattern) + return false if pattern.nil? + + previous_char = nil + pattern.value.each_char do |char| + if previous_char == "\\" + case char + when "Z" + return false + when "b", "B" + return false + when "d", "D", "h", "H", "p", "s", "S", "w", "W" + return false + when "X" + return false + when "k", "g", "1", "2", "3", "4", "5", "6", "7", "8", "9" + return false + when "\\" + previous_char = nil + next + end + else + case char + when ".", "[", "]", "|", "?", "+", "*", "{", "}", "^", "$", "(", ")" + return false + end + end + previous_char = char + end + true end def match_resolve_index_expression(expression) @@ -107,28 +144,96 @@ module Groonga n_codes = codes.size i = 0 while i < n_codes - code = codes[i] - value = code.value - case value - when Accessor - match_resolve_index_expression_accessor(code) - when FixedSizeColumn, VariableSizeColumn - match_resolve_index_expression_data_column(code) - when IndexColumn - section_id = 0 - rest_n_codes = n_codes - i - if rest_n_codes >= 2 and - codes[i + 1].value.is_a?(Bulk) and - codes[i + 1].value.domain == ID::UINT32 and - codes[i + 2].op == Operator::GET_MEMBER - section_id = codes[i + 1].value.value + 1 - code = codes[i + 2] - i += 2 + i = match_resolve_index_expression_codes(expression, codes, i, n_codes) + end + end + + def match_resolve_index_expression_codes(expression, codes, i, n_codes) + code = codes[i] + value = code.value + return i + 1 if value.nil? + + case value + when Accessor, Column + index_info, offset = + match_resolve_index_expression_find_index(expression, + codes, i, n_codes) + i += offset - 1 + if index_info + if value.is_a?(Accessor) + self.flags |= ScanInfo::Flags::ACCESSOR end - put_index(value, section_id, code.weight) + weight, offset = codes[i].weight + i += offset + put_search_index(index_info.index, index_info.section_id, weight) + end + when Procedure + unless value.scorer? + message = "procedure must be scorer: #{scorer.name}>" + raise ErrorMessage, message end + scorer = value i += 1 + index_info, offset = + match_resolve_index_expression_find_index(expression, + codes, i, n_codes) + i += offset + if index_info + scorer_args_expr_offset = 0 + if codes[i].op != Operator::CALL + scorer_args_expr_offset = i + end + while i < n_codes and codes[i].op != Operator::CALL + i += 1 + end + weight, offset = codes[i].weight + i += offset + search_index = ScanInfoSearchIndex.new(index_info.index, + index_info.section_id, + weight, + scorer, + expression, + scorer_args_expr_offset) + @search_indexes << search_index + end + when Table + raise ErrorMessage, "invalid match target: <#{value.name}>" end + i + 1 + end + + def match_resolve_index_expression_find_index(expression, codes, i, n_codes) + code = codes[i] + value = code.value + index_info = nil + offset = 1 + case value + when Accessor + accessor = value + index_info = accessor.find_index(@op) + if index_info + if accessor.have_next? and index_info.index != accessor.object + index_info = IndexInfo.new(accessor, index_info.section_id) + end + end + when FixedSizeColumn, VariableSizeColumn + index_info = value.find_index(@op) + when IndexColumn + index = value + section_id = 0 + rest_n_codes = n_codes - i + if rest_n_codes >= 2 and + codes[i + 1].value.is_a?(Bulk) and + (codes[i + 1].value.domain == ID::UINT32 or + codes[i + 1].value.domain == ID::INT32) and + codes[i + 2].op == Operator::GET_MEMBER + section_id = codes[i + 1].value.value + 1 + offset += 2 + end + index_info = IndexInfo.new(index, section_id) + end + + [index_info, offset] end def match_resolve_index_expression_accessor(expr_code) @@ -136,10 +241,13 @@ module Groonga self.flags |= ScanInfo::Flags::ACCESSOR index_info = accessor.find_index(op) return if index_info.nil? + + section_id = index_info.section_id + weight = expr_code.weight if accessor.next - put_index(accessor, index_info.section_id, expr_code.weight) + put_search_index(accessor, section_id, weight) else - put_index(index_info.index, index_info.section_id, expr_code.weight) + put_search_index(index_info.index, section_id, weight) end end @@ -147,13 +255,13 @@ module Groonga column = expr_code.value index_info = column.find_index(op) return if index_info.nil? - put_index(index_info.index, index_info.section_id, expr_code.weight) + put_search_index(index_info.index, index_info.section_id, expr_code.weight) end def match_resolve_index_db_obj(db_obj) index_info = db_obj.find_index(op) return if index_info.nil? - put_index(index_info.index, index_info.section_id, 1) + put_search_index(index_info.index, index_info.section_id, 1) end def match_resolve_index_accessor(accessor) @@ -161,9 +269,9 @@ module Groonga index_info = accessor.find_index(op) return if index_info.nil? if accessor.next - put_index(accessor, index_info.section_id, 1) + put_search_index(accessor, index_info.section_id, 1) else - put_index(index_info.index, index_info.section_id, 1) + put_search_index(index_info.index, index_info.section_id, 1) end end @@ -181,18 +289,19 @@ module Groonga def call_relational_resolve_index_db_obj(db_obj) index_info = db_obj.find_index(op) return if index_info.nil? - put_index(index_info.index, index_info.section_id, 1) + put_search_index(index_info.index, index_info.section_id, 1) end def call_relational_resolve_index_accessor(accessor) self.flags |= ScanInfo::Flags::ACCESSOR index_info = accessor.find_index(op) return if index_info.nil? - put_index(index_info.index, index_info.section_id, 1) + put_search_index(index_info.index, index_info.section_id, 1) end - def put_index(index, section_id, weight) - @indexes << [index, section_id, weight] + def put_search_index(index, section_id, weight) + search_index = ScanInfoSearchIndex.new(index, section_id, weight) + @search_indexes << search_index end end end diff --git a/storage/mroonga/vendor/groonga/lib/mrb/scripts/scan_info_search_index.rb b/storage/mroonga/vendor/groonga/lib/mrb/scripts/scan_info_search_index.rb new file mode 100644 index 00000000000..a2818160310 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/scripts/scan_info_search_index.rb @@ -0,0 +1,9 @@ +module Groonga + class ScanInfoSearchIndex < Struct.new(:index_column, + :section_id, + :weight, + :scorer, + :scorer_args_expr, + :scorer_args_expr_offset) + end +end diff --git a/storage/mroonga/vendor/groonga/lib/mrb/scripts/sources.am b/storage/mroonga/vendor/groonga/lib/mrb/scripts/sources.am index 93ade1627e6..5ddcba18d4c 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/scripts/sources.am +++ b/storage/mroonga/vendor/groonga/lib/mrb/scripts/sources.am @@ -1,13 +1,23 @@ RUBY_SCRIPT_FILES = \ backtrace_entry.rb \ + command.rb \ context.rb \ - context/error_level.rb \ - context/rc.rb \ + database.rb \ + error.rb \ eval_context.rb \ expression.rb \ + expression_size_estimator.rb \ + index_column.rb \ + index_cursor.rb \ index_info.rb \ logger.rb \ - logger/level.rb \ + object.rb \ + operator.rb \ + plugin_loader.rb \ + require.rb \ scan_info.rb \ scan_info_builder.rb \ - scan_info_data.rb + scan_info_data.rb \ + scan_info_search_index.rb \ + table_cursor.rb \ + writer.rb diff --git a/storage/mroonga/vendor/groonga/lib/mrb/scripts/table_cursor.rb b/storage/mroonga/vendor/groonga/lib/mrb/scripts/table_cursor.rb new file mode 100644 index 00000000000..a36d88d556d --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/scripts/table_cursor.rb @@ -0,0 +1,26 @@ +module Groonga + class TableCursor + class << self + def open(*arguments) + cursor = open_raw(*arguments) + if block_given? + begin + yield(cursor) + ensure + cursor.close + end + else + cursor + end + end + end + + def each + loop do + id = self.next + return if id == Groonga::ID::NIL + yield(id) + end + end + end +end diff --git a/storage/mroonga/vendor/groonga/lib/mrb/scripts/writer.rb b/storage/mroonga/vendor/groonga/lib/mrb/scripts/writer.rb new file mode 100644 index 00000000000..de2bc2611e9 --- /dev/null +++ b/storage/mroonga/vendor/groonga/lib/mrb/scripts/writer.rb @@ -0,0 +1,21 @@ +module Groonga + class Writer + def array(name, n_elements) + open_array(name, n_elements) + begin + yield + ensure + close_array + end + end + + def map(name, n_elements) + open_map(name, n_elements) + begin + yield + ensure + close_map + end + end + end +end diff --git a/storage/mroonga/vendor/groonga/lib/mrb/sources.am b/storage/mroonga/vendor/groonga/lib/mrb/sources.am index 947607c7ecd..5cda570bc37 100644 --- a/storage/mroonga/vendor/groonga/lib/mrb/sources.am +++ b/storage/mroonga/vendor/groonga/lib/mrb/sources.am @@ -1,33 +1,65 @@ libgrnmrb_la_SOURCES = \ mrb_accessor.c \ mrb_accessor.h \ + mrb_array.c \ + mrb_array.h \ mrb_bulk.c \ mrb_bulk.h \ mrb_column.c \ mrb_column.h \ + mrb_command.c \ + mrb_command.h \ + mrb_command_input.c \ + mrb_command_input.h \ + mrb_content_type.c \ + mrb_content_type.h \ mrb_converter.c \ mrb_converter.h \ mrb_ctx.c \ mrb_ctx.h \ + mrb_database.c \ + mrb_database.h \ + mrb_double_array_trie.c \ + mrb_double_array_trie.h \ mrb_error.c \ mrb_error.h \ mrb_expr.c \ mrb_expr.h \ mrb_fixed_size_column.c \ mrb_fixed_size_column.h \ + mrb_hash_table.c \ + mrb_hash_table.h \ mrb_id.c \ mrb_id.h \ mrb_index_column.c \ mrb_index_column.h \ + mrb_index_cursor.c \ + mrb_index_cursor.h \ mrb_logger.c \ mrb_logger.h \ - mrb_obj.c \ - mrb_obj.h \ + mrb_object.c \ + mrb_object.h \ + mrb_object_flags.c \ + mrb_object_flags.h \ mrb_operator.c \ mrb_operator.h \ + mrb_options.c \ + mrb_options.h \ + mrb_patricia_trie.c \ + mrb_patricia_trie.h \ mrb_procedure.c \ mrb_procedure.h \ + mrb_table.c \ + mrb_table.h \ + mrb_table_cursor.c \ + mrb_table_cursor.h \ + mrb_table_cursor_flags.c \ + mrb_table_cursor_flags.h \ + mrb_type.c \ + mrb_type.h \ mrb_variable_size_column.c \ mrb_variable_size_column.h \ mrb_void.c \ - mrb_void.h + mrb_void.h \ + mrb_writer.c \ + mrb_writer.h |