github.com/flower-corp/rosedb@v1.1.2-0.20230117132829-21dc4f7b319a/ds/art/art.go (about)

     1  package art
     2  
     3  import (
     4  	goart "github.com/plar/go-adaptive-radix-tree"
     5  )
     6  
     7  type AdaptiveRadixTree struct {
     8  	tree goart.Tree
     9  }
    10  
    11  func NewART() *AdaptiveRadixTree {
    12  	return &AdaptiveRadixTree{
    13  		tree: goart.New(),
    14  	}
    15  }
    16  
    17  func (art *AdaptiveRadixTree) Put(key []byte, value interface{}) (oldVal interface{}, updated bool) {
    18  	return art.tree.Insert(key, value)
    19  }
    20  
    21  func (art *AdaptiveRadixTree) Get(key []byte) interface{} {
    22  	value, _ := art.tree.Search(key)
    23  	return value
    24  }
    25  
    26  func (art *AdaptiveRadixTree) Delete(key []byte) (val interface{}, updated bool) {
    27  	return art.tree.Delete(key)
    28  }
    29  
    30  func (art *AdaptiveRadixTree) Iterator() goart.Iterator {
    31  	return art.tree.Iterator()
    32  }
    33  
    34  func (art *AdaptiveRadixTree) PrefixScan(prefix []byte, count int) (keys [][]byte) {
    35  	cb := func(node goart.Node) bool {
    36  		if node.Kind() != goart.Leaf {
    37  			return true
    38  		}
    39  		if count <= 0 {
    40  			return false
    41  		}
    42  		keys = append(keys, node.Key())
    43  		count--
    44  		return true
    45  	}
    46  
    47  	if len(prefix) == 0 {
    48  		art.tree.ForEach(cb)
    49  	} else {
    50  		art.tree.ForEachPrefix(prefix, cb)
    51  	}
    52  	return
    53  }
    54  
    55  func (art *AdaptiveRadixTree) Size() int {
    56  	return art.tree.Size()
    57  }