1999-01-20 07:59:39 +03:00
|
|
|
#
|
1998-01-16 15:19:09 +03:00
|
|
|
# How to use:
|
|
|
|
#
|
|
|
|
# db = PStore.new("/tmp/foo")
|
|
|
|
# db.transaction do
|
|
|
|
# p db.roots
|
|
|
|
# ary = db["root"] = [1,2,3,4]
|
|
|
|
# ary[0] = [1,1.5]
|
|
|
|
# end
|
|
|
|
|
|
|
|
# db.transaction do
|
|
|
|
# p db["root"]
|
|
|
|
# end
|
|
|
|
|
2004-03-07 16:37:52 +03:00
|
|
|
require "fileutils"
|
2001-12-01 17:07:01 +03:00
|
|
|
require "digest/md5"
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
class PStore
|
1999-01-20 07:59:39 +03:00
|
|
|
class Error < StandardError
|
|
|
|
end
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
def initialize(file)
|
|
|
|
dir = File::dirname(file)
|
|
|
|
unless File::directory? dir
|
|
|
|
raise PStore::Error, format("directory %s does not exist", dir)
|
|
|
|
end
|
|
|
|
if File::exist? file and not File::readable? file
|
|
|
|
raise PStore::Error, format("file %s not readable", file)
|
|
|
|
end
|
|
|
|
@transaction = false
|
|
|
|
@filename = file
|
|
|
|
@abort = false
|
|
|
|
end
|
|
|
|
|
|
|
|
def in_transaction
|
|
|
|
raise PStore::Error, "not in transaction" unless @transaction
|
|
|
|
end
|
2004-02-17 18:26:03 +03:00
|
|
|
def in_transaction_wr()
|
|
|
|
in_transaction()
|
|
|
|
raise PStore::Error, "in read-only transaction" if @rdonly
|
|
|
|
end
|
|
|
|
private :in_transaction, :in_transaction_wr
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
def [](name)
|
|
|
|
in_transaction
|
2002-07-11 12:22:18 +04:00
|
|
|
@table[name]
|
|
|
|
end
|
|
|
|
def fetch(name, default=PStore::Error)
|
2001-05-06 19:06:00 +04:00
|
|
|
unless @table.key? name
|
2002-07-11 12:22:18 +04:00
|
|
|
if default==PStore::Error
|
|
|
|
raise PStore::Error, format("undefined root name `%s'", name)
|
|
|
|
else
|
|
|
|
default
|
|
|
|
end
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
2002-07-11 12:22:18 +04:00
|
|
|
self[name]
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
|
|
|
def []=(name, value)
|
2004-02-17 18:26:03 +03:00
|
|
|
in_transaction_wr()
|
1998-01-16 15:19:09 +03:00
|
|
|
@table[name] = value
|
|
|
|
end
|
2000-03-06 07:15:42 +03:00
|
|
|
def delete(name)
|
2004-02-17 18:26:03 +03:00
|
|
|
in_transaction_wr()
|
2000-03-06 07:15:42 +03:00
|
|
|
@table.delete name
|
|
|
|
end
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
def roots
|
|
|
|
in_transaction
|
|
|
|
@table.keys
|
|
|
|
end
|
|
|
|
def root?(name)
|
|
|
|
in_transaction
|
|
|
|
@table.key? name
|
|
|
|
end
|
|
|
|
def path
|
|
|
|
@filename
|
|
|
|
end
|
|
|
|
|
|
|
|
def commit
|
2001-05-06 19:06:00 +04:00
|
|
|
in_transaction
|
1998-01-16 15:19:09 +03:00
|
|
|
@abort = false
|
|
|
|
throw :pstore_abort_transaction
|
|
|
|
end
|
|
|
|
def abort
|
2001-05-06 19:06:00 +04:00
|
|
|
in_transaction
|
1998-01-16 15:19:09 +03:00
|
|
|
@abort = true
|
|
|
|
throw :pstore_abort_transaction
|
|
|
|
end
|
|
|
|
|
2001-07-31 10:24:45 +04:00
|
|
|
def transaction(read_only=false)
|
1998-01-16 15:19:09 +03:00
|
|
|
raise PStore::Error, "nested transaction" if @transaction
|
|
|
|
begin
|
2004-02-17 18:26:03 +03:00
|
|
|
@rdonly = read_only
|
|
|
|
@abort = false
|
1998-01-16 15:19:09 +03:00
|
|
|
@transaction = true
|
1999-11-25 12:03:08 +03:00
|
|
|
value = nil
|
2004-02-17 18:26:03 +03:00
|
|
|
new_file = @filename + ".new"
|
|
|
|
|
|
|
|
content = nil
|
|
|
|
file = File.open(@filename, File::RDWR | File::CREAT)
|
|
|
|
if !read_only
|
|
|
|
file.flock(File::LOCK_EX)
|
2004-03-07 16:37:52 +03:00
|
|
|
commit_new(file) if FileTest.exist?(new_file)
|
2004-02-17 18:26:03 +03:00
|
|
|
content = file.read()
|
|
|
|
else
|
|
|
|
file.flock(File::LOCK_SH)
|
|
|
|
if FileTest.exist?(new_file)
|
|
|
|
File.open(new_file) {|fp| content = fp.read()}
|
|
|
|
else
|
|
|
|
content = file.read()
|
|
|
|
end
|
1999-11-17 10:30:37 +03:00
|
|
|
end
|
2004-02-17 18:26:03 +03:00
|
|
|
|
|
|
|
if content != ""
|
2001-05-06 19:06:00 +04:00
|
|
|
@table = Marshal::load(content)
|
2004-02-17 18:26:03 +03:00
|
|
|
if !read_only
|
|
|
|
size = content.size
|
|
|
|
md5 = Digest::MD5.digest(content)
|
|
|
|
end
|
2001-05-06 19:06:00 +04:00
|
|
|
else
|
|
|
|
@table = {}
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
2004-02-17 18:26:03 +03:00
|
|
|
content = nil # unreference huge data
|
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
begin
|
|
|
|
catch(:pstore_abort_transaction) do
|
|
|
|
value = yield(self)
|
|
|
|
end
|
1999-01-20 07:59:39 +03:00
|
|
|
rescue Exception
|
|
|
|
@abort = true
|
|
|
|
raise
|
1998-01-16 15:19:09 +03:00
|
|
|
ensure
|
2002-11-14 09:18:59 +03:00
|
|
|
if !read_only and !@abort
|
2004-02-17 18:26:03 +03:00
|
|
|
tmp_file = @filename + ".tmp"
|
2001-05-06 19:06:00 +04:00
|
|
|
content = Marshal::dump(@table)
|
2001-12-01 17:07:01 +03:00
|
|
|
if !md5 || size != content.size || md5 != Digest::MD5.digest(content)
|
2004-02-17 18:26:03 +03:00
|
|
|
File.open(tmp_file, "w") {|t|
|
|
|
|
t.write(content)
|
|
|
|
}
|
|
|
|
File.rename(tmp_file, new_file)
|
2004-03-07 16:37:52 +03:00
|
|
|
commit_new(file)
|
2004-02-17 18:26:03 +03:00
|
|
|
end
|
|
|
|
content = nil # unreference huge data
|
2002-11-14 09:18:59 +03:00
|
|
|
end
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
|
|
|
ensure
|
1999-01-20 07:59:39 +03:00
|
|
|
@table = nil
|
1998-01-16 15:19:09 +03:00
|
|
|
@transaction = false
|
2001-11-19 08:03:03 +03:00
|
|
|
file.close if file
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
|
|
|
value
|
|
|
|
end
|
2004-02-17 18:26:03 +03:00
|
|
|
|
|
|
|
private
|
2004-03-07 16:37:52 +03:00
|
|
|
def commit_new(f)
|
|
|
|
f.truncate(0)
|
|
|
|
f.rewind
|
2004-02-17 18:26:03 +03:00
|
|
|
new_file = @filename + ".new"
|
2004-03-07 16:37:52 +03:00
|
|
|
File.open(new_file) do |nf|
|
|
|
|
FileUtils.copy_stream(nf, f)
|
2004-02-17 18:26:03 +03:00
|
|
|
end
|
|
|
|
File.unlink(new_file)
|
|
|
|
end
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if __FILE__ == $0
|
|
|
|
db = PStore.new("/tmp/foo")
|
|
|
|
db.transaction do
|
|
|
|
p db.roots
|
|
|
|
ary = db["root"] = [1,2,3,4]
|
|
|
|
ary[1] = [1,1.5]
|
|
|
|
end
|
1998-01-16 15:19:09 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
1000.times do
|
|
|
|
db.transaction do
|
|
|
|
db["root"][0] += 1
|
|
|
|
p db["root"][0]
|
|
|
|
end
|
|
|
|
end
|
2001-05-06 19:06:00 +04:00
|
|
|
|
2001-07-31 10:24:45 +04:00
|
|
|
db.transaction(true) do
|
2001-05-06 19:06:00 +04:00
|
|
|
p db["root"]
|
|
|
|
end
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|