Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] prefix trie inside b-index #12575

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions erigon-lib/state/aggregator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"encoding/binary"
"encoding/hex"
"fmt"
"github.com/erigontech/erigon-lib/commitment"
"math"
"math/rand"
"os"
Expand All @@ -33,6 +32,8 @@ import (
"testing"
"time"

"github.com/erigontech/erigon-lib/commitment"

"github.com/erigontech/erigon-lib/common/background"

"github.com/c2h5oh/datasize"
Expand Down Expand Up @@ -424,13 +425,24 @@ func TestNewBtIndex(t *testing.T) {
defer kv.Close()
require.NotNil(t, kv)
require.NotNil(t, bt)
require.Len(t, bt.bplus.mx, keyCount/int(DefaultBtreeM))

for i := 1; i < len(bt.bplus.mx); i++ {
require.NotZero(t, bt.bplus.mx[i].di)
require.NotZero(t, bt.bplus.mx[i].off)
require.NotEmpty(t, bt.bplus.mx[i].key)
if !UseBTrie {
require.Len(t, bt.bplus.mx, keyCount/int(DefaultBtreeM))
for i := 1; i < len(bt.bplus.mx); i++ {
require.NotZero(t, bt.bplus.mx[i].di)
require.NotZero(t, bt.bplus.mx[i].off)
require.NotEmpty(t, bt.bplus.mx[i].key)
}
} else {
require.NotNil(t, bt.trie)
topLevelChild := 0
for _, c := range bt.trie.child {
if c != nil {
topLevelChild++
}
}
require.NotZero(t, topLevelChild)
}

}

func TestAggregatorV3_PruneSmallBatches(t *testing.T) {
Expand Down Expand Up @@ -1280,10 +1292,10 @@ func TestAggregator_RebuildCommitmentBasedOnFiles(t *testing.T) {
compression := ac.d[kv.CommitmentDomain].d.compression
fnames := []string{}
for _, f := range ac.d[kv.CommitmentDomain].files {
k, stateVal, _, found, err := f.src.bindex.Get(keyCommitmentState, seg.NewReader(f.src.decompressor.MakeGetter(), compression))
stateVal, _, found, err := f.src.bindex.Get2(keyCommitmentState, seg.NewReader(f.src.decompressor.MakeGetter(), compression))
require.NoError(t, err)
require.True(t, found)
require.EqualValues(t, keyCommitmentState, k)
// require.EqualValues(t, keyCommitmentState, k)
rh, err := commitment.HexTrieExtractStateRoot(stateVal)
require.NoError(t, err)

Expand Down
64 changes: 18 additions & 46 deletions erigon-lib/state/bps_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,34 +130,24 @@ func (it *BpsTreeIterator) Next() bool {
return true
}

//// If data[i] == key, returns 0 (equal) and value, nil err
//// if data[i] <> key, returns comparation result and nil value and error -- to be able to compare later
//func (b *BpsTree) matchKeyValue(g ArchiveGetter, i uint64, key []byte) (int, []byte, error) {
// if i >= b.offt.Count() {
// return 0, nil, ErrBtIndexLookupBounds
// }
// if b.trace {
// fmt.Printf("match %d-%x count %d\n", i, key, b.offt.Count())
// }
// g.Reset(b.offt.Get(i))
// buf, _ := g.Next(nil)
// if !bytes.Equal(buf, key) {
// return bytes.Compare(buf, key), nil, nil
// }
// val, _ := g.Next(nil)
// return 0, val, nil
//}
// // If data[i] == key, returns 0 (equal) and value, nil err
// // if data[i] <> key, returns comparation result and nil value and error -- to be able to compare later
//
//func (b *BpsTree) lookupKeyWGetter(g ArchiveGetter, i uint64) ([]byte, uint64) {
// if i >= b.offt.Count() {
// return nil, 0
// func (b *BpsTree) matchKeyValue(g ArchiveGetter, i uint64, key []byte) (int, []byte, error) {
// if i >= b.offt.Count() {
// return 0, nil, ErrBtIndexLookupBounds
// }
// if b.trace {
// fmt.Printf("match %d-%x count %d\n", i, key, b.offt.Count())
// }
// g.Reset(b.offt.Get(i))
// buf, _ := g.Next(nil)
// if !bytes.Equal(buf, key) {
// return bytes.Compare(buf, key), nil, nil
// }
// val, _ := g.Next(nil)
// return 0, val, nil
// }
// o := b.offt.Get(i)
// g.Reset(o)
// buf, _ := g.Next(nil)
// return buf, o
//}

type Node struct {
key []byte
off uint64 // offset in kv file to key
Expand Down Expand Up @@ -304,29 +294,11 @@ func (b *BpsTree) Seek(g *seg.Reader, seekKey []byte) (key, value []byte, di uin
var m uint64
var cmp int
for l < r {
m = (l + r) >> 1
if r-l <= DefaultBtreeStartSkip { // found small range, faster to scan now
cmp, key, err = b.keyCmpFunc(seekKey, l, g, key[:0])
if err != nil {
return nil, nil, 0, false, err
}
if b.trace {
fmt.Printf("fs di:[%d %d] k: %x\n", l, r, key)
}
//fmt.Printf("N %d l %d cmp %d (found %x want %x)\n", b.offt.Count(), l, cmp, key, seekKey)
if cmp == 0 {
r = l
break
} else if cmp < 0 { //found key is greater than seekKey
if l+1 < b.offt.Count() {
l++
continue
}
}
r = l
break
m = l
}

m = (l + r) >> 1
cmp, key, err = b.keyCmpFunc(seekKey, m, g, key[:0])
if err != nil {
return nil, nil, 0, false, err
Expand Down
Loading
Loading