Kouhei Sutou 2019-04-20 08:56:46 +0900 (Sat, 20 Apr 2019) Revision: e6016f62db280d403aa372ae12041d111a026794 https://github.com/groonga/groonga/commit/e6016f62db280d403aa372ae12041d111a026794 Message: mrb: Add Groonga::Vector Added files: lib/mrb/mrb_vector.c lib/mrb/mrb_vector.h Modified files: lib/ctx_impl_mrb.c lib/mrb/mrb_converter.c lib/mrb/mrb_expr.c lib/mrb/sources.am Modified: lib/ctx_impl_mrb.c (+2 -0) =================================================================== --- lib/ctx_impl_mrb.c 2019-04-20 08:55:29 +0900 (e762b3e59) +++ lib/ctx_impl_mrb.c 2019-04-20 08:56:46 +0900 (60c157b59) @@ -74,6 +74,7 @@ # include "mrb/mrb_window_function_executor.h" # include "mrb/mrb_locale_output.h" # include "mrb/mrb_output_columns.h" +# include "mrb/mrb_vector.h" # include <mruby/array.h> # include <mruby/string.h> @@ -210,6 +211,7 @@ mrb_groonga_init(mrb_state *mrb, mrb_value self) grn_mrb_window_function_executor_init(ctx); grn_mrb_locale_output_init(ctx); grn_mrb_output_columns_init(ctx); + grn_mrb_vector_init(ctx); grn_mrb_load(ctx, "initialize/post.rb"); Modified: lib/mrb/mrb_converter.c (+4 -0) =================================================================== --- lib/mrb/mrb_converter.c 2019-04-20 08:55:29 +0900 (4dae6ba2c) +++ lib/mrb/mrb_converter.c 2019-04-20 08:56:46 +0900 (e6fa03f76) @@ -1,6 +1,7 @@ /* -*- c-basic-offset: 2 -*- */ /* Copyright(C) 2013-2015 Brazil + Copyright(C) 2019 Kouhei Sutou <kou****@clear*****> This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -209,6 +210,9 @@ grn_mrb_class_from_grn_obj(mrb_state *mrb, grn_obj *object) case GRN_PTR : klass = mrb_class_get_under(mrb, data->module, "Pointer"); break; + case GRN_VECTOR : + klass = mrb_class_get_under(mrb, data->module, "Vector"); + break; case GRN_ACCESSOR : klass = mrb_class_get_under(mrb, data->module, "Accessor"); break; Modified: lib/mrb/mrb_expr.c (+7 -0) =================================================================== --- lib/mrb/mrb_expr.c 2019-04-20 08:55:29 +0900 (2bae9f9ed) +++ lib/mrb/mrb_expr.c 2019-04-20 08:56:46 +0900 (edca75c61) @@ -1,6 +1,7 @@ /* -*- c-basic-offset: 2 -*- */ /* Copyright(C) 2013-2018 Brazil + Copyright(C) 2019 Kouhei Sutou <kou****@clear*****> This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -844,6 +845,12 @@ mrb_grn_expression_append_constant(mrb_state *mrb, mrb_value self) GRN_RECORD_SET(ctx, &constant, id); grn_expr_append_const(ctx, expr, &constant, op, n_args); GRN_OBJ_FIN(ctx, &constant); + } else if (klass == mrb_class_get_under(mrb, data->module, "Bulk")) { + grn_obj *bulk = DATA_PTR(mrb_constant); + grn_expr_append_const(ctx, expr, bulk, op, n_args); + } else if (klass == mrb_class_get_under(mrb, data->module, "Vector")) { + grn_obj *vector = DATA_PTR(mrb_constant); + grn_expr_append_const(ctx, expr, vector, op, n_args); } else { mrb_raisef(mrb, E_ARGUMENT_ERROR, "unsupported constant to append to expression: %S", Added: lib/mrb/mrb_vector.c (+119 -0) 100644 =================================================================== --- /dev/null +++ lib/mrb/mrb_vector.c 2019-04-20 08:56:46 +0900 (9c3a9a302) @@ -0,0 +1,119 @@ +/* -*- c-basic-offset: 2 -*- */ +/* + Copyright(C) 2019 Kouhei Sutou <kou****@clear*****> + + 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/array.h> +#include <mruby/class.h> +#include <mruby/data.h> + +#include "../grn_db.h" +#include "mrb_bulk.h" +#include "mrb_object.h" + +static struct mrb_data_type mrb_grn_vector_type = { + "Groonga::Vector", + NULL +}; + +static mrb_value +mrb_grn_vector_initialize(mrb_state *mrb, mrb_value self) +{ + mrb_value mrb_vector_ptr; + + mrb_get_args(mrb, "o", &mrb_vector_ptr); + DATA_TYPE(self) = &mrb_grn_vector_type; + DATA_PTR(self) = mrb_cptr(mrb_vector_ptr); + return self; +} + +static mrb_value +mrb_grn_vector_get_value(mrb_state *mrb, mrb_value self) +{ + grn_ctx *ctx = (grn_ctx *)mrb->ud; + grn_obj *vector = DATA_PTR(self); + + if (!vector) { + return mrb_nil_value(); + } + + unsigned int n = grn_vector_size(ctx, vector); + mrb_value mrb_vector = mrb_ary_new_capa(mrb, n); + grn_obj element; + GRN_TEXT_INIT(&element, GRN_OBJ_DO_SHALLOW_COPY); + for (unsigned int i = 0; i < n; i++) { + const char *content; + unsigned int content_length; + content_length = grn_vector_get_element(ctx, + vector, + i, + &content, + NULL, + &(element.header.domain)); + GRN_TEXT_SET(ctx, &element, content, content_length); + mrb_ary_push(mrb, mrb_vector, grn_mrb_value_from_bulk(mrb, &element)); + element.header.domain = GRN_DB_TEXT; + } + GRN_OBJ_FIN(ctx, &element); + + return mrb_vector; +} + +static mrb_value +mrb_grn_vector_equal(mrb_state *mrb, mrb_value self) +{ + mrb_value mrb_other; + + mrb_get_args(mrb, "o", &mrb_other); + + if (!mrb_obj_is_kind_of(mrb, mrb_other, mrb_class(mrb, self))) { + return mrb_false_value(); + } + + return mrb_bool_value(DATA_PTR(self) == DATA_PTR(mrb_other)); +} + +void +grn_mrb_vector_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, "Vector", mrb->object_class); + MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA); + + mrb_define_method(mrb, klass, "initialize", + mrb_grn_vector_initialize, MRB_ARGS_REQ(1)); + mrb_define_method(mrb, klass, "value", + mrb_grn_vector_get_value, MRB_ARGS_NONE()); + mrb_define_method(mrb, klass, "true?", + grn_mrb_object_is_true, MRB_ARGS_NONE()); + mrb_define_method(mrb, klass, "==", + mrb_grn_vector_equal, MRB_ARGS_REQ(1)); + mrb_define_method(mrb, klass, "inspect", + grn_mrb_object_inspect, MRB_ARGS_NONE()); + mrb_define_method(mrb, klass, "close", + grn_mrb_object_close, MRB_ARGS_NONE()); + mrb_define_method(mrb, klass, "closed?", + grn_mrb_object_is_closed, MRB_ARGS_NONE()); +} +#endif Added: lib/mrb/mrb_vector.h (+31 -0) 100644 =================================================================== --- /dev/null +++ lib/mrb/mrb_vector.h 2019-04-20 08:56:46 +0900 (e0daf7d01) @@ -0,0 +1,31 @@ +/* -*- c-basic-offset: 2 -*- */ +/* + Copyright(C) 2019 Kouhei Sutou <kou****@clear*****> + + 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 +*/ + +#pragma once + +#include "../grn_ctx.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void grn_mrb_vector_init(grn_ctx *ctx); + +#ifdef __cplusplus +} +#endif Modified: lib/mrb/sources.am (+2 -0) =================================================================== --- lib/mrb/sources.am 2019-04-20 08:55:29 +0900 (aaf99e7a2) +++ lib/mrb/sources.am 2019-04-20 08:56:46 +0900 (156385edf) @@ -89,6 +89,8 @@ libgrnmrb_la_SOURCES = \ mrb_type.h \ mrb_variable_size_column.c \ mrb_variable_size_column.h \ + mrb_vector.c \ + mrb_vector.h \ mrb_void.c \ mrb_void.h \ mrb_window_definition.c \ -------------- next part -------------- An HTML attachment was scrubbed... URL: <https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20190420/b705fdc3/attachment-0001.html>