-
Notifications
You must be signed in to change notification settings - Fork 183
/
iterator.go
257 lines (238 loc) · 5.56 KB
/
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package lotusdb
import (
"bytes"
"container/heap"
"github.com/dgraph-io/badger/v4/y"
"github.com/rosedblabs/wal"
)
// baseIterator.
type baseIterator interface {
// Rewind seek the first key in the iterator.
Rewind()
// Seek move the iterator to the key which is
// greater(less when reverse is true) than or equal to the specified key.
Seek(key []byte)
// Next moves the iterator to the next key.
Next()
// Key get the current key.
Key() []byte
// Value get the current value.
Value() any
// Valid returns whether the iterator is exhausted.
Valid() bool
// Close the iterator.
Close() error
}
// Iterator holds a heap and a set of iterators that implement the baseIterator interface.
type Iterator struct {
h iterHeap
itrs []*singleIter // used for rebuilding heap
rankMap map[int]*singleIter // map rank->singleIter
db *DB
}
// Rewind seek the first key in the iterator.
func (mi *Iterator) Rewind() {
for _, v := range mi.itrs {
v.iter.Rewind()
}
h := iterHeap(mi.itrs)
heap.Init(&h)
mi.h = h
}
// Seek move the iterator to the key which is
// greater(less when reverse is true) than or equal to the specified key.
func (mi *Iterator) Seek(key []byte) {
seekItrs := make([]*singleIter, 0)
for _, v := range mi.itrs {
v.iter.Seek(key)
if v.iter.Valid() {
// reset idx
v.idx = len(seekItrs)
seekItrs = append(seekItrs, v)
}
}
h := iterHeap(seekItrs)
heap.Init(&h)
mi.h = h
}
// cleanKey Remove all unused keys from all iterators.
// If the iterators become empty after clearing, remove them from the heap.
func (mi *Iterator) cleanKey(oldKey []byte, rank int) {
defer func() {
if r := recover(); r != nil {
mi.db.mu.Unlock()
}
}()
for i := 0; i < len(mi.itrs); i++ {
if i == rank {
continue
}
itr := mi.rankMap[i]
if !itr.iter.Valid() {
continue
}
for itr.iter.Valid() &&
bytes.Equal(itr.iter.Key(), oldKey) {
if itr.rank > rank {
panic("rank error")
}
itr.iter.Next()
}
if itr.iter.Valid() {
heap.Fix(&mi.h, itr.idx)
} else {
heap.Remove(&mi.h, itr.idx)
}
}
}
// Next moves the iterator to the next key.
func (mi *Iterator) Next() {
if mi.h.Len() == 0 {
return
}
// Get the top item from the heap
topIter := mi.h[0]
mi.cleanKey(topIter.iter.Key(), topIter.rank)
// Move to the next key and update the heap
topIter.iter.Next()
if !topIter.iter.Valid() {
heap.Remove(&mi.h, topIter.idx)
} else {
heap.Fix(&mi.h, topIter.idx)
}
}
// Key get the current key.
func (mi *Iterator) Key() []byte {
return mi.h[0].iter.Key()
}
// Value get the current value.
func (mi *Iterator) Value() []byte {
defer func() {
if r := recover(); r != nil {
mi.db.mu.Unlock()
}
}()
topIter := mi.h[0]
switch topIter.iType {
case BptreeItr:
keyPos := new(KeyPosition)
keyPos.key = topIter.iter.Key()
keyPos.partition = uint32(mi.db.vlog.getKeyPartition(topIter.iter.Key()))
keyPos.position = wal.DecodeChunkPosition(topIter.iter.Value().([]byte))
record, err := mi.db.vlog.read(keyPos)
if err != nil {
panic(err)
}
return record.value
case MemItr:
return topIter.iter.Value().(y.ValueStruct).Value
default:
panic("iType not support")
}
}
// Valid returns whether the iterator is exhausted.
func (mi *Iterator) Valid() bool {
if mi.h.Len() == 0 {
return false
}
topIter := mi.h[0]
if topIter.iType == MemItr && topIter.iter.Value().(y.ValueStruct).Meta == LogRecordDeleted {
mi.cleanKey(topIter.iter.Key(), topIter.rank)
topIter.iter.Next()
if topIter.iter.Valid() {
heap.Fix(&mi.h, topIter.idx)
} else {
heap.Remove(&mi.h, topIter.idx)
}
return mi.Valid()
}
return true
}
// Close the iterator.
func (mi *Iterator) Close() error {
defer mi.db.mu.Unlock()
for _, itr := range mi.itrs {
err := itr.iter.Close()
if err != nil {
return err
}
}
return nil
}
// NewIterator returns a new iterator.
// The iterator will iterate all the keys in DB.
// It's the caller's responsibility to call Close when iterator is no longer
// used, otherwise resources will be leaked.
// The iterator is not goroutine-safe, you should not use the same iterator
// concurrently from multiple goroutines.
func (db *DB) NewIterator(options IteratorOptions) (*Iterator, error) {
if db.options.IndexType == Hash {
return nil, ErrDBIteratorUnsupportedTypeHASH
}
db.mu.Lock()
defer func() {
if r := recover(); r != nil {
db.mu.Unlock()
}
}()
itrs := make([]*singleIter, 0, db.options.PartitionNum+len(db.immuMems)+1)
itrsM := make(map[int]*singleIter)
rank := 0
index, ok := db.index.(*BPTree)
if !ok {
panic("index type not support")
}
for i := 0; i < db.options.PartitionNum; i++ {
tx, err := index.trees[i].Begin(false)
if err != nil {
return nil, err
}
itr := newBptreeIterator(
tx,
options,
)
itr.Rewind()
// is empty
if !itr.Valid() {
_ = itr.Close()
continue
}
itrs = append(itrs, &singleIter{
iType: BptreeItr,
options: options,
rank: rank,
idx: rank,
iter: itr,
})
itrsM[rank] = itrs[len(itrs)-1]
rank++
}
memtableList := make([]*memtable, len(db.immuMems)+1)
copy(memtableList, append(db.immuMems, db.activeMem))
for i := 0; i < len(memtableList); i++ {
itr := newMemtableIterator(options, memtableList[i])
itr.Rewind()
// is empty
if !itr.Valid() {
_ = itr.Close()
continue
}
itrs = append(itrs, &singleIter{
iType: MemItr,
options: options,
rank: rank,
idx: rank,
iter: itr,
})
itrsM[rank] = itrs[len(itrs)-1]
rank++
}
h := iterHeap(itrs)
heap.Init(&h)
return &Iterator{
h: h,
itrs: itrs,
rankMap: itrsM,
db: db,
}, nil
}