From f991aa89ab04027577b32ef7e3327224beb6855b Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Mon, 4 Nov 2024 20:17:34 +0700 Subject: [PATCH] save --- erigon-lib/seg/decompress.go | 21 +++++++++++++++++++++ erigon-lib/state/history.go | 14 ++------------ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/erigon-lib/seg/decompress.go b/erigon-lib/seg/decompress.go index 44b890b064e..916b8a10db7 100644 --- a/erigon-lib/seg/decompress.go +++ b/erigon-lib/seg/decompress.go @@ -23,6 +23,7 @@ import ( "fmt" "os" "path/filepath" + "sort" "strconv" "sync/atomic" "time" @@ -1064,3 +1065,23 @@ func (g *Getter) FastNext(buf []byte) ([]byte, uint64) { g.dataBit = 0 return buf[:wordLen], postLoopPos } + +func (g *Getter) BinarySearch(fromPrefix []byte, count int, f func(i uint64) (offset uint64)) (foundOffset uint64) { + //n := item.src.decompressor.Count() / 2 + foundItem := sort.Search(count, func(i int) bool { + offset := f(uint64(i)) + g.Reset(offset) + if g.HasNext() { + key, _ := g.Next(nil) + fmt.Printf("bs: %x, %x\n", fromPrefix, key) + return bytes.Compare(fromPrefix, key) >= 0 + } + return false + }) + if foundItem < count { + fmt.Printf("[dbg] bs1 !ok, %x, %d\n", fromPrefix, f(uint64(foundItem))) + return f(uint64(foundItem)) + } + fmt.Printf("[dbg] bs2 !ok, %x, %d\n", fromPrefix, 0) + return 0 +} diff --git a/erigon-lib/state/history.go b/erigon-lib/state/history.go index 8880e15dfa6..5988db11157 100644 --- a/erigon-lib/state/history.go +++ b/erigon-lib/state/history.go @@ -26,7 +26,6 @@ import ( "math" "path/filepath" "regexp" - "sort" "strconv" "sync" "time" @@ -1366,17 +1365,8 @@ func (ht *HistoryRoTx) WalkAsOf(ctx context.Context, startTxNum uint64, from, to var offset uint64 if len(from) > 0 { n := item.src.decompressor.Count() / 2 - found := sort.Search(n, func(i int) bool { - offset = idx.OrdinalLookup(uint64(i)) - g.Reset(offset) - if g.HasNext() { - key, _ := g.Next(nil) - fmt.Printf("bs: %x, %x\n", from, key) - return bytes.Compare(from, key) >= 0 - } - return false - }) - if found < n { + found := item.getter.BinarySearch(from, n, idx.OrdinalLookup) + if int(found) < n { fmt.Printf("[dbg] bs1 !ok, %x, %d\n", from, offset) } else { fmt.Printf("[dbg] bs2 !ok, %x, %d\n", from, offset)