Skip to content

Commit 7e8d09b

Browse files
committed
first review by ben
Signed-off-by: Thomas Jungblut <[email protected]>
1 parent 56634cd commit 7e8d09b

File tree

9 files changed

+47
-47
lines changed

9 files changed

+47
-47
lines changed

allocate_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func TestTx_allocatePageStats(t *testing.T) {
11-
for n, f := range map[string]freelist.Freelist{"hashmap": freelist.NewHashMap(), "array": freelist.NewArray()} {
11+
for n, f := range map[string]freelist.Interface{"hashmap": freelist.NewHashMapFreelist(), "array": freelist.NewArrayFreelist()} {
1212
t.Run(n, func(t *testing.T) {
1313
ids := []common.Pgid{2, 3}
1414
f.Init(ids)

db.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ type DB struct {
135135
rwtx *Tx
136136
txs []*Tx
137137

138-
freelist fl.Freelist
138+
freelist fl.Interface
139139
freelistSerializer fl.Serializable
140140
freelistLoad sync.Once
141141

@@ -1286,11 +1286,11 @@ func (db *DB) freepages() []common.Pgid {
12861286
return fids
12871287
}
12881288

1289-
func newFreelist(freelistType FreelistType) fl.Freelist {
1289+
func newFreelist(freelistType FreelistType) fl.Interface {
12901290
if freelistType == FreelistMapType {
1291-
return fl.NewHashMap()
1291+
return fl.NewHashMapFreelist()
12921292
}
1293-
return fl.NewArray()
1293+
return fl.NewArrayFreelist()
12941294
}
12951295

12961296
// Options represents the options that can be set when opening a database.

internal/freelist/array.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ import (
77
"go.etcd.io/bbolt/internal/common"
88
)
99

10-
type Array struct {
10+
type array struct {
1111
shared
1212

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

16-
func (f *Array) Init(ids common.Pgids) {
16+
func (f *array) Init(ids common.Pgids) {
1717
f.ids = ids
1818
f.reindex(f.FreePageIds(), f.pendingPageIds())
1919
}
2020

21-
func (f *Array) Allocate(txid common.Txid, n int) common.Pgid {
21+
func (f *array) Allocate(txid common.Txid, n int) common.Pgid {
2222
if len(f.ids) == 0 {
2323
return 0
2424
}
@@ -60,19 +60,19 @@ func (f *Array) Allocate(txid common.Txid, n int) common.Pgid {
6060
return 0
6161
}
6262

63-
func (f *Array) Count() int {
63+
func (f *array) Count() int {
6464
return f.FreeCount() + f.PendingCount()
6565
}
6666

67-
func (f *Array) FreeCount() int {
67+
func (f *array) FreeCount() int {
6868
return len(f.ids)
6969
}
7070

71-
func (f *Array) FreePageIds() common.Pgids {
71+
func (f *array) FreePageIds() common.Pgids {
7272
return f.ids
7373
}
7474

75-
func (f *Array) mergeSpans(ids common.Pgids) {
75+
func (f *array) mergeSpans(ids common.Pgids) {
7676
sort.Sort(ids)
7777
common.Verify(func() {
7878
idsIdx := make(map[common.Pgid]struct{})
@@ -102,8 +102,8 @@ func (f *Array) mergeSpans(ids common.Pgids) {
102102
f.ids = common.Pgids(f.ids).Merge(ids)
103103
}
104104

105-
func NewArray() *Array {
106-
a := &Array{
105+
func NewArrayFreelist() Interface {
106+
a := &array{
107107
shared: shared{
108108
pending: make(map[common.Txid]*txPending),
109109
allocs: make(map[common.Pgid]common.Txid),

internal/freelist/array_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
// Ensure that a freelist can find contiguous blocks of pages.
1111
func TestFreelistArray_allocate(t *testing.T) {
12-
f := NewArray()
12+
f := NewArrayFreelist()
1313
ids := []common.Pgid{3, 4, 5, 6, 7, 9, 12, 13, 18}
1414
f.Init(ids)
1515
if id := int(f.Allocate(1, 3)); id != 3 {

internal/freelist/freelist.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"go.etcd.io/bbolt/internal/common"
77
)
88

9-
type Freelist interface {
9+
type Interface interface {
1010
// Init initializes this freelist with the given list of pages.
1111
Init(ids common.Pgids)
1212

@@ -48,7 +48,7 @@ type Freelist interface {
4848

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

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

6666
// NoSyncReload reads the freelist from Pgids and filters out pending items.
67-
func NoSyncReload(f Freelist, pgIds common.Pgids) {
67+
func NoSyncReload(f Interface, pgIds common.Pgids) {
6868
// Build a cache of only pending pages.
6969
pcache := make(map[common.Pgid]bool)
7070
for _, txp := range f.pendingPageIds() {
@@ -76,7 +76,7 @@ func NoSyncReload(f Freelist, pgIds common.Pgids) {
7676
// Check each page in the freelist and build a new available freelist
7777
// with any pages not in the pending lists.
7878
var a []common.Pgid
79-
for _, id := range f.FreePageIds() {
79+
for _, id := range pgIds {
8080
if !pcache[id] {
8181
a = append(a, id)
8282
}

internal/freelist/freelist_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,10 @@ func Test_freelist_ReadIDs_and_getFreePageIDs(t *testing.T) {
226226
}
227227

228228
// newTestFreelist get the freelist type from env and initial the freelist
229-
func newTestFreelist() Freelist {
229+
func newTestFreelist() Interface {
230230
if env := os.Getenv(TestFreelistType); env == "map" {
231-
return NewHashMap()
231+
return NewHashMapFreelist()
232232
}
233233

234-
return NewArray()
234+
return NewArrayFreelist()
235235
}

internal/freelist/hashmap.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
// pidSet holds the set of starting pgids which have the same span size
1212
type pidSet map[common.Pgid]struct{}
1313

14-
type HashMap struct {
14+
type hashMap struct {
1515
shared
1616

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

23-
func (f *HashMap) Init(pgids common.Pgids) {
23+
func (f *hashMap) Init(pgids common.Pgids) {
2424
if len(pgids) == 0 {
2525
return
2626
}
@@ -58,7 +58,7 @@ func (f *HashMap) Init(pgids common.Pgids) {
5858
f.reindex(f.FreePageIds(), f.pendingPageIds())
5959
}
6060

61-
func (f *HashMap) Allocate(txid common.Txid, n int) common.Pgid {
61+
func (f *hashMap) Allocate(txid common.Txid, n int) common.Pgid {
6262
if n == 0 {
6363
return 0
6464
}
@@ -105,11 +105,11 @@ func (f *HashMap) Allocate(txid common.Txid, n int) common.Pgid {
105105
return 0
106106
}
107107

108-
func (f *HashMap) Count() int {
108+
func (f *hashMap) Count() int {
109109
return f.FreeCount() + f.PendingCount()
110110
}
111111

112-
func (f *HashMap) FreeCount() int {
112+
func (f *hashMap) FreeCount() int {
113113
common.Verify(func() {
114114
expectedFreePageCount := f.hashmapFreeCountSlow()
115115
common.Assert(int(f.freePagesCount) == expectedFreePageCount,
@@ -118,7 +118,7 @@ func (f *HashMap) FreeCount() int {
118118
return int(f.freePagesCount)
119119
}
120120

121-
func (f *HashMap) FreePageIds() common.Pgids {
121+
func (f *hashMap) FreePageIds() common.Pgids {
122122
count := f.FreeCount()
123123
if count == 0 {
124124
return nil
@@ -143,15 +143,15 @@ func (f *HashMap) FreePageIds() common.Pgids {
143143
return m
144144
}
145145

146-
func (f *HashMap) hashmapFreeCountSlow() int {
146+
func (f *hashMap) hashmapFreeCountSlow() int {
147147
count := 0
148148
for _, size := range f.forwardMap {
149149
count += int(size)
150150
}
151151
return count
152152
}
153153

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

165-
func (f *HashMap) delSpan(start common.Pgid, size uint64) {
165+
func (f *hashMap) delSpan(start common.Pgid, size uint64) {
166166
delete(f.forwardMap, start)
167167
delete(f.backwardMap, start+common.Pgid(size-1))
168168
delete(f.freemaps[size], start)
@@ -172,7 +172,7 @@ func (f *HashMap) delSpan(start common.Pgid, size uint64) {
172172
f.freePagesCount -= size
173173
}
174174

175-
func (f *HashMap) mergeSpans(ids common.Pgids) {
175+
func (f *hashMap) mergeSpans(ids common.Pgids) {
176176
common.Verify(func() {
177177
ids1Freemap := f.idsFromFreemaps()
178178
ids2Forward := f.idsFromForwardMap()
@@ -207,7 +207,7 @@ func (f *HashMap) mergeSpans(ids common.Pgids) {
207207
}
208208

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

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

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

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

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

287-
func NewHashMap() *HashMap {
288-
hm := &HashMap{
287+
func NewHashMapFreelist() Interface {
288+
hm := &hashMap{
289289
shared: shared{
290290
allocs: make(map[common.Pgid]common.Txid),
291291
cache: make(map[common.Pgid]struct{}),

internal/freelist/hashmap_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
func TestFreelistHashmap_allocate(t *testing.T) {
10-
f := NewHashMap()
10+
f := NewHashMapFreelist()
1111

1212
ids := []common.Pgid{3, 4, 5, 6, 7, 9, 12, 13, 18}
1313
f.Init(ids)

internal/freelist/serde.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@ import (
1010

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

1515
// Write writes the freelist into the given page.
16-
Write(f Freelist, page *common.Page)
16+
Write(f Interface, page *common.Page)
1717

18-
// EstimatedWritePageSize returns the size of the page after serialization in Write.
18+
// EstimatedWritePageSize returns the size of the freelist after serialization in Write.
1919
// This should never underestimate the size.
20-
EstimatedWritePageSize(f Freelist) int
20+
EstimatedWritePageSize(f Interface) int
2121
}
2222

2323
type Serializer struct {
2424
}
2525

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

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

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

5959
// Update the header flag.

0 commit comments

Comments
 (0)