Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
AskAlexSharov committed Nov 7, 2024
1 parent 8d5f89a commit 9c828f3
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 37 deletions.
17 changes: 12 additions & 5 deletions erigon-lib/state/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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:]
Expand All @@ -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:]
Expand All @@ -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)
Expand All @@ -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)
}
Expand All @@ -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]
Expand All @@ -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:]
Expand Down
26 changes: 19 additions & 7 deletions erigon-lib/state/domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
54 changes: 29 additions & 25 deletions erigon-lib/state/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 9c828f3

Please sign in to comment.