| 1 | module Groonga |
| 2 | class Database |
| 3 | def each_raw(options=nil) |
| 4 | return to_enum(__method__, options) unless block_given? |
| 5 | |
| 6 | if options |
| 7 | min = options[:prefix] |
| 8 | order = options[:order] |
| 9 | order_by = options[:order_by] |
| 10 | else |
| 11 | min = nil |
| 12 | order = :ascending |
| 13 | order_by = :id |
| 14 | end |
| 15 | flags = 0 |
| 16 | |
| 17 | if order == :descending |
| 18 | flags |= TableCursorFlags::DESCENDING |
| 19 | else |
| 20 | flags |= TableCursorFlags::ASCENDING |
| 21 | end |
| 22 | if order_by == :id |
| 23 | flags |= TableCursorFlags::BY_ID |
| 24 | else |
| 25 | flags |= TableCursorFlags::BY_KEY |
| 26 | end |
| 27 | flags |= TableCursorFlags::PREFIX if min |
| 28 | TableCursor.open(self, :min => min, :flags => flags) do |cursor| |
| 29 | cursor.each do |id| |
| 30 | yield(id, cursor) |
| 31 | end |
| 32 | end |
| 33 | end |
| 34 | |
| 35 | def each(options=nil) |
| 36 | return to_enum(__method__, options) unless block_given? |
| 37 | |
| 38 | context = Context.instance |
| 39 | use_temporary_open_space = options[:use_temporary_open_space] |
| 40 | each_raw(options) do |id, cursor| |
| 41 | context.with_temporary_open_space(use_temporary_open_space) do |
| 42 | object = context[id] |
| 43 | yield(object) if object |
| 44 | end |
| 45 | end |
| 46 | end |
| 47 | |
| 48 | def each_name(options=nil) |
| 49 | return to_enum(__method__, options) unless block_given? |
| 50 | |
| 51 | each_raw(options) do |id, cursor| |
| 52 | name = cursor.key |
| 53 | yield(name) |
| 54 | end |
| 55 | end |
| 56 | |
| 57 | def each_table(options={}) |
| 58 | return to_enum(__method__, options) unless block_given? |
| 59 | |
| 60 | context = Context.instance |
| 61 | each_name(options) do |name| |
| 62 | next if name.include?(".") |
| 63 | object = context[name] |
| 64 | yield(object) if object.is_a?(Table) |
| 65 | end |
| 66 | end |
| 67 | end |
| 68 | end |