[Groonga-commit] groonga/groonga at f81704d [master] mrb: add Database#each_raw

Back to archive index

Kouhei Sutou null+****@clear*****
Fri Aug 26 14:52:48 JST 2016


Kouhei Sutou	2016-08-26 14:52:48 +0900 (Fri, 26 Aug 2016)

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

  Message:
    mrb: add Database#each_raw

  Modified files:
    lib/mrb/scripts/database.rb

  Modified: lib/mrb/scripts/database.rb (+36 -18)
===================================================================
--- lib/mrb/scripts/database.rb    2016-08-26 11:04:47 +0900 (a5a89ed)
+++ lib/mrb/scripts/database.rb    2016-08-26 14:52:48 +0900 (e0c6b4a)
@@ -1,27 +1,25 @@
 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_raw(options=nil)
+      return to_enum(__method__, options) unless block_given?
 
-    def each_name(options={})
-      min = options[:prefix]
+      if options
+        min = options[:prefix]
+        order = options[:order]
+        order_by = options[:order_by]
+      else
+        min = nil
+        order = :ascending
+        order_by = :id
+      end
       flags = 0
-      if options[:order] == :descending
+
+      if order == :descending
         flags |= TableCursorFlags::DESCENDING
       else
         flags |= TableCursorFlags::ASCENDING
       end
-      if options[:order_by] == :id
+      if order_by == :id
         flags |= TableCursorFlags::BY_ID
       else
         flags |= TableCursorFlags::BY_KEY
@@ -29,13 +27,33 @@ module Groonga
       flags |= TableCursorFlags::PREFIX if min
       TableCursor.open(self, :min => min, :flags => flags) do |cursor|
         cursor.each do |id|
-          name = cursor.key
-          yield(name)
+          yield(id, cursor)
         end
       end
     end
 
+    def each(options=nil)
+      return to_enum(__method__, options) unless block_given?
+
+      context = Context.instance
+      each_raw(options) do |id, cursor|
+        object = context[id]
+        yield(object) if object
+      end
+    end
+
+    def each_name(options=nil)
+      return to_enum(__method__, options) unless block_given?
+
+      each_raw(options) do |id, cursor|
+        name = cursor.key
+        yield(name)
+      end
+    end
+
     def each_table(options={})
+      return to_enum(__method__, options) unless block_given?
+
       context = Context.instance
       each_name(options) do |name|
         next if name.include?(".")
-------------- next part --------------
HTML����������������������������...
Download 



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