Browse Source

remove size since each put/get will have to update the root node

pull/2354/head
Chris Lu 3 years ago
parent
commit
2d237da74a
  1. 10
      weed/util/bptree/bpmap.go
  2. 8
      weed/util/bptree/bptree.go
  3. 9
      weed/util/bptree/bptree_test.go
  4. 5
      weed/util/bptree/types.go

10
weed/util/bptree/bpmap.go

@ -12,28 +12,19 @@ type BpMap BpTree
func NewBpMap(node_size int) *BpMap {
return &BpMap{
root: NewLeaf(node_size, true),
size: 0,
}
}
func (self *BpMap) Size() int {
return (*BpTree)(self).Size()
}
func (self *BpMap) Has(key Hashable) bool {
return (*BpTree)(self).Has(key)
}
func (self *BpMap) Put(key Hashable, value interface{}) (err error) {
had := self.Has(key)
new_root, err := self.root.put(key, value)
if err != nil {
return err
}
self.root = new_root
if !had {
self.size += 1
}
return nil
}
@ -60,7 +51,6 @@ func (self *BpMap) Remove(key Hashable) (value interface{}, err error) {
} else {
self.root = new_root
}
self.size--
return value, nil
}

8
weed/util/bptree/bptree.go

@ -8,7 +8,6 @@ package bptree
*/
type BpTree struct {
root *BpNode
size int
}
type loc_iterator func() (i int, leaf *BpNode, li loc_iterator)
@ -16,14 +15,9 @@ type loc_iterator func() (i int, leaf *BpNode, li loc_iterator)
func NewBpTree(node_size int) *BpTree {
return &BpTree{
root: NewLeaf(node_size, false),
size: 0,
}
}
func (self *BpTree) Size() int {
return self.size
}
func (self *BpTree) Has(key Hashable) bool {
if len(self.root.keys) == 0 {
return false
@ -52,7 +46,6 @@ func (self *BpTree) Add(key Hashable, value interface{}) (err error) {
return err
}
self.root = new_root
self.size += 1
return nil
}
@ -100,7 +93,6 @@ func (self *BpTree) RemoveWhere(key Hashable, where WhereFunc) (err error) {
} else {
self.root = new_root
}
self.size -= 1
return nil
}

9
weed/util/bptree/bptree_test.go

@ -110,9 +110,6 @@ func TestAddHasCountFindIterateRemove(t *testing.T) {
if err != nil {
t.Error(err)
}
if bpt.Size() != (i + 1) {
t.Error("size was wrong", bpt.Size(), i+1)
}
}
for i, r := range recs {
@ -236,9 +233,6 @@ func TestBpMap(t *testing.T) {
if err != nil {
t.Error(err)
}
if table.Size() != (i + 1) {
t.Error("size was wrong", table.Size(), i+1)
}
}
for _, r := range recs {
@ -274,9 +268,6 @@ func TestBpMap(t *testing.T) {
t.Error("wrong value")
}
}
if table.Size() != (len(recs) - (i + 1)) {
t.Error("size was wrong", table.Size(), (len(recs) - (i + 1)))
}
}
}

5
weed/util/bptree/types.go

@ -32,12 +32,7 @@ type KVIterable interface {
Iterate() KVIterator
}
type Sized interface {
Size() int
}
type MapOperable interface {
Sized
Has(key Hashable) bool
Put(key Hashable, value interface{}) (err error)
Get(key Hashable) (value interface{}, err error)

Loading…
Cancel
Save