Skip to content

Commit

Permalink
Merge pull request #460 from ahrtr/freelist_type_20230412
Browse files Browse the repository at this point in the history
Move FreelistType from `internal/common` to top level package `bbolt`
  • Loading branch information
ahrtr authored Apr 13, 2023
2 parents 5602d88 + 6adc0c4 commit bc572b8
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 33 deletions.
21 changes: 13 additions & 8 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@ import (
// The time elapsed between consecutive file locking attempts.
const flockRetryTimeout = 50 * time.Millisecond

// Export both FreelistArrayType and FreelistMapType so as to keep API compatibility.
// FreelistType is the type of the freelist backend
type FreelistType string

// TODO(ahrtr): eventually we should (step by step)
// 1. default to FreelistMapType;
// 2. remove the FreelistArrayType and do not export FreelistMapType;
// 1. default to `FreelistMapType`;
// 2. remove the `FreelistArrayType`, do not export `FreelistMapType`
// and remove field `FreelistType' from both `DB` and `Options`;
const (
FreelistArrayType = common.FreelistArrayType
FreelistMapType = common.FreelistMapType
// FreelistArrayType indicates backend freelist type is array
FreelistArrayType = FreelistType("array")
// FreelistMapType indicates backend freelist type is hashmap
FreelistMapType = FreelistType("hashmap")
)

// DB represents a collection of buckets persisted to a file on disk.
Expand Down Expand Up @@ -59,7 +64,7 @@ type DB struct {
// The alternative one is using hashmap, it is faster in almost all circumstances
// but it doesn't guarantee that it offers the smallest page id available. In normal case it is safe.
// The default type is array
FreelistType common.FreelistType
FreelistType FreelistType

// When true, skips the truncate call when growing the database.
// Setting this to true is only safe on non-ext3/ext4 systems.
Expand Down Expand Up @@ -1211,7 +1216,7 @@ type Options struct {
// The alternative one is using hashmap, it is faster in almost all circumstances
// but it doesn't guarantee that it offers the smallest page id available. In normal case it is safe.
// The default type is array
FreelistType common.FreelistType
FreelistType FreelistType

// Open database in read-only mode. Uses flock(..., LOCK_SH |LOCK_NB) to
// grab a shared lock (UNIX).
Expand Down Expand Up @@ -1253,7 +1258,7 @@ type Options struct {
var DefaultOptions = &Options{
Timeout: 0,
NoGrowSync: false,
FreelistType: common.FreelistArrayType,
FreelistType: FreelistArrayType,
}

// Stats represents statistics about the database.
Expand Down
6 changes: 3 additions & 3 deletions freelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type pidSet map[common.Pgid]struct{}
// freelist represents a list of all pages that are available for allocation.
// It also tracks pages that have been freed but are still in use by open transactions.
type freelist struct {
freelistType common.FreelistType // freelist type
freelistType FreelistType // freelist type
ids []common.Pgid // all free and available free page ids.
allocs map[common.Pgid]common.Txid // mapping of Txid that allocated a pgid.
pending map[common.Txid]*txPending // mapping of soon-to-be free page ids by tx.
Expand All @@ -38,7 +38,7 @@ type freelist struct {
}

// newFreelist returns an empty, initialized freelist.
func newFreelist(freelistType common.FreelistType) *freelist {
func newFreelist(freelistType FreelistType) *freelist {
f := &freelist{
freelistType: freelistType,
allocs: make(map[common.Pgid]common.Txid),
Expand All @@ -49,7 +49,7 @@ func newFreelist(freelistType common.FreelistType) *freelist {
backwardMap: make(map[common.Pgid]uint64),
}

if freelistType == common.FreelistMapType {
if freelistType == FreelistMapType {
f.allocate = f.hashmapAllocate
f.free_count = f.hashmapFreeCount
f.mergeSpans = f.hashmapMergeSpans
Expand Down
16 changes: 8 additions & 8 deletions freelist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func TestFreelist_releaseRange(t *testing.T) {

func TestFreelistHashmap_allocate(t *testing.T) {
f := newTestFreelist()
if f.freelistType != common.FreelistMapType {
if f.freelistType != FreelistMapType {
t.Skip()
}

Expand Down Expand Up @@ -211,7 +211,7 @@ func TestFreelistHashmap_allocate(t *testing.T) {
// Ensure that a freelist can find contiguous blocks of pages.
func TestFreelistArray_allocate(t *testing.T) {
f := newTestFreelist()
if f.freelistType != common.FreelistArrayType {
if f.freelistType != FreelistArrayType {
t.Skip()
}
ids := []common.Pgid{3, 4, 5, 6, 7, 9, 12, 13, 18}
Expand Down Expand Up @@ -403,7 +403,7 @@ func Test_freelist_mergeWithExist(t *testing.T) {
}
for _, tt := range tests {
f := newTestFreelist()
if f.freelistType == common.FreelistArrayType {
if f.freelistType == FreelistArrayType {
t.Skip()
}
f.readIDs(tt.ids)
Expand All @@ -427,17 +427,17 @@ func Test_freelist_mergeWithExist(t *testing.T) {

// newTestFreelist get the freelist type from env and initial the freelist
func newTestFreelist() *freelist {
freelistType := common.FreelistArrayType
if env := os.Getenv(TestFreelistType); env == string(common.FreelistMapType) {
freelistType = common.FreelistMapType
freelistType := FreelistArrayType
if env := os.Getenv(TestFreelistType); env == string(FreelistMapType) {
freelistType = FreelistMapType
}

return newFreelist(freelistType)
}

func Test_freelist_hashmapGetFreePageIDs(t *testing.T) {
f := newTestFreelist()
if f.freelistType == common.FreelistArrayType {
if f.freelistType == FreelistArrayType {
t.Skip()
}

Expand All @@ -461,7 +461,7 @@ func Test_freelist_hashmapGetFreePageIDs(t *testing.T) {

func Benchmark_freelist_hashmapGetFreePageIDs(b *testing.B) {
f := newTestFreelist()
if f.freelistType == common.FreelistArrayType {
if f.freelistType == FreelistArrayType {
b.Skip()
}

Expand Down
7 changes: 3 additions & 4 deletions internal/btesting/btesting.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/stretchr/testify/require"

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

var statsFlag = flag.Bool("stats", false, "show performance stats")
Expand Down Expand Up @@ -50,9 +49,9 @@ func MustOpenDBWithOption(t testing.TB, f string, o *bolt.Options) *DB {
o = bolt.DefaultOptions
}

freelistType := common.FreelistArrayType
if env := os.Getenv(TestFreelistType); env == string(common.FreelistMapType) {
freelistType = common.FreelistMapType
freelistType := bolt.FreelistArrayType
if env := os.Getenv(TestFreelistType); env == string(bolt.FreelistMapType) {
freelistType = bolt.FreelistMapType
}

o.FreelistType = freelistType
Expand Down
10 changes: 0 additions & 10 deletions internal/common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,5 @@ const (
// DefaultPageSize is the default page size for db which is set to the OS page size.
var DefaultPageSize = os.Getpagesize()

// FreelistType is the type of the freelist backend
type FreelistType string

const (
// FreelistArrayType indicates backend freelist type is array
FreelistArrayType = FreelistType("array")
// FreelistMapType indicates backend freelist type is hashmap
FreelistMapType = FreelistType("hashmap")
)

// Txid represents the internal transaction identifier.
type Txid uint64

0 comments on commit bc572b8

Please sign in to comment.