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

8
weed/util/bptree/bptree.go

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

9
weed/util/bptree/bptree_test.go

@ -110,9 +110,6 @@ func TestAddHasCountFindIterateRemove(t *testing.T) {
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
if bpt.Size() != (i + 1) {
t.Error("size was wrong", bpt.Size(), i+1)
}
} }
for i, r := range recs { for i, r := range recs {
@ -236,9 +233,6 @@ func TestBpMap(t *testing.T) {
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
if table.Size() != (i + 1) {
t.Error("size was wrong", table.Size(), i+1)
}
} }
for _, r := range recs { for _, r := range recs {
@ -274,9 +268,6 @@ func TestBpMap(t *testing.T) {
t.Error("wrong value") 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 Iterate() KVIterator
} }
type Sized interface {
Size() int
}
type MapOperable interface { type MapOperable interface {
Sized
Has(key Hashable) bool Has(key Hashable) bool
Put(key Hashable, value interface{}) (err error) Put(key Hashable, value interface{}) (err error)
Get(key Hashable) (value interface{}, err error) Get(key Hashable) (value interface{}, err error)

Loading…
Cancel
Save