lib/daybreak/db.rb in daybreak-0.1.2 vs lib/daybreak/db.rb in daybreak-0.1.3
- old
+ new
@@ -14,11 +14,10 @@
# @yield [key] a block that will return the default value to store.
# @yieldparam [String] key the key to be stored.
def initialize(file, default=nil, &blk)
@table = {}
@file_name = file
- @reader = Reader.new(@file_name)
@writer = Writer.new(@file_name)
@default = block_given? ? blk : default
read!
end
@@ -160,10 +159,16 @@
end
# Read all values from the log file. If you want to check for changed data
# call this again.
def read!
- @reader.read do |(key, data, deleted)|
+ buf = nil
+ File.open(@file_name, 'rb') do |fd|
+ fd.flock(File::LOCK_SH)
+ buf = fd.read
+ end
+ until buf.empty?
+ key, data, deleted = Record.deserialize(buf)
if deleted
@table.delete key
else
@table[key] = parse(data)
end