Skip to content

Commit

Permalink
add freelist interface unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Jungblut <[email protected]>
  • Loading branch information
tjungblu committed Jul 31, 2024
1 parent df83d9d commit 815322f
Show file tree
Hide file tree
Showing 5 changed files with 361 additions and 24 deletions.
10 changes: 10 additions & 0 deletions internal/freelist/array_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package freelist

import (
"github.com/stretchr/testify/require"
"reflect"
"testing"

Expand Down Expand Up @@ -50,3 +51,12 @@ func TestFreelistArray_allocate(t *testing.T) {
t.Fatalf("exp=%v; got=%v", exp, f.freePageIds())
}
}

func TestInvalidArrayAllocation(t *testing.T) {
f := NewArrayFreelist()
ids := []common.Pgid{1}
f.Init(ids)
require.Panics(t, func() {
f.Allocate(common.Txid(1), 1)
})
}
7 changes: 4 additions & 3 deletions internal/freelist/freelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type ReadWriter interface {
// Write writes the freelist into the given page.
Write(page *common.Page)

// EstimatedWritePageSize returns the size of the freelist after serialization in Write.
// EstimatedWritePageSize returns the size in bytes of the freelist after serialization in Write.
// This should never underestimate the size.
EstimatedWritePageSize() int
}
Expand Down Expand Up @@ -46,13 +46,14 @@ type Interface interface {
ReleasePendingPages()

// Free releases a page and its overflow for a given transaction id.
// If the page is already free then a panic will occur.
// If the page is already free or is one of the meta pages, then a panic will occur.
Free(txId common.Txid, p *common.Page)

// Freed returns whether a given page is in the free list.
Freed(pgId common.Pgid) bool

// Rollback removes the pages from a given pending tx.
// Should always be followed by Reload or NoSyncReload from last freelist state.
Rollback(txId common.Txid)

// Copyall copies a list of all free ids and all pending ids in one sorted list.
Expand All @@ -65,7 +66,7 @@ type Interface interface {
// NoSyncReload reads the freelist from Pgids and filters out pending items.
NoSyncReload(pgIds common.Pgids)

// freePageIds returns the IDs of all free pages.
// freePageIds returns the IDs of all free pages. No free pages returns an empty slice.
freePageIds() common.Pgids

// pendingPageIds returns all pending pages by transaction id.
Expand Down
Loading

0 comments on commit 815322f

Please sign in to comment.