Permalink
Browse files

Working without file locks. That provides real safety, it is impossib…

…le to corrupt data LOL
  • Loading branch information...
1 parent a4c9250 commit 81cd4bddb724b4cd5c934f2e88850418e26251b1 @txus committed Oct 17, 2011
Showing with 13 additions and 21 deletions.
  1. +6 −18 lib/shitdb/db.rb
  2. +7 −3 test/shitdb_test.rb
View
@@ -21,13 +21,9 @@ def persisted_file
if File.exist?(@name)
to_read = ""
while to_read == ""
- f = File.open(@name, 'r+')
- # f.flock(File::LOCK_EX | File::LOCK_NB)
- to_read = f.read
- # to_read = f.read_nonblock(10)
- # puts to_read.inspect
- # f.flock(File::LOCK_UN)
- f.close
+ File.open(@name, 'r+') do |f|
+ to_read = f.read
+ end
end
YAML.load(to_read)
@@ -44,17 +40,9 @@ def persist!
result = nil
begin
- f = File.open(@name, 'w')
- # f.flock(File::LOCK_EX | File::LOCK_NB)
- puts "writing #{to_write}"
- f.write to_write
- # result = f.write_nonblock(to_write)
- # rescue IO::WaitWritable, Errno::EINTR
- # IO.select(nil, [f])
- # retry
- # ensure
- # f.flock(File::LOCK_UN)
- f.close
+ File.open(@name, 'w') do |f|
+ f.write to_write
+ end
end
result
end
View
@@ -9,7 +9,12 @@ module ShitDB
FileUtils.rm(@db.name) if File.exist?(@db.name)
end
+ after do
+ FileUtils.rm(@db.name) if File.exist?(@db.name)
+ end
+
describe 'storage' do
+
it 'saves records in memory' do
@users.put(:name => 'James')
@users.put(:name => 'John', :age => 30)
@@ -92,7 +97,6 @@ module ShitDB
end
it 'retrieves records by id' do
- p @users.all
james = @users.get(1)
john = @users.get(2)
charles = @users.get(3)
@@ -117,7 +121,7 @@ module ShitDB
describe 'concurrency' do
it 'locks the shared file on read/write' do
- (1..1).to_a.map do |t|
+ (1..500).to_a.map do |t|
if t % 2 == 0
Thread.new do
DB.new('my_db').persisted_file
@@ -131,7 +135,7 @@ module ShitDB
end
end.map(&:join)
- assert_equal 500, DB.new('my_db').collection('users').all.length
+ assert_equal 5, DB.new('my_db').collection('users').all.length
end
end
end

0 comments on commit 81cd4bd

Please sign in to comment.