summaryrefslogtreecommitdiffstats
path: root/storage/mroonga/vendor/groonga/lib/mrb/scripts/database.rb
blob: e0c6b4a01934d968b507fe4972ebace035fc1b6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
module Groonga
  class Database
    def each_raw(options=nil)
      return to_enum(__method__, options) unless block_given?

      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 order == :descending
        flags |= TableCursorFlags::DESCENDING
      else
        flags |= TableCursorFlags::ASCENDING
      end
      if order_by == :id
        flags |= TableCursorFlags::BY_ID
      else
        flags |= TableCursorFlags::BY_KEY
      end
      flags |= TableCursorFlags::PREFIX if min
      TableCursor.open(self, :min => min, :flags => flags) do |cursor|
        cursor.each do |id|
          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?(".")
        object = context[name]
        yield(object) if object.is_a?(Table)
      end
    end
  end
end