Class: GrnMini::Array

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

Defined Under Namespace

Classes: IdIsGreaterThanZero

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Array



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/grn_mini/array.rb', line 17

def initialize(path)
  unless File.exist?(path)
    Groonga::Database.create(path: path)
  else
    Groonga::Database.open(path)
  end

  unless Groonga["Array"]
    @grn = Groonga::Array.create(name: "Array", persistent: true) 
    @terms = Groonga::PatriciaTrie.create(name: "Terms", key_normalize: true, default_tokenizer: "TokenBigramSplitSymbolAlphaDigit")
  else
    @grn = Groonga["Array"]
    @terms = Groonga["Terms"]
  end
end

Instance Attribute Details

#grnObject

Returns the value of attribute grn.



7
8
9
# File 'lib/grn_mini/array.rb', line 7

def grn
  @grn
end

Class Method Details

.tmpdbObject



10
11
12
13
14
15
# File 'lib/grn_mini/array.rb', line 10

def self.tmpdb
  Dir.mktmpdir do |dir|
    # p dir
    yield self.new(File.join(dir, "tmp.db"))
  end
end

Instance Method Details

#[](id) ⇒ Object



79
80
81
82
# File 'lib/grn_mini/array.rb', line 79

def [](id)
  raise IdIsGreaterThanZero if id == 0
  @grn[id]
end

#add(hash) ⇒ Object Also known as: <<



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/grn_mini/array.rb', line 33

def add(hash)
  if @grn.empty?
    hash.each do |key, value|
      column = key.to_s

      # @todo Need define_index_column ?
      if value.is_a?(Time)
        @grn.define_column(column, "Time")
      elsif value.is_a?(Float)
        @grn.define_column(column, "Float")
      elsif value.is_a?(Numeric)
        @grn.define_column(column, "Int32")
      else
        @grn.define_column(column, "ShortText")
        @terms.define_index_column("array_#{column}", @grn, source: "Array.#{column}", with_position: true)
      end
    end
  end
  
  @grn.add(hash)
end

#delete(id = nil, &block) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/grn_mini/array.rb', line 84

def delete(id = nil, &block)
  if block_given?
    @grn.delete(&block)
  else
    raise IdIsGreaterThanZero if id == 0
    @grn.delete(id)
  end
end

#eachObject



71
72
73
74
75
# File 'lib/grn_mini/array.rb', line 71

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

#empty?Boolean



67
68
69
# File 'lib/grn_mini/array.rb', line 67

def empty?
  size == 0
end

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



97
98
99
# File 'lib/grn_mini/array.rb', line 97

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

#select(query, options = {default_column: "text"}) ⇒ Object



57
58
59
# File 'lib/grn_mini/array.rb', line 57

def select(query, options = {default_column: "text"})
  @grn.select(query, options)
end

#sizeObject Also known as: length



61
62
63
# File 'lib/grn_mini/array.rb', line 61

def size
  @grn.size
end

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



93
94
95
# File 'lib/grn_mini/array.rb', line 93

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