Updated for latest btree

This commit is contained in:
tidwall 2021-12-24 11:05:07 -07:00
parent cf4e3bfc63
commit 46f673238a
3 changed files with 33 additions and 36 deletions

View File

@ -63,19 +63,19 @@ var (
// DB represents a collection of key-value pairs that persist on disk. // DB represents a collection of key-value pairs that persist on disk.
// Transactions are used for all forms of data access to the DB. // Transactions are used for all forms of data access to the DB.
type DB struct { type DB struct {
mu sync.RWMutex // the gatekeeper for all fields mu sync.RWMutex // the gatekeeper for all fields
file *os.File // the underlying file file *os.File // the underlying file
buf []byte // a buffer to write to buf []byte // a buffer to write to
keys *btree.BTree[*dbItem] // a tree of all item ordered by key keys *btree.Generic[*dbItem] // a tree of all item ordered by key
exps *btree.BTree[*dbItem] // a tree of items ordered by expiration exps *btree.Generic[*dbItem] // a tree of items ordered by expiration
idxs map[string]*index // the index trees. idxs map[string]*index // the index trees.
insIdxs []*index // a reuse buffer for gathering indexes insIdxs []*index // a reuse buffer for gathering indexes
flushes int // a count of the number of disk flushes flushes int // a count of the number of disk flushes
closed bool // set when the database has been closed closed bool // set when the database has been closed
config Config // the database configuration config Config // the database configuration
persist bool // do we write to disk persist bool // do we write to disk
shrinking bool // when an aof shrink is in-process. shrinking bool // when an aof shrink is in-process.
lastaofsz int // the size of the last shrink aof size lastaofsz int // the size of the last shrink aof size
} }
// SyncPolicy represents how often data is synced to disk. // SyncPolicy represents how often data is synced to disk.
@ -245,7 +245,7 @@ func (db *DB) Load(rd io.Reader) error {
// index represents a b-tree or r-tree index and also acts as the // index represents a b-tree or r-tree index and also acts as the
// b-tree/r-tree context for itself. // b-tree/r-tree context for itself.
type index struct { type index struct {
btr *btree.BTree[*dbItem] // contains the items btr *btree.Generic[*dbItem] // contains the items
rtr *rtred.RTree // contains the items rtr *rtred.RTree // contains the items
name string // name of the index name string // name of the index
pattern string // a required key pattern pattern string // a required key pattern
@ -1037,9 +1037,9 @@ type Tx struct {
type txWriteContext struct { type txWriteContext struct {
// rollback when deleteAll is called // rollback when deleteAll is called
rbkeys *btree.BTree[*dbItem] // a tree of all item ordered by key rbkeys *btree.Generic[*dbItem] // a tree of all item ordered by key
rbexps *btree.BTree[*dbItem] // a tree of items ordered by expiration rbexps *btree.Generic[*dbItem] // a tree of items ordered by expiration
rbidxs map[string]*index // the index trees. rbidxs map[string]*index // the index trees.
rollbackItems map[string]*dbItem // details for rolling back tx. rollbackItems map[string]*dbItem // details for rolling back tx.
commitItems map[string]*dbItem // details for committing tx. commitItems map[string]*dbItem // details for committing tx.
@ -1616,7 +1616,7 @@ func (tx *Tx) scan(desc, gt, lt bool, index, start, stop string,
iter := func(dbi *dbItem) bool { iter := func(dbi *dbItem) bool {
return iterator(dbi.key, dbi.val) return iterator(dbi.key, dbi.val)
} }
var tr *btree.BTree[*dbItem] var tr *btree.Generic[*dbItem]
if index == "" { if index == "" {
// empty index means we will use the keys tree. // empty index means we will use the keys tree.
tr = tx.db.keys tr = tx.db.keys
@ -2299,23 +2299,20 @@ func Desc(less func(a, b string) bool) func(a, b string) bool {
//// Wrappers around btree Ascend/Descend //// Wrappers around btree Ascend/Descend
func bLT(tr *btree.BTree[*dbItem], a, b *dbItem) bool { func bLT(tr *btree.Generic[*dbItem], a, b *dbItem) bool {
return tr.Less(a, b) return tr.Less(a, b)
} }
func bGT(tr *btree.BTree[*dbItem], a, b *dbItem) bool { func bGT(tr *btree.Generic[*dbItem], a, b *dbItem) bool {
return tr.Less(b, a) return tr.Less(b, a)
} }
// func bLTE(tr *btree.BTree, a, b *dbItem) bool { return !tr.Less(b, a) }
// func bGTE(tr *btree.BTree, a, b *dbItem) bool { return !tr.Less(a, b) }
// Ascend // Ascend
func btreeAscend(tr *btree.BTree[*dbItem], iter func(item *dbItem) bool) { func btreeAscend(tr *btree.Generic[*dbItem], iter func(item *dbItem) bool) {
tr.Scan(iter) tr.Scan(iter)
} }
func btreeAscendLessThan(tr *btree.BTree[*dbItem], pivot *dbItem, func btreeAscendLessThan(tr *btree.Generic[*dbItem], pivot *dbItem,
iter func(item *dbItem) bool, iter func(item *dbItem) bool,
) { ) {
tr.Scan(func(item *dbItem) bool { tr.Scan(func(item *dbItem) bool {
@ -2323,13 +2320,13 @@ func btreeAscendLessThan(tr *btree.BTree[*dbItem], pivot *dbItem,
}) })
} }
func btreeAscendGreaterOrEqual(tr *btree.BTree[*dbItem], pivot *dbItem, func btreeAscendGreaterOrEqual(tr *btree.Generic[*dbItem], pivot *dbItem,
iter func(item *dbItem) bool, iter func(item *dbItem) bool,
) { ) {
tr.Ascend(pivot, iter) tr.Ascend(pivot, iter)
} }
func btreeAscendRange(tr *btree.BTree[*dbItem], greaterOrEqual, func btreeAscendRange(tr *btree.Generic[*dbItem], greaterOrEqual,
lessThan *dbItem, iter func(item *dbItem) bool, lessThan *dbItem, iter func(item *dbItem) bool,
) { ) {
tr.Ascend(greaterOrEqual, func(item *dbItem) bool { tr.Ascend(greaterOrEqual, func(item *dbItem) bool {
@ -2339,11 +2336,11 @@ func btreeAscendRange(tr *btree.BTree[*dbItem], greaterOrEqual,
// Descend // Descend
func btreeDescend(tr *btree.BTree[*dbItem], iter func(item *dbItem) bool) { func btreeDescend(tr *btree.Generic[*dbItem], iter func(item *dbItem) bool) {
tr.Reverse(iter) tr.Reverse(iter)
} }
func btreeDescendGreaterThan(tr *btree.BTree[*dbItem], pivot *dbItem, func btreeDescendGreaterThan(tr *btree.Generic[*dbItem], pivot *dbItem,
iter func(item *dbItem) bool, iter func(item *dbItem) bool,
) { ) {
tr.Reverse(func(item *dbItem) bool { tr.Reverse(func(item *dbItem) bool {
@ -2351,7 +2348,7 @@ func btreeDescendGreaterThan(tr *btree.BTree[*dbItem], pivot *dbItem,
}) })
} }
func btreeDescendRange(tr *btree.BTree[*dbItem], lessOrEqual, func btreeDescendRange(tr *btree.Generic[*dbItem], lessOrEqual,
greaterThan *dbItem, iter func(item *dbItem) bool, greaterThan *dbItem, iter func(item *dbItem) bool,
) { ) {
tr.Descend(lessOrEqual, func(item *dbItem) bool { tr.Descend(lessOrEqual, func(item *dbItem) bool {
@ -2359,12 +2356,12 @@ func btreeDescendRange(tr *btree.BTree[*dbItem], lessOrEqual,
}) })
} }
func btreeDescendLessOrEqual(tr *btree.BTree[*dbItem], pivot *dbItem, func btreeDescendLessOrEqual(tr *btree.Generic[*dbItem], pivot *dbItem,
iter func(item *dbItem) bool, iter func(item *dbItem) bool,
) { ) {
tr.Descend(pivot, iter) tr.Descend(pivot, iter)
} }
func btreeNew(less func(a, b *dbItem) bool) *btree.BTree[*dbItem] { func btreeNew(less func(a, b *dbItem) bool) *btree.Generic[*dbItem] {
return btree.NewOptions(less, btree.Options{NoLocks: true}) return btree.NewGenericOptions(less, btree.Options{NoLocks: true})
} }

2
go.mod
View File

@ -4,7 +4,7 @@ go 1.18
require ( require (
github.com/tidwall/assert v0.1.0 github.com/tidwall/assert v0.1.0
github.com/tidwall/btree v0.7.2-0.20211218005449-cbb03286d2f2 github.com/tidwall/btree v1.1.1-0.20211224172409-40e5f803476c
github.com/tidwall/gjson v1.12.1 github.com/tidwall/gjson v1.12.1
github.com/tidwall/grect v0.1.4 github.com/tidwall/grect v0.1.4
github.com/tidwall/lotsa v1.0.2 github.com/tidwall/lotsa v1.0.2

4
go.sum
View File

@ -1,7 +1,7 @@
github.com/tidwall/assert v0.1.0 h1:aWcKyRBUAdLoVebxo95N7+YZVTFF/ASTr7BN4sLP6XI= github.com/tidwall/assert v0.1.0 h1:aWcKyRBUAdLoVebxo95N7+YZVTFF/ASTr7BN4sLP6XI=
github.com/tidwall/assert v0.1.0/go.mod h1:QLYtGyeqse53vuELQheYl9dngGCJQ+mTtlxcktb+Kj8= github.com/tidwall/assert v0.1.0/go.mod h1:QLYtGyeqse53vuELQheYl9dngGCJQ+mTtlxcktb+Kj8=
github.com/tidwall/btree v0.7.2-0.20211218005449-cbb03286d2f2 h1:ehSxDCH+l3iIecsbPBzed/grEc5R24bhLbJdU+QMbHQ= github.com/tidwall/btree v1.1.1-0.20211224172409-40e5f803476c h1:jSRtuFkVrUK4P3VUVYD4WssGige8x3iYKyDBW+FuIsA=
github.com/tidwall/btree v0.7.2-0.20211218005449-cbb03286d2f2/go.mod h1:LGm8L/DZjPLmeWGjv5kFrY8dL4uVhMmzmmLYmsObdKE= github.com/tidwall/btree v1.1.1-0.20211224172409-40e5f803476c/go.mod h1:LGm8L/DZjPLmeWGjv5kFrY8dL4uVhMmzmmLYmsObdKE=
github.com/tidwall/gjson v1.12.1 h1:ikuZsLdhr8Ws0IdROXUS1Gi4v9Z4pGqpX/CvJkxvfpo= github.com/tidwall/gjson v1.12.1 h1:ikuZsLdhr8Ws0IdROXUS1Gi4v9Z4pGqpX/CvJkxvfpo=
github.com/tidwall/gjson v1.12.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/gjson v1.12.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/grect v0.1.4 h1:dA3oIgNgWdSspFzn1kS4S/RDpZFLrIxAZOdJKjYapOg= github.com/tidwall/grect v0.1.4 h1:dA3oIgNgWdSspFzn1kS4S/RDpZFLrIxAZOdJKjYapOg=