Class: Milkode::Database
- Inherits:
-
Object
- Object
- Milkode::Database
- Includes:
- Singleton
- Defined in:
- lib/milkode/cdweb/lib/database.rb
Overview
TODO:
データベースアクセスは将来的にはGroongaDatabaseに収束させる
Constant Summary collapse
- @@db_dir =
nil
Instance Attribute Summary collapse
-
#grndb ⇒ Object
readonly
Returns the value of attribute grndb.
-
#yaml ⇒ Object
readonly
Returns the value of attribute yaml.
Class Method Summary collapse
Instance Method Summary collapse
- #fav?(name) ⇒ Boolean
- #fileList(base) ⇒ Object
-
#initialize ⇒ Database
constructor
A new instance of Database.
- #open ⇒ Object
- #open_force ⇒ Object
-
#package_records(name) ⇒ Object
指定パッケージに属する全てのレコードを得る.
- #packages(sort_kind) ⇒ Object
- #record(shortpath) ⇒ Object
- #search(patterns, keywords, packages, current_path, fpaths, suffixs, fpath_or_packages, offset = 0, limit = -1)) ⇒ Object
- #selectAll(offset, limit) ⇒ Object
- #set_fav(name, favorited) ⇒ Object
-
#totalRecords ⇒ Object
レコード数を得る.
- #touch_viewtime(path) ⇒ Object
- #update(name) ⇒ Object
- #update_all ⇒ Object
-
#yaml_package_num ⇒ Object
yamlからパッケージの総数を得る.
- #yaml_reload ⇒ Object
Constructor Details
#initialize ⇒ Database
Returns a new instance of Database.
37 38 39 |
# File 'lib/milkode/cdweb/lib/database.rb', line 37 def initialize open end |
Instance Attribute Details
#grndb ⇒ Object (readonly)
Returns the value of attribute grndb.
35 36 37 |
# File 'lib/milkode/cdweb/lib/database.rb', line 35 def grndb @grndb end |
#yaml ⇒ Object (readonly)
Returns the value of attribute yaml.
34 35 36 |
# File 'lib/milkode/cdweb/lib/database.rb', line 34 def yaml @yaml end |
Class Method Details
.dbdir ⇒ Object
30 31 32 |
# File 'lib/milkode/cdweb/lib/database.rb', line 30 def self.dbdir @@db_dir || Dbdir.default_dir end |
.setup(db_dir) ⇒ Object
26 27 28 |
# File 'lib/milkode/cdweb/lib/database.rb', line 26 def self.setup(db_dir) @@db_dir = db_dir end |
.validate? ⇒ Boolean
209 210 211 |
# File 'lib/milkode/cdweb/lib/database.rb', line 209 def self.validate? YamlFileWrapper.load_if(Database.dbdir) != nil end |
Instance Method Details
#fav?(name) ⇒ Boolean
186 187 188 |
# File 'lib/milkode/cdweb/lib/database.rb', line 186 def fav?(name) @grndb.packages.fav?(name) end |
#fileList(base) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/milkode/cdweb/lib/database.rb', line 135 def fileList(base) base_parts = base.split("/") base_depth = base_parts.length # 'depth==0'の時はMilkodeYaml#contentsからファイルリストを生成して高速化 if (base_depth == 0) return yaml_load.contents.sort_by{|v| v.name}.map{|v| [v.name, false] } end # base/以下のファイルを全て取得 records = @documents.find_shortpath_below(base) # ファイルリストの生成 paths = records.map {|record| DocumentRecord.new(record).shortpath.split("/") }.find_all {|parts| # 先頭フォルダ名が一致するものをピックアップ parts.length > base_depth && parts[0, base_depth] == base_parts }.map {|parts| # [path, is_file] [parts[0, base_depth + 1].join("/"), parts.length == base_depth + 1] }.sort_by {|parts| # 配列の比較を利用したディレクトリ優先ソート # aaa, bbb/, aaa/, bbb -> [aaa/, bbb/, aaa, bbb] [parts[1] ? 1 : 0, parts[0].downcase] # [is_file(int), path(downcase)] }.uniq paths end |
#open ⇒ Object
45 46 47 48 49 |
# File 'lib/milkode/cdweb/lib/database.rb', line 45 def open if !@grndb || @grndb.closed? open_force end end |
#open_force ⇒ Object
51 52 53 54 55 56 |
# File 'lib/milkode/cdweb/lib/database.rb', line 51 def open_force @grndb = GroongaDatabase.new @grndb.open(Database.dbdir) @grndb.yaml_sync(yaml_load.contents) @documents = @grndb.documents end |
#package_records(name) ⇒ Object
指定パッケージに属する全てのレコードを得る
124 125 126 |
# File 'lib/milkode/cdweb/lib/database.rb', line 124 def package_records(name) @documents.package_records(name) end |
#packages(sort_kind) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/milkode/cdweb/lib/database.rb', line 165 def packages(sort_kind) sorted = nil if sort_kind == "favtime" sorted = @grndb.packages.favs elsif (sort_kind) sorted = @grndb.packages.sort(sort_kind) else # 大文字/小文字を無視してソートするため、速度を犠牲に # sorted = @grndb.packages.sort("name", "ascending") sorted = @grndb.packages.to_a.sort_by {|r| r.name.downcase} end sorted.map {|r| r.name} end |
#record(shortpath) ⇒ Object
58 59 60 |
# File 'lib/milkode/cdweb/lib/database.rb', line 58 def record(shortpath) DocumentRecord.create @documents.find_shortpath(shortpath) end |
#search(patterns, keywords, packages, current_path, fpaths, suffixs, fpath_or_packages, offset = 0, limit = -1)) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/milkode/cdweb/lib/database.rb', line 62 def search(patterns, keywords, packages, current_path, fpaths, suffixs, fpath_or_packages, offset = 0, limit = -1) paths = [] strict_packages = [] is_not_search = false # パッケージ名未指定の時は現在位置を検索条件に追加 if packages.empty? && current_path != '' package, restpath = Util::divide_shortpath(current_path) grn_package = @grndb.packages[package] if grn_package # パッケージ名 strict_packages << package # ファイルパス directory = grn_package.directory if restpath paths << File.join(directory, restpath) else paths << directory end else is_not_search = true end end # 検索 result, total_records = [], 0 begin unless is_not_search result, total_records = @documents.search_with_match( :patterns => patterns, :keywords => keywords, :paths => paths, :packages => packages, :strict_packages => strict_packages, :restpaths => fpaths, :suffixs => suffixs, :fpath_or_packages => fpath_or_packages, :offset => offset, :limit => limit ) end rescue Groonga::TooLargeOffset end # 結果 return result.map{|r| DocumentRecord.new(r)}, total_records end |
#selectAll(offset, limit) ⇒ Object
114 115 116 |
# File 'lib/milkode/cdweb/lib/database.rb', line 114 def selectAll(offset, limit) @documents.select_all_sort_by_shortpath(offset, limit) end |
#set_fav(name, favorited) ⇒ Object
190 191 192 193 |
# File 'lib/milkode/cdweb/lib/database.rb', line 190 def set_fav(name, favorited) time = favorited ? Time.now : Time.at(0) @grndb.packages.touch_if(name, :favtime, time) end |
#totalRecords ⇒ Object
レコード数を得る
119 120 121 |
# File 'lib/milkode/cdweb/lib/database.rb', line 119 def totalRecords @documents.size end |
#touch_viewtime(path) ⇒ Object
181 182 183 184 |
# File 'lib/milkode/cdweb/lib/database.rb', line 181 def touch_viewtime(path) package, restpath = Util::divide_shortpath(path) @grndb.packages.touch_if(package, :viewtime) if package end |
#update(name) ⇒ Object
195 196 197 198 199 |
# File 'lib/milkode/cdweb/lib/database.rb', line 195 def update(name) result = Updater::ResultAccumulator.new result << update_in(yaml_load.find_name(name)) result end |
#update_all ⇒ Object
201 202 203 204 205 206 207 |
# File 'lib/milkode/cdweb/lib/database.rb', line 201 def update_all result = Updater::ResultAccumulator.new yaml_load.contents.each do |package| result << update_in(package) end result end |
#yaml_package_num ⇒ Object
TODO:
PackageTableから取得するように変更する
yamlからパッケージの総数を得る
130 131 132 |
# File 'lib/milkode/cdweb/lib/database.rb', line 130 def yaml_package_num yaml_load.contents.size end |
#yaml_reload ⇒ Object
41 42 43 |
# File 'lib/milkode/cdweb/lib/database.rb', line 41 def yaml_reload # @yaml = YamlFileWrapper.load_if(@@db_dir || Dbdir.default_dir) end |