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 1 commit
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
Prev Previous commit
Next Next commit
Use a Mutex rather than RWMutex for strict backwards compatibility
  • Loading branch information
erikgrinaker committed Mar 4, 2020
commit 2b2bff6ce202a4f5a527f8fb73e5f1f9e78ec045
4 changes: 2 additions & 2 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
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
24 changes: 12 additions & 12 deletions mem_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func init() {
var _ DB = (*MemDB)(nil)

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

Expand All @@ -63,14 +63,14 @@ func NewMemDB() *MemDB {
}

// 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 @@ -82,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 @@ -175,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 @@ -194,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 Down