-
Notifications
You must be signed in to change notification settings - Fork 723
/
Copy pathview_iterator.go
170 lines (144 loc) · 4.26 KB
/
view_iterator.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package merkledb
import (
"bytes"
"slices"
"github.com/ava-labs/avalanchego/database"
)
func (v *view) NewIterator() database.Iterator {
return v.NewIteratorWithStartAndPrefix(nil, nil)
}
func (v *view) NewIteratorWithStart(start []byte) database.Iterator {
return v.NewIteratorWithStartAndPrefix(start, nil)
}
func (v *view) NewIteratorWithPrefix(prefix []byte) database.Iterator {
return v.NewIteratorWithStartAndPrefix(nil, prefix)
}
func (v *view) NewIteratorWithStartAndPrefix(start, prefix []byte) database.Iterator {
var (
changes = make([]KeyChange, 0, len(v.changes.values))
startKey = ToKey(start)
prefixKey = ToKey(prefix)
)
for key, change := range v.changes.values {
if len(start) > 0 && startKey.Greater(key) || !key.HasPrefix(prefixKey) {
continue
}
changes = append(changes, KeyChange{
Key: key.Bytes(),
Value: change.after,
})
}
// sort [changes] so they can be merged with the parent trie's state
slices.SortFunc(changes, func(a, b KeyChange) int {
return bytes.Compare(a.Key, b.Key)
})
return &viewIterator{
view: v,
parentIter: v.parentTrie.NewIteratorWithStartAndPrefix(start, prefix),
sortedChanges: changes,
}
}
// viewIterator walks over both the in memory database and the underlying database
// at the same time.
type viewIterator struct {
view *view
parentIter database.Iterator
key, value []byte
err error
sortedChanges []KeyChange
initialized, parentIterExhausted bool
}
// Next moves the iterator to the next key/value pair. It returns whether the
// iterator is exhausted. We must pay careful attention to set the proper values
// based on if the in memory changes or the underlying db should be read next
func (it *viewIterator) Next() bool {
switch {
case it.view.isInvalid():
it.key = nil
it.value = nil
it.err = ErrInvalid
return false
case !it.initialized:
it.parentIterExhausted = !it.parentIter.Next()
it.initialized = true
}
for {
switch {
case it.parentIterExhausted && len(it.sortedChanges) == 0:
// there are no more changes or underlying key/values
it.key = nil
it.value = nil
return false
case it.parentIterExhausted:
// there are no more underlying key/values, so use the local changes
nextKeyValue := it.sortedChanges[0]
// move to next change
it.sortedChanges = it.sortedChanges[1:]
// If current change is not a deletion, return it.
// Otherwise go to next loop iteration.
if !nextKeyValue.Value.IsNothing() {
it.key = nextKeyValue.Key
it.value = nextKeyValue.Value.Value()
return true
}
case len(it.sortedChanges) == 0:
it.key = it.parentIter.Key()
it.value = it.parentIter.Value()
it.parentIterExhausted = !it.parentIter.Next()
return true
default:
memKey := it.sortedChanges[0].Key
memValue := it.sortedChanges[0].Value
parentKey := it.parentIter.Key()
switch bytes.Compare(memKey, parentKey) {
case -1:
// The current change has a smaller key than the parent key.
// Move to the next change.
it.sortedChanges = it.sortedChanges[1:]
// If current change is not a deletion, return it.
// Otherwise, go to next loop iteration.
if memValue.HasValue() {
it.key = memKey
it.value = slices.Clone(memValue.Value())
return true
}
case 1:
// The parent key is smaller, so return it and iterate the parent iterator
it.key = parentKey
it.value = it.parentIter.Value()
it.parentIterExhausted = !it.parentIter.Next()
return true
default:
// the keys are the same, so use the local change and
// iterate both the sorted changes and the parent iterator
it.sortedChanges = it.sortedChanges[1:]
it.parentIterExhausted = !it.parentIter.Next()
if memValue.HasValue() {
it.key = memKey
it.value = slices.Clone(memValue.Value())
return true
}
}
}
}
}
func (it *viewIterator) Error() error {
if it.err != nil {
return it.err
}
return it.parentIter.Error()
}
func (it *viewIterator) Key() []byte {
return it.key
}
func (it *viewIterator) Value() []byte {
return it.value
}
func (it *viewIterator) Release() {
it.key = nil
it.value = nil
it.sortedChanges = nil
it.parentIter.Release()
}