[Groonga-commit] groonga/groonga at 2656d5e [master] mrb: support auto close for cursor by open {|cursor| ...}

Back to archive index

Kouhei Sutou null+****@clear*****
Thu Feb 5 13:12:24 JST 2015


Kouhei Sutou	2015-02-05 13:12:24 +0900 (Thu, 05 Feb 2015)

  New Revision: 2656d5e6bc06b91d7837bd2d554f41c8f17e2df1
  https://github.com/groonga/groonga/commit/2656d5e6bc06b91d7837bd2d554f41c8f17e2df1

  Message:
    mrb: support auto close for cursor by open {|cursor| ...}

  Added files:
    lib/mrb/scripts/index_cursor.rb
  Modified files:
    lib/mrb/mrb_index_cursor.c
    lib/mrb/mrb_table_cursor.c
    lib/mrb/scripts/initialize/post.rb
    lib/mrb/scripts/sources.am
    lib/mrb/scripts/table_cursor.rb

  Modified: lib/mrb/mrb_index_cursor.c (+3 -3)
===================================================================
--- lib/mrb/mrb_index_cursor.c    2015-02-05 13:05:43 +0900 (cfd78dd)
+++ lib/mrb/mrb_index_cursor.c    2015-02-05 13:12:24 +0900 (f57d59d)
@@ -34,7 +34,7 @@ static struct mrb_data_type mrb_grn_index_cursor_type = {
 };
 
 static mrb_value
-mrb_grn_index_cursor_singleton_open(mrb_state *mrb, mrb_value klass)
+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;
@@ -114,8 +114,8 @@ grn_mrb_index_cursor_init(grn_ctx *ctx)
   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",
-                              mrb_grn_index_cursor_singleton_open,
+  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",

  Modified: lib/mrb/mrb_table_cursor.c (+4 -4)
===================================================================
--- lib/mrb/mrb_table_cursor.c    2015-02-05 13:05:43 +0900 (4d68512)
+++ lib/mrb/mrb_table_cursor.c    2015-02-05 13:12:24 +0900 (6605960)
@@ -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
@@ -83,7 +83,7 @@ mrb_value_to_border_value(mrb_state *mrb,
 }
 
 static mrb_value
-mrb_grn_table_cursor_singleton_open(mrb_state *mrb, mrb_value klass)
+mrb_grn_table_cursor_singleton_open_raw(mrb_state *mrb, mrb_value klass)
 {
   grn_ctx *ctx = (grn_ctx *)mrb->ud;
   mrb_value mrb_table;
@@ -195,8 +195,8 @@ grn_mrb_table_cursor_init(grn_ctx *ctx)
   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_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",

  Added: lib/mrb/scripts/index_cursor.rb (+18 -0) 100644
===================================================================
--- /dev/null
+++ lib/mrb/scripts/index_cursor.rb    2015-02-05 13:12:24 +0900 (8044066)
@@ -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

  Modified: lib/mrb/scripts/initialize/post.rb (+1 -0)
===================================================================
--- lib/mrb/scripts/initialize/post.rb    2015-02-05 13:05:43 +0900 (4b5e3d7)
+++ lib/mrb/scripts/initialize/post.rb    2015-02-05 13:12:24 +0900 (94daf05)
@@ -5,6 +5,7 @@ require "context"
 require "database"
 require "command"
 require "table_cursor"
+require "index_cursor"
 
 require "plugin_loader"
 

  Modified: lib/mrb/scripts/sources.am (+1 -0)
===================================================================
--- lib/mrb/scripts/sources.am    2015-02-05 13:05:43 +0900 (79ef475)
+++ lib/mrb/scripts/sources.am    2015-02-05 13:12:24 +0900 (9c64b1d)
@@ -9,6 +9,7 @@ RUBY_SCRIPT_FILES =				\
 	error.rb				\
 	eval_context.rb				\
 	expression.rb				\
+	index_cursor.rb				\
 	index_info.rb				\
 	initialize/pre.rb			\
 	initialize/post.rb			\

  Modified: lib/mrb/scripts/table_cursor.rb (+15 -0)
===================================================================
--- lib/mrb/scripts/table_cursor.rb    2015-02-05 13:05:43 +0900 (a1f1801)
+++ lib/mrb/scripts/table_cursor.rb    2015-02-05 13:12:24 +0900 (a36d88d)
@@ -1,5 +1,20 @@
 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
-------------- next part --------------
HTML����������������������������...
Download 



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