Skip to content

Commit

Permalink
first review by ben
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Jungblut <[email protected]>
  • Loading branch information
tjungblu committed Jun 28, 2024
1 parent 56634cd commit 51cbf71
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 46 deletions.
2 changes: 1 addition & 1 deletion allocate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestTx_allocatePageStats(t *testing.T) {
for n, f := range map[string]freelist.Freelist{"hashmap": freelist.NewHashMap(), "array": freelist.NewArray()} {
for n, f := range map[string]freelist.Interface{"hashmap": freelist.NewHashMapFreelist(), "array": freelist.NewArrayFreelist()} {
t.Run(n, func(t *testing.T) {
ids := []common.Pgid{2, 3}
f.Init(ids)
Expand Down
8 changes: 4 additions & 4 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ type DB struct {
rwtx *Tx
txs []*Tx

freelist fl.Freelist
freelist fl.Interface
freelistSerializer fl.Serializable
freelistLoad sync.Once

Expand Down Expand Up @@ -1286,11 +1286,11 @@ func (db *DB) freepages() []common.Pgid {
return fids
}

func newFreelist(freelistType FreelistType) fl.Freelist {
func newFreelist(freelistType FreelistType) fl.Interface {
if freelistType == FreelistMapType {
return fl.NewHashMap()
return fl.NewHashMapFreelist()
}
return fl.NewArray()
return fl.NewArrayFreelist()
}

// Options represents the options that can be set when opening a database.
Expand Down
18 changes: 9 additions & 9 deletions internal/freelist/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ import (
"go.etcd.io/bbolt/internal/common"
)

type Array struct {
type array struct {
shared

ids []common.Pgid // all free and available free page ids.
}

func (f *Array) Init(ids common.Pgids) {
func (f *array) Init(ids common.Pgids) {
f.ids = ids
f.reindex(f.FreePageIds(), f.pendingPageIds())
}

func (f *Array) Allocate(txid common.Txid, n int) common.Pgid {
func (f *array) Allocate(txid common.Txid, n int) common.Pgid {
if len(f.ids) == 0 {
return 0
}
Expand Down Expand Up @@ -60,19 +60,19 @@ func (f *Array) Allocate(txid common.Txid, n int) common.Pgid {
return 0
}

func (f *Array) Count() int {
func (f *array) Count() int {
return f.FreeCount() + f.PendingCount()
}

func (f *Array) FreeCount() int {
func (f *array) FreeCount() int {
return len(f.ids)
}

func (f *Array) FreePageIds() common.Pgids {
func (f *array) FreePageIds() common.Pgids {
return f.ids
}

func (f *Array) mergeSpans(ids common.Pgids) {
func (f *array) mergeSpans(ids common.Pgids) {
sort.Sort(ids)
common.Verify(func() {
idsIdx := make(map[common.Pgid]struct{})
Expand Down Expand Up @@ -102,8 +102,8 @@ func (f *Array) mergeSpans(ids common.Pgids) {
f.ids = common.Pgids(f.ids).Merge(ids)
}

func NewArray() *Array {
a := &Array{
func NewArrayFreelist() Interface {
a := &array{
shared: shared{
pending: make(map[common.Txid]*txPending),
allocs: make(map[common.Pgid]common.Txid),
Expand Down
2 changes: 1 addition & 1 deletion internal/freelist/array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

// Ensure that a freelist can find contiguous blocks of pages.
func TestFreelistArray_allocate(t *testing.T) {
f := NewArray()
f := NewArrayFreelist()
ids := []common.Pgid{3, 4, 5, 6, 7, 9, 12, 13, 18}
f.Init(ids)
if id := int(f.Allocate(1, 3)); id != 3 {
Expand Down
10 changes: 5 additions & 5 deletions internal/freelist/freelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"go.etcd.io/bbolt/internal/common"
)

type Freelist interface {
type Interface interface {
// Init initializes this freelist with the given list of pages.
Init(ids common.Pgids)

Expand Down Expand Up @@ -48,7 +48,7 @@ type Freelist interface {

// Copyall copies a list of all free ids and all pending ids in one sorted list.
// f.count returns the minimum length required for dst.
func Copyall(f Freelist, dst []common.Pgid) {
func Copyall(f Interface, dst []common.Pgid) {
m := make(common.Pgids, 0, f.PendingCount())
for _, txp := range f.pendingPageIds() {
m = append(m, txp.ids...)
Expand All @@ -58,13 +58,13 @@ func Copyall(f Freelist, dst []common.Pgid) {
}

// Reload reads the freelist from a page and filters out pending items.
func Reload(s Serializable, f Freelist, p *common.Page) {
func Reload(s Serializable, f Interface, p *common.Page) {
s.Read(f, p)
NoSyncReload(f, p.FreelistPageIds())
}

// NoSyncReload reads the freelist from Pgids and filters out pending items.
func NoSyncReload(f Freelist, pgIds common.Pgids) {
func NoSyncReload(f Interface, pgIds common.Pgids) {
// Build a cache of only pending pages.
pcache := make(map[common.Pgid]bool)
for _, txp := range f.pendingPageIds() {
Expand All @@ -76,7 +76,7 @@ func NoSyncReload(f Freelist, 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
for _, id := range f.FreePageIds() {
for _, id := range pgIds {
if !pcache[id] {
a = append(a, id)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/freelist/freelist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,10 @@ func Test_freelist_ReadIDs_and_getFreePageIDs(t *testing.T) {
}

// newTestFreelist get the freelist type from env and initial the freelist
func newTestFreelist() Freelist {
func newTestFreelist() Interface {
if env := os.Getenv(TestFreelistType); env == "map" {
return NewHashMap()
return NewHashMapFreelist()
}

return NewArray()
return NewArrayFreelist()
}
32 changes: 16 additions & 16 deletions internal/freelist/hashmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// pidSet holds the set of starting pgids which have the same span size
type pidSet map[common.Pgid]struct{}

type HashMap struct {
type hashMap struct {
shared

freePagesCount uint64 // count of free pages(hashmap version)
Expand All @@ -20,7 +20,7 @@ type HashMap struct {
backwardMap map[common.Pgid]uint64 // key is end pgid, value is its span size
}

func (f *HashMap) Init(pgids common.Pgids) {
func (f *hashMap) Init(pgids common.Pgids) {
if len(pgids) == 0 {
return
}
Expand Down Expand Up @@ -58,7 +58,7 @@ func (f *HashMap) Init(pgids common.Pgids) {
f.reindex(f.FreePageIds(), f.pendingPageIds())
}

func (f *HashMap) Allocate(txid common.Txid, n int) common.Pgid {
func (f *hashMap) Allocate(txid common.Txid, n int) common.Pgid {
if n == 0 {
return 0
}
Expand Down Expand Up @@ -105,11 +105,11 @@ func (f *HashMap) Allocate(txid common.Txid, n int) common.Pgid {
return 0
}

func (f *HashMap) Count() int {
func (f *hashMap) Count() int {
return f.FreeCount() + f.PendingCount()
}

func (f *HashMap) FreeCount() int {
func (f *hashMap) FreeCount() int {
common.Verify(func() {
expectedFreePageCount := f.hashmapFreeCountSlow()
common.Assert(int(f.freePagesCount) == expectedFreePageCount,
Expand All @@ -118,7 +118,7 @@ func (f *HashMap) FreeCount() int {
return int(f.freePagesCount)
}

func (f *HashMap) FreePageIds() common.Pgids {
func (f *hashMap) FreePageIds() common.Pgids {
count := f.FreeCount()
if count == 0 {
return nil
Expand All @@ -143,15 +143,15 @@ func (f *HashMap) FreePageIds() common.Pgids {
return m
}

func (f *HashMap) hashmapFreeCountSlow() int {
func (f *hashMap) hashmapFreeCountSlow() int {
count := 0
for _, size := range f.forwardMap {
count += int(size)
}
return count
}

func (f *HashMap) addSpan(start common.Pgid, size uint64) {
func (f *hashMap) addSpan(start common.Pgid, size uint64) {
f.backwardMap[start-1+common.Pgid(size)] = size
f.forwardMap[start] = size
if _, ok := f.freemaps[size]; !ok {
Expand All @@ -162,7 +162,7 @@ func (f *HashMap) addSpan(start common.Pgid, size uint64) {
f.freePagesCount += size
}

func (f *HashMap) delSpan(start common.Pgid, size uint64) {
func (f *hashMap) delSpan(start common.Pgid, size uint64) {
delete(f.forwardMap, start)
delete(f.backwardMap, start+common.Pgid(size-1))
delete(f.freemaps[size], start)
Expand All @@ -172,7 +172,7 @@ func (f *HashMap) delSpan(start common.Pgid, size uint64) {
f.freePagesCount -= size
}

func (f *HashMap) mergeSpans(ids common.Pgids) {
func (f *hashMap) mergeSpans(ids common.Pgids) {
common.Verify(func() {
ids1Freemap := f.idsFromFreemaps()
ids2Forward := f.idsFromForwardMap()
Expand Down Expand Up @@ -207,7 +207,7 @@ func (f *HashMap) mergeSpans(ids common.Pgids) {
}

// mergeWithExistingSpan merges pid to the existing free spans, try to merge it backward and forward
func (f *HashMap) mergeWithExistingSpan(pid common.Pgid) {
func (f *hashMap) mergeWithExistingSpan(pid common.Pgid) {
prev := pid - 1
next := pid + 1

Expand Down Expand Up @@ -236,7 +236,7 @@ func (f *HashMap) mergeWithExistingSpan(pid common.Pgid) {

// idsFromFreemaps get all free page IDs from f.freemaps.
// used by test only.
func (f *HashMap) idsFromFreemaps() map[common.Pgid]struct{} {
func (f *hashMap) idsFromFreemaps() map[common.Pgid]struct{} {
ids := make(map[common.Pgid]struct{})
for size, idSet := range f.freemaps {
for start := range idSet {
Expand All @@ -254,7 +254,7 @@ func (f *HashMap) idsFromFreemaps() map[common.Pgid]struct{} {

// idsFromForwardMap get all free page IDs from f.forwardMap.
// used by test only.
func (f *HashMap) idsFromForwardMap() map[common.Pgid]struct{} {
func (f *hashMap) idsFromForwardMap() map[common.Pgid]struct{} {
ids := make(map[common.Pgid]struct{})
for start, size := range f.forwardMap {
for i := 0; i < int(size); i++ {
Expand All @@ -270,7 +270,7 @@ func (f *HashMap) idsFromForwardMap() map[common.Pgid]struct{} {

// idsFromBackwardMap get all free page IDs from f.backwardMap.
// used by test only.
func (f *HashMap) idsFromBackwardMap() map[common.Pgid]struct{} {
func (f *hashMap) idsFromBackwardMap() map[common.Pgid]struct{} {
ids := make(map[common.Pgid]struct{})
for end, size := range f.backwardMap {
for i := 0; i < int(size); i++ {
Expand All @@ -284,8 +284,8 @@ func (f *HashMap) idsFromBackwardMap() map[common.Pgid]struct{} {
return ids
}

func NewHashMap() *HashMap {
hm := &HashMap{
func NewHashMapFreelist() Interface {
hm := &hashMap{
shared: shared{
allocs: make(map[common.Pgid]common.Txid),
cache: make(map[common.Pgid]struct{}),
Expand Down
2 changes: 1 addition & 1 deletion internal/freelist/hashmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestFreelistHashmap_allocate(t *testing.T) {
f := NewHashMap()
f := NewHashMapFreelist()

ids := []common.Pgid{3, 4, 5, 6, 7, 9, 12, 13, 18}
f.Init(ids)
Expand Down
12 changes: 6 additions & 6 deletions internal/freelist/serde.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ import (

type Serializable interface {
// Read calls Init with the page ids stored in te given page.
Read(f Freelist, page *common.Page)
Read(f Interface, page *common.Page)

// Write writes the freelist into the given page.
Write(f Freelist, page *common.Page)
Write(f Interface, page *common.Page)

// EstimatedWritePageSize returns the size of the page after serialization in Write.
// This should never underestimate the size.
EstimatedWritePageSize(f Freelist) int
EstimatedWritePageSize(f Interface) int
}

type Serializer struct {
}

func (s Serializer) Read(f Freelist, p *common.Page) {
func (s Serializer) Read(f Interface, p *common.Page) {
if !p.IsFreelistPage() {
panic(fmt.Sprintf("invalid freelist page: %d, page type is %s", p.Id(), p.Typ()))
}
Expand All @@ -44,7 +44,7 @@ func (s Serializer) Read(f Freelist, p *common.Page) {
}
}

func (s Serializer) EstimatedWritePageSize(f Freelist) int {
func (s Serializer) EstimatedWritePageSize(f Interface) int {
n := f.Count()
if n >= 0xFFFF {
// The first element will be used to store the count. See freelist.write.
Expand All @@ -53,7 +53,7 @@ func (s Serializer) EstimatedWritePageSize(f Freelist) int {
return int(common.PageHeaderSize) + (int(unsafe.Sizeof(common.Pgid(0))) * n)
}

func (s Serializer) Write(f Freelist, p *common.Page) {
func (s Serializer) Write(f Interface, p *common.Page) {
// Combine the old free pgids and pgids waiting on an open transaction.

// Update the header flag.
Expand Down

0 comments on commit 51cbf71

Please sign in to comment.