Class: Findrr::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/findrr/database.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_dir = default_base_dir) ⇒ Database

Returns a new instance of Database.



8
9
10
11
# 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

Instance Attribute Details

#base_dirObject

Returns the value of attribute base_dir.



7
8
9
# File 'lib/findrr/database.rb', line 7

def base_dir
  @base_dir
end

Instance Method Details

#collect(target) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# 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

#destroyObject



40
41
42
43
# File 'lib/findrr/database.rb', line 40

def destroy
  FileUtils.rm(Dir.glob(File.join(database_dir, "findrr.db*")))
  FileUtils.rmdir(database_dir)
end

#search(part_of_filename) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/findrr/database.rb', line 27

def search(part_of_filename)
  Groonga::Database.open(database_path) do
    files = Groonga["Files"]
    found_files = files.select do |record|
                    record.basename =~ part_of_filename
                  end
    found_files.each do |file|
      puts file._key.force_encoding("locale")
    end
    found_files.size
  end
end