Class: GrnMini::Table

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/grn_mini/table.rb

Direct Known Subclasses

Array, Hash

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, grn) ⇒ Table

Returns a new instance of Table.



11
12
13
14
15
16
# File 'lib/grn_mini/table.rb', line 11

def initialize(name, grn)
  @name = name
  @grn  = grn
  @terms = Groonga["Terms"] || Groonga::PatriciaTrie.create(name: "Terms", key_normalize: true, default_tokenizer: GrnMini::default_tokenizer)
  @setup_columns_once = false
end

Instance Attribute Details

#grnObject

Returns the value of attribute grn.



8
9
10
# File 'lib/grn_mini/table.rb', line 8

def grn
  @grn
end

Instance Method Details

#eachObject



102
103
104
105
106
# File 'lib/grn_mini/table.rb', line 102

def each
  @grn.each do |record|
    yield record
  end
end

#empty?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/grn_mini/table.rb', line 98

def empty?
  size == 0
end

#group(key, options = {}) ⇒ Object



112
113
114
# File 'lib/grn_mini/table.rb', line 112

def group(key, options = {})
  @grn.group(key, options)
end

#need_setup_columns?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/grn_mini/table.rb', line 18

def need_setup_columns?
  !@setup_columns_once && empty?
end

#select(*args, &block) ⇒ Object



90
91
92
# File 'lib/grn_mini/table.rb', line 90

def select(*args, &block)
  @grn.select(*args, &block)
end

#setup_columns(hash) ⇒ Object



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
66
# File 'lib/grn_mini/table.rb', line 22

def setup_columns(hash)
  Groonga::Schema.define do |schema|
    schema.change_table(@name) do |table|
      hash.each do |key, value|
        column = key.to_s

        if value.is_a?(::Array)
          table.column(column, value_type(value), type: :vector)
        else
          table.column(column, value_type(value))
        end
      end
    end

    schema.change_table("Terms") do |table|
      hash.each do |key, value|
        column = key.to_s

        if value.is_a?(String)
          table.index("#{@name}.#{column}", with_position: true)
        end
      end
    end

    hash.each do |key, value|
      column = key.to_s
      
      if value.is_a?(::Array)
        elem = value.first
        
        if elem.is_a?(GrnMini::Table)
          schema.change_table(elem.grn.name) do |table|
            table.index("#{@grn.name}.#{column}")
          end
        elsif elem.is_a?(Groonga::Table)
          schema.change_table(elem.name) do |table|
            table.index("#{@grn.name}.#{column}")
          end
        end
      end
    end
  end

  @setup_columns_once = true
end

#sizeObject



94
95
96
# File 'lib/grn_mini/table.rb', line 94

def size
  @grn.size
end

#sort(keys, options = {}) ⇒ Object



108
109
110
# File 'lib/grn_mini/table.rb', line 108

def sort(keys, options = {})
  @grn.sort(keys, options)
end

#value_type(value) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/grn_mini/table.rb', line 68

def value_type(value)
  if value.is_a?(TrueClass) || value.is_a?(FalseClass)
    "Bool"
  elsif value.is_a?(Integer)
    "Int32"
  elsif value.is_a?(Float)
    "Float"
  elsif value.is_a?(Time)
    "Time"
  elsif value.is_a?(String)
    "ShortText"
  elsif value.is_a?(GrnMini::Table)
    value.grn.name
  elsif value.is_a?(Groonga::Table)
    value.name
  elsif value.is_a?(::Array)
    value_type(value.first)
  else
    raise NotSupportColumnType, value
  end
end