Fixes a possible issue with DB reopening.

This commit is contained in:
Luka Vandervelden 2020-01-07 16:06:18 +01:00
parent 6dd5fc4637
commit 603a65c6f4
2 changed files with 24 additions and 5 deletions

View File

@ -30,9 +30,10 @@ class Ship
# and can easily be extended.
class_getter kisaragi = Ship.new("Kisaragi", "Mutsuki")
class_getter mutsuki = Ship.new("Mutsuki", "Mutsuki", tags: ["name ship"])
class_getter destroyers = [
@@kisaragi,
Ship.new("Mutsuki", "Mutsuki", tags: ["name ship"]),
@@mutsuki,
Ship.new("Yayoi", "Mutsuki"),
Ship.new("Uzuki", "Mutsuki"),
Ship.new("Satsuki", "Mutsuki"),
@ -89,10 +90,12 @@ class PrimitiveShip
end
class DODB::SpecDataBase < DODB::DataBase(Ship)
def initialize(storage_ext = "")
def initialize(storage_ext = "", remove_previous_data = true)
storage_dir = "test-storage#{storage_ext}"
::FileUtils.rm_rf storage_dir
if remove_previous_data
::FileUtils.rm_rf storage_dir
end
super storage_dir
end
@ -142,6 +145,18 @@ describe "DODB::DataBase" do
db[i]?.should be_nil
end
end
it "preserves data on reopening" do
db1 = DODB::SpecDataBase.new
db1 << Ship.kisaragi
db1.to_a.size.should eq(1)
db2 = DODB::SpecDataBase.new remove_previous_data: false
db2 << Ship.mutsuki
db1.to_a.size.should eq(2)
end
end
describe "indices" do

View File

@ -9,7 +9,11 @@ class DODB::DataBase(V)
def initialize(@directory_name : String)
Dir.mkdir_p data_path
self.last_index = -1
begin
self.last_index
rescue
self.last_index = -1
end
end
private def index_file
@ -21,7 +25,7 @@ class DODB::DataBase(V)
def last_index=(x : Int32)
file = File.open(index_file, "w")
file << stringify_key(x)
file << x.to_s
file.close