class Marsdawn::Search::Rroonga

Public Class Methods

new(opts, storage) click to toggle source
# File lib/marsdawn/search/rroonga.rb, line 5
def initialize opts, storage
  unless Module.const_defined?('Groonga')
    raise "Gem 'rroonga' should be installed." unless require 'groonga'
  end
  raise "The groonga database path should be specified" unless opts.key?(:path)
  @opts = {
    index_table: {}
  }.merge(opts)
  @storage = storage
  Groonga::Database.open(opts[:path])
  @table_prefix = "#{@storage.key}-#{@storage.lang}-#{@storage.version.tr('.','_')}"
end

Public Instance Methods

create_index() click to toggle source
# File lib/marsdawn/search/rroonga.rb, line 40
def create_index
  drop_tables
  create_tables
  docs = table('documents')
  index = @storage.get_document_info[:site_index]
  index.each do |uri, title|
    page = @storage.get(uri)
    docs.add(uri, :title => title, :content => Marsdawn::Util.strip_tags(page[:content]))
  end
end
create_tables() click to toggle source
# File lib/marsdawn/search/rroonga.rb, line 18
def create_tables
  index_table = {
    :type => :patricia_trie,
    :normalizer => :NormalizerAuto,
    :default_tokenizer => 'TokenBigram'
  }.merge(@opts[:index_table])
  Groonga::Schema.create_table(table_name('documents'), :type => :hash) do |tbl|
    tbl.text('title')
    tbl.text('content')
  end
  Groonga::Schema.create_table(table_name('terms'), index_table) do |tbl|
    tbl.index(col_name('documents', 'title'))
    tbl.index(col_name('documents', 'content'))
  end
end
drop_tables() click to toggle source
# File lib/marsdawn/search/rroonga.rb, line 34
def drop_tables
  Groonga::Schema.remove_table(table_name('documents'))
  Groonga::Schema.remove_table(table_name('terms'))
rescue Groonga::Schema::TableNotExists
end

Private Instance Methods

col_name(table, column) click to toggle source
# File lib/marsdawn/search/rroonga.rb, line 79
def col_name table, column
  "#{table_name(table)}.#{column}"
end
table(name) click to toggle source
# File lib/marsdawn/search/rroonga.rb, line 75
def table name
  Groonga[table_name(name)]
end
table_name(name) click to toggle source
# File lib/marsdawn/search/rroonga.rb, line 71
def table_name name
  "#{@table_prefix}-#{name}"
end