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 <tjungblu@redhat.com>
  • Loading branch information
tjungblu committed Jul 22, 2024
1 parent efc3eb6 commit 23807ad
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 9 deletions.
41 changes: 41 additions & 0 deletions internal/freelist/freelist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"testing"
"unsafe"

"github.com/stretchr/testify/require"

"go.etcd.io/bbolt/internal/common"
)

Expand Down Expand Up @@ -179,6 +181,45 @@ func TestFreelist_releaseRange(t *testing.T) {
}
}

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

p := common.LoadPage(buf)
f.Write(p)

f2 := newTestFreelist()
f2.Read(p)
require.Equal(t, common.Pgids{5, 6, 8}, f2.freePageIds())

// When initializing the freelist with an empty list of page ID,
// it should reset the freelist page IDs.
f2.Init([]common.Pgid{})
require.Equal(t, common.Pgids{}, f2.freePageIds())
}

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

p := common.LoadPage(buf)
f.Write(p)

f2 := newTestFreelist()
f2.Read(p)
require.Equal(t, common.Pgids{5, 6, 8}, f2.freePageIds())

f2.Free(common.Txid(5), common.NewPage(10, common.LeafPageFlag, 0, 2))

// reload shouldn't affect the pending list
f2.Reload(p)

require.Equal(t, common.Pgids{5, 6, 8}, f2.freePageIds())
require.Equal(t, []common.Pgid{10, 11, 12}, f2.pendingPageIds()[5].ids)
}

// Ensure that a freelist can deserialize from a freelist page.
func TestFreelist_read(t *testing.T) {
// Create a page.
Expand Down
18 changes: 9 additions & 9 deletions internal/freelist/hashmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@ type hashMap struct {
}

func (f *hashMap) Init(pgids common.Pgids) {
// 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] }) {
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 Expand Up @@ -117,7 +117,7 @@ func (f *hashMap) FreeCount() int {
func (f *hashMap) freePageIds() common.Pgids {
count := f.FreeCount()
if count == 0 {
return nil
return common.Pgids{}
}

m := make([]common.Pgid, 0, count)
Expand Down

0 comments on commit 23807ad

Please sign in to comment.