From d4c361c59c678b8bbdbd90dcc3208df4deccc030 Mon Sep 17 00:00:00 2001 From: tidwall Date: Sat, 31 Jul 2021 07:36:38 -0700 Subject: [PATCH] Use btree with non-concurrent mode Minor optimization --- buntdb.go | 21 +++++++++++++-------- go.mod | 2 +- go.sum | 4 ++-- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/buntdb.go b/buntdb.go index f8ee6b5..d087744 100644 --- a/buntdb.go +++ b/buntdb.go @@ -140,8 +140,8 @@ type exctx struct { func Open(path string) (*DB, error) { db := &DB{} // initialize trees and indexes - db.keys = btree.New(lessCtx(nil)) - db.exps = btree.New(lessCtx(&exctx{db})) + db.keys = btreeNew(lessCtx(nil)) + db.exps = btreeNew(lessCtx(&exctx{db})) db.idxs = make(map[string]*index) // initialize default configuration db.config = Config{ @@ -284,7 +284,7 @@ func (idx *index) clearCopy() *index { } // initialize with empty trees if nidx.less != nil { - nidx.btr = btree.New(lessCtx(nidx)) + nidx.btr = btreeNew(lessCtx(nidx)) } if nidx.rect != nil { nidx.rtr = rtred.New(nidx) @@ -296,7 +296,7 @@ func (idx *index) clearCopy() *index { func (idx *index) rebuild() { // initialize trees if idx.less != nil { - idx.btr = btree.New(lessCtx(idx)) + idx.btr = btreeNew(lessCtx(idx)) } if idx.rect != nil { 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]}) } else if (parts[0][0] == 'f' || parts[0][0] == 'F') && strings.ToLower(parts[0]) == "flushdb" { - db.keys = btree.New(lessCtx(nil)) - db.exps = btree.New(lessCtx(&exctx{db})) + db.keys = btreeNew(lessCtx(nil)) + db.exps = btreeNew(lessCtx(&exctx{db})) db.idxs = make(map[string]*index) } else { return totalSize, ErrInvalid @@ -1066,8 +1066,8 @@ func (tx *Tx) DeleteAll() error { } // now reset the live database trees - tx.db.keys = btree.New(lessCtx(nil)) - tx.db.exps = btree.New(lessCtx(&exctx{tx.db})) + tx.db.keys = btreeNew(lessCtx(nil)) + tx.db.exps = btreeNew(lessCtx(&exctx{tx.db})) tx.db.idxs = make(map[string]*index) // finally re-create the indexes @@ -2312,3 +2312,8 @@ func btreeDescendLessOrEqual(tr *btree.BTree, pivot interface{}, ) { 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) +} diff --git a/go.mod b/go.mod index 67c3f6b..171f550 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/tidwall/buntdb go 1.16 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/grect v0.1.2 github.com/tidwall/lotsa v1.0.2 diff --git a/go.sum b/go.sum index 1d65454..421bd3e 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/tidwall/btree v0.5.0 h1:IBfCtOj4uOMQcodv3wzYVo0zPqSJObm71mE039/dlXY= -github.com/tidwall/btree v0.5.0/go.mod h1:TzIRzen6yHbibdSfK6t8QimqbUnoxUSrZfeW7Uob0q4= +github.com/tidwall/btree v0.6.0 h1:JLYAFGV+1gjyFi3iQbO/fupBin+Ooh7dxqVV0twJ1Bo= +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/go.mod h1:5/xDoumyyDNerp2U36lyolv46b3uF/9Bu6OfyQ9GImk= github.com/tidwall/grect v0.1.2 h1:wKVeQVZhjaFCKTTlpkDe3Ex4ko3cMGW3MRKawRe8uQ4=