Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

memdb: use btree for storage #53

Merged
merged 9 commits into from
Mar 9, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func newTempDB(t *testing.T, backend BackendType) (db DB, dbDir string) {
// NOTE: not actually goroutine safe.
// If you want something goroutine safe, maybe you just want a MemDB.
type mockDB struct {
mtx sync.RWMutex
mtx sync.Mutex
calls map[string]int
}

Expand All @@ -90,7 +90,7 @@ func newMockDB() *mockDB {
}
}

func (mdb *mockDB) Mutex() *sync.RWMutex {
func (mdb *mockDB) Mutex() *sync.Mutex {
return &(mdb.mtx)
}

Expand Down Expand Up @@ -213,7 +213,8 @@ func benchmarkRangeScans(b *testing.B, db DB, dbSize int64) {
bytes := int642Bytes(i)
err := db.Set(bytes, bytes)
if err != nil {
// for performance
// require.NoError() is very expensive (according to profiler), so we do a cheap if as
// well since this is a tight loop.
require.NoError(b, err)
erikgrinaker marked this conversation as resolved.
Show resolved Hide resolved
}
}
Expand Down Expand Up @@ -257,6 +258,8 @@ func benchmarkRandomReadsWrites(b *testing.B, db DB) {
//fmt.Printf("Set %X -> %X\n", idxBytes, valBytes)
err := db.Set(idxBytes, valBytes)
if err != nil {
erikgrinaker marked this conversation as resolved.
Show resolved Hide resolved
// require.NoError() is very expensive (according to profiler), so we do a cheap if
// as well since this is a tight loop.
require.NoError(b, err)
}
}
Expand All @@ -268,6 +271,8 @@ func benchmarkRandomReadsWrites(b *testing.B, db DB) {
idxBytes := int642Bytes(idx)
valBytes, err := db.Get(idxBytes)
if err != nil {
erikgrinaker marked this conversation as resolved.
Show resolved Hide resolved
// require.NoError() is very expensive (according to profiler), so we do a cheap if
// as well since this is a tight loop.
require.NoError(b, err)
}
//fmt.Printf("Get %X -> %X\n", idxBytes, valBytes)
Expand Down
2 changes: 1 addition & 1 deletion mem_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package db
import "sync"

type atomicSetDeleter interface {
Mutex() *sync.RWMutex
Mutex() *sync.Mutex
SetNoLock(key, value []byte)
SetNoLockSync(key, value []byte)
DeleteNoLock(key []byte)
Expand Down
39 changes: 25 additions & 14 deletions mem_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ import (
"github.com/google/btree"
)

const (
// The approximate number of items and children per B-tree node. Tuned with benchmarks.
bTreeDegree = 32

// Size of the channel buffer between traversal goroutine and iterator. Using an unbuffered
// channel causes two context switches per item sent, while buffering allows more work per
// context switch. Tuned with benchmarks.
chBufferSize = 64
)

// item is a btree.Item with byte slices as keys and values
type item struct {
key []byte
value []byte
Expand Down Expand Up @@ -40,26 +51,26 @@ func init() {
var _ DB = (*MemDB)(nil)

type MemDB struct {
mtx sync.RWMutex
mtx sync.Mutex
btree *btree.BTree
}

func NewMemDB() *MemDB {
database := &MemDB{
btree: btree.New(32),
btree: btree.New(bTreeDegree),
}
return database
}

// Implements atomicSetDeleter.
func (db *MemDB) Mutex() *sync.RWMutex {
func (db *MemDB) Mutex() *sync.Mutex {
return &(db.mtx)
}

// Implements DB.
func (db *MemDB) Get(key []byte) ([]byte, error) {
db.mtx.RLock()
defer db.mtx.RUnlock()
db.mtx.Lock()
defer db.mtx.Unlock()
key = nonNilBytes(key)

i := db.btree.Get(newKey(key))
Expand All @@ -71,8 +82,8 @@ func (db *MemDB) Get(key []byte) ([]byte, error) {

// Implements DB.
func (db *MemDB) Has(key []byte) (bool, error) {
db.mtx.RLock()
defer db.mtx.RUnlock()
db.mtx.Lock()
defer db.mtx.Unlock()
key = nonNilBytes(key)

return db.btree.Has(newKey(key)), nil
Expand Down Expand Up @@ -164,8 +175,8 @@ func (db *MemDB) Print() error {

// Implements DB.
func (db *MemDB) Stats() map[string]string {
db.mtx.RLock()
defer db.mtx.RUnlock()
db.mtx.Lock()
defer db.mtx.Unlock()

stats := make(map[string]string)
stats["database.type"] = "memDB"
Expand All @@ -183,16 +194,16 @@ func (db *MemDB) NewBatch() Batch {

// Implements DB.
func (db *MemDB) Iterator(start, end []byte) (Iterator, error) {
db.mtx.RLock()
defer db.mtx.RUnlock()
db.mtx.Lock()
defer db.mtx.Unlock()

return newMemDBIterator(db.btree, start, end, false), nil
}

// Implements DB.
func (db *MemDB) ReverseIterator(start, end []byte) (Iterator, error) {
db.mtx.RLock()
defer db.mtx.RUnlock()
db.mtx.Lock()
defer db.mtx.Unlock()

return newMemDBIterator(db.btree, start, end, true), nil
}
Expand All @@ -209,7 +220,7 @@ var _ Iterator = (*memDBIterator)(nil)

func newMemDBIterator(bt *btree.BTree, start []byte, end []byte, reverse bool) *memDBIterator {
ctx, cancel := context.WithCancel(context.Background())
ch := make(chan *item, 64)
ch := make(chan *item, chBufferSize)
iter := &memDBIterator{
ch: ch,
cancel: cancel,
Expand Down