Skip to content

Commit

Permalink
Add new pruners implementation
Browse files Browse the repository at this point in the history
Co-authored-by: Gabriel Díaz <[email protected]>
  • Loading branch information
aalda and gdiazlo committed Feb 25, 2019
1 parent 6445c72 commit 6aa6af5
Show file tree
Hide file tree
Showing 15 changed files with 1,436 additions and 10 deletions.
12 changes: 2 additions & 10 deletions balloon/history/navigation/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,14 @@ func (p Position) IsLeaf() bool {

func (p Position) FirstDescendant() *Position {
if p.IsLeaf() {
return nil
return &p
}
return NewPosition(p.Index, 0)
}

func (p Position) LastDescendant() *Position {
if p.IsLeaf() {
return nil
return &p
}
return NewPosition(p.Index+1<<p.Height-1, 0)
}

func (p Position) IsAncestorOf(pos *Position) bool {
return p.FirstDescendant().Index <= pos.Index && pos.Index <= p.LastDescendant().Index
}

func (p Position) IsDescendantOf(pos *Position) bool {
return pos.FirstDescendant().Index <= p.Index && p.Index <= pos.LastDescendant().Index
}
18 changes: 18 additions & 0 deletions balloon/history/pruning/insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"github.com/bbva/qed/balloon/cache"
"github.com/bbva/qed/balloon/history/visit"
"github.com/bbva/qed/hashing"
"github.com/bbva/qed/log"
"github.com/bbva/qed/testutils/rand"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -119,3 +121,19 @@ func TestInsertPruner(t *testing.T) {
}

}

func BenchmarkInsertPruner(b *testing.B) {

log.SetLogger("BenchmarkInsertPruner", log.SILENT)

cache := cache.NewFakeCache(hashing.Digest{0x0})

b.ResetTimer()
for i := uint64(0); i < uint64(b.N); i++ {
eventDigest := rand.Bytes(32)
context := NewPruningContext(NewSingleTargetedCacheResolver(i), cache)
_, err := NewInsertPruner(i, eventDigest, context).Prune()
assert.NoError(b, err)
}

}
16 changes: 16 additions & 0 deletions balloon/history/pruning/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/bbva/qed/balloon/cache"
"github.com/bbva/qed/balloon/history/visit"
"github.com/bbva/qed/hashing"
"github.com/bbva/qed/log"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -322,3 +323,18 @@ func TestSearchPrunerIncremental(t *testing.T) {
}

}

func BenchmarkSearchPruner(b *testing.B) {

log.SetLogger("BenchmarkSearchPruner", log.SILENT)

cache := cache.NewFakeCache(hashing.Digest{0x0})

b.ResetTimer()
for i := uint64(0); i < uint64(b.N); i++ {
context := NewPruningContext(NewDoubleTargetedCacheResolver(0, i), cache)
_, err := NewSearchPruner(i, context).Prune()
assert.NoError(b, err)
}

}
122 changes: 122 additions & 0 deletions balloon/history/pruning5/consistency.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package pruning5

import (
"sort"

"github.com/bbva/qed/balloon/history/navigation"
)

type Targets []uint64

func (t Targets) InsertSorted(version uint64) Targets {

if len(t) == 0 {
t = append(t, version)
return t
}

index := sort.Search(len(t), func(i int) bool {
return t[i] > version
})

if index > 0 && t[index-1] == version {
return t
}

t = append(t, version)
copy(t[index+1:], t[index:])
t[index] = version
return t

}

func (t Targets) Split(version uint64) (left, right Targets) {
// the smallest index i where t[i] >= version
index := sort.Search(len(t), func(i int) bool {
return t[i] >= version //bytes.Compare(r[i].Key, key) >= 0
})
return t[:index], t[index:]
}

func PruneToFindConsistent(index, version uint64) Operation {

var traverse func(pos *navigation.Position, targets Targets, shortcut bool) Operation

traverse = func(pos *navigation.Position, targets Targets, shortcut bool) Operation {

if len(targets) == 0 {
if !shortcut {
return NewCollectOp(NewGetCacheOp(pos))
}
return NewGetCacheOp(pos)
}

if pos.IsLeaf() {
if pos.Index == index {
return NewLeafHashOp(pos, nil)
}
if !shortcut {
return NewCollectOp(NewGetCacheOp(pos))
}
return NewGetCacheOp(pos)
}

if len(targets) == 1 && targets[0] != index {
if !shortcut {
return NewCollectOp(traverse(pos, targets, true))
}
}

rightPos := pos.Right()
leftTargets, rightTargets := targets.Split(rightPos.Index)

left := traverse(pos.Left(), leftTargets, shortcut)
right := traverse(rightPos, rightTargets, shortcut)

if version < rightPos.Index {
return NewPartialInnerHashOp(pos, left)
}

return NewInnerHashOp(pos, left, right)
}

targets := make(Targets, 0)
targets = targets.InsertSorted(index)
targets = targets.InsertSorted(version)
return traverse(navigation.NewRootPosition(version), targets, false)

}

func PruneToCheckConsistency(start, end uint64) Operation {

var traverse func(pos *navigation.Position, targets Targets) Operation

traverse = func(pos *navigation.Position, targets Targets) Operation {

if len(targets) == 0 {
return NewCollectOp(NewGetCacheOp(pos))
}

if pos.IsLeaf() {
return NewCollectOp(NewGetCacheOp(pos))
}

rightPos := pos.Right()
leftTargets, rightTargets := targets.Split(rightPos.Index)

left := traverse(pos.Left(), leftTargets)
right := traverse(rightPos, rightTargets)

if end < rightPos.Index {
return NewPartialInnerHashOp(pos, left)
}

return NewInnerHashOp(pos, left, right)
}

targets := make(Targets, 0)
targets = targets.InsertSorted(start)
targets = targets.InsertSorted(end)
return traverse(navigation.NewRootPosition(end), targets)

}
Loading

0 comments on commit 6aa6af5

Please sign in to comment.