Class: CodeStock::Database

Inherits:
Object
  • Object
show all
Includes:
CodeStock, Singleton
Defined in:
lib/cdweb/database.rb

Constant Summary

Constants included from CodeStock

DEFAULT_PATH

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CodeStock

#db_default_dir, #db_expand_groonga_path, #db_groonga_path, #db_yaml_path, #dbdir?

Constructor Details

#initializeDatabase

Returns a new instance of Database.



24
25
26
# File 'lib/cdweb/database.rb', line 24

def initialize
  open(@@db_dir)
end

Class Method Details

.setup(db_dir) ⇒ Object



20
21
22
# File 'lib/cdweb/database.rb', line 20

def self.setup(db_dir)
  @@db_dir = db_dir
end

Instance Method Details

#fileNumObject



47
48
49
# File 'lib/cdweb/database.rb', line 47

def fileNum
  @documents.select.size
end

#open(db_dir) ⇒ Object



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

def open(db_dir)
  dbfile = db_expand_groonga_path(db_dir)
  
  if File.exist? dbfile
    Groonga::Database.open(dbfile)
  else
    raise "error    : #{dbfile} not found!!"
  end
  
  @documents = Groonga::Context.default["documents"]
end

#record(shortpath) ⇒ Object



40
41
42
43
44
45
# File 'lib/cdweb/database.rb', line 40

def record(shortpath)
  before = Time.now
  table = @documents.select { |record| record.shortpath == shortpath }
  elapsed = Time.now - before
  return table.records[0], elapsed
end

#search(patterns, packages, fpaths, suffixs, page = 0, limit = -1)) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cdweb/database.rb', line 51

def search(patterns, packages, fpaths, suffixs, page = 0, limit = -1)
  before = Time.now

  # 全てのパターンを検索
  if (fpaths.include?("*"))
    records, total_records = selectAll(page, limit)
  else
    records, total_records = searchMain(patterns, packages, fpaths, suffixs, page, limit)
  end
  
  # 検索にかかった時間
  elapsed = Time.now - before

  # 結果
  return records, total_records, elapsed
end

#selectAll(page, limit) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/cdweb/database.rb', line 68

def selectAll(page, limit)
  table = @documents.select

  # マッチ数
  total_records = table.size

  # 2010/10/29 ongaeshi
  # 本当はこのようにgroongaAPIでソートしたいのだが上手くいかなかった
  #       # ファイル名順にソート
  #       records = table.sort([{:key => "shortpath", :order => "descending"}],
  #                            :offset => page * limit,
  #                            :limit => limit)
  
  # ソート
  records = table.records.sort_by{|record| record.shortpath.downcase }[page * limit, limit]

  return records, total_records
end