Skip to content

Commit

Permalink
Fix hyper tree's pruner algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
aalda committed Dec 5, 2018
1 parent f131ab9 commit ee813c6
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 32 deletions.
6 changes: 3 additions & 3 deletions balloon/hyper/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ func NewSingleTargetedCacheResolver(numBits, cacheLevel uint16, targetKey []byte
}

func (r SingleTargetedCacheResolver) ShouldBeInCache(pos navigator.Position) bool {
return pos.Height() > r.cacheLevel && !r.IsOnPath(pos)
return pos.Height() >= r.cacheLevel && !r.IsOnPath(pos)
}

func (r SingleTargetedCacheResolver) ShouldCache(pos navigator.Position) bool {
return pos.Height() > r.cacheLevel
return pos.Height() == r.cacheLevel
}

func (r SingleTargetedCacheResolver) ShouldCollect(pos navigator.Position) bool {
return pos.Height() == r.cacheLevel+1
return pos.Height() == r.cacheLevel
}

/*
Expand Down
6 changes: 3 additions & 3 deletions balloon/hyper/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestShouldBeInCache(t *testing.T) {
expectedResult bool
}{
{"Position on path", 8, 3, []byte{0}, NewPosition([]byte{0}, 2), false},
{"Height <= cacheLevel", 8, 3, []byte{0}, NewPosition([]byte{8}, 3), false},
{"Height <= cacheLevel", 8, 3, []byte{0}, NewPosition([]byte{8}, 4), false},
{"All conditions", 8, 3, []byte{0}, NewPosition([]byte{16}, 4), true},
}

Expand All @@ -53,8 +53,8 @@ func TestShouldCache(t *testing.T) {
expectedResult bool
}{
{8, 3, []byte{0}, NewPosition([]byte{0}, 0), false},
{8, 3, []byte{0}, NewPosition([]byte{0}, 3), false},
{8, 3, []byte{0}, NewPosition([]byte{0}, 4), true},
{8, 3, []byte{0}, NewPosition([]byte{0}, 4), false},
{8, 3, []byte{0}, NewPosition([]byte{0}, 3), true},
}

for i, test := range testCases {
Expand Down
52 changes: 38 additions & 14 deletions balloon/hyper/pruner.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ func (p *InsertPruner) traverse(pos navigator.Position, leaves storage.KVRange)
}

// if we are over the cache level, we need to do a range query to get the leaves
if !p.cacheResolver.ShouldCache(pos) {
var atLastLevel bool
if atLastLevel = p.cacheResolver.ShouldCache(pos); atLastLevel {
//fmt.Println(pos.Height())
first := p.navigator.DescendToFirst(pos)
last := p.navigator.DescendToLast(pos)

Expand All @@ -79,17 +81,28 @@ func (p *InsertPruner) traverse(pos navigator.Position, leaves storage.KVRange)
kvRange = kvRange.InsertSorted(l)
}
leaves = kvRange

return p.traverseWithoutCache(pos, leaves)
}

rightPos := p.navigator.GoToRight(pos)
leftPos := p.navigator.GoToLeft(pos)
leftSlice, rightSlice := leaves.Split(rightPos.Index())
left, err := p.traverse(p.navigator.GoToLeft(pos), leftSlice)
if err != nil {
return nil, err

var left, right visitor.Visitable
var err error
if atLastLevel {
left, err = p.traverseWithoutCache(leftPos, leftSlice)
if err != nil {
return nil, err
}
right, err = p.traverseWithoutCache(rightPos, rightSlice)

} else {
left, err = p.traverse(leftPos, leftSlice)
if err != nil {
return nil, err
}
right, err = p.traverse(rightPos, rightSlice)
}
right, err := p.traverse(rightPos, rightSlice)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -162,7 +175,8 @@ func (p *SearchPruner) traverseCache(pos navigator.Position, leaves storage.KVRa
}

// if we are over the cache level, we need to do a range query to get the leaves
if !p.cacheResolver.ShouldCache(pos) {
var atLastLevel bool
if atLastLevel = p.cacheResolver.ShouldCache(pos); atLastLevel {
first := p.navigator.DescendToFirst(pos)
last := p.navigator.DescendToLast(pos)
kvRange, _ := p.store.GetRange(storage.IndexPrefix, first.Index(), last.Index())
Expand All @@ -172,17 +186,27 @@ func (p *SearchPruner) traverseCache(pos navigator.Position, leaves storage.KVRa
kvRange = kvRange.InsertSorted(l)
}
leaves = kvRange
return p.traverse(pos, leaves)
}

rightPos := p.navigator.GoToRight(pos)
leftPos := p.navigator.GoToLeft(pos)
leftSlice, rightSlice := leaves.Split(rightPos.Index())
left, err := p.traverseCache(p.navigator.GoToLeft(pos), leftSlice)
if err != nil {
return nil, err
}

right, err := p.traverseCache(rightPos, rightSlice)
var left, right visitor.Visitable
var err error
if atLastLevel {
left, err = p.traverse(leftPos, leftSlice)
if err != nil {
return nil, err
}
right, err = p.traverse(rightPos, rightSlice)
} else {
left, err = p.traverseCache(leftPos, leftSlice)
if err != nil {
return nil, err
}
right, err = p.traverseCache(rightPos, rightSlice)
}
if err != nil {
return nil, err
}
Expand Down
22 changes: 11 additions & 11 deletions balloon/hyper/pruner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,17 @@ func TestInsertPruner(t *testing.T) {
expectedPruned: root(pos(0, 8),
cacheable(node(pos(0, 7),
cacheable(node(pos(0, 6),
collectable(cacheable(node(pos(0, 5),
node(pos(0, 4),
cacheable(node(pos(0, 5),
collectable(cacheable(node(pos(0, 4),
node(pos(0, 3),
node(pos(0, 2),
node(pos(0, 1),
leaf(pos(0, 0), 0),
cached(pos(1, 0))),
cached(pos(2, 1))),
cached(pos(4, 2))),
cached(pos(8, 3))),
cached(pos(16, 4))))),
cached(pos(8, 3))))),
cached(pos(16, 4)))),
cached(pos(32, 5)))),
cached(pos(64, 6)))),
cached(pos(128, 7)),
Expand All @@ -103,8 +103,8 @@ func TestInsertPruner(t *testing.T) {
expectedPruned: root(pos(0, 8),
cacheable(node(pos(0, 7),
cacheable(node(pos(0, 6),
collectable(cacheable(node(pos(0, 5),
node(pos(0, 4),
cacheable(node(pos(0, 5),
collectable(cacheable(node(pos(0, 4),
node(pos(0, 3),
node(pos(0, 2),
node(pos(0, 1),
Expand All @@ -114,8 +114,8 @@ func TestInsertPruner(t *testing.T) {
leaf(pos(2, 0), 1),
cached(pos(3, 0)))),
cached(pos(4, 2))),
cached(pos(8, 3))),
cached(pos(16, 4))))),
cached(pos(8, 3))))),
cached(pos(16, 4)))),
cached(pos(32, 5)))),
cached(pos(64, 6)))),
cached(pos(128, 7)),
Expand All @@ -134,17 +134,17 @@ func TestInsertPruner(t *testing.T) {
cached(pos(128, 6)),
cacheable(node(pos(192, 6),
cached(pos(192, 5)),
collectable(cacheable(node(pos(224, 5),
cacheable(node(pos(224, 5),
cached(pos(224, 4)),
node(pos(240, 4),
collectable(cacheable(node(pos(240, 4),
cached(pos(240, 3)),
node(pos(248, 3),
cached(pos(248, 2)),
node(pos(252, 2),
cached(pos(252, 1)),
node(pos(254, 1),
cached(pos(254, 0)),
leaf(pos(255, 0), 2)))))))))),
leaf(pos(255, 0), 2))))))))))),
)),
),
},
Expand Down
2 changes: 1 addition & 1 deletion balloon/hyper/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func (t *HyperTree) RebuildCache() error {
func (t *HyperTree) populateCache(pos navigator.Position, nav navigator.TreeNavigator) hashing.Digest {
stats := metrics.Hyper
stats.Add("populateCache_hits", 1)
if pos.Height() == t.cacheLevel+1 {
if pos.Height() == t.cacheLevel {
cached, ok := t.cache.Get(pos)
if !ok {
return nil
Expand Down

0 comments on commit ee813c6

Please sign in to comment.