class Milkode::Database

@todo データベースアクセスは将来的にはGroongaDatabaseに収束させる

Attributes

documents[R]
grndb[R]
yaml[R]

Public Class Methods

dbdir() click to toggle source
# File lib/milkode/cdweb/lib/database.rb, line 31
def self.dbdir
  @@db_dir || Dbdir.default_dir
end
new() click to toggle source
# File lib/milkode/cdweb/lib/database.rb, line 38
def initialize
  open
end
setup(db_dir) click to toggle source
# File lib/milkode/cdweb/lib/database.rb, line 27
def self.setup(db_dir)
  @@db_dir = db_dir
end
validate?() click to toggle source
# File lib/milkode/cdweb/lib/database.rb, line 261
def self.validate?
  YamlFileWrapper.load_if(Database.dbdir) != nil
end

Public Instance Methods

fav?(name) click to toggle source
# File lib/milkode/cdweb/lib/database.rb, line 238
def fav?(name)
  @grndb.packages.fav?(name)
end
fileList(base) click to toggle source

@sample test/test_database.rb:43 TestDatabase#t_fileList

# File lib/milkode/cdweb/lib/database.rb, line 187
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() click to toggle source
# File lib/milkode/cdweb/lib/database.rb, line 46
def open
  if !@grndb || @grndb.closed?
    open_force
  end
end
open_force() click to toggle source
# File lib/milkode/cdweb/lib/database.rb, line 52
def open_force
  @grndb = GroongaDatabase.new
  @grndb.open(Database.dbdir)
  @grndb.yaml_sync(yaml_load.contents)
  @documents = @grndb.documents
end
package_records(name) click to toggle source

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

# File lib/milkode/cdweb/lib/database.rb, line 172
def package_records(name)
  @documents.package_records(name)
end
packages(sort_kind) click to toggle source
# File lib/milkode/cdweb/lib/database.rb, line 217
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) click to toggle source
# File lib/milkode/cdweb/lib/database.rb, line 59
def record(shortpath)
  DocumentRecord.create @documents.find_shortpath(shortpath)
end
selectAll(current_path, offset, limit) click to toggle source
# File lib/milkode/cdweb/lib/database.rb, line 115
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) click to toggle source
# File lib/milkode/cdweb/lib/database.rb, line 242
def set_fav(name, favorited)
  time = favorited ? Time.now : Time.at(0)
  @grndb.packages.touch_if(name, :favtime, time)
end
totalRecords() click to toggle source

レコード数を得る

# File lib/milkode/cdweb/lib/database.rb, line 167
def totalRecords
  @documents.size
end
touch_viewtime(path) click to toggle source
# File lib/milkode/cdweb/lib/database.rb, line 233
def touch_viewtime(path)
  package, restpath = Util.divide_shortpath(path)
  @grndb.packages.touch_if(package, :viewtime) if package
end
update(name) click to toggle source
# File lib/milkode/cdweb/lib/database.rb, line 247
def update(name)
  result = Updater::ResultAccumulator.new
  result << update_in(yaml_load.find_name(name))
  result
end
update_all() click to toggle source
# File lib/milkode/cdweb/lib/database.rb, line 253
def update_all
  result = Updater::ResultAccumulator.new
  yaml_load.contents.each do |package|
    result << update_in(package)
  end
  result
end
yaml_package(name) click to toggle source
# File lib/milkode/cdweb/lib/database.rb, line 182
def yaml_package(name)
  yaml_load.find_name(name)
end
yaml_package_num() click to toggle source

yamlからパッケージの総数を得る @todo PackageTableから取得するように変更する

# File lib/milkode/cdweb/lib/database.rb, line 178
def yaml_package_num
  yaml_load.contents.size
end
yaml_reload() click to toggle source
# File lib/milkode/cdweb/lib/database.rb, line 42
def yaml_reload
  # @yaml = YamlFileWrapper.load_if(@@db_dir || Dbdir.default_dir)
end

Private Instance Methods

update_in(package) click to toggle source
# File lib/milkode/cdweb/lib/database.rb, line 271
def update_in(package)
  updater = Updater.new(@grndb, package.name)

  yaml = yaml_load
  
  updater.set_global_gitignore(yaml.global_gitignore) if yaml.global_gitignore
  updater.set_package_ignore IgnoreSetting.new("/", package.ignore)
  updater.enable_no_auto_ignore         if package.options[:no_auto_ignore]
  
  updater.enable_update_with_git_pull   if package.options[:update_with_git_pull]
  updater.enable_update_with_svn_update if package.options[:update_with_svn_update]
  updater.enable_update_with_ctags      if package.options[:update_with_ctags]
  updater.enable_update_with_ctags_e    if package.options[:update_with_ctags_e]

  updater.exec
  updater.result
end
yaml_load() click to toggle source
# File lib/milkode/cdweb/lib/database.rb, line 267
def yaml_load
  YamlFileWrapper.load_if(Database.dbdir)
end