Kouhei Sutou
null+****@clear*****
Fri Dec 26 00:23:16 JST 2014
Kouhei Sutou 2014-12-26 00:23:16 +0900 (Fri, 26 Dec 2014) New Revision: 9e4a3da0e9893d3ece15ae2ff02d875435954ee6 https://github.com/groonga/groonga/commit/9e4a3da0e9893d3ece15ae2ff02d875435954ee6 Message: mrb: bind grn_table_cursor Added files: lib/mrb/mrb_table_cursor.c lib/mrb/mrb_table_cursor.h lib/mrb/scripts/table_cursor.rb Modified files: lib/ctx_impl_mrb.c lib/mrb/scripts/initialize/post.rb lib/mrb/scripts/sources.am lib/mrb/sources.am Modified: lib/ctx_impl_mrb.c (+2 -0) =================================================================== --- lib/ctx_impl_mrb.c 2014-12-26 00:21:50 +0900 (de40b49) +++ lib/ctx_impl_mrb.c 2014-12-26 00:23:16 +0900 (dcbb147) @@ -38,6 +38,7 @@ #include "mrb/mrb_expr.h" #include "mrb/mrb_accessor.h" #include "mrb/mrb_procedure.h" +#include "mrb/mrb_table_cursor.h" #ifdef GRN_WITH_MRUBY # include <mruby/array.h> @@ -105,6 +106,7 @@ grn_ctx_impl_mrb_init_bindings(grn_ctx *ctx) grn_mrb_expr_init(ctx); grn_mrb_accessor_init(ctx); grn_mrb_procedure_init(ctx); + grn_mrb_table_cursor_init(ctx); grn_mrb_load(ctx, "initialize/post.rb"); } Added: lib/mrb/mrb_table_cursor.c (+134 -0) 100644 =================================================================== --- /dev/null +++ lib/mrb/mrb_table_cursor.c 2014-12-26 00:23:16 +0900 (9407bb0) @@ -0,0 +1,134 @@ +/* -*- 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 <mruby/hash.h> + +#include "mrb_ctx.h" +#include "mrb_table_cursor.h" + +static struct mrb_data_type mrb_grn_table_cursor_type = { + "Groonga::TableCursor", + NULL +}; + +static mrb_value +mrb_grn_table_cursor_singleton_open(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; + void *max = NULL; + unsigned int max_size = 0; + int offset = 0; + int limit = -1; + int flags = 0; + + mrb_get_args(mrb, "o|H", &mrb_table, &mrb_options); + + table = DATA_PTR(mrb_table); + if (!mrb_nil_p(mrb_options)) { + mrb_value mrb_flags; + + mrb_flags = mrb_hash_get(mrb, mrb_options, + mrb_symbol_value(mrb_intern_lit(mrb, "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_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); +} + +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", + mrb_grn_table_cursor_singleton_open, + 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()); +} +#endif Added: lib/mrb/mrb_table_cursor.h (+34 -0) 100644 =================================================================== --- /dev/null +++ lib/mrb/mrb_table_cursor.h 2014-12-26 00:23:16 +0900 (f05ab09) @@ -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 */ Modified: lib/mrb/scripts/initialize/post.rb (+2 -0) =================================================================== --- lib/mrb/scripts/initialize/post.rb 2014-12-26 00:21:50 +0900 (924aff8) +++ lib/mrb/scripts/initialize/post.rb 2014-12-26 00:23:16 +0900 (a5dca9a) @@ -1 +1,3 @@ +require "table_cursor" + require "eval_context" Modified: lib/mrb/scripts/sources.am (+2 -1) =================================================================== --- lib/mrb/scripts/sources.am 2014-12-26 00:21:50 +0900 (7b69271) +++ lib/mrb/scripts/sources.am 2014-12-26 00:23:16 +0900 (5b73778) @@ -12,4 +12,5 @@ RUBY_SCRIPT_FILES = \ require.rb \ scan_info.rb \ scan_info_builder.rb \ - scan_info_data.rb + scan_info_data.rb \ + table_cursor.rb Added: lib/mrb/scripts/table_cursor.rb (+13 -0) 100644 =================================================================== --- /dev/null +++ lib/mrb/scripts/table_cursor.rb 2014-12-26 00:23:16 +0900 (6132679) @@ -0,0 +1,13 @@ +module Groonga + class TableCursor + def each + loop do + id = self.next + p [id] + p Groonga::ID::NIL + return if id == Groonga::ID::NIL + yield(id) + end + end + end +end Modified: lib/mrb/sources.am (+2 -0) =================================================================== --- lib/mrb/sources.am 2014-12-26 00:21:50 +0900 (10f0ba7) +++ lib/mrb/sources.am 2014-12-26 00:23:16 +0900 (b223b4a) @@ -29,6 +29,8 @@ libgrnmrb_la_SOURCES = \ mrb_operator.h \ mrb_procedure.c \ mrb_procedure.h \ + mrb_table_cursor.c \ + mrb_table_cursor.h \ mrb_variable_size_column.c \ mrb_variable_size_column.h \ mrb_void.c \ -------------- next part -------------- HTML����������������������������... Download