Class: Milkode::Database

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

Constant Summary collapse

@@db_dir =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDatabase

Returns a new instance of Database.



39
40
41
# File 'lib/milkode/cdweb/lib/database.rb', line 39

def initialize
  open
end

Instance Attribute Details

#documentsObject (readonly)

Returns the value of attribute documents.



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

def documents
  @documents
end

#grndbObject (readonly)

Returns the value of attribute grndb.



37
38
39
# File 'lib/milkode/cdweb/lib/database.rb', line 37

def grndb
  @grndb
end

#yamlObject (readonly)

Returns the value of attribute yaml.



36
37
38
# File 'lib/milkode/cdweb/lib/database.rb', line 36

def yaml
  @yaml
end

Class Method Details

.dbdirObject



32
33
34
# File 'lib/milkode/cdweb/lib/database.rb', line 32

def self.dbdir
  @@db_dir || Dbdir.default_dir
end

.setup(db_dir) ⇒ Object



28
29
30
# File 'lib/milkode/cdweb/lib/database.rb', line 28

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

.validate?Boolean

Returns:

  • (Boolean)


262
263
264
# File 'lib/milkode/cdweb/lib/database.rb', line 262

def self.validate?
  YamlFileWrapper.load_if(Database.dbdir) != nil
end

Instance Method Details

#fav?(name) ⇒ Boolean

Returns:

  • (Boolean)


239
240
241
# File 'lib/milkode/cdweb/lib/database.rb', line 239

def fav?(name)
  @grndb.packages.fav?(name)
end

#fileList(base) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/milkode/cdweb/lib/database.rb', line 188

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

#openObject



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

def open
  if !@grndb || @grndb.closed?
    open_force
  end
end

#open_forceObject



53
54
55
56
57
58
# File 'lib/milkode/cdweb/lib/database.rb', line 53

def open_force
  @grndb = GroongaDatabase.new
  @grndb.open(Database.dbdir)
  @grndb.yaml_sync(yaml_load.contents)
  @documents = @grndb.documents
end

#package_records(name) ⇒ Object

指定パッケージに属する全てのレコードを得る



173
174
175
# File 'lib/milkode/cdweb/lib/database.rb', line 173

def package_records(name)
  @documents.package_records(name)
end

#packages(sort_kind) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/milkode/cdweb/lib/database.rb', line 218

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



60
61
62
# File 'lib/milkode/cdweb/lib/database.rb', line 60

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



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
113
114
# File 'lib/milkode/cdweb/lib/database.rb', line 64

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

  # 検索
  records, total_records, result = [], 0, nil

  begin
    unless is_not_search
      records, total_records, result = @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 records.map{|r| DocumentRecord.new(r)}, total_records, result
end

#selectAll(current_path, offset, limit) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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
164
165
# File 'lib/milkode/cdweb/lib/database.rb', line 116

def selectAll(current_path, offset, limit)
  if current_path == ''
    return @documents.select_all_sort_by_shortpath(offset, limit)
  end

  paths = []
  strict_packages = []
  is_not_search = false

  package, restpath = Util.divide_shortpath(current_path)

  grn_package = @grndb.packages[package]
  if grn_package
    # package name
    strict_packages << package

    # file path
    directory = grn_package.directory
    if restpath
      paths << File.join(directory, restpath)
    else
      paths << directory
    end
  else
    is_not_search = true
  end

  # search
  records, total_records, result = [], 0, nil

  begin
    unless is_not_search
      records, total_records, result = @documents.search_with_match(
        :paths     => paths,
        :strict_packages  => strict_packages,
        :offset    => offset,
        :limit     => limit
      )
    end
  rescue Groonga::TooLargeOffset
  end

  if (limit != -1)
    records = records.sort_by{|record| DocumentRecord::shortpath(record).downcase }
  else
    records = records.sort_by{|record| DocumentRecord::shortpath(record).downcase }
  end

  return records, result.size
end

#set_fav(name, favorited) ⇒ Object



243
244
245
246
# File 'lib/milkode/cdweb/lib/database.rb', line 243

def set_fav(name, favorited)
  time = favorited ? Time.now : Time.at(0)
  @grndb.packages.touch_if(name, :favtime, time)
end

#totalRecordsObject

レコード数を得る



168
169
170
# File 'lib/milkode/cdweb/lib/database.rb', line 168

def totalRecords
  @documents.size
end

#touch_viewtime(path) ⇒ Object



234
235
236
237
# File 'lib/milkode/cdweb/lib/database.rb', line 234

def touch_viewtime(path)
  package, restpath = Util.divide_shortpath(path)
  @grndb.packages.touch_if(package, :viewtime) if package
end

#update(name) ⇒ Object



248
249
250
251
252
# File 'lib/milkode/cdweb/lib/database.rb', line 248

def update(name)
  result = Updater::ResultAccumulator.new
  result << update_in(yaml_load.find_name(name))
  result
end

#update_allObject



254
255
256
257
258
259
260
# File 'lib/milkode/cdweb/lib/database.rb', line 254

def update_all
  result = Updater::ResultAccumulator.new
  yaml_load.contents.each do |package|
    result << update_in(package)
  end
  result
end

#yaml_package(name) ⇒ Object



183
184
185
# File 'lib/milkode/cdweb/lib/database.rb', line 183

def yaml_package(name)
  yaml_load.find_name(name)
end

#yaml_package_numObject

TODO:

PackageTableから取得するように変更する

yamlからパッケージの総数を得る



179
180
181
# File 'lib/milkode/cdweb/lib/database.rb', line 179

def yaml_package_num
  yaml_load.contents.size
end

#yaml_reloadObject



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

def yaml_reload
  # @yaml = YamlFileWrapper.load_if(@@db_dir || Dbdir.default_dir)
end