From 99b8b5e20b53ef40189d0fe732df18aefc34c289 Mon Sep 17 00:00:00 2001 From: Karchnu Date: Thu, 16 Jul 2020 23:37:52 +0200 Subject: [PATCH] For now, DODB::DataBase::Cached works only without indexes. --- spec/cached.cr | 33 ++++++++++++++++----------------- src/cached.cr | 5 ++--- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/spec/cached.cr b/spec/cached.cr index 5178cec..e61734b 100644 --- a/spec/cached.cr +++ b/spec/cached.cr @@ -81,24 +81,23 @@ describe "DODB::DataBase::Cached" do # Actual reversal is tested here. db.to_a(reversed: true).should eq db.to_a.reverse end - end -## it "respects the provided offsets if any" do -## db = DODB::SpecDataBase.new -## -## Ship.all_ships.each do |ship| -## 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(start_offset: 0, end_offset: 2).should eq [ -## Ship.mutsuki, Ship.kisaragi, Ship.yayoi -## ] -## end -## end + it "respects the provided offsets if any" do + db = DODB::SpecDataBase.new + + Ship.all_ships.each do |ship| + 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(start_offset: 0, end_offset: 2).should eq [ + Ship.mutsuki, Ship.kisaragi, Ship.yayoi + ] + end + end # describe "indices" do # it "do basic indexing" do diff --git a/src/cached.cr b/src/cached.cr index 0bea1a8..e663309 100644 --- a/src/cached.cr +++ b/src/cached.cr @@ -91,7 +91,7 @@ class DODB::DataBase::Cached(V) (reversed ? @data.reverse : @data).each do |index, v| i += 1 next if start_offset > i - break unless end_offset.nil? || i < end_offset + break unless end_offset.nil? || i <= end_offset yield v, index end @@ -117,8 +117,7 @@ class DODB::DataBase::Cached(V) def to_h(reversed : Bool = false, start_offset = 0, end_offset : Int32? = nil) res = Hash(Int32, V).new - # (reversed ? @data.reverse : @data).each do |index, v| - @data.each do |index, v| + (reversed ? @data.reverse : @data).each do |index, v| next if start_offset > index break unless end_offset.nil? || index < end_offset