[Groonga-commit] groonga/groonga at eee33af [master] mrb: add Writer for grouping output related functions

Back to archive index

Kouhei Sutou null+****@clear*****
Thu Feb 5 18:30:20 JST 2015


Kouhei Sutou	2015-02-05 18:30:20 +0900 (Thu, 05 Feb 2015)

  New Revision: eee33af8f98402a6e4edce3774c217274ac67d90
  https://github.com/groonga/groonga/commit/eee33af8f98402a6e4edce3774c217274ac67d90

  Message:
    mrb: add Writer for grouping output related functions

  Added files:
    lib/mrb/mrb_writer.c
    lib/mrb/mrb_writer.h
  Modified files:
    lib/ctx_impl_mrb.c
    lib/mrb/mrb_ctx.c
    lib/mrb/scripts/command.rb
    lib/mrb/scripts/context.rb
    lib/mrb/sources.am
    plugins/sharding/logical_count.rb

  Modified: lib/ctx_impl_mrb.c (+2 -0)
===================================================================
--- lib/ctx_impl_mrb.c    2015-02-05 15:52:34 +0900 (e9442aa)
+++ lib/ctx_impl_mrb.c    2015-02-05 18:30:20 +0900 (05692ee)
@@ -52,6 +52,7 @@
 # include "mrb/mrb_command_input.h"
 # include "mrb/mrb_table_cursor.h"
 # include "mrb/mrb_table_cursor_flags.h"
+# include "mrb/mrb_writer.h"
 
 # include <mruby/array.h>
 # include <mruby/variable.h>
@@ -134,6 +135,7 @@ grn_ctx_impl_mrb_init_bindings(grn_ctx *ctx)
   grn_mrb_command_input_init(ctx);
   grn_mrb_table_cursor_init(ctx);
   grn_mrb_table_cursor_flags_init(ctx);
+  grn_mrb_writer_init(ctx);
 
   grn_mrb_load(ctx, "initialize/post.rb");
 }

  Modified: lib/mrb/mrb_ctx.c (+0 -37)
===================================================================
--- lib/mrb/mrb_ctx.c    2015-02-05 15:52:34 +0900 (6de7cee)
+++ lib/mrb/mrb_ctx.c    2015-02-05 18:30:20 +0900 (f52342a)
@@ -26,7 +26,6 @@
 #include <mruby/string.h>
 
 #include "../grn_mrb.h"
-#include "../grn_output.h"
 #include "mrb_ctx.h"
 #include "mrb_converter.h"
 
@@ -206,39 +205,6 @@ ctx_get_database(mrb_state *mrb, mrb_value self)
   return grn_mrb_value_from_grn_obj(mrb, grn_ctx_db(ctx));
 }
 
-static mrb_value
-ctx_output(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();
-}
-
 void
 grn_mrb_ctx_check(mrb_state *mrb)
 {
@@ -744,8 +710,5 @@ grn_mrb_ctx_init(grn_ctx *ctx)
 
   mrb_define_method(mrb, klass, "database", ctx_get_database,
                     MRB_ARGS_NONE());
-
-  mrb_define_method(mrb, klass, "output", ctx_output,
-                    MRB_ARGS_REQ(1));
 }
 #endif

  Added: lib/mrb/mrb_writer.c (+74 -0) 100644
===================================================================
--- /dev/null
+++ lib/mrb/mrb_writer.c    2015-02-05 18:30:20 +0900 (08d1e5a)
@@ -0,0 +1,74 @@
+/* -*- 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 "../grn_mrb.h"
+#include "../grn_output.h"
+#include "mrb_writer.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();
+}
+
+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));
+}
+#endif

  Added: lib/mrb/mrb_writer.h (+34 -0) 100644
===================================================================
--- /dev/null
+++ lib/mrb/mrb_writer.h    2015-02-05 18:30:20 +0900 (a6b4a6b)
@@ -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 */

  Modified: lib/mrb/scripts/command.rb (+4 -4)
===================================================================
--- lib/mrb/scripts/command.rb    2015-02-05 15:52:34 +0900 (532ec39)
+++ lib/mrb/scripts/command.rb    2015-02-05 18:30:20 +0900 (c4e3e94)
@@ -16,6 +16,10 @@ module Groonga
       @context ||= Context.instance
     end
 
+    def writer
+      @writer ||= context.writer
+    end
+
     def run_internal(input)
       begin
         run_body(input)
@@ -27,9 +31,5 @@ module Groonga
         nil
       end
     end
-
-    def output(object)
-      context.output(object)
-    end
   end
 end

  Modified: lib/mrb/scripts/context.rb (+4 -0)
===================================================================
--- lib/mrb/scripts/context.rb    2015-02-05 15:52:34 +0900 (042faaf)
+++ lib/mrb/scripts/context.rb    2015-02-05 18:30:20 +0900 (e65d665)
@@ -16,6 +16,10 @@ 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,

  Modified: lib/mrb/sources.am (+3 -1)
===================================================================
--- lib/mrb/sources.am    2015-02-05 15:52:34 +0900 (874a16f)
+++ lib/mrb/sources.am    2015-02-05 18:30:20 +0900 (c4f02fc)
@@ -54,4 +54,6 @@ libgrnmrb_la_SOURCES =				\
 	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

  Modified: plugins/sharding/logical_count.rb (+1 -1)
===================================================================
--- plugins/sharding/logical_count.rb    2015-02-05 15:52:34 +0900 (657decf)
+++ plugins/sharding/logical_count.rb    2015-02-05 18:30:20 +0900 (1d87260)
@@ -22,7 +22,7 @@ module Groonga
                                    shard_key, shard_range,
                                    enumerator.target_range, cover_type)
         end
-        output(total)
+        writer.write(total)
       end
 
       private
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index