1
// Copyright 2018 The CubeFS Authors.
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
7
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12
// implied. See the License for the specific language governing
13
// permissions and limitations under the License.
20
"github.com/cubefs/cubefs/util/btree"
23
const defaultBTreeDegree = 32
26
// BtreeItem type alias google btree Item
27
BtreeItem = btree.Item
30
// BTree is the wrapper of Google's btree.
36
// NewBtree creates a new btree.
37
func NewBtree() *BTree {
39
tree: btree.New(defaultBTreeDegree),
43
// Get returns the object of the given key in the btree.
44
func (b *BTree) Get(key BtreeItem) (item BtreeItem) {
46
item = b.tree.Get(key)
51
func (b *BTree) CopyGet(key BtreeItem) (item BtreeItem) {
53
item = b.tree.CopyGet(key)
58
// Find searches for the given key in the btree.
59
func (b *BTree) Find(key BtreeItem, fn func(i BtreeItem)) {
61
item := b.tree.Get(key)
69
func (b *BTree) CopyFind(key BtreeItem, fn func(i BtreeItem)) {
71
item := b.tree.CopyGet(key)
76
// Has checks if the key exists in the btree.
77
func (b *BTree) Has(key BtreeItem) (ok bool) {
84
// Delete deletes the object by the given key.
85
func (b *BTree) Delete(key BtreeItem) (item BtreeItem) {
87
item = b.tree.Delete(key)
92
func (b *BTree) Execute(fn func(tree *btree.BTree) interface{}) interface{} {
98
// ReplaceOrInsert is the wrapper of google's btree ReplaceOrInsert.
99
func (b *BTree) ReplaceOrInsert(key BtreeItem, replace bool) (item BtreeItem, ok bool) {
102
item = b.tree.ReplaceOrInsert(key)
108
item = b.tree.Get(key)
110
item = b.tree.ReplaceOrInsert(key)
120
// Ascend is the wrapper of the google's btree Ascend.
121
// This function scans the entire btree. When the data is huge, it is not recommended to use this function online.
122
// Instead, it is recommended to call GetTree to obtain the snapshot of the current btree, and then do the scan on the snapshot.
123
func (b *BTree) Ascend(fn func(i BtreeItem) bool) {
129
// AscendRange is the wrapper of the google's btree AscendRange.
130
func (b *BTree) AscendRange(greaterOrEqual, lessThan BtreeItem, iterator func(i BtreeItem) bool) {
132
b.tree.AscendRange(greaterOrEqual, lessThan, iterator)
136
// AscendGreaterOrEqual is the wrapper of the google's btree AscendGreaterOrEqual
137
func (b *BTree) AscendGreaterOrEqual(pivot BtreeItem, iterator func(i BtreeItem) bool) {
139
b.tree.AscendGreaterOrEqual(pivot, iterator)
143
// GetTree returns the snapshot of a btree.
144
func (b *BTree) GetTree() *BTree {
153
// Reset resets the current btree.
154
func (b *BTree) Reset() {
160
// Len returns the total number of items in the btree.
161
func (b *BTree) Len() (size int) {
168
// MaxItem returns the largest item in the btree.
169
func (b *BTree) MaxItem() BtreeItem {