add back non_dedup

This commit is contained in:
Chris Lu
2021-08-21 15:13:13 -07:00
parent 849f185a20
commit 38c8470d1d
4 changed files with 83 additions and 51 deletions

View File

@@ -6,6 +6,7 @@ type BpNode struct {
pointers []*BpNode
next *BpNode
prev *BpNode
no_dup bool
}
func NewInternal(size int) *BpNode {
@@ -18,13 +19,14 @@ func NewInternal(size int) *BpNode {
}
}
func NewLeaf(size int) *BpNode {
func NewLeaf(size int, no_dup bool) *BpNode {
if size < 0 {
panic(NegativeSize())
}
return &BpNode{
keys: make([]Hashable, 0, size),
values: make([]interface{}, 0, size),
no_dup: no_dup,
}
}
@@ -274,6 +276,13 @@ func (self *BpNode) leaf_insert(key Hashable, value interface{}) (a, b *BpNode,
if self.Internal() {
return nil, nil, BpTreeError("Expected a leaf node")
}
if self.no_dup {
i, has := self.find(key)
if has {
self.values[i] = value
return self, nil, nil
}
}
if self.Full() {
return self.leaf_split(key, value)
} else {
@@ -298,7 +307,7 @@ func (self *BpNode) leaf_split(key Hashable, value interface{}) (a, b *BpNode, e
return self.pure_leaf_split(key, value)
}
a = self
b = NewLeaf(self.NodeSize())
b = NewLeaf(self.NodeSize(), self.no_dup)
insert_linked_list_node(b, a, a.getNext())
balance_nodes(a, b)
if key.Less(b.keys[0]) {
@@ -330,7 +339,7 @@ func (self *BpNode) pure_leaf_split(key Hashable, value interface{}) (a, b *BpNo
return nil, nil, BpTreeError("Expected a pure leaf node")
}
if key.Less(self.keys[0]) {
a = NewLeaf(self.NodeSize())
a = NewLeaf(self.NodeSize(), self.no_dup)
b = self
if err := a.put_kv(key, value); err != nil {
return nil, nil, err
@@ -346,7 +355,7 @@ func (self *BpNode) pure_leaf_split(key Hashable, value interface{}) (a, b *BpNo
}
return a, nil, nil
} else {
b = NewLeaf(self.NodeSize())
b = NewLeaf(self.NodeSize(), self.no_dup)
if err := b.put_kv(key, value); err != nil {
return nil, nil, err
}