From 9c828f3d0bd2e36ee07bb96966f6549ade58897b Mon Sep 17 00:00:00 2001 From: "alex.sharov" Date: Thu, 7 Nov 2024 10:30:35 +0700 Subject: [PATCH] save --- erigon-lib/state/domain.go | 17 ++++++++--- erigon-lib/state/domain_test.go | 26 +++++++++++----- erigon-lib/state/history.go | 54 ++++++++++++++++++--------------- 3 files changed, 60 insertions(+), 37 deletions(-) diff --git a/erigon-lib/state/domain.go b/erigon-lib/state/domain.go index 96b770101dc..0ff092ccc78 100644 --- a/erigon-lib/state/domain.go +++ b/erigon-lib/state/domain.go @@ -1968,11 +1968,12 @@ func (dt *DomainRoTx) DomainRange(ctx context.Context, tx kv.Tx, fromKey, toKey return nil, err } fmt.Printf("[dbg] DomainRange 2: %s\n", dt.d.name) - lastestStateIt, err := dt.DomainRangeLatest(tx, fromKey, toKey, limit) - if err != nil { - return nil, err - } - return stream.UnionKV(histStateIt, lastestStateIt, limit), nil + //lastestStateIt, err := dt.DomainRangeLatest(tx, fromKey, toKey, limit) + //if err != nil { + // return nil, err + //} + //return stream.UnionKV(histStateIt, lastestStateIt, limit), nil + return stream.UnionKV(histStateIt, stream.EmptyKV, limit), nil } func (dt *DomainRoTx) DomainRangeLatest(roTx kv.Tx, fromKey, toKey []byte, limit int) (stream.KV, error) { @@ -2264,6 +2265,7 @@ func (hi *DomainLatestIterFile) init(dc *DomainRoTx) error { if key, value, err = valsCursor.Seek(hi.from); err != nil { return err } + fmt.Printf("[dbg] rangeLatest: %x\n", key) if key != nil && (hi.to == nil || bytes.Compare(key[:len(key)-8], hi.to) < 0) { k := key[:len(key)-8] stepBytes := key[len(key)-8:] @@ -2281,6 +2283,7 @@ func (hi *DomainLatestIterFile) init(dc *DomainRoTx) error { if key, value, err = valsCursor.Seek(hi.from); err != nil { return err } + fmt.Printf("[dbg] rangeLatest2: %x\n", key) if key != nil && (hi.to == nil || bytes.Compare(key, hi.to) < 0) { stepBytes := value[:8] value = value[8:] @@ -2302,6 +2305,7 @@ func (hi *DomainLatestIterFile) init(dc *DomainRoTx) error { } key := btCursor.Key() + fmt.Printf("[dbg] rangeLatest3: %x\n", key) if key != nil && (hi.to == nil || bytes.Compare(key, hi.to) < 0) { val := btCursor.Value() txNum := item.endTxNum - 1 // !important: .kv files have semantic [from, t) @@ -2324,6 +2328,7 @@ func (hi *DomainLatestIterFile) advanceInFiles() error { if ci1.btCursor.Next() { ci1.key = ci1.btCursor.Key() ci1.val = ci1.btCursor.Value() + fmt.Printf("[dbg] advInFiles1: %x\n", ci1.key) if ci1.key != nil && (hi.to == nil || bytes.Compare(ci1.key, hi.to) < 0) { heap.Push(hi.h, ci1) } @@ -2346,6 +2351,7 @@ func (hi *DomainLatestIterFile) advanceInFiles() error { } } + fmt.Printf("[dbg] advInFiles2: %x\n", k) if len(k) > 0 && (hi.to == nil || bytes.Compare(k[:len(k)-8], hi.to) < 0) { stepBytes := k[len(k)-8:] k = k[:len(k)-8] @@ -2364,6 +2370,7 @@ func (hi *DomainLatestIterFile) advanceInFiles() error { return err } + fmt.Printf("[dbg] advInFiles3: %x\n", k) if len(k) > 0 && (hi.to == nil || bytes.Compare(k, hi.to) < 0) { stepBytes := stepBytesWithValue[:8] v := stepBytesWithValue[8:] diff --git a/erigon-lib/state/domain_test.go b/erigon-lib/state/domain_test.go index 67e24daddb5..1b88d3e0355 100644 --- a/erigon-lib/state/domain_test.go +++ b/erigon-lib/state/domain_test.go @@ -1515,13 +1515,25 @@ func TestDomainRange(t *testing.T) { dc = d.BeginFilesRo() defer dc.Close() - it, err := dc.DomainRange(context.Background(), tx, nil, nil, 190, order.Asc, -1) - require.NoError(err) - keys, vals, err := stream.ToArrayKV(it) - require.NoError(err) - require.Equal(5, len(keys)) - require.Equal(5, len(vals)) - fmt.Printf("keys: %x\n", keys) + { + it, err := dc.ht.WalkAsOf(context.Background(), 190, nil, nil, order.Asc, -1, tx) + require.NoError(err) + keys, vals, err := stream.ToArrayKV(it) + fmt.Printf("keys: %x\n", keys) + require.NoError(err) + require.Equal(3, len(keys)) + require.Equal(3, len(vals)) + } + + { + it, err := dc.DomainRangeLatest(tx, nil, nil, -1) + require.NoError(err) + keys, vals, err := stream.ToArrayKV(it) + fmt.Printf("keys: %x\n", keys) + require.NoError(err) + require.Equal(3, len(keys)) + require.Equal(3, len(vals)) + } //it, err := dc.DomainRange(context.Background(), tx, []byte{""}, nil, 190, order.Asc, -1) //require.NoError(err) diff --git a/erigon-lib/state/history.go b/erigon-lib/state/history.go index d7373b9ec7d..4e8047616e7 100644 --- a/erigon-lib/state/history.go +++ b/erigon-lib/state/history.go @@ -1356,31 +1356,7 @@ func (ht *HistoryRoTx) WalkAsOf(ctx context.Context, startTxNum uint64, from, to ctx: ctx, } - for i, item := range ht.iit.files { - if item.endTxNum <= startTxNum { - continue - } - // TODO: seek(from) - g := seg.NewReader(item.src.decompressor.MakeGetter(), ht.h.compression) - - idx := ht.iit.statelessIdxReader(i) - var offset uint64 - if len(from) > 0 { - n := item.src.decompressor.Count() / 2 - var ok bool - offset, ok = g.BinarySearch(from, n, idx.OrdinalLookup) - if !ok { - offset = 0 - } - } - g.Reset(offset) - if g.HasNext() { - key, offset := g.Next(nil) - heap.Push(&hi.h, &ReconItem{g: g, key: key, startTxNum: item.startTxNum, endTxNum: item.endTxNum, txNum: item.endTxNum, startOffset: offset, lastOffset: offset}) - } - } - binary.BigEndian.PutUint64(hi.startTxKey[:], startTxNum) - if err := hi.advanceInFiles(); err != nil { + if err := hi.init(ht.iit.files); err != nil { hi.Close() //it's responsibility of constructor (our) to close resource on error return nil, err } @@ -1426,6 +1402,34 @@ type StateAsOfIterF struct { func (hi *StateAsOfIterF) Close() { } +func (hi *StateAsOfIterF) init(files visibleFiles) error { + for i, item := range files { + if item.endTxNum <= hi.startTxNum { + continue + } + // TODO: seek(from) + g := seg.NewReader(item.src.decompressor.MakeGetter(), hi.hc.h.compression) + + idx := hi.hc.iit.statelessIdxReader(i) + var offset uint64 + if len(hi.from) > 0 { + n := item.src.decompressor.Count() / 2 + var ok bool + offset, ok = g.BinarySearch(hi.from, n, idx.OrdinalLookup) + if !ok { + offset = 0 + } + } + g.Reset(offset) + if g.HasNext() { + key, offset := g.Next(nil) + heap.Push(&hi.h, &ReconItem{g: g, key: key, startTxNum: item.startTxNum, endTxNum: item.endTxNum, txNum: item.endTxNum, startOffset: offset, lastOffset: offset}) + } + } + binary.BigEndian.PutUint64(hi.startTxKey[:], hi.startTxNum) + return hi.advanceInFiles() +} + func (hi *StateAsOfIterF) advanceInFiles() error { for hi.h.Len() > 0 { top := heap.Pop(&hi.h).(*ReconItem)