1999-01-19 23:59:39 -05:00
|
|
|
#
|
1998-01-16 07:19:09 -05: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 08:37:52 -05:00
|
|
|
require "fileutils"
|
2001-12-01 09:07:01 -05:00
|
|
|
require "digest/md5"
|
1998-01-16 07:19:09 -05:00
|
|
|
|
|
|
|
class PStore
|
1999-01-19 23:59:39 -05:00
|
|
|
class Error < StandardError
|
|
|
|
end
|
1998-01-16 07:19:09 -05: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 10:26:03 -05: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 07:19:09 -05:00
|
|
|
|
|
|
|
def [](name)
|
|
|
|
in_transaction
|
2002-07-11 04:22:18 -04:00
|
|
|
@table[name]
|
|
|
|
end
|
|
|
|
def fetch(name, default=PStore::Error)
|
2001-05-06 11:06:00 -04:00
|
|
|
unless @table.key? name
|
2002-07-11 04:22:18 -04:00
|
|
|
if default==PStore::Error
|
|
|
|
raise PStore::Error, format("undefined root name `%s'", name)
|
|
|
|
else
|
|
|
|
default
|
|
|
|
end
|
1998-01-16 07:19:09 -05:00
|
|
|
end
|
2002-07-11 04:22:18 -04:00
|
|
|
self[name]
|
1998-01-16 07:19:09 -05:00
|
|
|
end
|
|
|
|
def []=(name, value)
|
2004-02-17 10:26:03 -05:00
|
|
|
in_transaction_wr()
|
1998-01-16 07:19:09 -05:00
|
|
|
@table[name] = value
|
|
|
|
end
|
2000-03-05 23:15:42 -05:00
|
|
|
def delete(name)
|
2004-02-17 10:26:03 -05:00
|
|
|
in_transaction_wr()
|
2000-03-05 23:15:42 -05:00
|
|
|
@table.delete name
|
|
|
|
end
|
1998-01-16 07:19:09 -05: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 11:06:00 -04:00
|
|
|
in_transaction
|
1998-01-16 07:19:09 -05:00
|
|
|
@abort = false
|
|
|
|
throw :pstore_abort_transaction
|
|
|
|
end
|
|
|
|
def abort
|
2001-05-06 11:06:00 -04:00
|
|
|
in_transaction
|
1998-01-16 07:19:09 -05:00
|
|
|
@abort = true
|
|
|
|
throw :pstore_abort_transaction
|
|
|
|
end
|
|
|
|
|
2001-07-31 02:24:45 -04:00
|
|
|
def transaction(read_only=false)
|
1998-01-16 07:19:09 -05:00
|
|
|
raise PStore::Error, "nested transaction" if @transaction
|
|
|
|
begin
|
2004-02-17 10:26:03 -05:00
|
|
|
@rdonly = read_only
|
|
|
|
@abort = false
|
1998-01-16 07:19:09 -05:00
|
|
|
@transaction = true
|
1999-11-25 04:03:08 -05:00
|
|
|
value = nil
|
2004-02-17 10:26:03 -05:00
|
|
|
new_file = @filename + ".new"
|
|
|
|
|
|
|
|
content = nil
|
|
|
|
file = File.open(@filename, File::RDWR | File::CREAT)
|
2004-05-27 03:43:38 -04:00
|
|
|
unless read_only
|
2004-02-17 10:26:03 -05:00
|
|
|
file.flock(File::LOCK_EX)
|
2004-03-07 08:37:52 -05:00
|
|
|
commit_new(file) if FileTest.exist?(new_file)
|
2004-02-17 10:26:03 -05: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 02:30:37 -05:00
|
|
|
end
|
2004-02-17 10:26:03 -05:00
|
|
|
|
|
|
|
if content != ""
|
2004-05-27 03:43:38 -04:00
|
|
|
@table = load(content)
|
2004-02-17 10:26:03 -05:00
|
|
|
if !read_only
|
|
|
|
size = content.size
|
|
|
|
md5 = Digest::MD5.digest(content)
|
|
|
|
end
|
2001-05-06 11:06:00 -04:00
|
|
|
else
|
|
|
|
@table = {}
|
1998-01-16 07:19:09 -05:00
|
|
|
end
|
2004-02-17 10:26:03 -05:00
|
|
|
content = nil # unreference huge data
|
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
begin
|
|
|
|
catch(:pstore_abort_transaction) do
|
|
|
|
value = yield(self)
|
|
|
|
end
|
1999-01-19 23:59:39 -05:00
|
|
|
rescue Exception
|
|
|
|
@abort = true
|
|
|
|
raise
|
1998-01-16 07:19:09 -05:00
|
|
|
ensure
|
2002-11-14 01:18:59 -05:00
|
|
|
if !read_only and !@abort
|
2004-02-17 10:26:03 -05:00
|
|
|
tmp_file = @filename + ".tmp"
|
2004-05-27 03:43:38 -04:00
|
|
|
content = dump(@table)
|
2001-12-01 09:07:01 -05:00
|
|
|
if !md5 || size != content.size || md5 != Digest::MD5.digest(content)
|
2004-02-17 10:26:03 -05:00
|
|
|
File.open(tmp_file, "w") {|t|
|
|
|
|
t.write(content)
|
|
|
|
}
|
|
|
|
File.rename(tmp_file, new_file)
|
2004-03-07 08:37:52 -05:00
|
|
|
commit_new(file)
|
2004-02-17 10:26:03 -05:00
|
|
|
end
|
|
|
|
content = nil # unreference huge data
|
2002-11-14 01:18:59 -05:00
|
|
|
end
|
1998-01-16 07:19:09 -05:00
|
|
|
end
|
|
|
|
ensure
|
1999-01-19 23:59:39 -05:00
|
|
|
@table = nil
|
1998-01-16 07:19:09 -05:00
|
|
|
@transaction = false
|
2001-11-19 00:03:03 -05:00
|
|
|
file.close if file
|
1998-01-16 07:19:09 -05:00
|
|
|
end
|
|
|
|
value
|
|
|
|
end
|
2004-02-17 10:26:03 -05:00
|
|
|
|
2004-05-27 03:43:38 -04:00
|
|
|
def dump(table)
|
|
|
|
Marshal::dump(table)
|
|
|
|
end
|
|
|
|
|
|
|
|
def load(content)
|
|
|
|
Marshal::load(content)
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_file(file)
|
|
|
|
Marshal::load(file)
|
|
|
|
end
|
|
|
|
|
2004-02-17 10:26:03 -05:00
|
|
|
private
|
2004-03-07 08:37:52 -05:00
|
|
|
def commit_new(f)
|
|
|
|
f.truncate(0)
|
|
|
|
f.rewind
|
2004-02-17 10:26:03 -05:00
|
|
|
new_file = @filename + ".new"
|
2004-03-07 08:37:52 -05:00
|
|
|
File.open(new_file) do |nf|
|
|
|
|
FileUtils.copy_stream(nf, f)
|
2004-02-17 10:26:03 -05:00
|
|
|
end
|
|
|
|
File.unlink(new_file)
|
|
|
|
end
|
1998-01-16 07:19:09 -05:00
|
|
|
end
|
|
|
|
|
1999-01-19 23:59:39 -05: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 07:19:09 -05:00
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
1000.times do
|
|
|
|
db.transaction do
|
|
|
|
db["root"][0] += 1
|
|
|
|
p db["root"][0]
|
|
|
|
end
|
|
|
|
end
|
2001-05-06 11:06:00 -04:00
|
|
|
|
2001-07-31 02:24:45 -04:00
|
|
|
db.transaction(true) do
|
2001-05-06 11:06:00 -04:00
|
|
|
p db["root"]
|
|
|
|
end
|
1998-01-16 07:19:09 -05:00
|
|
|
end
|