class Findrr::Database

Attributes

base_dir[RW]

Public Class Methods

new(base_dir=default_base_dir) click to toggle source
# File lib/findrr/database.rb, line 8
def initialize(base_dir=default_base_dir)
  @base_dir = base_dir
  Groonga::Context.default_options = {:encoding => :utf8}
end

Public Instance Methods

collect(target) click to toggle source
# File lib/findrr/database.rb, line 13
def collect(target)
  create_database_dir
  create_database
  Groonga::Database.open(database_path) do
    files = Groonga["Files"]
    Find.find(File.expand_path(target)) do |path|
      unless files.has_key?(path)
        files.add(path, :basename => File.basename(path))
      end
    end
    files.size
  end
end
destroy() click to toggle source
# File lib/findrr/database.rb, line 40
def destroy
  FileUtils.rm(Dir.glob(File.join(database_dir, "findrr.db*")))
  FileUtils.rmdir(database_dir)
end

Private Instance Methods

create_database() click to toggle source
# File lib/findrr/database.rb, line 63
def create_database
  return if File.exist?(database_path)

  Groonga::Database.create(:path => database_path)

  Groonga::Schema.create_table("Files", :type => :patricia_trie) do |table|
    table.short_text("basename")
  end

  Groonga::Schema.create_table("Terms",
                               :type => :patricia_trie,
                               :normalizer => :NormalizerAuto,
                               :default_tokenizer => "TokenBigram") do |table|
    table.index("Files.basename")
  end
end
create_database_dir() click to toggle source
# File lib/findrr/database.rb, line 58
def create_database_dir
  return if File.exist?(database_dir)
  FileUtils.mkdir_p(database_dir)
end
database_dir() click to toggle source
# File lib/findrr/database.rb, line 50
def database_dir
  File.join(@base_dir, "db")
end
database_path() click to toggle source
# File lib/findrr/database.rb, line 54
def database_path
  File.join(database_dir, "findrr.db")
end
default_base_dir() click to toggle source
# File lib/findrr/database.rb, line 46
def default_base_dir
  File.join(File.expand_path("~"), ".findrr")
end