Skip to content

Commit

Permalink
ensure hashmap init clears maps
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
tjungblu committed Jul 11, 2024
1 parent 46d9b10 commit 6f4711a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
18 changes: 9 additions & 9 deletions freelist_hmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,22 +195,22 @@ func (f *freelist) delSpan(start common.Pgid, size uint64) {
// initial from pgids using when use hashmap version
// pgids must be sorted
func (f *freelist) init(pgids []common.Pgid) {
// reset the counter when freelist init
f.freePagesCount = 0
f.freemaps = make(map[uint64]pidSet)
f.forwardMap = make(map[common.Pgid]uint64)
f.backwardMap = make(map[common.Pgid]uint64)

if len(pgids) == 0 {
return
}

size := uint64(1)
start := pgids[0]
// reset the counter when freelist init
f.freePagesCount = 0

if !sort.SliceIsSorted([]common.Pgid(pgids), func(i, j int) bool { return pgids[i] < pgids[j] }) {
if !sort.SliceIsSorted(pgids, func(i, j int) bool { return pgids[i] < pgids[j] }) {
panic("pgids not sorted")
}

f.freemaps = make(map[uint64]pidSet)
f.forwardMap = make(map[common.Pgid]uint64)
f.backwardMap = make(map[common.Pgid]uint64)
size := uint64(1)
start := pgids[0]

for i := 1; i < len(pgids); i++ {
// continuous page
Expand Down
26 changes: 26 additions & 0 deletions freelist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,32 @@ func TestFreelist_releaseRange(t *testing.T) {
}
}

func TestFreeList_reload_page_dedupe(t *testing.T) {
buf := make([]byte, 4096)
f := newTestFreelist()
f.readIDs([]common.Pgid{5, 6, 8})

p := common.LoadPage(buf)
if err := f.write(p); err != nil {
t.Fatal(err)
}

f2 := newTestFreelist()
f.readIDs([]common.Pgid{})

f2.free(common.Txid(5), common.NewPage(5, common.LeafPageFlag, 0, 4))

// reload should deduplicate as a pending page when reading from p's freelist
f2.reload(p)

if len(f2.getFreePageIDs()) != 0 {
t.Fatalf("expected empty; got=%v", f2.getFreePageIDs())
}
if exp := []common.Pgid{5, 6, 7, 8, 9}; !reflect.DeepEqual(exp, f2.pending[5].ids) {
t.Fatalf("exp=%v; got=%v", exp, f2.pending[5].ids)
}
}

func TestFreelistHashmap_allocate(t *testing.T) {
f := newTestFreelist()
if f.freelistType != FreelistMapType {
Expand Down

0 comments on commit 6f4711a

Please sign in to comment.