Class: CodeStock::Database

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

Constant Summary collapse

@@db_dir =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDatabase

Returns a new instance of Database.



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

def initialize
  open(@@db_dir || db_default_dir)
end

Class Method Details

.setup(db_dir) ⇒ Object



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

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

Instance Method Details

#fileList(base) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/codestock/cdweb/lib/database.rb', line 80

def fileList(base)
  base_parts = base.split("/")
  base_depth = base_parts.length
  
  # shortpathにマッチするものだけに絞り込む
  if (base == "")
    records = @documents.select.records
  else
    records = @documents.select {|record| record.shortpath =~ base }.to_a
  end

  # ファイルリストの生成
  paths = records.map {|record|
    record.shortpath.split("/")
  }.find_all {|parts|
    parts.length > base_depth and parts[0, base_depth] == base_parts
  }.map {|parts|
    is_file = parts.length == base_depth + 1
    path = parts[0, base_depth + 1].join("/")
    [path, is_file]
  }.uniq
  
  paths
end

#fileNumObject



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

def fileNum
  @documents.select.size
end

#open(db_dir) ⇒ Object



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

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



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

def record(shortpath)
  table = @documents.select { |record| record.shortpath == shortpath }
  return table.records[0]
end

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



50
51
52
53
# File 'lib/codestock/cdweb/lib/database.rb', line 50

def search(patterns, packages, fpaths, suffixs, offset = 0, limit = -1)
  # @todo fpathを厳密に検索するには、検索結果からさらに先頭からのパスではないものを除外する
  records, total_records = searchMain(patterns, packages, fpaths, suffixs, offset, limit)
end

#selectAll(offset = 0, limit = -1)) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/codestock/cdweb/lib/database.rb', line 55

def selectAll(offset = 0, limit = -1)
  table = @documents.select

  # マッチ数
  total_records = table.size

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

  return records, total_records
end