Skip to content

Commit

Permalink
Merge pull request #804 from tjungblu/duplicated_code
Browse files Browse the repository at this point in the history
Dedupe Reload/NoSyncReload, prefer empty instead of nil init
  • Loading branch information
ahrtr committed Aug 6, 2024
2 parents 6e95525 + 33b71a5 commit f777377
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 24 deletions.
6 changes: 3 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,7 +46,7 @@ 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.
Expand All @@ -65,7 +65,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. Returns an empty slice if no free pages are available.
freePageIds() common.Pgids

// pendingPageIds returns all pending pages by transaction id.
Expand Down
24 changes: 3 additions & 21 deletions internal/freelist/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,25 +208,7 @@ func (t *shared) Copyall(dst []common.Pgid) {

func (t *shared) Reload(p *common.Page) {
t.Read(p)

// Build a cache of only pending pages.
pcache := make(map[common.Pgid]bool)
for _, txp := range t.pending {
for _, pendingID := range txp.ids {
pcache[pendingID] = true
}
}

// Check each page in the freelist and build a new available freelist
// with any pages not in the pending lists.
var a []common.Pgid
for _, id := range t.freePageIds() {
if !pcache[id] {
a = append(a, id)
}
}

t.Init(a)
t.NoSyncReload(t.freePageIds())
}

func (t *shared) NoSyncReload(pgIds common.Pgids) {
Expand All @@ -240,7 +222,7 @@ func (t *shared) NoSyncReload(pgIds common.Pgids) {

// Check each page in the freelist and build a new available freelist
// with any pages not in the pending lists.
var a []common.Pgid
a := []common.Pgid{}
for _, id := range pgIds {
if !pcache[id] {
a = append(a, id)
Expand Down Expand Up @@ -274,7 +256,7 @@ func (t *shared) Read(p *common.Page) {

// Copy the list of page ids from the freelist.
if len(ids) == 0 {
t.Init(nil)
t.Init([]common.Pgid{})
} else {
// copy the ids, so we don't modify on the freelist page directly
idsCopy := make([]common.Pgid, len(ids))
Expand Down

0 comments on commit f777377

Please sign in to comment.