Class: Grenweb::Database

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/grenweb/database.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDatabase

Returns a new instance of Database.



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

def initialize
  open(@@dbFile)
end

Class Method Details

.setup(dbFile) ⇒ Object



16
17
18
# File 'lib/grenweb/database.rb', line 16

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

Instance Method Details

#fileNumObject



43
44
45
# File 'lib/grenweb/database.rb', line 43

def fileNum
  @documents.select.size
end

#open(dbFile) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/grenweb/database.rb', line 24

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

#record(shortpath) ⇒ Object



36
37
38
39
40
41
# File 'lib/grenweb/database.rb', line 36

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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/grenweb/database.rb', line 47

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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/grenweb/database.rb', line 64

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