Browse Source

change key type to ItemKey

pull/2354/head
Chris Lu 3 years ago
parent
commit
51c8f2518f
  1. 8
      weed/util/bptree/bpmap.go
  2. 22
      weed/util/bptree/bptree.go
  3. 51
      weed/util/bptree/bptree_node.go
  4. 18
      weed/util/bptree/types.go

8
weed/util/bptree/bpmap.go

@ -15,11 +15,11 @@ func NewBpMap(node_size int) *BpMap {
}
}
func (self *BpMap) Has(key Hashable) bool {
func (self *BpMap) Has(key ItemKey) bool {
return (*BpTree)(self).Has(key)
}
func (self *BpMap) Put(key Hashable, value ItemValue) (err error) {
func (self *BpMap) Put(key ItemKey, value ItemValue) (err error) {
new_root, err := self.getRoot().put(key, value)
if err != nil {
return err
@ -28,7 +28,7 @@ func (self *BpMap) Put(key Hashable, value ItemValue) (err error) {
return nil
}
func (self *BpMap) Get(key Hashable) (value ItemValue, err error) {
func (self *BpMap) Get(key ItemKey) (value ItemValue, err error) {
j, l := self.getRoot().get_start(key)
if l.keys[j].Equals(key) {
return l.values[j], nil
@ -36,7 +36,7 @@ func (self *BpMap) Get(key Hashable) (value ItemValue, err error) {
return nil, fmt.Errorf("key not found: %s", key)
}
func (self *BpMap) Remove(key Hashable) (value ItemValue, err error) {
func (self *BpMap) Remove(key ItemKey) (value ItemValue, err error) {
value, err = self.Get(key)
if err != nil {
return nil, err

22
weed/util/bptree/bptree.go

@ -18,7 +18,7 @@ func NewBpTree(node_size int) *BpTree {
}
}
func (self *BpTree) Has(key Hashable) bool {
func (self *BpTree) Has(key ItemKey) bool {
if len(self.getRoot().keys) == 0 {
return false
}
@ -26,7 +26,7 @@ func (self *BpTree) Has(key Hashable) bool {
return l.keys[j].Equals(key)
}
func (self *BpTree) Count(key Hashable) int {
func (self *BpTree) Count(key ItemKey) int {
if len(self.root.keys) == 0 {
return 0
}
@ -40,7 +40,7 @@ func (self *BpTree) Count(key Hashable) int {
return count
}
func (self *BpTree) Add(key Hashable, value ItemValue) (err error) {
func (self *BpTree) Add(key ItemKey, value ItemValue) (err error) {
new_root, err := self.getRoot().put(key, value)
if err != nil {
return err
@ -49,7 +49,7 @@ func (self *BpTree) Add(key Hashable, value ItemValue) (err error) {
return nil
}
func (self *BpTree) Replace(key Hashable, where WhereFunc, value ItemValue) (err error) {
func (self *BpTree) Replace(key ItemKey, where WhereFunc, value ItemValue) (err error) {
li := self.getRoot().forward(key, key)
for i, leaf, next := li(); next != nil; i, leaf, next = next() {
if where(leaf.values[i]) {
@ -59,18 +59,18 @@ func (self *BpTree) Replace(key Hashable, where WhereFunc, value ItemValue) (err
return nil
}
func (self *BpTree) Find(key Hashable) (kvi KVIterator) {
func (self *BpTree) Find(key ItemKey) (kvi KVIterator) {
return self.Range(key, key)
}
func (self *BpTree) Range(from, to Hashable) (kvi KVIterator) {
func (self *BpTree) Range(from, to ItemKey) (kvi KVIterator) {
var li loc_iterator
if !to.Less(from) {
li = self.getRoot().forward(from, to)
} else {
li = self.getRoot().backward(from, to)
}
kvi = func() (key Hashable, value ItemValue, next KVIterator) {
kvi = func() (key ItemKey, value ItemValue, next KVIterator) {
var i int
var leaf *BpNode
i, leaf, li = li()
@ -82,7 +82,7 @@ func (self *BpTree) Range(from, to Hashable) (kvi KVIterator) {
return kvi
}
func (self *BpTree) RemoveWhere(key Hashable, where WhereFunc) (err error) {
func (self *BpTree) RemoveWhere(key ItemKey, where WhereFunc) (err error) {
ns := self.getRoot().NodeSize()
new_root, err := self.getRoot().remove(key, where)
if err != nil {
@ -99,7 +99,7 @@ func (self *BpTree) RemoveWhere(key Hashable, where WhereFunc) (err error) {
func (self *BpTree) Keys() (ki KIterator) {
li := self.getRoot().all()
var prev Equatable
ki = func() (key Hashable, next KIterator) {
ki = func() (key ItemKey, next KIterator) {
var i int
var leaf *BpNode
i, leaf, li = li()
@ -125,7 +125,7 @@ func (self *BpTree) Items() (vi KIterator) {
func (self *BpTree) Iterate() (kvi KVIterator) {
li := self.getRoot().all()
kvi = func() (key Hashable, value ItemValue, next KVIterator) {
kvi = func() (key ItemKey, value ItemValue, next KVIterator) {
var i int
var leaf *BpNode
i, leaf, li = li()
@ -139,7 +139,7 @@ func (self *BpTree) Iterate() (kvi KVIterator) {
func (self *BpTree) Backward() (kvi KVIterator) {
li := self.getRoot().all_backward()
kvi = func() (key Hashable, value ItemValue, next KVIterator) {
kvi = func() (key ItemKey, value ItemValue, next KVIterator) {
var i int
var leaf *BpNode
i, leaf, li = li()

51
weed/util/bptree/bptree_node.go

@ -1,9 +1,10 @@
package bptree
type ItemKey Hashable
type ItemValue Equatable
type BpNode struct {
keys []Hashable
keys []ItemKey
values []ItemValue
pointers []*BpNode
next *BpNode
@ -17,7 +18,7 @@ func NewInternal(size int) *BpNode {
panic(NegativeSize())
}
return &BpNode{
keys: make([]Hashable, 0, size),
keys: make([]ItemKey, 0, size),
pointers: make([]*BpNode, 0, size),
}
}
@ -27,7 +28,7 @@ func NewLeaf(size int, no_dup bool) *BpNode {
panic(NegativeSize())
}
return &BpNode{
keys: make([]Hashable, 0, size),
keys: make([]ItemKey, 0, size),
values: make([]ItemValue, 0, size),
no_dup: no_dup,
}
@ -67,7 +68,7 @@ func (self *BpNode) Height() int {
return self.pointers[0].Height() + 1
}
func (self *BpNode) count(key Hashable) int {
func (self *BpNode) count(key ItemKey) int {
i, _ := self.find(key)
count := 0
for ; i < len(self.keys); i++ {
@ -80,7 +81,7 @@ func (self *BpNode) count(key Hashable) int {
return count
}
func (self *BpNode) has(key Hashable) bool {
func (self *BpNode) has(key ItemKey) bool {
_, has := self.find(key)
return has
}
@ -103,7 +104,7 @@ func (self *BpNode) right_most_leaf() *BpNode {
* the search key. (unless the search key is greater than all the keys in the
* tree, in that case it will be the last key in the tree)
*/
func (self *BpNode) get_start(key Hashable) (i int, leaf *BpNode) {
func (self *BpNode) get_start(key ItemKey) (i int, leaf *BpNode) {
if self.Internal() {
return self.internal_get_start(key)
} else {
@ -140,7 +141,7 @@ func prev_location(i int, leaf *BpNode) (int, *BpNode, bool) {
* than all the keys in the tree, in that case it will be the last key in the
* tree)
*/
func (self *BpNode) get_end(key Hashable) (i int, leaf *BpNode) {
func (self *BpNode) get_end(key ItemKey) (i int, leaf *BpNode) {
end := false
i, leaf = self.get_start(key)
pi, pleaf := i, leaf
@ -151,7 +152,7 @@ func (self *BpNode) get_end(key Hashable) (i int, leaf *BpNode) {
return pi, pleaf
}
func (self *BpNode) internal_get_start(key Hashable) (i int, leaf *BpNode) {
func (self *BpNode) internal_get_start(key ItemKey) (i int, leaf *BpNode) {
if !self.Internal() {
panic(BpTreeError("Expected a internal node"))
}
@ -165,7 +166,7 @@ func (self *BpNode) internal_get_start(key Hashable) (i int, leaf *BpNode) {
return child.get_start(key)
}
func (self *BpNode) leaf_get_start(key Hashable) (i int, leaf *BpNode) {
func (self *BpNode) leaf_get_start(key ItemKey) (i int, leaf *BpNode) {
i, has := self.find(key)
if i >= len(self.keys) && i > 0 {
i = len(self.keys) - 1
@ -179,7 +180,7 @@ func (self *BpNode) leaf_get_start(key Hashable) (i int, leaf *BpNode) {
/* This puts the k/v pair into the B+Tree rooted at this node and returns the
* (possibly) new root of the tree.
*/
func (self *BpNode) put(key Hashable, value ItemValue) (root *BpNode, err error) {
func (self *BpNode) put(key ItemKey, value ItemValue) (root *BpNode, err error) {
a, b, err := self.insert(key, value)
if err != nil {
return nil, err
@ -197,7 +198,7 @@ func (self *BpNode) put(key Hashable, value ItemValue) (root *BpNode, err error)
// left is always set. When split is false left is the pointer to block
// When split is true left is the pointer to the new left
// block
func (self *BpNode) insert(key Hashable, value ItemValue) (a, b *BpNode, err error) {
func (self *BpNode) insert(key ItemKey, value ItemValue) (a, b *BpNode, err error) {
if self.Internal() {
return self.internal_insert(key, value)
} else { // leaf node
@ -211,7 +212,7 @@ func (self *BpNode) insert(key Hashable, value ItemValue) (a, b *BpNode, err err
* - if the block is full, split this block
* - else insert the new key/pointer into this block
*/
func (self *BpNode) internal_insert(key Hashable, value ItemValue) (a, b *BpNode, err error) {
func (self *BpNode) internal_insert(key ItemKey, value ItemValue) (a, b *BpNode, err error) {
if !self.Internal() {
return nil, nil, BpTreeError("Expected a internal node")
}
@ -248,7 +249,7 @@ func (self *BpNode) internal_insert(key Hashable, value ItemValue) (a, b *BpNode
* - balance the two blocks.
* - insert the new key/pointer combo into the correct block
*/
func (self *BpNode) internal_split(key Hashable, ptr *BpNode) (a, b *BpNode, err error) {
func (self *BpNode) internal_split(key ItemKey, ptr *BpNode) (a, b *BpNode, err error) {
if !self.Internal() {
return nil, nil, BpTreeError("Expected a internal node")
}
@ -275,7 +276,7 @@ func (self *BpNode) internal_split(key Hashable, ptr *BpNode) (a, b *BpNode, err
* a pure block with a matching key)
* else this leaf will get a new entry.
*/
func (self *BpNode) leaf_insert(key Hashable, value ItemValue) (a, b *BpNode, err error) {
func (self *BpNode) leaf_insert(key ItemKey, value ItemValue) (a, b *BpNode, err error) {
if self.Internal() {
return nil, nil, BpTreeError("Expected a leaf node")
}
@ -302,7 +303,7 @@ func (self *BpNode) leaf_insert(key Hashable, value ItemValue) (a, b *BpNode, er
* - the two blocks will be balanced with balanced_nodes
* - if the key is less than b.keys[0] it will go in a else b
*/
func (self *BpNode) leaf_split(key Hashable, value ItemValue) (a, b *BpNode, err error) {
func (self *BpNode) leaf_split(key ItemKey, value ItemValue) (a, b *BpNode, err error) {
if self.Internal() {
return nil, nil, BpTreeError("Expected a leaf node")
}
@ -337,7 +338,7 @@ func (self *BpNode) leaf_split(key Hashable, value ItemValue) (a, b *BpNode, err
* and putting the new key there.
* - always return the current block as "a" and the new block as "b"
*/
func (self *BpNode) pure_leaf_split(key Hashable, value ItemValue) (a, b *BpNode, err error) {
func (self *BpNode) pure_leaf_split(key ItemKey, value ItemValue) (a, b *BpNode, err error) {
if self.Internal() || !self.Pure() {
return nil, nil, BpTreeError("Expected a pure leaf node")
}
@ -371,7 +372,7 @@ func (self *BpNode) pure_leaf_split(key Hashable, value ItemValue) (a, b *BpNode
}
}
func (self *BpNode) put_kp(key Hashable, ptr *BpNode) error {
func (self *BpNode) put_kp(key ItemKey, ptr *BpNode) error {
if self.Full() {
return BpTreeError("Block is full.")
}
@ -395,7 +396,7 @@ func (self *BpNode) put_kp(key Hashable, ptr *BpNode) error {
return nil
}
func (self *BpNode) put_kv(key Hashable, value ItemValue) error {
func (self *BpNode) put_kv(key ItemKey, value ItemValue) error {
if self.Full() {
return BpTreeError("Block is full.")
}
@ -417,7 +418,7 @@ func (self *BpNode) put_kv(key Hashable, value ItemValue) error {
return nil
}
func (self *BpNode) put_key_at(i int, key Hashable) error {
func (self *BpNode) put_key_at(i int, key ItemKey) error {
if self.Full() {
return BpTreeError("Block is full.")
}
@ -459,7 +460,7 @@ func (self *BpNode) put_pointer_at(i int, pointer *BpNode) error {
return nil
}
func (self *BpNode) remove(key Hashable, where WhereFunc) (a *BpNode, err error) {
func (self *BpNode) remove(key ItemKey, where WhereFunc) (a *BpNode, err error) {
if self.Internal() {
return self.internal_remove(key, nil, where)
} else {
@ -467,7 +468,7 @@ func (self *BpNode) remove(key Hashable, where WhereFunc) (a *BpNode, err error)
}
}
func (self *BpNode) internal_remove(key Hashable, sibling *BpNode, where WhereFunc) (a *BpNode, err error) {
func (self *BpNode) internal_remove(key ItemKey, sibling *BpNode, where WhereFunc) (a *BpNode, err error) {
if !self.Internal() {
panic(BpTreeError("Expected a internal node"))
}
@ -512,7 +513,7 @@ func (self *BpNode) internal_remove(key Hashable, sibling *BpNode, where WhereFu
return self, nil
}
func (self *BpNode) leaf_remove(key, stop Hashable, where WhereFunc) (a *BpNode, err error) {
func (self *BpNode) leaf_remove(key, stop ItemKey, where WhereFunc) (a *BpNode, err error) {
if self.Internal() {
return nil, BpTreeError("Expected a leaf node")
}
@ -575,7 +576,7 @@ func (self *BpNode) remove_ptr_at(i int) error {
return nil
}
func (self *BpNode) find(key Hashable) (int, bool) {
func (self *BpNode) find(key ItemKey) (int, bool) {
var l = 0
var r = len(self.keys) - 1
var m int
@ -641,7 +642,7 @@ func (self *BpNode) all_backward() (li loc_iterator) {
return li
}
func (self *BpNode) forward(from, to Hashable) (li loc_iterator) {
func (self *BpNode) forward(from, to ItemKey) (li loc_iterator) {
j, l := self.get_start(from)
end := false
j--
@ -655,7 +656,7 @@ func (self *BpNode) forward(from, to Hashable) (li loc_iterator) {
return li
}
func (self *BpNode) backward(from, to Hashable) (li loc_iterator) {
func (self *BpNode) backward(from, to ItemKey) (li loc_iterator) {
j, l := self.get_end(from)
end := false
li = func() (i int, leaf *BpNode, next loc_iterator) {

18
weed/util/bptree/types.go

@ -26,17 +26,17 @@ func NegativeSize() error {
}
type Iterator func() (item ItemValue, next Iterator)
type KIterator func() (key Hashable, next KIterator)
type KVIterator func() (key Hashable, value ItemValue, next KVIterator)
type KIterator func() (key ItemKey, next KIterator)
type KVIterator func() (key ItemKey, value ItemValue, next KVIterator)
type KVIterable interface {
Iterate() KVIterator
}
type MapOperable interface {
Has(key Hashable) bool
Put(key Hashable, value ItemValue) (err error)
Get(key Hashable) (value ItemValue, err error)
Remove(key Hashable) (value ItemValue, err error)
Has(key ItemKey) bool
Put(key ItemKey, value ItemValue) (err error)
Get(key ItemKey) (value ItemValue, err error)
Remove(key ItemKey) (value ItemValue, err error)
}
type WhereFunc func(value ItemValue) bool
@ -56,8 +56,8 @@ func MakeValuesIterator(obj KVIterable) Iterator {
func MakeItemsIterator(obj KVIterable) (kit KIterator) {
kv_iterator := obj.Iterate()
kit = func() (item Hashable, next KIterator) {
var key Hashable
kit = func() (item ItemKey, next KIterator) {
var key ItemKey
var value ItemValue
key, value, kv_iterator = kv_iterator()
if kv_iterator == nil {
@ -69,7 +69,7 @@ func MakeItemsIterator(obj KVIterable) (kit KIterator) {
}
type MapEntry struct {
Key Hashable
Key ItemKey
Value ItemValue
}

Loading…
Cancel
Save