forked from go-bond/bond
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iter.go
228 lines (177 loc) · 4.28 KB
/
iter.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
package bond
import (
"github.com/cockroachdb/pebble"
)
type IterOptions struct {
pebble.IterOptions
releaseBufferOnClose func()
}
type Iterator interface {
First() bool
Last() bool
Prev() bool
Next() bool
Valid() bool
Error() error
SeekGE(key []byte) bool
SeekPrefixGE(key []byte) bool
SeekLT(key []byte) bool
Key() []byte
Value() []byte
Close() error
}
type _iterConstructor interface {
NewIter(opts *pebble.IterOptions) *pebble.Iterator
}
type _iterator struct {
Iterator
opts *IterOptions
}
func newIterator(itc _iterConstructor, opts *IterOptions) *_iterator {
return &_iterator{Iterator: itc.NewIter(&opts.IterOptions), opts: opts}
}
func (it *_iterator) Close() error {
defer func() {
if it.opts.releaseBufferOnClose != nil {
it.opts.releaseBufferOnClose()
it.opts.releaseBufferOnClose = nil
}
}()
return it.Iterator.Close()
}
type _iteratorMulti struct {
iteratorOptions []*IterOptions
iteratorOptionsIndex int
iteratorConstuctor Iterationer
iterator Iterator
}
func newIteratorMulti(itc Iterationer, opts []*IterOptions) *_iteratorMulti {
return &_iteratorMulti{
iteratorOptions: opts,
iteratorOptionsIndex: 0,
iteratorConstuctor: itc,
iterator: itc.Iter(childIteratorOptions(opts[0])),
}
}
func (it *_iteratorMulti) First() bool {
if it.iteratorOptionsIndex != 0 {
_ = it.iterator.Close()
it.iteratorOptionsIndex = 0
it.iterator = it.iteratorConstuctor.Iter(childIteratorOptions(it.iteratorOptions[it.iteratorOptionsIndex]))
}
return it.iterator.First()
}
func (it *_iteratorMulti) Last() bool {
if it.iteratorOptionsIndex != len(it.iteratorOptions)-1 {
_ = it.iterator.Close()
it.iteratorOptionsIndex = len(it.iteratorOptions) - 1
it.iterator = it.iteratorConstuctor.Iter(childIteratorOptions(it.iteratorOptions[it.iteratorOptionsIndex]))
}
return it.iterator.Last()
}
func (it *_iteratorMulti) Prev() bool {
if !it.iterator.Prev() {
if it.iteratorOptionsIndex == 0 {
return false
}
_ = it.iterator.Close()
it.iteratorOptionsIndex--
it.iterator = it.iteratorConstuctor.Iter(childIteratorOptions(it.iteratorOptions[it.iteratorOptionsIndex]))
return it.iterator.Last()
}
return true
}
func (it *_iteratorMulti) Next() bool {
if !it.iterator.Next() {
if it.iteratorOptionsIndex == len(it.iteratorOptions)-1 {
return false
}
_ = it.iterator.Close()
it.iteratorOptionsIndex++
it.iterator = it.iteratorConstuctor.Iter(childIteratorOptions(it.iteratorOptions[it.iteratorOptionsIndex]))
return it.iterator.First()
}
return true
}
func (it *_iteratorMulti) Valid() bool {
return it.iterator.Valid()
}
func (it *_iteratorMulti) Error() error {
return it.iterator.Error()
}
func (it *_iteratorMulti) SeekGE(key []byte) bool {
//TODO implement me
panic("implement me")
}
func (it *_iteratorMulti) SeekPrefixGE(key []byte) bool {
//TODO implement me
panic("implement me")
}
func (it *_iteratorMulti) SeekLT(key []byte) bool {
//TODO implement me
panic("implement me")
}
func (it *_iteratorMulti) Key() []byte {
return it.iterator.Key()
}
func (it *_iteratorMulti) Value() []byte {
return it.iterator.Value()
}
func (it *_iteratorMulti) Close() error {
defer func() {
for _, opts := range it.iteratorOptions {
if opts.releaseBufferOnClose != nil {
opts.releaseBufferOnClose()
opts.releaseBufferOnClose = nil
}
}
}()
return it.iterator.Close()
}
func childIteratorOptions(opt *IterOptions) *IterOptions {
var subOpts IterOptions
subOpts = *opt
subOpts.releaseBufferOnClose = nil
return &subOpts
}
var _ Iterator = (*_iteratorMulti)(nil)
type errIterator struct {
err error
}
func (e errIterator) First() bool {
return false
}
func (e errIterator) Last() bool {
return false
}
func (e errIterator) Prev() bool {
return false
}
func (e errIterator) Next() bool {
return false
}
func (e errIterator) Valid() bool {
return false
}
func (e errIterator) Error() error {
return e.err
}
func (e errIterator) SeekGE(key []byte) bool {
return false
}
func (e errIterator) SeekPrefixGE(key []byte) bool {
return false
}
func (e errIterator) SeekLT(key []byte) bool {
return false
}
func (e errIterator) Key() []byte {
return nil
}
func (e errIterator) Value() []byte {
return nil
}
func (e errIterator) Close() error {
return nil
}
var _ Iterator = (*errIterator)(nil)