From 39b427886f014ade8dabdde83ace83b11658385d Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Mon, 20 May 2024 03:14:47 +0200 Subject: [PATCH] spec/test-ships.cr: tests are passing. --- spec/test-ships.cr | 32 ++++++++++++++++---------------- src/cached.cr | 5 +++++ src/dodb.cr | 6 ++++++ 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/spec/test-ships.cr b/spec/test-ships.cr index 718044e..0200373 100644 --- a/spec/test-ships.cr +++ b/spec/test-ships.cr @@ -71,11 +71,11 @@ describe "DODB::DataBase" do end # The two #each test iteration. - db.each_with_index do |item, index| + db.each_with_key do |item, index| item.should eq Ship.all_ships[index] end - db.each_with_index(reversed: true) do |item, index| + db.each_with_key(reversed: true) do |item, index| item.should eq Ship.all_ships[index] end @@ -90,11 +90,11 @@ describe "DODB::DataBase" do db << ship end - db.to_a(start_offset: 0, end_offset: 0)[0]?.should eq Ship.mutsuki - db.to_a(start_offset: 1, end_offset: 1)[0]?.should eq Ship.kisaragi - db.to_a(start_offset: 2, end_offset: 2)[0]?.should eq Ship.yayoi + db.to_a(offset: 0, limit: 1)[0]?.should eq Ship.mutsuki + db.to_a(offset: 1, limit: 1)[0]?.should eq Ship.kisaragi + db.to_a(offset: 2, limit: 1)[0]?.should eq Ship.yayoi - db.to_a(start_offset: 0, end_offset: 2).should eq [ + db.to_a(offset: 0, limit: 3).should eq [ Ship.mutsuki, Ship.kisaragi, Ship.yayoi ] end @@ -362,7 +362,7 @@ describe "DODB::DataBase" do new_ships_by_class = new_db.new_partition "class", &.klass new_ships_by_tags = new_db.new_tags "tags", &.tags - old_db.each_with_index do |ship, index| + old_db.each_with_key do |ship, index| new_ship = Ship.new ship.name, klass: ship.class_name, id: ship.id, @@ -376,7 +376,7 @@ describe "DODB::DataBase" do # At this point, the conversion is done, so… we’re making a few # arbitrary tests on the new data. - old_db.each_with_index do |old_ship, old_index| + old_db.each_with_key do |old_ship, old_index| ship = new_db[old_index] ship.id.should eq(old_ship.id) @@ -555,11 +555,11 @@ describe "DODB::CachedDataBase" do end # The two #each test iteration. - db.each_with_index do |item, index| + db.each_with_key do |item, index| item.should eq Ship.all_ships[index] end - db.each_with_index(reversed: true) do |item, index| + db.each_with_key(reversed: true) do |item, index| item.should eq Ship.all_ships[index] end @@ -576,11 +576,11 @@ describe "DODB::CachedDataBase" do db << ship end - db.to_a(start_offset: 0, end_offset: 0)[0]?.should eq Ship.mutsuki - db.to_a(start_offset: 1, end_offset: 1)[0]?.should eq Ship.kisaragi - db.to_a(start_offset: 2, end_offset: 2)[0]?.should eq Ship.yayoi + db.to_a(offset: 0, limit: 1)[0]?.should eq Ship.mutsuki + db.to_a(offset: 1, limit: 1)[0]?.should eq Ship.kisaragi + db.to_a(offset: 2, limit: 1)[0]?.should eq Ship.yayoi - db.to_a(start_offset: 0, end_offset: 2).should eq [ + db.to_a(offset: 0, limit: 3).should eq [ Ship.mutsuki, Ship.kisaragi, Ship.yayoi ] @@ -874,7 +874,7 @@ describe "DODB::CachedDataBase" do new_ships_by_class = new_db.new_partition "class", &.klass new_ships_by_tags = new_db.new_tags "tags", &.tags - old_db.each_with_index do |ship, index| + old_db.each_with_key do |ship, index| new_ship = Ship.new ship.name, klass: ship.class_name, id: ship.id, @@ -888,7 +888,7 @@ describe "DODB::CachedDataBase" do # At this point, the conversion is done, so… we’re making a few # arbitrary tests on the new data. - old_db.each_with_index do |old_ship, old_index| + old_db.each_with_key do |old_ship, old_index| ship = new_db[old_index] ship.id.should eq(old_ship.id) diff --git a/src/cached.cr b/src/cached.cr index 98113a2..579c649 100644 --- a/src/cached.cr +++ b/src/cached.cr @@ -84,6 +84,11 @@ class DODB::CachedDataBase(V) < DODB::Storage(V) # :inherit: def each_with_key(reversed : Bool = false, offset = 0, limit : Int32? = -1) + limit = if l = limit + l + else + -1 + end (reversed ? @data.reverse : @data).each do |key, v| offset -= 1 if offset >= 0 next if offset >= 0 diff --git a/src/dodb.cr b/src/dodb.cr index 7882c8f..13b1e71 100644 --- a/src/dodb.cr +++ b/src/dodb.cr @@ -473,6 +473,12 @@ class DODB::DataBase(V) < DODB::Storage(V) def each_with_key(reversed : Bool = false, offset = 0, limit : Int32? = -1) dirname = data_path + limit = if l = limit + l + else + -1 + end + each_key(reversed) do |key, path| offset -= 1 if offset >= 0 next if offset >= 0