Skip to content

Commit 6f4711a

Browse files
committed
ensure hashmap init clears maps
This reorders some statements in the hashmap initialization to ensure we always start fresh, even when no pageids were passed to it. fixes #791 Signed-off-by: Thomas Jungblut <tjungblu@redhat.com>
1 parent 46d9b10 commit 6f4711a

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

freelist_hmap.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -195,22 +195,22 @@ func (f *freelist) delSpan(start common.Pgid, size uint64) {
195195
// initial from pgids using when use hashmap version
196196
// pgids must be sorted
197197
func (f *freelist) init(pgids []common.Pgid) {
198+
// reset the counter when freelist init
199+
f.freePagesCount = 0
200+
f.freemaps = make(map[uint64]pidSet)
201+
f.forwardMap = make(map[common.Pgid]uint64)
202+
f.backwardMap = make(map[common.Pgid]uint64)
203+
198204
if len(pgids) == 0 {
199205
return
200206
}
201207

202-
size := uint64(1)
203-
start := pgids[0]
204-
// reset the counter when freelist init
205-
f.freePagesCount = 0
206-
207-
if !sort.SliceIsSorted([]common.Pgid(pgids), func(i, j int) bool { return pgids[i] < pgids[j] }) {
208+
if !sort.SliceIsSorted(pgids, func(i, j int) bool { return pgids[i] < pgids[j] }) {
208209
panic("pgids not sorted")
209210
}
210211

211-
f.freemaps = make(map[uint64]pidSet)
212-
f.forwardMap = make(map[common.Pgid]uint64)
213-
f.backwardMap = make(map[common.Pgid]uint64)
212+
size := uint64(1)
213+
start := pgids[0]
214214

215215
for i := 1; i < len(pgids); i++ {
216216
// continuous page

freelist_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,32 @@ func TestFreelist_releaseRange(t *testing.T) {
179179
}
180180
}
181181

182+
func TestFreeList_reload_page_dedupe(t *testing.T) {
183+
buf := make([]byte, 4096)
184+
f := newTestFreelist()
185+
f.readIDs([]common.Pgid{5, 6, 8})
186+
187+
p := common.LoadPage(buf)
188+
if err := f.write(p); err != nil {
189+
t.Fatal(err)
190+
}
191+
192+
f2 := newTestFreelist()
193+
f.readIDs([]common.Pgid{})
194+
195+
f2.free(common.Txid(5), common.NewPage(5, common.LeafPageFlag, 0, 4))
196+
197+
// reload should deduplicate as a pending page when reading from p's freelist
198+
f2.reload(p)
199+
200+
if len(f2.getFreePageIDs()) != 0 {
201+
t.Fatalf("expected empty; got=%v", f2.getFreePageIDs())
202+
}
203+
if exp := []common.Pgid{5, 6, 7, 8, 9}; !reflect.DeepEqual(exp, f2.pending[5].ids) {
204+
t.Fatalf("exp=%v; got=%v", exp, f2.pending[5].ids)
205+
}
206+
}
207+
182208
func TestFreelistHashmap_allocate(t *testing.T) {
183209
f := newTestFreelist()
184210
if f.freelistType != FreelistMapType {

0 commit comments

Comments
 (0)