-
Notifications
You must be signed in to change notification settings - Fork 47
/
walk.go
91 lines (82 loc) · 2.83 KB
/
walk.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright 2023 The AmazeChain Authors
// This file is part of the AmazeChain library.
//
// The AmazeChain library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The AmazeChain library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
package ethdb
import (
"bytes"
"github.com/ledgerwatch/erigon-lib/kv"
)
// splitCursor implements cursor with two keys
// it is used to ignore incarnations in the middle
// of composite storage key, but without
// reconstructing the key
// Instead, the key is split into two parts and
// functions `Seek` and `Next` deliver both
// parts as well as the corresponding value
type splitCursor struct {
c kv.Cursor // Unlerlying cursor
startkey []byte // Starting key (also contains bits that need to be preserved)
matchBytes int
mask uint8
part1end int // Position in the key where the first part ends
part2start int // Position in the key where the second part starts
part3start int // Position in the key where the third part starts
}
func NewSplitCursor(c kv.Cursor, startkey []byte, matchBits int, part1end, part2start, part3start int) *splitCursor {
var sc splitCursor
sc.c = c
sc.startkey = startkey
sc.part1end = part1end
sc.part2start = part2start
sc.part3start = part3start
sc.matchBytes, sc.mask = Bytesmask(matchBits)
return &sc
}
func (sc *splitCursor) matchKey(k []byte) bool {
if k == nil {
return false
}
if sc.matchBytes == 0 {
return true
}
if len(k) < sc.matchBytes {
return false
}
if !bytes.Equal(k[:sc.matchBytes-1], sc.startkey[:sc.matchBytes-1]) {
return false
}
return (k[sc.matchBytes-1] & sc.mask) == (sc.startkey[sc.matchBytes-1] & sc.mask)
}
func (sc *splitCursor) Seek() (key1, key2, key3, val []byte, err error) {
k, v, err1 := sc.c.Seek(sc.startkey)
if err1 != nil {
return nil, nil, nil, nil, err1
}
if !sc.matchKey(k) {
return nil, nil, nil, nil, nil
}
return k[:sc.part1end], k[sc.part2start:sc.part3start], k[sc.part3start:], v, nil
}
func (sc *splitCursor) Next() (key1, key2, key3, val []byte, err error) {
k, v, err1 := sc.c.Next()
if err1 != nil {
return nil, nil, nil, nil, err1
}
if !sc.matchKey(k) {
return nil, nil, nil, nil, nil
}
return k[:sc.part1end], k[sc.part2start:sc.part3start], k[sc.part3start:], v, nil
}
var EndSuffix = []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}