forked from External/ergo
upgrade buntdb
This commit is contained in:
parent
1389d89a9b
commit
c5a9916302
15 changed files with 519 additions and 288 deletions
43
vendor/github.com/tidwall/btree/PATH_HINT.md
generated
vendored
Normal file
43
vendor/github.com/tidwall/btree/PATH_HINT.md
generated
vendored
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
# B-tree Path Hints
|
||||
|
||||
I use a thing I call path hints in my B-tree [C](https://github.com/tidwall/btree.c) and [Go](https://github.com/tidwall/btree) implementations. It's a search optimization.
|
||||
|
||||
## The B-tree
|
||||
|
||||
A standard [B-tree](https://en.wikipedia.org/wiki/B-tree) is an ordered tree-based data structure that stores its items in nodes. The B-tree has a single root node, which may have children nodes, and those children nodes may also have children nodes.
|
||||
|
||||
<img width="322" alt="image" src="https://user-images.githubusercontent.com/1156077/127664015-14ca38bb-1a3b-4d2f-80ff-27be0bd3d886.png">
|
||||
|
||||
Searching for items in a B-tree is fast. [O(log N)](https://en.wikipedia.org/wiki/Big_O_notation) to be exact.
|
||||
This is because the [binary search algorithm](https://en.wikipedia.org/wiki/Binary_search_algorithm) is used.
|
||||
|
||||
A binary search works by first comparing the item at the middle-most index of the root node with the target item.
|
||||
If the middle item is greater than the target item, then it divides the node in two and does the binary search on the left part of the node. If the middle is less, it searches the right part. And so on. If the target item is found, then the search stop. If the item is not found, then the search is passed to the child node at the appropriate index. This traversal terminates when item is found or there are no more child nodes.
|
||||
|
||||
<img width="600" alt="image" src="https://user-images.githubusercontent.com/1156077/127664822-6ab4f8f6-8ab5-477e-8e17-f52346f02819.png">
|
||||
|
||||
## The Path
|
||||
|
||||
Each index is a component of the path to the item (or where the item should be stored, if it does not exist in the tree).
|
||||
|
||||
Take the first example image. The item 9 is at path “1/0”. The item 16 is at path “1”. The item 21 is at path “2/1”. The item 5 is at path “0/2”.
|
||||
|
||||
## The Path Hint
|
||||
|
||||
A Path Hint is a predefined path that is provided to B-tree operations. It’s just a hint that says, “Hey B-tree, instead of starting your binary search with the middle index, start with what I provide you. My path may be wrong, and if so please provide me with the correct path so I get it right the next time.”
|
||||
|
||||
I’ve found using path hints can lead to a little performance increase of 150% - 300%. This is because in real-world cases the items that I’m working with are usually nearby each other in the tree.
|
||||
|
||||
Take for example inserting a group of timeseries points. They may often be received as chucks of near-contiguous items.
|
||||
Or, I'm sequentially inserting an ordered group of rows somewhere in the middle of a table.
|
||||
Or, I have a Redis-style key/value store, where the keys look have the common structure “user:98512:name”, “user:98512:email”, and I want to update a bunch of values for specified user.
|
||||
Using a path hint may help to avoid the unnecessary binary searching in each of these examples.
|
||||
|
||||
While I may see a 3x boost in when the path hint is right on, I'll only see around 5% decrease when the path hint is totally wrong.
|
||||
|
||||
## Using a Path Hint
|
||||
|
||||
For single-threaded programs, it’s possible to use one shared path hint per B-tree for the life of the program.
|
||||
For multi-threaded programs, I find it best to use one path hint per B-tree , per thread.
|
||||
For server-client programs, one path hint per B-tree, per client should suffice.
|
||||
|
||||
38
vendor/github.com/tidwall/btree/README.md
generated
vendored
38
vendor/github.com/tidwall/btree/README.md
generated
vendored
|
|
@ -9,7 +9,7 @@ An [efficient](#performance) [B-tree](https://en.wikipedia.org/wiki/B-tree) impl
|
|||
- `Copy()` method with copy-on-write support.
|
||||
- Fast bulk loading for pre-ordered data using the `Load()` method.
|
||||
- All operations are thread-safe.
|
||||
- Path hinting optimization for operations with nearby keys.
|
||||
- [Path hinting](PATH_HINT.md) optimization for operations with nearby keys.
|
||||
|
||||
## Installing
|
||||
|
||||
|
|
@ -155,7 +155,7 @@ DeleteHint(item, *hint) # delete an item
|
|||
|
||||
This implementation was designed with performance in mind.
|
||||
|
||||
The following benchmarks were run on my 2019 Macbook Pro (2.4 GHz 8-Core Intel Core i9) using Go 1.15.3. The items are simple 8-byte ints.
|
||||
The following benchmarks were run on my 2019 Macbook Pro (2.4 GHz 8-Core Intel Core i9) using Go 1.16.5. The items are simple 8-byte ints.
|
||||
|
||||
- `google`: The [google/btree](https://github.com/google/btree) package
|
||||
- `tidwall`: The [tidwall/btree](https://github.com/tidwall/btree) package
|
||||
|
|
@ -163,29 +163,29 @@ The following benchmarks were run on my 2019 Macbook Pro (2.4 GHz 8-Core Intel C
|
|||
|
||||
```
|
||||
** sequential set **
|
||||
google: set-seq 1,000,000 ops in 160ms, 6,262,097/sec, 159 ns/op, 31.0 MB, 32 bytes/op
|
||||
tidwall: set-seq 1,000,000 ops in 142ms, 7,020,721/sec, 142 ns/op, 36.6 MB, 38 bytes/op
|
||||
tidwall: set-seq-hint 1,000,000 ops in 87ms, 11,503,315/sec, 86 ns/op, 36.6 MB, 38 bytes/op
|
||||
tidwall: load-seq 1,000,000 ops in 37ms, 27,177,242/sec, 36 ns/op, 36.6 MB, 38 bytes/op
|
||||
go-arr: append 1,000,000 ops in 49ms, 20,574,760/sec, 48 ns/op
|
||||
google: set-seq 1,000,000 ops in 163ms, 6,140,597/sec, 162 ns/op, 30.9 MB, 32 bytes/op
|
||||
tidwall: set-seq 1,000,000 ops in 141ms, 7,075,240/sec, 141 ns/op, 36.6 MB, 38 bytes/op
|
||||
tidwall: set-seq-hint 1,000,000 ops in 79ms, 12,673,902/sec, 78 ns/op, 36.6 MB, 38 bytes/op
|
||||
tidwall: load-seq 1,000,000 ops in 40ms, 24,887,293/sec, 40 ns/op, 36.6 MB, 38 bytes/op
|
||||
go-arr: append 1,000,000 ops in 51ms, 19,617,269/sec, 50 ns/op
|
||||
|
||||
** random set **
|
||||
google: set-rand 1,000,000 ops in 606ms, 1,649,921/sec, 606 ns/op, 21.5 MB, 22 bytes/op
|
||||
tidwall: set-rand 1,000,000 ops in 543ms, 1,841,590/sec, 543 ns/op, 26.7 MB, 27 bytes/op
|
||||
tidwall: set-rand-hint 1,000,000 ops in 573ms, 1,745,624/sec, 572 ns/op, 26.4 MB, 27 bytes/op
|
||||
tidwall: set-again 1,000,000 ops in 452ms, 2,212,581/sec, 451 ns/op, 27.1 MB, 28 bytes/op
|
||||
tidwall: set-after-copy 1,000,000 ops in 472ms, 2,117,457/sec, 472 ns/op, 27.9 MB, 29 bytes/op
|
||||
tidwall: load-rand 1,000,000 ops in 551ms, 1,816,498/sec, 550 ns/op, 26.1 MB, 27 bytes/op
|
||||
google: set-rand 1,000,000 ops in 666ms, 1,501,583/sec, 665 ns/op, 21.5 MB, 22 bytes/op
|
||||
tidwall: set-rand 1,000,000 ops in 569ms, 1,756,845/sec, 569 ns/op, 26.7 MB, 27 bytes/op
|
||||
tidwall: set-rand-hint 1,000,000 ops in 670ms, 1,491,637/sec, 670 ns/op, 26.4 MB, 27 bytes/op
|
||||
tidwall: set-again 1,000,000 ops in 488ms, 2,050,667/sec, 487 ns/op, 27.1 MB, 28 bytes/op
|
||||
tidwall: set-after-copy 1,000,000 ops in 494ms, 2,022,980/sec, 494 ns/op, 27.9 MB, 29 bytes/op
|
||||
tidwall: load-rand 1,000,000 ops in 594ms, 1,682,937/sec, 594 ns/op, 26.1 MB, 27 bytes/op
|
||||
|
||||
** sequential get **
|
||||
google: get-seq 1,000,000 ops in 133ms, 7,497,604/sec, 133 ns/op
|
||||
tidwall: get-seq 1,000,000 ops in 110ms, 9,082,972/sec, 110 ns/op
|
||||
tidwall: get-seq-hint 1,000,000 ops in 55ms, 18,289,945/sec, 54 ns/op
|
||||
google: get-seq 1,000,000 ops in 141ms, 7,078,690/sec, 141 ns/op
|
||||
tidwall: get-seq 1,000,000 ops in 124ms, 8,075,925/sec, 123 ns/op
|
||||
tidwall: get-seq-hint 1,000,000 ops in 40ms, 25,142,979/sec, 39 ns/op
|
||||
|
||||
** random get **
|
||||
google: get-rand 1,000,000 ops in 149ms, 6,704,337/sec, 149 ns/op
|
||||
tidwall: get-rand 1,000,000 ops in 131ms, 7,616,296/sec, 131 ns/op
|
||||
tidwall: get-rand-hint 1,000,000 ops in 216ms, 4,632,532/sec, 215 ns/op
|
||||
google: get-rand 1,000,000 ops in 152ms, 6,593,518/sec, 151 ns/op
|
||||
tidwall: get-rand 1,000,000 ops in 128ms, 7,783,293/sec, 128 ns/op
|
||||
tidwall: get-rand-hint 1,000,000 ops in 135ms, 7,403,823/sec, 135 ns/op
|
||||
```
|
||||
|
||||
*You can find the benchmark utility at [tidwall/btree-benchmark](https://github.com/tidwall/btree-benchmark)*
|
||||
|
|
|
|||
392
vendor/github.com/tidwall/btree/btree.go
generated
vendored
392
vendor/github.com/tidwall/btree/btree.go
generated
vendored
|
|
@ -4,9 +4,11 @@
|
|||
|
||||
package btree
|
||||
|
||||
import "sync"
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
const maxItems = 255
|
||||
const maxItems = 255 // max items per node. max children is +1
|
||||
const minItems = maxItems * 40 / 100
|
||||
|
||||
type cow struct {
|
||||
|
|
@ -17,18 +19,19 @@ type node struct {
|
|||
cow *cow
|
||||
leaf bool
|
||||
numItems int16
|
||||
count int
|
||||
items [maxItems]interface{}
|
||||
children *[maxItems + 1]*node
|
||||
}
|
||||
|
||||
// BTree is an ordered set items
|
||||
type BTree struct {
|
||||
mu *sync.RWMutex
|
||||
cow *cow
|
||||
root *node
|
||||
length int
|
||||
less func(a, b interface{}) bool
|
||||
lnode *node
|
||||
mu *sync.RWMutex
|
||||
cow *cow
|
||||
root *node
|
||||
count int
|
||||
less func(a, b interface{}) bool
|
||||
locks bool
|
||||
}
|
||||
|
||||
func (tr *BTree) newNode(leaf bool) *node {
|
||||
|
|
@ -43,17 +46,32 @@ func (tr *BTree) newNode(leaf bool) *node {
|
|||
// PathHint is a utility type used with the *Hint() functions. Hints provide
|
||||
// faster operations for clustered keys.
|
||||
type PathHint struct {
|
||||
used [8]bool
|
||||
path [8]uint8
|
||||
}
|
||||
|
||||
// New returns a new BTree
|
||||
func New(less func(a, b interface{}) bool) *BTree {
|
||||
return newBTree(less, true)
|
||||
}
|
||||
|
||||
// NewNonConcurrent returns a new BTree which is not safe for concurrent
|
||||
// write operations by multiple goroutines.
|
||||
//
|
||||
// This is useful for when you do not need the BTree to manage the locking,
|
||||
// but would rather do it yourself.
|
||||
func NewNonConcurrent(less func(a, b interface{}) bool) *BTree {
|
||||
return newBTree(less, false)
|
||||
}
|
||||
|
||||
func newBTree(less func(a, b interface{}) bool, locks bool) *BTree {
|
||||
if less == nil {
|
||||
panic("nil less")
|
||||
}
|
||||
tr := new(BTree)
|
||||
tr.mu = new(sync.RWMutex)
|
||||
tr.less = less
|
||||
tr.locks = locks
|
||||
return tr
|
||||
}
|
||||
|
||||
|
|
@ -68,18 +86,32 @@ func (n *node) find(key interface{}, less func(a, b interface{}) bool,
|
|||
) (index int16, found bool) {
|
||||
low := int16(0)
|
||||
high := n.numItems - 1
|
||||
if hint != nil && depth < 8 {
|
||||
if hint != nil && depth < 8 && hint.used[depth] {
|
||||
index = int16(hint.path[depth])
|
||||
if index > n.numItems-1 {
|
||||
if index >= n.numItems {
|
||||
// tail item
|
||||
if less(n.items[n.numItems-1], key) {
|
||||
if less(key, n.items[n.numItems-1]) {
|
||||
index = n.numItems - 1
|
||||
found = true
|
||||
goto path_match
|
||||
} else {
|
||||
index = n.numItems
|
||||
goto path_match
|
||||
}
|
||||
}
|
||||
index = n.numItems - 1
|
||||
}
|
||||
if less(key, n.items[index]) {
|
||||
if index == 0 || less(n.items[index-1], key) {
|
||||
goto path_match
|
||||
}
|
||||
high = index - 1
|
||||
} else if less(n.items[index], key) {
|
||||
low = index + 1
|
||||
} else {
|
||||
found = true
|
||||
goto done
|
||||
goto path_match
|
||||
}
|
||||
}
|
||||
for low <= high {
|
||||
|
|
@ -97,13 +129,16 @@ func (n *node) find(key interface{}, less func(a, b interface{}) bool,
|
|||
index = low
|
||||
found = false
|
||||
}
|
||||
done:
|
||||
if hint != nil && depth < 8 {
|
||||
if n.leaf && found {
|
||||
hint.path[depth] = byte(index + 1)
|
||||
} else {
|
||||
hint.path[depth] = byte(index)
|
||||
}
|
||||
if hint == nil || depth >= 8 {
|
||||
return index, found
|
||||
}
|
||||
|
||||
path_match:
|
||||
hint.used[depth] = true
|
||||
if n.leaf && found {
|
||||
hint.path[depth] = byte(index + 1)
|
||||
} else {
|
||||
hint.path[depth] = byte(index)
|
||||
}
|
||||
return index, found
|
||||
}
|
||||
|
|
@ -113,10 +148,10 @@ func (tr *BTree) SetHint(item interface{}, hint *PathHint) (prev interface{}) {
|
|||
if item == nil {
|
||||
panic("nil item")
|
||||
}
|
||||
tr.mu.Lock()
|
||||
prev = tr.setHint(item, hint)
|
||||
tr.mu.Unlock()
|
||||
return prev
|
||||
if tr.lock() {
|
||||
defer tr.unlock()
|
||||
}
|
||||
return tr.setHint(item, hint)
|
||||
}
|
||||
|
||||
func (tr *BTree) setHint(item interface{}, hint *PathHint) (prev interface{}) {
|
||||
|
|
@ -124,14 +159,14 @@ func (tr *BTree) setHint(item interface{}, hint *PathHint) (prev interface{}) {
|
|||
tr.root = tr.newNode(true)
|
||||
tr.root.items[0] = item
|
||||
tr.root.numItems = 1
|
||||
tr.length = 1
|
||||
tr.root.count = 1
|
||||
tr.count = 1
|
||||
return
|
||||
}
|
||||
prev = tr.nodeSet(&tr.root, item, tr.less, hint, 0)
|
||||
if prev != nil {
|
||||
return prev
|
||||
}
|
||||
tr.lnode = nil
|
||||
if tr.root.numItems == maxItems {
|
||||
n := tr.cowLoad(&tr.root)
|
||||
right, median := tr.nodeSplit(n)
|
||||
|
|
@ -140,8 +175,9 @@ func (tr *BTree) setHint(item interface{}, hint *PathHint) (prev interface{}) {
|
|||
tr.root.items[0] = median
|
||||
tr.root.children[1] = right
|
||||
tr.root.numItems = 1
|
||||
tr.root.count = n.count + 1 + right.count
|
||||
}
|
||||
tr.length++
|
||||
tr.count++
|
||||
return prev
|
||||
}
|
||||
|
||||
|
|
@ -167,10 +203,25 @@ func (tr *BTree) nodeSplit(n *node) (right *node, median interface{}) {
|
|||
n.items[i] = nil
|
||||
}
|
||||
n.numItems = maxItems / 2
|
||||
// update counts
|
||||
n.updateCount()
|
||||
right.updateCount()
|
||||
return right, median
|
||||
}
|
||||
|
||||
//go:noinline
|
||||
func (n *node) updateCount() {
|
||||
n.count = int(n.numItems)
|
||||
if !n.leaf {
|
||||
for i := 0; i <= int(n.numItems); i++ {
|
||||
n.count += n.children[i].count
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// This operation should not be inlined because it's expensive and rarely
|
||||
// called outside of heavy copy-on-write situations. Marking it "noinline"
|
||||
// allows for the parent cowLoad to be inlined.
|
||||
// go:noinline
|
||||
func (tr *BTree) copy(n *node) *node {
|
||||
n2 := *n
|
||||
n2.cow = tr.cow
|
||||
|
|
@ -182,7 +233,7 @@ func (tr *BTree) copy(n *node) *node {
|
|||
return &n2
|
||||
}
|
||||
|
||||
// cowLoad loaded the provide node and, if needed, performs a copy-on-write.
|
||||
// cowLoad loads the provide node and, if needed, performs a copy-on-write.
|
||||
func (tr *BTree) cowLoad(cn **node) *node {
|
||||
if (*cn).cow != tr.cow {
|
||||
*cn = tr.copy(*cn)
|
||||
|
|
@ -204,6 +255,7 @@ func (tr *BTree) nodeSet(cn **node, item interface{},
|
|||
copy(n.items[i+1:n.numItems+1], n.items[i:n.numItems])
|
||||
n.items[i] = item
|
||||
n.numItems++
|
||||
n.count++
|
||||
return nil
|
||||
}
|
||||
prev = tr.nodeSet(&n.children[i], item, less, hint, depth+1)
|
||||
|
|
@ -218,7 +270,8 @@ func (tr *BTree) nodeSet(cn **node, item interface{},
|
|||
n.children[i+1] = right
|
||||
n.numItems++
|
||||
}
|
||||
return prev
|
||||
n.count++
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *node) scan(iter func(item interface{}) bool) bool {
|
||||
|
|
@ -248,8 +301,9 @@ func (tr *BTree) Get(key interface{}) interface{} {
|
|||
|
||||
// GetHint gets a value for key using a path hint
|
||||
func (tr *BTree) GetHint(key interface{}, hint *PathHint) interface{} {
|
||||
tr.mu.RLock()
|
||||
defer tr.mu.RUnlock()
|
||||
if tr.rlock() {
|
||||
defer tr.runlock()
|
||||
}
|
||||
if tr.root == nil || key == nil {
|
||||
return nil
|
||||
}
|
||||
|
|
@ -270,7 +324,7 @@ func (tr *BTree) GetHint(key interface{}, hint *PathHint) interface{} {
|
|||
|
||||
// Len returns the number of items in the tree
|
||||
func (tr *BTree) Len() int {
|
||||
return tr.length
|
||||
return tr.count
|
||||
}
|
||||
|
||||
// Delete a value for a key
|
||||
|
|
@ -280,10 +334,10 @@ func (tr *BTree) Delete(key interface{}) interface{} {
|
|||
|
||||
// DeleteHint deletes a value for a key using a path hint
|
||||
func (tr *BTree) DeleteHint(key interface{}, hint *PathHint) interface{} {
|
||||
tr.mu.Lock()
|
||||
prev := tr.deleteHint(key, hint)
|
||||
tr.mu.Unlock()
|
||||
return prev
|
||||
if tr.lock() {
|
||||
defer tr.unlock()
|
||||
}
|
||||
return tr.deleteHint(key, hint)
|
||||
}
|
||||
|
||||
func (tr *BTree) deleteHint(key interface{}, hint *PathHint) interface{} {
|
||||
|
|
@ -294,12 +348,11 @@ func (tr *BTree) deleteHint(key interface{}, hint *PathHint) interface{} {
|
|||
if prev == nil {
|
||||
return nil
|
||||
}
|
||||
tr.lnode = nil
|
||||
if tr.root.numItems == 0 && !tr.root.leaf {
|
||||
tr.root = tr.root.children[0]
|
||||
}
|
||||
tr.length--
|
||||
if tr.length == 0 {
|
||||
tr.count--
|
||||
if tr.count == 0 {
|
||||
tr.root = nil
|
||||
}
|
||||
return prev
|
||||
|
|
@ -323,6 +376,7 @@ func (tr *BTree) delete(cn **node, max bool, key interface{},
|
|||
copy(n.items[i:], n.items[i+1:n.numItems])
|
||||
n.items[n.numItems-1] = nil
|
||||
n.numItems--
|
||||
n.count--
|
||||
return prev
|
||||
}
|
||||
return nil
|
||||
|
|
@ -344,6 +398,7 @@ func (tr *BTree) delete(cn **node, max bool, key interface{},
|
|||
if prev == nil {
|
||||
return nil
|
||||
}
|
||||
n.count--
|
||||
if n.children[i].numItems >= minItems {
|
||||
return prev
|
||||
}
|
||||
|
|
@ -364,10 +419,11 @@ func (tr *BTree) delete(cn **node, max bool, key interface{},
|
|||
n.children[i+1].children[:n.children[i+1].numItems+1])
|
||||
}
|
||||
n.children[i].numItems += n.children[i+1].numItems + 1
|
||||
n.children[i].count += n.children[i+1].count + 1
|
||||
copy(n.items[i:], n.items[i+1:n.numItems])
|
||||
copy(n.children[i+1:], n.children[i+2:n.numItems+1])
|
||||
n.items[n.numItems] = nil
|
||||
n.children[n.numItems+1] = nil
|
||||
n.items[n.numItems-1] = nil
|
||||
n.children[n.numItems] = nil
|
||||
n.numItems--
|
||||
} else if n.children[i].numItems > n.children[i+1].numItems {
|
||||
// move left -> right
|
||||
|
|
@ -381,30 +437,42 @@ func (tr *BTree) delete(cn **node, max bool, key interface{},
|
|||
if !n.children[0].leaf {
|
||||
n.children[i+1].children[0] =
|
||||
n.children[i].children[n.children[i].numItems]
|
||||
n.children[i+1].count += n.children[i+1].children[0].count
|
||||
}
|
||||
n.children[i+1].numItems++
|
||||
n.children[i+1].count++
|
||||
n.items[i] = n.children[i].items[n.children[i].numItems-1]
|
||||
n.children[i].items[n.children[i].numItems-1] = nil
|
||||
if !n.children[0].leaf {
|
||||
n.children[i].children[n.children[i].numItems] = nil
|
||||
n.children[i].count -= n.children[i+1].children[0].count
|
||||
}
|
||||
n.children[i].numItems--
|
||||
n.children[i].count--
|
||||
} else {
|
||||
// move right -> left
|
||||
// move left <- right
|
||||
n.children[i].items[n.children[i].numItems] = n.items[i]
|
||||
if !n.children[0].leaf {
|
||||
n.children[i].children[n.children[i].numItems+1] =
|
||||
n.children[i+1].children[0]
|
||||
n.children[i].count +=
|
||||
n.children[i].children[n.children[i].numItems+1].count
|
||||
}
|
||||
n.children[i].numItems++
|
||||
n.children[i].count++
|
||||
n.items[i] = n.children[i+1].items[0]
|
||||
copy(n.children[i+1].items[:],
|
||||
n.children[i+1].items[1:n.children[i+1].numItems])
|
||||
n.children[i+1].items[n.children[i+1].numItems-1] = nil
|
||||
if !n.children[0].leaf {
|
||||
copy(n.children[i+1].children[:],
|
||||
n.children[i+1].children[1:n.children[i+1].numItems+1])
|
||||
n.children[i+1].children[n.children[i+1].numItems] = nil
|
||||
n.children[i+1].count -=
|
||||
n.children[i].children[n.children[i].numItems].count
|
||||
}
|
||||
n.children[i+1].numItems--
|
||||
n.children[i+1].count--
|
||||
}
|
||||
return prev
|
||||
}
|
||||
|
|
@ -413,8 +481,9 @@ func (tr *BTree) delete(cn **node, max bool, key interface{},
|
|||
// Pass nil for pivot to scan all item in ascending order
|
||||
// Return false to stop iterating
|
||||
func (tr *BTree) Ascend(pivot interface{}, iter func(item interface{}) bool) {
|
||||
tr.mu.RLock()
|
||||
defer tr.mu.RUnlock()
|
||||
if tr.rlock() {
|
||||
defer tr.runlock()
|
||||
}
|
||||
if tr.root == nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -476,8 +545,9 @@ func (n *node) reverse(iter func(item interface{}) bool) bool {
|
|||
// Pass nil for pivot to scan all item in descending order
|
||||
// Return false to stop iterating
|
||||
func (tr *BTree) Descend(pivot interface{}, iter func(item interface{}) bool) {
|
||||
tr.mu.RLock()
|
||||
defer tr.mu.RUnlock()
|
||||
if tr.rlock() {
|
||||
defer tr.runlock()
|
||||
}
|
||||
if tr.root == nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -518,40 +588,46 @@ func (tr *BTree) Load(item interface{}) interface{} {
|
|||
if item == nil {
|
||||
panic("nil item")
|
||||
}
|
||||
tr.mu.Lock()
|
||||
defer tr.mu.Unlock()
|
||||
|
||||
// Load does not need a cowGrid because the Copy operation sets the
|
||||
// lnode to nil.
|
||||
|
||||
if tr.lnode != nil && tr.lnode.numItems < maxItems-2 {
|
||||
if tr.less(tr.lnode.items[tr.lnode.numItems-1], item) {
|
||||
tr.lnode.items[tr.lnode.numItems] = item
|
||||
tr.lnode.numItems++
|
||||
tr.length++
|
||||
return nil
|
||||
}
|
||||
if tr.lock() {
|
||||
defer tr.unlock()
|
||||
}
|
||||
prev := tr.setHint(item, nil)
|
||||
if prev != nil {
|
||||
return prev
|
||||
if tr.root == nil {
|
||||
return tr.setHint(item, nil)
|
||||
}
|
||||
n := tr.root
|
||||
n := tr.cowLoad(&tr.root)
|
||||
for {
|
||||
n.count++ // optimistically update counts
|
||||
if n.leaf {
|
||||
if n.numItems < maxItems-2 {
|
||||
if tr.less(n.items[n.numItems-1], item) {
|
||||
n.items[n.numItems] = item
|
||||
n.numItems++
|
||||
tr.count++
|
||||
return nil
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
n = tr.cowLoad(&n.children[n.numItems])
|
||||
}
|
||||
// revert the counts
|
||||
n = tr.root
|
||||
for {
|
||||
n.count--
|
||||
if n.leaf {
|
||||
tr.lnode = n
|
||||
break
|
||||
}
|
||||
n = n.children[n.numItems]
|
||||
}
|
||||
return nil
|
||||
return tr.setHint(item, nil)
|
||||
}
|
||||
|
||||
// Min returns the minimum item in tree.
|
||||
// Returns nil if the tree has no items.
|
||||
func (tr *BTree) Min() interface{} {
|
||||
tr.mu.RLock()
|
||||
defer tr.mu.RUnlock()
|
||||
if tr.rlock() {
|
||||
defer tr.runlock()
|
||||
}
|
||||
if tr.root == nil {
|
||||
return nil
|
||||
}
|
||||
|
|
@ -567,8 +643,9 @@ func (tr *BTree) Min() interface{} {
|
|||
// Max returns the maximum item in tree.
|
||||
// Returns nil if the tree has no items.
|
||||
func (tr *BTree) Max() interface{} {
|
||||
tr.mu.RLock()
|
||||
defer tr.mu.RUnlock()
|
||||
if tr.rlock() {
|
||||
defer tr.runlock()
|
||||
}
|
||||
if tr.root == nil {
|
||||
return nil
|
||||
}
|
||||
|
|
@ -584,65 +661,179 @@ func (tr *BTree) Max() interface{} {
|
|||
// PopMin removes the minimum item in tree and returns it.
|
||||
// Returns nil if the tree has no items.
|
||||
func (tr *BTree) PopMin() interface{} {
|
||||
tr.mu.Lock()
|
||||
defer tr.mu.Unlock()
|
||||
if tr.lock() {
|
||||
defer tr.unlock()
|
||||
}
|
||||
if tr.root == nil {
|
||||
return nil
|
||||
}
|
||||
tr.lnode = nil
|
||||
n := tr.cowLoad(&tr.root)
|
||||
var item interface{}
|
||||
for {
|
||||
n.count-- // optimistically update counts
|
||||
if n.leaf {
|
||||
item := n.items[0]
|
||||
item = n.items[0]
|
||||
if n.numItems == minItems {
|
||||
return tr.deleteHint(item, nil)
|
||||
break
|
||||
}
|
||||
copy(n.items[:], n.items[1:])
|
||||
n.items[n.numItems-1] = nil
|
||||
n.numItems--
|
||||
tr.length--
|
||||
if tr.length == 0 {
|
||||
tr.count--
|
||||
if tr.count == 0 {
|
||||
tr.root = nil
|
||||
}
|
||||
return item
|
||||
}
|
||||
n = tr.cowLoad(&n.children[0])
|
||||
}
|
||||
// revert the counts
|
||||
n = tr.root
|
||||
for {
|
||||
n.count++
|
||||
if n.leaf {
|
||||
break
|
||||
}
|
||||
n = n.children[0]
|
||||
}
|
||||
return tr.deleteHint(item, nil)
|
||||
}
|
||||
|
||||
// PopMax removes the minimum item in tree and returns it.
|
||||
// Returns nil if the tree has no items.
|
||||
func (tr *BTree) PopMax() interface{} {
|
||||
tr.mu.Lock()
|
||||
defer tr.mu.Unlock()
|
||||
if tr.lock() {
|
||||
defer tr.unlock()
|
||||
}
|
||||
if tr.root == nil {
|
||||
return nil
|
||||
}
|
||||
tr.lnode = nil
|
||||
n := tr.cowLoad(&tr.root)
|
||||
var item interface{}
|
||||
for {
|
||||
n.count-- // optimistically update counts
|
||||
if n.leaf {
|
||||
item := n.items[n.numItems-1]
|
||||
item = n.items[n.numItems-1]
|
||||
if n.numItems == minItems {
|
||||
return tr.deleteHint(item, nil)
|
||||
break
|
||||
}
|
||||
n.items[n.numItems-1] = nil
|
||||
n.numItems--
|
||||
tr.length--
|
||||
if tr.length == 0 {
|
||||
tr.count--
|
||||
if tr.count == 0 {
|
||||
tr.root = nil
|
||||
}
|
||||
return item
|
||||
}
|
||||
n = tr.cowLoad(&n.children[n.numItems])
|
||||
}
|
||||
// revert the counts
|
||||
n = tr.root
|
||||
for {
|
||||
n.count++
|
||||
if n.leaf {
|
||||
break
|
||||
}
|
||||
n = n.children[n.numItems]
|
||||
}
|
||||
return tr.deleteHint(item, nil)
|
||||
}
|
||||
|
||||
// GetAt returns the value at index.
|
||||
// Return nil if the tree is empty or the index is out of bounds.
|
||||
func (tr *BTree) GetAt(index int) interface{} {
|
||||
if tr.rlock() {
|
||||
defer tr.runlock()
|
||||
}
|
||||
if tr.root == nil || index < 0 || index >= tr.count {
|
||||
return nil
|
||||
}
|
||||
n := tr.root
|
||||
for {
|
||||
if n.leaf {
|
||||
return n.items[index]
|
||||
}
|
||||
i := 0
|
||||
for ; i < int(n.numItems); i++ {
|
||||
if index < n.children[i].count {
|
||||
break
|
||||
} else if index == n.children[i].count {
|
||||
return n.items[i]
|
||||
}
|
||||
index -= n.children[i].count + 1
|
||||
}
|
||||
n = n.children[i]
|
||||
}
|
||||
}
|
||||
|
||||
// DeleteAt deletes the item at index.
|
||||
// Return nil if the tree is empty or the index is out of bounds.
|
||||
func (tr *BTree) DeleteAt(index int) interface{} {
|
||||
if tr.lock() {
|
||||
defer tr.unlock()
|
||||
}
|
||||
if tr.root == nil || index < 0 || index >= tr.count {
|
||||
return nil
|
||||
}
|
||||
var pathbuf [8]uint8 // track the path
|
||||
path := pathbuf[:0]
|
||||
var item interface{}
|
||||
n := tr.cowLoad(&tr.root)
|
||||
outer:
|
||||
for {
|
||||
n.count-- // optimistically update counts
|
||||
if n.leaf {
|
||||
// the index is the item position
|
||||
item = n.items[index]
|
||||
if n.numItems == minItems {
|
||||
path = append(path, uint8(index))
|
||||
break outer
|
||||
}
|
||||
copy(n.items[index:], n.items[index+1:n.numItems])
|
||||
n.items[n.numItems-1] = nil
|
||||
n.numItems--
|
||||
tr.count--
|
||||
if tr.count == 0 {
|
||||
tr.root = nil
|
||||
}
|
||||
return item
|
||||
}
|
||||
i := 0
|
||||
for ; i < int(n.numItems); i++ {
|
||||
if index < n.children[i].count {
|
||||
break
|
||||
} else if index == n.children[i].count {
|
||||
item = n.items[i]
|
||||
path = append(path, uint8(i))
|
||||
break outer
|
||||
}
|
||||
index -= n.children[i].count + 1
|
||||
}
|
||||
path = append(path, uint8(i))
|
||||
n = tr.cowLoad(&n.children[i])
|
||||
}
|
||||
// revert the counts
|
||||
var hint PathHint
|
||||
n = tr.root
|
||||
for i := 0; i < len(path); i++ {
|
||||
if i < len(hint.path) {
|
||||
hint.path[i] = path[i]
|
||||
hint.used[i] = true
|
||||
}
|
||||
n.count++
|
||||
if !n.leaf {
|
||||
n = n.children[uint8(path[i])]
|
||||
}
|
||||
}
|
||||
return tr.deleteHint(item, &hint)
|
||||
}
|
||||
|
||||
// Height returns the height of the tree.
|
||||
// Returns zero if tree has no items.
|
||||
func (tr *BTree) Height() int {
|
||||
tr.mu.RLock()
|
||||
defer tr.mu.RUnlock()
|
||||
if tr.rlock() {
|
||||
defer tr.runlock()
|
||||
}
|
||||
var height int
|
||||
if tr.root != nil {
|
||||
n := tr.root
|
||||
|
|
@ -660,8 +851,9 @@ func (tr *BTree) Height() int {
|
|||
// Walk iterates over all items in tree, in order.
|
||||
// The items param will contain one or more items.
|
||||
func (tr *BTree) Walk(iter func(item []interface{})) {
|
||||
tr.mu.RLock()
|
||||
defer tr.mu.RUnlock()
|
||||
if tr.rlock() {
|
||||
defer tr.runlock()
|
||||
}
|
||||
if tr.root != nil {
|
||||
tr.root.walk(iter)
|
||||
}
|
||||
|
|
@ -682,12 +874,34 @@ func (n *node) walk(iter func(item []interface{})) {
|
|||
// Copy the tree. This operation is very fast because it only performs a
|
||||
// shadowed copy.
|
||||
func (tr *BTree) Copy() *BTree {
|
||||
tr.mu.Lock()
|
||||
tr.lnode = nil
|
||||
if tr.lock() {
|
||||
defer tr.unlock()
|
||||
}
|
||||
tr.cow = new(cow)
|
||||
tr2 := *tr
|
||||
tr2.mu = new(sync.RWMutex)
|
||||
tr2.cow = new(cow)
|
||||
tr.mu.Unlock()
|
||||
return &tr2
|
||||
}
|
||||
|
||||
func (tr *BTree) lock() bool {
|
||||
if tr.locks {
|
||||
tr.mu.Lock()
|
||||
}
|
||||
return tr.locks
|
||||
}
|
||||
|
||||
func (tr *BTree) unlock() {
|
||||
tr.mu.Unlock()
|
||||
}
|
||||
|
||||
func (tr *BTree) rlock() bool {
|
||||
if tr.locks {
|
||||
tr.mu.RLock()
|
||||
}
|
||||
return tr.locks
|
||||
}
|
||||
|
||||
func (tr *BTree) runlock() {
|
||||
tr.mu.RUnlock()
|
||||
}
|
||||
|
|
|
|||
3
vendor/github.com/tidwall/btree/go.mod
generated
vendored
Normal file
3
vendor/github.com/tidwall/btree/go.mod
generated
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
module github.com/tidwall/btree
|
||||
|
||||
go 1.16
|
||||
Loading…
Add table
Add a link
Reference in a new issue