Use btree with non-concurrent mode

Minor optimization
This commit is contained in:
tidwall 2021-07-31 07:36:38 -07:00
parent 89544ad8fc
commit d4c361c59c
3 changed files with 16 additions and 11 deletions

View File

@ -140,8 +140,8 @@ type exctx struct {
func Open(path string) (*DB, error) { func Open(path string) (*DB, error) {
db := &DB{} db := &DB{}
// initialize trees and indexes // initialize trees and indexes
db.keys = btree.New(lessCtx(nil)) db.keys = btreeNew(lessCtx(nil))
db.exps = btree.New(lessCtx(&exctx{db})) db.exps = btreeNew(lessCtx(&exctx{db}))
db.idxs = make(map[string]*index) db.idxs = make(map[string]*index)
// initialize default configuration // initialize default configuration
db.config = Config{ db.config = Config{
@ -284,7 +284,7 @@ func (idx *index) clearCopy() *index {
} }
// initialize with empty trees // initialize with empty trees
if nidx.less != nil { if nidx.less != nil {
nidx.btr = btree.New(lessCtx(nidx)) nidx.btr = btreeNew(lessCtx(nidx))
} }
if nidx.rect != nil { if nidx.rect != nil {
nidx.rtr = rtred.New(nidx) nidx.rtr = rtred.New(nidx)
@ -296,7 +296,7 @@ func (idx *index) clearCopy() *index {
func (idx *index) rebuild() { func (idx *index) rebuild() {
// initialize trees // initialize trees
if idx.less != nil { if idx.less != nil {
idx.btr = btree.New(lessCtx(idx)) idx.btr = btreeNew(lessCtx(idx))
} }
if idx.rect != nil { if idx.rect != nil {
idx.rtr = rtred.New(idx) idx.rtr = rtred.New(idx)
@ -920,8 +920,8 @@ func (db *DB) readLoad(rd io.Reader, modTime time.Time) (n int64, err error) {
db.deleteFromDatabase(&dbItem{key: parts[1]}) db.deleteFromDatabase(&dbItem{key: parts[1]})
} else if (parts[0][0] == 'f' || parts[0][0] == 'F') && } else if (parts[0][0] == 'f' || parts[0][0] == 'F') &&
strings.ToLower(parts[0]) == "flushdb" { strings.ToLower(parts[0]) == "flushdb" {
db.keys = btree.New(lessCtx(nil)) db.keys = btreeNew(lessCtx(nil))
db.exps = btree.New(lessCtx(&exctx{db})) db.exps = btreeNew(lessCtx(&exctx{db}))
db.idxs = make(map[string]*index) db.idxs = make(map[string]*index)
} else { } else {
return totalSize, ErrInvalid return totalSize, ErrInvalid
@ -1066,8 +1066,8 @@ func (tx *Tx) DeleteAll() error {
} }
// now reset the live database trees // now reset the live database trees
tx.db.keys = btree.New(lessCtx(nil)) tx.db.keys = btreeNew(lessCtx(nil))
tx.db.exps = btree.New(lessCtx(&exctx{tx.db})) tx.db.exps = btreeNew(lessCtx(&exctx{tx.db}))
tx.db.idxs = make(map[string]*index) tx.db.idxs = make(map[string]*index)
// finally re-create the indexes // finally re-create the indexes
@ -2312,3 +2312,8 @@ func btreeDescendLessOrEqual(tr *btree.BTree, pivot interface{},
) { ) {
tr.Descend(pivot, iter) tr.Descend(pivot, iter)
} }
func btreeNew(less func(a, b interface{}) bool) *btree.BTree {
// Using NewNonConcurrent because we're managing our own locks.
return btree.NewNonConcurrent(less)
}

2
go.mod
View File

@ -3,7 +3,7 @@ module github.com/tidwall/buntdb
go 1.16 go 1.16
require ( require (
github.com/tidwall/btree v0.5.0 github.com/tidwall/btree v0.6.0
github.com/tidwall/gjson v1.8.0 github.com/tidwall/gjson v1.8.0
github.com/tidwall/grect v0.1.2 github.com/tidwall/grect v0.1.2
github.com/tidwall/lotsa v1.0.2 github.com/tidwall/lotsa v1.0.2

4
go.sum
View File

@ -1,5 +1,5 @@
github.com/tidwall/btree v0.5.0 h1:IBfCtOj4uOMQcodv3wzYVo0zPqSJObm71mE039/dlXY= github.com/tidwall/btree v0.6.0 h1:JLYAFGV+1gjyFi3iQbO/fupBin+Ooh7dxqVV0twJ1Bo=
github.com/tidwall/btree v0.5.0/go.mod h1:TzIRzen6yHbibdSfK6t8QimqbUnoxUSrZfeW7Uob0q4= github.com/tidwall/btree v0.6.0/go.mod h1:TzIRzen6yHbibdSfK6t8QimqbUnoxUSrZfeW7Uob0q4=
github.com/tidwall/gjson v1.8.0 h1:Qt+orfosKn0rbNTZqHYDqBrmm3UDA4KRkv70fDzG+PQ= github.com/tidwall/gjson v1.8.0 h1:Qt+orfosKn0rbNTZqHYDqBrmm3UDA4KRkv70fDzG+PQ=
github.com/tidwall/gjson v1.8.0/go.mod h1:5/xDoumyyDNerp2U36lyolv46b3uF/9Bu6OfyQ9GImk= github.com/tidwall/gjson v1.8.0/go.mod h1:5/xDoumyyDNerp2U36lyolv46b3uF/9Bu6OfyQ9GImk=
github.com/tidwall/grect v0.1.2 h1:wKVeQVZhjaFCKTTlpkDe3Ex4ko3cMGW3MRKawRe8uQ4= github.com/tidwall/grect v0.1.2 h1:wKVeQVZhjaFCKTTlpkDe3Ex4ko3cMGW3MRKawRe8uQ4=