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. # and can easily be extended.
class_getter kisaragi = Ship.new("Kisaragi", "Mutsuki") class_getter kisaragi = Ship.new("Kisaragi", "Mutsuki")
class_getter mutsuki = Ship.new("Mutsuki", "Mutsuki", tags: ["name ship"])
class_getter destroyers = [ class_getter destroyers = [
@@kisaragi, @@kisaragi,
Ship.new("Mutsuki", "Mutsuki", tags: ["name ship"]), @@mutsuki,
Ship.new("Yayoi", "Mutsuki"), Ship.new("Yayoi", "Mutsuki"),
Ship.new("Uzuki", "Mutsuki"), Ship.new("Uzuki", "Mutsuki"),
Ship.new("Satsuki", "Mutsuki"), Ship.new("Satsuki", "Mutsuki"),
@ -89,10 +90,12 @@ class PrimitiveShip
end end
class DODB::SpecDataBase < DODB::DataBase(Ship) class DODB::SpecDataBase < DODB::DataBase(Ship)
def initialize(storage_ext = "") def initialize(storage_ext = "", remove_previous_data = true)
storage_dir = "test-storage#{storage_ext}" storage_dir = "test-storage#{storage_ext}"
if remove_previous_data
::FileUtils.rm_rf storage_dir ::FileUtils.rm_rf storage_dir
end
super storage_dir super storage_dir
end end
@ -142,6 +145,18 @@ describe "DODB::DataBase" do
db[i]?.should be_nil db[i]?.should be_nil
end end
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 end
describe "indices" do describe "indices" do

View File

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