Updated btree library

This commit is contained in:
tidwall 2021-08-23 07:33:57 -07:00
parent c8389fe52c
commit b7674349cf
4 changed files with 39 additions and 7 deletions

2
go.mod
View File

@ -15,7 +15,7 @@ require (
github.com/peterh/liner v1.2.1 github.com/peterh/liner v1.2.1
github.com/prometheus/client_golang v1.10.0 github.com/prometheus/client_golang v1.10.0
github.com/streadway/amqp v1.0.0 github.com/streadway/amqp v1.0.0
github.com/tidwall/btree v0.6.0 github.com/tidwall/btree v0.6.1
github.com/tidwall/buntdb v1.2.6 github.com/tidwall/buntdb v1.2.6
github.com/tidwall/geoindex v1.4.4 github.com/tidwall/geoindex v1.4.4
github.com/tidwall/geojson v1.3.0 github.com/tidwall/geojson v1.3.0

3
go.sum
View File

@ -410,8 +410,9 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tidwall/btree v0.4.2/go.mod h1:huei1BkDWJ3/sLXmO+bsCNELL+Bp2Kks9OLyQFkzvA8= github.com/tidwall/btree v0.4.2/go.mod h1:huei1BkDWJ3/sLXmO+bsCNELL+Bp2Kks9OLyQFkzvA8=
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/btree v0.6.0/go.mod h1:TzIRzen6yHbibdSfK6t8QimqbUnoxUSrZfeW7Uob0q4=
github.com/tidwall/btree v0.6.1 h1:75VVgBeviiDO+3g4U+7+BaNBNhNINxB0ULPT3fs9pMY=
github.com/tidwall/btree v0.6.1/go.mod h1:TzIRzen6yHbibdSfK6t8QimqbUnoxUSrZfeW7Uob0q4=
github.com/tidwall/buntdb v1.2.6 h1:eS0QSmzHfCKjxxYGh8eH6wnK5VLsJ7UjyyIr29JmnEg= github.com/tidwall/buntdb v1.2.6 h1:eS0QSmzHfCKjxxYGh8eH6wnK5VLsJ7UjyyIr29JmnEg=
github.com/tidwall/buntdb v1.2.6/go.mod h1:zpXqlA5D2772I4cTqV3ifr2AZihDgi8FV7xAQu6edfc= github.com/tidwall/buntdb v1.2.6/go.mod h1:zpXqlA5D2772I4cTqV3ifr2AZihDgi8FV7xAQu6edfc=
github.com/tidwall/cities v0.1.0 h1:CVNkmMf7NEC9Bvokf5GoSsArHCKRMTgLuubRTHnH0mE= github.com/tidwall/cities v0.1.0 h1:CVNkmMf7NEC9Bvokf5GoSsArHCKRMTgLuubRTHnH0mE=

View File

@ -296,7 +296,38 @@ func (n *node) scan(iter func(item interface{}) bool) bool {
// Get a value for key // Get a value for key
func (tr *BTree) Get(key interface{}) interface{} { func (tr *BTree) Get(key interface{}) interface{} {
return tr.GetHint(key, nil) // This operation is basically the same as calling:
// return tr.GetHint(key, nil)
// But here we inline the bsearch to avoid the hint logic and extra
// function call.
if tr.rlock() {
defer tr.runlock()
}
if tr.root == nil || key == nil {
return nil
}
depth := 0
n := tr.root
for {
low := int16(0)
high := n.numItems - 1
for low <= high {
mid := low + ((high+1)-low)/2
if !tr.less(key, n.items[mid]) {
low = mid + 1
} else {
high = mid - 1
}
}
if low > 0 && !tr.less(n.items[low-1], key) {
return n.items[low-1]
}
if n.leaf {
return nil
}
n = n.children[low]
depth++
}
} }
// GetHint gets a value for key using a path hint // GetHint gets a value for key using a path hint
@ -310,14 +341,14 @@ func (tr *BTree) GetHint(key interface{}, hint *PathHint) interface{} {
depth := 0 depth := 0
n := tr.root n := tr.root
for { for {
i, found := n.find(key, tr.less, hint, depth) index, found := n.find(key, tr.less, hint, depth)
if found { if found {
return n.items[i] return n.items[index]
} }
if n.leaf { if n.leaf {
return nil return nil
} }
n = n.children[i] n = n.children[index]
depth++ depth++
} }
} }

2
vendor/modules.txt vendored
View File

@ -162,7 +162,7 @@ github.com/rcrowley/go-metrics
# github.com/streadway/amqp v1.0.0 # github.com/streadway/amqp v1.0.0
## explicit ## explicit
github.com/streadway/amqp github.com/streadway/amqp
# github.com/tidwall/btree v0.6.0 # github.com/tidwall/btree v0.6.1
## explicit ## explicit
github.com/tidwall/btree github.com/tidwall/btree
# github.com/tidwall/buntdb v1.2.6 # github.com/tidwall/buntdb v1.2.6