Browse Source

change value type to ItemValue

pull/2354/head
Chris Lu 3 years ago
parent
commit
b3e49d2758
  1. 8
      weed/util/bptree/bpmap.go
  2. 10
      weed/util/bptree/bptree.go
  3. 33
      weed/util/bptree/bptree.pb.go
  4. 29
      weed/util/bptree/bptree_node.go
  5. 188
      weed/util/bptree/bptree_test.go
  6. 18
      weed/util/bptree/types.go

8
weed/util/bptree/bpmap.go

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

10
weed/util/bptree/bptree.go

@ -40,7 +40,7 @@ func (self *BpTree) Count(key Hashable) int {
return count return count
} }
func (self *BpTree) Add(key Hashable, value interface{}) (err error) {
func (self *BpTree) Add(key Hashable, value ItemValue) (err error) {
new_root, err := self.getRoot().put(key, value) new_root, err := self.getRoot().put(key, value)
if err != nil { if err != nil {
return err return err
@ -49,7 +49,7 @@ func (self *BpTree) Add(key Hashable, value interface{}) (err error) {
return nil return nil
} }
func (self *BpTree) Replace(key Hashable, where WhereFunc, value interface{}) (err error) {
func (self *BpTree) Replace(key Hashable, where WhereFunc, value ItemValue) (err error) {
li := self.getRoot().forward(key, key) li := self.getRoot().forward(key, key)
for i, leaf, next := li(); next != nil; i, leaf, next = next() { for i, leaf, next := li(); next != nil; i, leaf, next = next() {
if where(leaf.values[i]) { if where(leaf.values[i]) {
@ -70,7 +70,7 @@ func (self *BpTree) Range(from, to Hashable) (kvi KVIterator) {
} else { } else {
li = self.getRoot().backward(from, to) li = self.getRoot().backward(from, to)
} }
kvi = func() (key Hashable, value interface{}, next KVIterator) {
kvi = func() (key Hashable, value ItemValue, next KVIterator) {
var i int var i int
var leaf *BpNode var leaf *BpNode
i, leaf, li = li() i, leaf, li = li()
@ -125,7 +125,7 @@ func (self *BpTree) Items() (vi KIterator) {
func (self *BpTree) Iterate() (kvi KVIterator) { func (self *BpTree) Iterate() (kvi KVIterator) {
li := self.getRoot().all() li := self.getRoot().all()
kvi = func() (key Hashable, value interface{}, next KVIterator) {
kvi = func() (key Hashable, value ItemValue, next KVIterator) {
var i int var i int
var leaf *BpNode var leaf *BpNode
i, leaf, li = li() i, leaf, li = li()
@ -139,7 +139,7 @@ func (self *BpTree) Iterate() (kvi KVIterator) {
func (self *BpTree) Backward() (kvi KVIterator) { func (self *BpTree) Backward() (kvi KVIterator) {
li := self.getRoot().all_backward() li := self.getRoot().all_backward()
kvi = func() (key Hashable, value interface{}, next KVIterator) {
kvi = func() (key Hashable, value ItemValue, next KVIterator) {
var i int var i int
var leaf *BpNode var leaf *BpNode
i, leaf, li = li() i, leaf, li = li()

33
weed/util/bptree/bptree.pb.go

@ -35,6 +35,7 @@ type ProtoNode struct {
Pointers []int64 `protobuf:"varint,3,rep,packed,name=pointers,proto3" json:"pointers,omitempty"` Pointers []int64 `protobuf:"varint,3,rep,packed,name=pointers,proto3" json:"pointers,omitempty"`
Next int64 `protobuf:"varint,4,opt,name=next,proto3" json:"next,omitempty"` Next int64 `protobuf:"varint,4,opt,name=next,proto3" json:"next,omitempty"`
Prev int64 `protobuf:"varint,5,opt,name=prev,proto3" json:"prev,omitempty"` Prev int64 `protobuf:"varint,5,opt,name=prev,proto3" json:"prev,omitempty"`
Id int64 `protobuf:"varint,6,opt,name=id,proto3" json:"id,omitempty"`
} }
func (x *ProtoNode) Reset() { func (x *ProtoNode) Reset() {
@ -104,22 +105,30 @@ func (x *ProtoNode) GetPrev() int64 {
return 0 return 0
} }
func (x *ProtoNode) GetId() int64 {
if x != nil {
return x.Id
}
return 0
}
var File_bptree_proto protoreflect.FileDescriptor var File_bptree_proto protoreflect.FileDescriptor
var file_bptree_proto_rawDesc = []byte{ var file_bptree_proto_rawDesc = []byte{
0x0a, 0x0c, 0x62, 0x70, 0x74, 0x72, 0x65, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x06, 0x0a, 0x0c, 0x62, 0x70, 0x74, 0x72, 0x65, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x06,
0x62, 0x70, 0x74, 0x72, 0x65, 0x65, 0x22, 0x7b, 0x0a, 0x09, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x4e,
0x6f, 0x64, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x6b, 0x65, 0x79, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28,
0x0c, 0x52, 0x04, 0x6b, 0x65, 0x79, 0x73, 0x12, 0x16, 0x0a, 0x06, 0x76, 0x61, 0x6c, 0x75, 0x65,
0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0c, 0x52, 0x06, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x73, 0x12,
0x1a, 0x0a, 0x08, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x73, 0x18, 0x03, 0x20, 0x03, 0x28,
0x03, 0x52, 0x08, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x73, 0x12, 0x12, 0x0a, 0x04, 0x6e,
0x65, 0x78, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x03, 0x52, 0x04, 0x6e, 0x65, 0x78, 0x74, 0x12,
0x12, 0x0a, 0x04, 0x70, 0x72, 0x65, 0x76, 0x18, 0x05, 0x20, 0x01, 0x28, 0x03, 0x52, 0x04, 0x70,
0x72, 0x65, 0x76, 0x42, 0x31, 0x5a, 0x2f, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f,
0x6d, 0x2f, 0x63, 0x68, 0x72, 0x69, 0x73, 0x6c, 0x75, 0x73, 0x66, 0x2f, 0x73, 0x65, 0x61, 0x77,
0x65, 0x65, 0x64, 0x66, 0x73, 0x2f, 0x77, 0x65, 0x65, 0x64, 0x2f, 0x75, 0x74, 0x69, 0x6c, 0x2f,
0x62, 0x70, 0x74, 0x72, 0x65, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
0x62, 0x70, 0x74, 0x72, 0x65, 0x65, 0x22, 0x8b, 0x01, 0x0a, 0x09, 0x50, 0x72, 0x6f, 0x74, 0x6f,
0x4e, 0x6f, 0x64, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x6b, 0x65, 0x79, 0x73, 0x18, 0x01, 0x20, 0x03,
0x28, 0x0c, 0x52, 0x04, 0x6b, 0x65, 0x79, 0x73, 0x12, 0x16, 0x0a, 0x06, 0x76, 0x61, 0x6c, 0x75,
0x65, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0c, 0x52, 0x06, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x73,
0x12, 0x1a, 0x0a, 0x08, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x73, 0x18, 0x03, 0x20, 0x03,
0x28, 0x03, 0x52, 0x08, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x73, 0x12, 0x12, 0x0a, 0x04,
0x6e, 0x65, 0x78, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x03, 0x52, 0x04, 0x6e, 0x65, 0x78, 0x74,
0x12, 0x12, 0x0a, 0x04, 0x70, 0x72, 0x65, 0x76, 0x18, 0x05, 0x20, 0x01, 0x28, 0x03, 0x52, 0x04,
0x70, 0x72, 0x65, 0x76, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x06, 0x20, 0x01, 0x28, 0x03,
0x52, 0x02, 0x69, 0x64, 0x42, 0x31, 0x5a, 0x2f, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63,
0x6f, 0x6d, 0x2f, 0x63, 0x68, 0x72, 0x69, 0x73, 0x6c, 0x75, 0x73, 0x66, 0x2f, 0x73, 0x65, 0x61,
0x77, 0x65, 0x65, 0x64, 0x66, 0x73, 0x2f, 0x77, 0x65, 0x65, 0x64, 0x2f, 0x75, 0x74, 0x69, 0x6c,
0x2f, 0x62, 0x70, 0x74, 0x72, 0x65, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
} }
var ( var (

29
weed/util/bptree/bptree_node.go

@ -1,12 +1,15 @@
package bptree package bptree
type ItemValue Equatable
type BpNode struct { type BpNode struct {
keys []Hashable keys []Hashable
values []interface{}
values []ItemValue
pointers []*BpNode pointers []*BpNode
next *BpNode next *BpNode
prev *BpNode prev *BpNode
no_dup bool no_dup bool
protoNode *ProtoNode
} }
func NewInternal(size int) *BpNode { func NewInternal(size int) *BpNode {
@ -25,7 +28,7 @@ func NewLeaf(size int, no_dup bool) *BpNode {
} }
return &BpNode{ return &BpNode{
keys: make([]Hashable, 0, size), keys: make([]Hashable, 0, size),
values: make([]interface{}, 0, size),
values: make([]ItemValue, 0, size),
no_dup: no_dup, no_dup: no_dup,
} }
} }
@ -176,7 +179,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 /* This puts the k/v pair into the B+Tree rooted at this node and returns the
* (possibly) new root of the tree. * (possibly) new root of the tree.
*/ */
func (self *BpNode) put(key Hashable, value interface{}) (root *BpNode, err error) {
func (self *BpNode) put(key Hashable, value ItemValue) (root *BpNode, err error) {
a, b, err := self.insert(key, value) a, b, err := self.insert(key, value)
if err != nil { if err != nil {
return nil, err return nil, err
@ -194,7 +197,7 @@ func (self *BpNode) put(key Hashable, value interface{}) (root *BpNode, err erro
// left is always set. When split is false left is the pointer to block // 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 // When split is true left is the pointer to the new left
// block // block
func (self *BpNode) insert(key Hashable, value interface{}) (a, b *BpNode, err error) {
func (self *BpNode) insert(key Hashable, value ItemValue) (a, b *BpNode, err error) {
if self.Internal() { if self.Internal() {
return self.internal_insert(key, value) return self.internal_insert(key, value)
} else { // leaf node } else { // leaf node
@ -208,7 +211,7 @@ func (self *BpNode) insert(key Hashable, value interface{}) (a, b *BpNode, err e
* - if the block is full, split this block * - if the block is full, split this block
* - else insert the new key/pointer into this block * - else insert the new key/pointer into this block
*/ */
func (self *BpNode) internal_insert(key Hashable, value interface{}) (a, b *BpNode, err error) {
func (self *BpNode) internal_insert(key Hashable, value ItemValue) (a, b *BpNode, err error) {
if !self.Internal() { if !self.Internal() {
return nil, nil, BpTreeError("Expected a internal node") return nil, nil, BpTreeError("Expected a internal node")
} }
@ -272,7 +275,7 @@ func (self *BpNode) internal_split(key Hashable, ptr *BpNode) (a, b *BpNode, err
* a pure block with a matching key) * a pure block with a matching key)
* else this leaf will get a new entry. * else this leaf will get a new entry.
*/ */
func (self *BpNode) leaf_insert(key Hashable, value interface{}) (a, b *BpNode, err error) {
func (self *BpNode) leaf_insert(key Hashable, value ItemValue) (a, b *BpNode, err error) {
if self.Internal() { if self.Internal() {
return nil, nil, BpTreeError("Expected a leaf node") return nil, nil, BpTreeError("Expected a leaf node")
} }
@ -299,7 +302,7 @@ func (self *BpNode) leaf_insert(key Hashable, value interface{}) (a, b *BpNode,
* - the two blocks will be balanced with balanced_nodes * - 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 * - if the key is less than b.keys[0] it will go in a else b
*/ */
func (self *BpNode) leaf_split(key Hashable, value interface{}) (a, b *BpNode, err error) {
func (self *BpNode) leaf_split(key Hashable, value ItemValue) (a, b *BpNode, err error) {
if self.Internal() { if self.Internal() {
return nil, nil, BpTreeError("Expected a leaf node") return nil, nil, BpTreeError("Expected a leaf node")
} }
@ -334,7 +337,7 @@ func (self *BpNode) leaf_split(key Hashable, value interface{}) (a, b *BpNode, e
* and putting the new key there. * and putting the new key there.
* - always return the current block as "a" and the new block as "b" * - always return the current block as "a" and the new block as "b"
*/ */
func (self *BpNode) pure_leaf_split(key Hashable, value interface{}) (a, b *BpNode, err error) {
func (self *BpNode) pure_leaf_split(key Hashable, value ItemValue) (a, b *BpNode, err error) {
if self.Internal() || !self.Pure() { if self.Internal() || !self.Pure() {
return nil, nil, BpTreeError("Expected a pure leaf node") return nil, nil, BpTreeError("Expected a pure leaf node")
} }
@ -392,7 +395,7 @@ func (self *BpNode) put_kp(key Hashable, ptr *BpNode) error {
return nil return nil
} }
func (self *BpNode) put_kv(key Hashable, value interface{}) error {
func (self *BpNode) put_kv(key Hashable, value ItemValue) error {
if self.Full() { if self.Full() {
return BpTreeError("Block is full.") return BpTreeError("Block is full.")
} }
@ -426,7 +429,7 @@ func (self *BpNode) put_key_at(i int, key Hashable) error {
return nil return nil
} }
func (self *BpNode) put_value_at(i int, value interface{}) error {
func (self *BpNode) put_value_at(i int, value ItemValue) error {
if len(self.values) == cap(self.values) { if len(self.values) == cap(self.values) {
return BpTreeError("Block is full.") return BpTreeError("Block is full.")
} }
@ -573,8 +576,8 @@ func (self *BpNode) remove_ptr_at(i int) error {
} }
func (self *BpNode) find(key Hashable) (int, bool) { func (self *BpNode) find(key Hashable) (int, bool) {
var l int = 0
var r int = len(self.keys) - 1
var l = 0
var r = len(self.keys) - 1
var m int var m int
for l <= r { for l <= r {
m = ((r - l) >> 1) + l m = ((r - l) >> 1) + l
@ -718,7 +721,7 @@ func balance_nodes(a, b *BpNode) {
m-- m--
} }
} }
var lim int = len(a.keys) - m
var lim = len(a.keys) - m
b.keys = b.keys[:lim] b.keys = b.keys[:lim]
if cap(a.values) > 0 { if cap(a.values) > 0 {
if cap(a.values) != cap(a.keys) { if cap(a.values) != cap(a.keys) {

188
weed/util/bptree/bptree_test.go

@ -48,7 +48,7 @@ func (self Strings) Swap(i, j int) {
type record struct { type record struct {
key String key String
value String
value ItemValue
} }
type records []*record type records []*record
@ -84,7 +84,7 @@ func BenchmarkBpTree(b *testing.B) {
t.Add(r.key, r.value) t.Add(r.key, r.value)
} }
for _, r := range recs { for _, r := range recs {
t.RemoveWhere(r.key, func(value interface{}) bool { return true })
t.RemoveWhere(r.key, func(value ItemValue) bool { return true })
} }
} }
} }
@ -133,7 +133,7 @@ func TestAddHasCountFindIterateRemove(t *testing.T) {
t.Error(bpt, "Find Failed Value Error") t.Error(bpt, "Find Failed Value Error")
} }
} }
err = bpt.Replace(r.key, func(value interface{}) bool { return true }, new_recs[i].value)
err = bpt.Replace(r.key, func(value ItemValue) bool { return true }, new_recs[i].value)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -193,7 +193,7 @@ func TestAddHasCountFindIterateRemove(t *testing.T) {
if count := bpt.Count(r.key); count != 1 { if count := bpt.Count(r.key); count != 1 {
t.Error(bpt, "Missing key") t.Error(bpt, "Missing key")
} }
if err := bpt.RemoveWhere(r.key, func(value interface{}) bool { return true }); err != nil {
if err := bpt.RemoveWhere(r.key, func(value ItemValue) bool { return true }); err != nil {
t.Fatal(bpt, err) t.Fatal(bpt, err)
} }
if has := bpt.Has(r.key); has { if has := bpt.Has(r.key); has {
@ -276,15 +276,15 @@ func TestBpMap(t *testing.T) {
func Test_get_start(t *testing.T) { func Test_get_start(t *testing.T) {
root := NewLeaf(2, false) root := NewLeaf(2, false)
root, err := root.put(Int(1), 1)
root, err := root.put(Int(1), Int(1))
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
root, err = root.put(Int(5), 3)
root, err = root.put(Int(5), Int(3))
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
root, err = root.put(Int(3), 2)
root, err = root.put(Int(3), Int(2))
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -345,31 +345,31 @@ func Test_get_start(t *testing.T) {
func Test_get_end(t *testing.T) { func Test_get_end(t *testing.T) {
root := NewLeaf(3, false) root := NewLeaf(3, false)
root, err := root.put(Int(1), -1)
root, err := root.put(Int(1), Int(-1))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
root, err = root.put(Int(4), -1)
root, err = root.put(Int(4), Int(-1))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
root, err = root.put(Int(3), 1)
root, err = root.put(Int(3), Int(1))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
root, err = root.put(Int(3), 2)
root, err = root.put(Int(3), Int(2))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
root, err = root.put(Int(3), 3)
root, err = root.put(Int(3), Int(3))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
root, err = root.put(Int(3), 4)
root, err = root.put(Int(3), Int(4))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
root, err = root.put(Int(3), 5)
root, err = root.put(Int(3), Int(5))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -437,10 +437,10 @@ func Test_get_end(t *testing.T) {
func Test_put_no_root_split(t *testing.T) { func Test_put_no_root_split(t *testing.T) {
a := NewLeaf(2, false) a := NewLeaf(2, false)
if err := a.put_kv(Int(1), 1); err != nil {
if err := a.put_kv(Int(1), Int(1)); err != nil {
t.Error(err) t.Error(err)
} }
p, err := a.put(Int(1), 2)
p, err := a.put(Int(1), Int(2))
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} else { } else {
@ -451,7 +451,7 @@ func Test_put_no_root_split(t *testing.T) {
t.Error("p didn't have the right keys", p) t.Error("p didn't have the right keys", p)
} }
} }
p, err = a.put(Int(1), 3)
p, err = a.put(Int(1), Int(3))
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} else { } else {
@ -471,7 +471,7 @@ func Test_put_no_root_split(t *testing.T) {
func Test_put_root_split(t *testing.T) { func Test_put_root_split(t *testing.T) {
a := NewLeaf(2, false) a := NewLeaf(2, false)
p, err := a.put(Int(1), 1)
p, err := a.put(Int(1), Int(1))
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} else { } else {
@ -482,7 +482,7 @@ func Test_put_root_split(t *testing.T) {
t.Error("p didn't have the right keys", p) t.Error("p didn't have the right keys", p)
} }
} }
p, err = a.put(Int(3), 3)
p, err = a.put(Int(3), Int(3))
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} else { } else {
@ -493,7 +493,7 @@ func Test_put_root_split(t *testing.T) {
t.Error("p didn't have the right keys", p) t.Error("p didn't have the right keys", p)
} }
} }
p, err = a.put(Int(2), 2)
p, err = a.put(Int(2), Int(2))
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} else { } else {
@ -521,7 +521,7 @@ func Test_put_root_split(t *testing.T) {
func Test_internal_insert_no_split(t *testing.T) { func Test_internal_insert_no_split(t *testing.T) {
a := NewInternal(3) a := NewInternal(3)
leaf := NewLeaf(1, false) leaf := NewLeaf(1, false)
if err := leaf.put_kv(Int(1), 1); err != nil {
if err := leaf.put_kv(Int(1), Int(1)); err != nil {
t.Error(err) t.Error(err)
} }
if err := a.put_kp(Int(1), leaf); err != nil { if err := a.put_kp(Int(1), leaf); err != nil {
@ -549,7 +549,7 @@ func Test_internal_insert_no_split(t *testing.T) {
func Test_internal_insert_split_less(t *testing.T) { func Test_internal_insert_split_less(t *testing.T) {
a := NewInternal(3) a := NewInternal(3)
leaf := NewLeaf(1, false) leaf := NewLeaf(1, false)
if err := leaf.put_kv(Int(1), 1); err != nil {
if err := leaf.put_kv(Int(1), Int(1)); err != nil {
t.Error(err) t.Error(err)
} }
if err := a.put_kp(Int(1), leaf); err != nil { if err := a.put_kp(Int(1), leaf); err != nil {
@ -660,13 +660,13 @@ func Test_internal_split_greater(t *testing.T) {
func Test_leaf_insert_no_split(t *testing.T) { func Test_leaf_insert_no_split(t *testing.T) {
a := NewLeaf(3, false) a := NewLeaf(3, false)
insert_linked_list_node(a, nil, nil) insert_linked_list_node(a, nil, nil)
if err := a.put_kv(Int(1), 1); err != nil {
if err := a.put_kv(Int(1), Int(1)); err != nil {
t.Error(err) t.Error(err)
} }
if err := a.put_kv(Int(3), 3); err != nil {
if err := a.put_kv(Int(3), Int(3)); err != nil {
t.Error(err) t.Error(err)
} }
p, q, err := a.leaf_insert(Int(2), 2)
p, q, err := a.leaf_insert(Int(2), Int(2))
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} else { } else {
@ -686,16 +686,16 @@ func Test_leaf_insert_no_split(t *testing.T) {
func Test_leaf_insert_split_less(t *testing.T) { func Test_leaf_insert_split_less(t *testing.T) {
a := NewLeaf(3, false) a := NewLeaf(3, false)
insert_linked_list_node(a, nil, nil) insert_linked_list_node(a, nil, nil)
if err := a.put_kv(Int(1), 1); err != nil {
if err := a.put_kv(Int(1), Int(1)); err != nil {
t.Error(err) t.Error(err)
} }
if err := a.put_kv(Int(3), 3); err != nil {
if err := a.put_kv(Int(3), Int(3)); err != nil {
t.Error(err) t.Error(err)
} }
if err := a.put_kv(Int(5), 5); err != nil {
if err := a.put_kv(Int(5), Int(5)); err != nil {
t.Error(err) t.Error(err)
} }
p, q, err := a.leaf_insert(Int(2), 2)
p, q, err := a.leaf_insert(Int(2), Int(2))
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} else { } else {
@ -717,16 +717,16 @@ func Test_leaf_insert_split_less(t *testing.T) {
func Test_leaf_split_less(t *testing.T) { func Test_leaf_split_less(t *testing.T) {
a := NewLeaf(3, false) a := NewLeaf(3, false)
insert_linked_list_node(a, nil, nil) insert_linked_list_node(a, nil, nil)
if err := a.put_kv(Int(1), 1); err != nil {
if err := a.put_kv(Int(1), Int(1)); err != nil {
t.Error(err) t.Error(err)
} }
if err := a.put_kv(Int(3), 3); err != nil {
if err := a.put_kv(Int(3), Int(3)); err != nil {
t.Error(err) t.Error(err)
} }
if err := a.put_kv(Int(5), 5); err != nil {
if err := a.put_kv(Int(5), Int(5)); err != nil {
t.Error(err) t.Error(err)
} }
p, q, err := a.leaf_split(Int(2), 2)
p, q, err := a.leaf_split(Int(2), Int(2))
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} else { } else {
@ -748,16 +748,16 @@ func Test_leaf_split_less(t *testing.T) {
func Test_leaf_split_equal(t *testing.T) { func Test_leaf_split_equal(t *testing.T) {
a := NewLeaf(3, false) a := NewLeaf(3, false)
insert_linked_list_node(a, nil, nil) insert_linked_list_node(a, nil, nil)
if err := a.put_kv(Int(1), 1); err != nil {
if err := a.put_kv(Int(1), Int(1)); err != nil {
t.Error(err) t.Error(err)
} }
if err := a.put_kv(Int(3), 3); err != nil {
if err := a.put_kv(Int(3), Int(3)); err != nil {
t.Error(err) t.Error(err)
} }
if err := a.put_kv(Int(5), 5); err != nil {
if err := a.put_kv(Int(5), Int(5)); err != nil {
t.Error(err) t.Error(err)
} }
p, q, err := a.leaf_split(Int(3), 2)
p, q, err := a.leaf_split(Int(3), Int(2))
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} else { } else {
@ -779,16 +779,16 @@ func Test_leaf_split_equal(t *testing.T) {
func Test_leaf_split_greater(t *testing.T) { func Test_leaf_split_greater(t *testing.T) {
a := NewLeaf(3, false) a := NewLeaf(3, false)
insert_linked_list_node(a, nil, nil) insert_linked_list_node(a, nil, nil)
if err := a.put_kv(Int(1), 1); err != nil {
if err := a.put_kv(Int(1), Int(1)); err != nil {
t.Error(err) t.Error(err)
} }
if err := a.put_kv(Int(3), 3); err != nil {
if err := a.put_kv(Int(3), Int(3)); err != nil {
t.Error(err) t.Error(err)
} }
if err := a.put_kv(Int(5), 5); err != nil {
if err := a.put_kv(Int(5), Int(5)); err != nil {
t.Error(err) t.Error(err)
} }
p, q, err := a.leaf_split(Int(4), 2)
p, q, err := a.leaf_split(Int(4), Int(2))
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} else { } else {
@ -817,28 +817,28 @@ func Test_pure_leaf_insert_split_less(t *testing.T) {
insert_linked_list_node(c, b, nil) insert_linked_list_node(c, b, nil)
d := NewLeaf(2, false) d := NewLeaf(2, false)
insert_linked_list_node(d, c, nil) insert_linked_list_node(d, c, nil)
if err := a.put_kv(Int(3), 1); err != nil {
if err := a.put_kv(Int(3), Int(1)); err != nil {
t.Error(err) t.Error(err)
} }
if err := a.put_kv(Int(3), 2); err != nil {
if err := a.put_kv(Int(3), Int(2)); err != nil {
t.Error(err) t.Error(err)
} }
if err := b.put_kv(Int(3), 3); err != nil {
if err := b.put_kv(Int(3), Int(3)); err != nil {
t.Error(err) t.Error(err)
} }
if err := b.put_kv(Int(3), 4); err != nil {
if err := b.put_kv(Int(3), Int(4)); err != nil {
t.Error(err) t.Error(err)
} }
if err := c.put_kv(Int(3), 5); err != nil {
if err := c.put_kv(Int(3), Int(5)); err != nil {
t.Error(err) t.Error(err)
} }
if err := c.put_kv(Int(3), 6); err != nil {
if err := c.put_kv(Int(3), Int(6)); err != nil {
t.Error(err) t.Error(err)
} }
if err := d.put_kv(Int(4), 6); err != nil {
if err := d.put_kv(Int(4), Int(6)); err != nil {
t.Error(err) t.Error(err)
} }
p, q, err := a.leaf_insert(Int(2), 1)
p, q, err := a.leaf_insert(Int(2), Int(1))
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} else { } else {
@ -890,28 +890,28 @@ func Test_pure_leaf_split_less(t *testing.T) {
insert_linked_list_node(c, b, nil) insert_linked_list_node(c, b, nil)
d := NewLeaf(2, false) d := NewLeaf(2, false)
insert_linked_list_node(d, c, nil) insert_linked_list_node(d, c, nil)
if err := a.put_kv(Int(3), 1); err != nil {
if err := a.put_kv(Int(3), Int(1)); err != nil {
t.Error(err) t.Error(err)
} }
if err := a.put_kv(Int(3), 2); err != nil {
if err := a.put_kv(Int(3), Int(2)); err != nil {
t.Error(err) t.Error(err)
} }
if err := b.put_kv(Int(3), 3); err != nil {
if err := b.put_kv(Int(3), Int(3)); err != nil {
t.Error(err) t.Error(err)
} }
if err := b.put_kv(Int(3), 4); err != nil {
if err := b.put_kv(Int(3), Int(4)); err != nil {
t.Error(err) t.Error(err)
} }
if err := c.put_kv(Int(3), 5); err != nil {
if err := c.put_kv(Int(3), Int(5)); err != nil {
t.Error(err) t.Error(err)
} }
if err := c.put_kv(Int(3), 6); err != nil {
if err := c.put_kv(Int(3), Int(6)); err != nil {
t.Error(err) t.Error(err)
} }
if err := d.put_kv(Int(4), 6); err != nil {
if err := d.put_kv(Int(4), Int(6)); err != nil {
t.Error(err) t.Error(err)
} }
p, q, err := a.pure_leaf_split(Int(2), 1)
p, q, err := a.pure_leaf_split(Int(2), Int(1))
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} else { } else {
@ -963,25 +963,25 @@ func Test_pure_leaf_split_equal(t *testing.T) {
insert_linked_list_node(c, b, nil) insert_linked_list_node(c, b, nil)
d := NewLeaf(2, false) d := NewLeaf(2, false)
insert_linked_list_node(d, c, nil) insert_linked_list_node(d, c, nil)
if err := a.put_kv(Int(3), 1); err != nil {
if err := a.put_kv(Int(3), Int(1)); err != nil {
t.Error(err) t.Error(err)
} }
if err := a.put_kv(Int(3), 2); err != nil {
if err := a.put_kv(Int(3), Int(2)); err != nil {
t.Error(err) t.Error(err)
} }
if err := b.put_kv(Int(3), 3); err != nil {
if err := b.put_kv(Int(3), Int(3)); err != nil {
t.Error(err) t.Error(err)
} }
if err := b.put_kv(Int(3), 4); err != nil {
if err := b.put_kv(Int(3), Int(4)); err != nil {
t.Error(err) t.Error(err)
} }
if err := c.put_kv(Int(3), 5); err != nil {
if err := c.put_kv(Int(3), Int(5)); err != nil {
t.Error(err) t.Error(err)
} }
if err := d.put_kv(Int(4), 6); err != nil {
if err := d.put_kv(Int(4), Int(6)); err != nil {
t.Error(err) t.Error(err)
} }
p, q, err := a.pure_leaf_split(Int(3), 1)
p, q, err := a.pure_leaf_split(Int(3), Int(1))
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} else { } else {
@ -1027,25 +1027,25 @@ func Test_pure_leaf_split_greater(t *testing.T) {
insert_linked_list_node(c, b, nil) insert_linked_list_node(c, b, nil)
d := NewLeaf(2, false) d := NewLeaf(2, false)
insert_linked_list_node(d, c, nil) insert_linked_list_node(d, c, nil)
if err := a.put_kv(Int(3), 1); err != nil {
if err := a.put_kv(Int(3), Int(1)); err != nil {
t.Error(err) t.Error(err)
} }
if err := a.put_kv(Int(3), 2); err != nil {
if err := a.put_kv(Int(3), Int(2)); err != nil {
t.Error(err) t.Error(err)
} }
if err := b.put_kv(Int(3), 3); err != nil {
if err := b.put_kv(Int(3), Int(3)); err != nil {
t.Error(err) t.Error(err)
} }
if err := b.put_kv(Int(3), 4); err != nil {
if err := b.put_kv(Int(3), Int(4)); err != nil {
t.Error(err) t.Error(err)
} }
if err := c.put_kv(Int(3), 5); err != nil {
if err := c.put_kv(Int(3), Int(5)); err != nil {
t.Error(err) t.Error(err)
} }
if err := d.put_kv(Int(5), 6); err != nil {
if err := d.put_kv(Int(5), Int(6)); err != nil {
t.Error(err) t.Error(err)
} }
p, q, err := a.pure_leaf_split(Int(4), 1)
p, q, err := a.pure_leaf_split(Int(4), Int(1))
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} else { } else {
@ -1097,25 +1097,25 @@ func Test_find_end_of_pure_run(t *testing.T) {
insert_linked_list_node(c, b, nil) insert_linked_list_node(c, b, nil)
d := NewLeaf(2, false) d := NewLeaf(2, false)
insert_linked_list_node(d, c, nil) insert_linked_list_node(d, c, nil)
if err := a.put_kv(Int(3), 1); err != nil {
if err := a.put_kv(Int(3), Int(1)); err != nil {
t.Error(err) t.Error(err)
} }
if err := a.put_kv(Int(3), 2); err != nil {
if err := a.put_kv(Int(3), Int(2)); err != nil {
t.Error(err) t.Error(err)
} }
if err := b.put_kv(Int(3), 3); err != nil {
if err := b.put_kv(Int(3), Int(3)); err != nil {
t.Error(err) t.Error(err)
} }
if err := b.put_kv(Int(3), 4); err != nil {
if err := b.put_kv(Int(3), Int(4)); err != nil {
t.Error(err) t.Error(err)
} }
if err := c.put_kv(Int(3), 5); err != nil {
if err := c.put_kv(Int(3), Int(5)); err != nil {
t.Error(err) t.Error(err)
} }
if err := c.put_kv(Int(3), 6); err != nil {
if err := c.put_kv(Int(3), Int(6)); err != nil {
t.Error(err) t.Error(err)
} }
if err := d.put_kv(Int(4), 6); err != nil {
if err := d.put_kv(Int(4), Int(6)); err != nil {
t.Error(err) t.Error(err)
} }
e := a.find_end_of_pure_run() e := a.find_end_of_pure_run()
@ -1237,13 +1237,13 @@ func Test_remove_linked_list_node(t *testing.T) {
func Test_balance_leaf_nodes_with_dup(t *testing.T) { func Test_balance_leaf_nodes_with_dup(t *testing.T) {
a := NewLeaf(3, false) a := NewLeaf(3, false)
b := NewLeaf(3, false) b := NewLeaf(3, false)
if err := a.put_kv(Int(1), 1); err != nil {
if err := a.put_kv(Int(1), Int(1)); err != nil {
t.Error(err) t.Error(err)
} }
if err := a.put_kv(Int(1), 1); err != nil {
if err := a.put_kv(Int(1), Int(1)); err != nil {
t.Error(err) t.Error(err)
} }
if err := a.put_kv(Int(2), 1); err != nil {
if err := a.put_kv(Int(2), Int(1)); err != nil {
t.Error(err) t.Error(err)
} }
balance_nodes(a, b) balance_nodes(a, b)
@ -1258,25 +1258,25 @@ func Test_balance_leaf_nodes_with_dup(t *testing.T) {
func Test_balance_leaf_nodes(t *testing.T) { func Test_balance_leaf_nodes(t *testing.T) {
a := NewLeaf(7, false) a := NewLeaf(7, false)
b := NewLeaf(7, false) b := NewLeaf(7, false)
if err := a.put_kv(Int(1), 1); err != nil {
if err := a.put_kv(Int(1), Int(1)); err != nil {
t.Error(err) t.Error(err)
} }
if err := a.put_kv(Int(2), 2); err != nil {
if err := a.put_kv(Int(2), Int(2)); err != nil {
t.Error(err) t.Error(err)
} }
if err := a.put_kv(Int(3), 3); err != nil {
if err := a.put_kv(Int(3), Int(3)); err != nil {
t.Error(err) t.Error(err)
} }
if err := a.put_kv(Int(4), 4); err != nil {
if err := a.put_kv(Int(4), Int(4)); err != nil {
t.Error(err) t.Error(err)
} }
if err := a.put_kv(Int(5), 5); err != nil {
if err := a.put_kv(Int(5), Int(5)); err != nil {
t.Error(err) t.Error(err)
} }
if err := a.put_kv(Int(6), 6); err != nil {
if err := a.put_kv(Int(6), Int(6)); err != nil {
t.Error(err) t.Error(err)
} }
if err := a.put_kv(Int(7), 7); err != nil {
if err := a.put_kv(Int(7), Int(7)); err != nil {
t.Error(err) t.Error(err)
} }
balance_nodes(a, b) balance_nodes(a, b)
@ -1291,12 +1291,12 @@ func Test_balance_leaf_nodes(t *testing.T) {
} }
} }
for i, v := range a.values { for i, v := range a.values {
if v.(int) != i+1 {
if int(v.(Int)) != i+1 {
t.Errorf("k != %d", i+1) t.Errorf("k != %d", i+1)
} }
} }
for i, v := range b.values { for i, v := range b.values {
if v.(int) != 3+i+1 {
if int(v.(Int)) != 3+i+1 {
t.Errorf("v != %d", 3+i+1) t.Errorf("v != %d", 3+i+1)
} }
} }
@ -1413,10 +1413,14 @@ func read(p []byte, int63 func() int64, readVal *int64, readPos *int8) (n int, e
type T testing.T type T testing.T
func (t *T) Assert(ok bool, msg string, vars ...interface{}) {
func (t *T) Assert(ok bool, msg string, vars ...ItemValue) {
if !ok { if !ok {
t.Log("\n" + string(debug.Stack())) t.Log("\n" + string(debug.Stack()))
t.Fatalf(msg, vars...)
var objects []interface{}
for _, t := range vars {
objects = append(objects, t)
}
t.Fatalf(msg, objects...)
} }
} }

18
weed/util/bptree/types.go

@ -25,26 +25,26 @@ func NegativeSize() error {
return errors.New("negative size") return errors.New("negative size")
} }
type Iterator func() (item interface{}, next Iterator)
type Iterator func() (item ItemValue, next Iterator)
type KIterator func() (key Hashable, next KIterator) type KIterator func() (key Hashable, next KIterator)
type KVIterator func() (key Hashable, value interface{}, next KVIterator)
type KVIterator func() (key Hashable, value ItemValue, next KVIterator)
type KVIterable interface { type KVIterable interface {
Iterate() KVIterator Iterate() KVIterator
} }
type MapOperable interface { type MapOperable interface {
Has(key Hashable) bool Has(key Hashable) bool
Put(key Hashable, value interface{}) (err error)
Get(key Hashable) (value interface{}, err error)
Remove(key Hashable) (value interface{}, err error)
Put(key Hashable, value ItemValue) (err error)
Get(key Hashable) (value ItemValue, err error)
Remove(key Hashable) (value ItemValue, err error)
} }
type WhereFunc func(value interface{}) bool
type WhereFunc func(value ItemValue) bool
func MakeValuesIterator(obj KVIterable) Iterator { func MakeValuesIterator(obj KVIterable) Iterator {
kv_iterator := obj.Iterate() kv_iterator := obj.Iterate()
var v_iterator Iterator var v_iterator Iterator
v_iterator = func() (value interface{}, next Iterator) {
v_iterator = func() (value ItemValue, next Iterator) {
_, value, kv_iterator = kv_iterator() _, value, kv_iterator = kv_iterator()
if kv_iterator == nil { if kv_iterator == nil {
return nil, nil return nil, nil
@ -58,7 +58,7 @@ func MakeItemsIterator(obj KVIterable) (kit KIterator) {
kv_iterator := obj.Iterate() kv_iterator := obj.Iterate()
kit = func() (item Hashable, next KIterator) { kit = func() (item Hashable, next KIterator) {
var key Hashable var key Hashable
var value interface{}
var value ItemValue
key, value, kv_iterator = kv_iterator() key, value, kv_iterator = kv_iterator()
if kv_iterator == nil { if kv_iterator == nil {
return nil, nil return nil, nil
@ -70,7 +70,7 @@ func MakeItemsIterator(obj KVIterable) (kit KIterator) {
type MapEntry struct { type MapEntry struct {
Key Hashable Key Hashable
Value interface{}
Value ItemValue
} }
func (m *MapEntry) Equals(other Equatable) bool { func (m *MapEntry) Equals(other Equatable) bool {

Loading…
Cancel
Save