Class: Pione::Package::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/pione/package/package-database.rb

Overview

Package::Database is a repository of package informations and the digest for cache reference. The keys of database are package name, editor, and tag.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDatabase

Returns a new instance of Database.



38
39
40
41
42
# File 'lib/pione/package/package-database.rb', line 38

def initialize
  # make a 3d table
  @table = Hash.new {|h1,k1| h1[k1] = Hash.new {|h2,k2| h2[k2] = Hash.new}}
  @digests = []
end

Class Method Details

.load(location = Global.package_database_location) ⇒ Object

Load package database from the location. The location should be local.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pione/package/package-database.rb', line 19

def load(location=Global.package_database_location)
  unless location.local?
    raise DatabaseError.new("package database file should be in local: %s" % location.address)
  end

  if location.exist?
    read location.path.open(File::RDONLY) do |f|
      f.flock(File::LOCK_SH)
      f.read
    end
  else
    db = new
    db.save
    Log::SystemLog.info "PIONE created a new package database at %s" % Global.package_database_location.address
    return db
  end
end

.read(str) ⇒ Object

Read package database from the string.



9
10
11
12
13
14
15
16
# File 'lib/pione/package/package-database.rb', line 9

def read(str)
  JSON.load(str).each_with_object(new) do |data, db|
    db.add(DatabaseRecord.new(
        name: data["PackageName"], editor: data["Editor"], tag: data["Tag"],
        location: data["Location"], state: data["State"], digest: data["Digest"]
    ))
  end
end

Instance Method Details

#add(record) ⇒ Object

Add the record.



45
46
47
48
49
50
51
# File 'lib/pione/package/package-database.rb', line 45

def add(record)
  unless record.kind_of?(DatabaseRecord)
    record = DatabaseRecord.new(record)
  end
  @table[record.name][record.editor || "origin"][record.tag] = record
  @digests << record.digest
end

#countObject

Return record number of the database.



60
61
62
63
64
65
66
# File 'lib/pione/package/package-database.rb', line 60

def count
  @table.inject(0) do |i1, (_, t1)|
    t1.inject(i1) do |i2, (_, t2)|
      t2.inject(i2) {|i3, (_, val)| val ? i3 + 1 : i3}
    end
  end
end

#delete(name, editor, tag) ⇒ Object

Delete a record by the tuple of name, editor, and tag. Editor is "origin" if it is nil.



55
56
57
# File 'lib/pione/package/package-database.rb', line 55

def delete(name, editor, tag)
  @table[name][editor || "origin"][tag] = nil
end

#exist?(name, editor, tag) ⇒ Boolean

Return true if the package exists in database.

Parameters:

  • name (String)

    package name

  • editor (String)

    editor name

  • tag (String)

    tag name

Returns:

  • (Boolean)

    true if the package exists in database



88
89
90
# File 'lib/pione/package/package-database.rb', line 88

def exist?(name, editor, tag)
  not(find(name, editor, tag).nil?)
end

#find(name, editor, tag) ⇒ Object

Find a record by the tuple of name, editor, and tag. Editor is "origin" if it is nil.



74
75
76
# File 'lib/pione/package/package-database.rb', line 74

def find(name, editor, tag)
  @table[name][editor || "origin"][tag]
end

#has_digest?(digest) ⇒ Boolean

Return true if the digest is included in package database.

Returns:

  • (Boolean)


69
70
71
# File 'lib/pione/package/package-database.rb', line 69

def has_digest?(digest)
  @digests.include?(digest)
end

#save(location = Global.package_database_location) ⇒ Object

Save the database to the location.



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/pione/package/package-database.rb', line 93

def save(location=Global.package_database_location)
  json = JSON.generate(self)

  # NOTE: File#flock needs read & write mode to avoid truncating without lock
  location.path.open(File::RDWR|File::CREAT) do |f|
    f.flock(File::LOCK_EX)
    f.rewind
    f.write(json)
    f.flush
    f.truncate(f.pos)
  end
end

#to_json(*args) ⇒ Object

Convert to JSON.



107
108
109
110
111
# File 'lib/pione/package/package-database.rb', line 107

def to_json(*args)
  @table.each_with_object([]) do |(_, t1), list|
    t1.each{|_, t2| t2.each{|_, record| list << record if record}}
  end.to_json(*args)
end