Module: Gpr::Actions::Search

Included in:
Commands::Search
Defined in:
lib/gpr/actions/search.rb

Constant Summary collapse

DB_PATH =
"#{::Gpr::APP_PATH}/.database"

Instance Method Summary collapse

Instance Method Details

#add_file(path) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/gpr/actions/search.rb', line 36

def add_file(path)
  repo_info = parse_repository(path)

  GitHelper.ls_files(path).each do |file|
    next unless FileTest.file?(file)
    Groonga['Files'].add(
      File.expand_path(file),
      filename: File.basename(file),
      body: File.open(file).read,
      host: repo_info[:host],
      repository: repo_info[:repository]
    )
  end
end

#initialize_groongaObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/gpr/actions/search.rb', line 12

def initialize_groonga
  if FileTest.exist?("#{DB_PATH}/gpr.db")
    Groonga::Database.open("#{DB_PATH}/gpr.db")
  else
    FileUtils.mkdir_p(DB_PATH)
    Groonga::Context.default_options = { encoding: :utf8 }
    Groonga::Database.create(path: "#{DB_PATH}/gpr.db")
    Groonga::Schema.create_table('Files', type: :patricia_trie) do |t|
      t.text('filename')
      t.text('body')
      t.text('host')
      t.text('repository')
    end
    Groonga::Schema.create_table(
      'Terms',
      type: :patricia_trie,
      normalizer: :NormalizerAuto,
      default_tokenizer: 'TokenBigramSplitSymbolAlpha'
    ) do |t|
      t.index('Files.body')
    end
  end
end