Skip to content

Commit 6edd850

Browse files
committed
db: rework fileCache to use genericcache
We no longer intermingle the cache infrastructure logic with the file cache specifics. This will help when we extend the file cache to support blob files.
1 parent 8cc384d commit 6edd850

File tree

8 files changed

+256
-745
lines changed

8 files changed

+256
-745
lines changed

db_test.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -695,8 +695,10 @@ func TestIterLeak(t *testing.T) {
695695
t.Run(fmt.Sprintf("leak=%t", leak), func(t *testing.T) {
696696
for _, flush := range []bool{true, false} {
697697
t.Run(fmt.Sprintf("flush=%t", flush), func(t *testing.T) {
698+
fc := NewFileCache(10, 100)
698699
d, err := Open("", testingRandomized(t, &Options{
699-
FS: vfs.NewMem(),
700+
FS: vfs.NewMem(),
701+
FileCache: fc,
700702
}))
701703
require.NoError(t, err)
702704

@@ -710,15 +712,16 @@ func TestIterLeak(t *testing.T) {
710712
require.NoError(t, iter.Close())
711713
require.NoError(t, d.Close())
712714
} else {
713-
defer iter.Close()
714715
if err := d.Close(); err == nil {
715716
t.Fatalf("expected failure, but found success")
716717
} else if !strings.HasPrefix(err.Error(), "leaked iterators:") {
717718
t.Fatalf("expected leaked iterators, but found %+v", err)
718719
} else {
719720
t.Log(err.Error())
720721
}
722+
iter.Close()
721723
}
724+
fc.Unref()
722725
})
723726
}
724727
})
@@ -732,13 +735,16 @@ func TestIterLeakSharedCache(t *testing.T) {
732735
t.Run(fmt.Sprintf("leak=%t", leak), func(t *testing.T) {
733736
for _, flush := range []bool{true, false} {
734737
t.Run(fmt.Sprintf("flush=%t", flush), func(t *testing.T) {
738+
fc := NewFileCache(10, 100)
735739
d1, err := Open("", &Options{
736-
FS: vfs.NewMem(),
740+
FS: vfs.NewMem(),
741+
FileCache: fc,
737742
})
738743
require.NoError(t, err)
739744

740745
d2, err := Open("", &Options{
741-
FS: vfs.NewMem(),
746+
FS: vfs.NewMem(),
747+
FileCache: fc,
742748
})
743749
require.NoError(t, err)
744750

@@ -789,6 +795,22 @@ func TestIterLeakSharedCache(t *testing.T) {
789795
}
790796
}
791797

798+
if !leak {
799+
fc.Unref()
800+
} else if flush {
801+
require.Panics(t, func() {
802+
fc.Unref()
803+
})
804+
} else {
805+
// When we're not flushing and we leak an iterator, Unref might or
806+
// might not panic, depending on whether there was a file involved.
807+
func() {
808+
defer func() {
809+
recover()
810+
}()
811+
fc.Unref()
812+
}()
813+
}
792814
})
793815
}
794816
})

0 commit comments

Comments
 (0)