-
Notifications
You must be signed in to change notification settings - Fork 1
/
iter.go
271 lines (231 loc) · 5.57 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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
* Copyright 2022 National Library of Norway.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package tikvidx
import (
"context"
"github.com/tikv/client-go/v2/rawkv"
"github.com/nlnwa/gowarcserver/index"
"github.com/nlnwa/gowarcserver/internal/keyvalue"
"github.com/nlnwa/gowarcserver/timestamp"
)
type maybeKV struct {
k []byte
v []byte
error error
}
type scanner func([]byte, []byte) ([][]byte, [][]byte, error)
func repeatScan(scan scanner, key, endKey []byte, result chan<- maybeKV, done <-chan struct{}) {
defer close(result)
for {
keys, values, err := scan(key, endKey)
if err != nil {
select {
case <-done:
return
case result <- maybeKV{error: err}:
return
}
}
for j, k := range keys {
select {
case <-done:
return
case result <- maybeKV{k: k, v: values[j]}:
}
}
select {
case <-done:
return
default:
}
if len(keys) < rawkv.MaxRawKVScanLimit {
return
}
key = append(keys[len(keys)-1], 0x0)
}
}
var startDate = []byte("19700101000000")
var endDate = []byte("99991231235959")
type closestIter struct {
forward, backward chan maybeKV
a, b *maybeKV
kv *maybeKV
prefix string
valid bool
cmp func(int64, int64) bool
done chan struct{}
}
func newClosestIter(ctx context.Context, client *rawkv.Client, req index.Request, prefix string) (iterator, error) {
t, err := timestamp.Parse(req.Closest())
if err != nil {
return nil, err
}
limit := req.Limit()
if limit == 0 || limit > rawkv.MaxRawKVScanLimit {
limit = rawkv.MaxRawKVScanLimit
}
var fScanner scanner = func(key, endKey []byte) ([][]byte, [][]byte, error) {
return client.Scan(ctx, key, endKey, limit)
}
var bScanner scanner = func(key, endKey []byte) ([][]byte, [][]byte, error) {
return client.ReverseScan(ctx, key, endKey, limit)
}
forwardChannel := make(chan maybeKV)
backwardChannel := make(chan maybeKV)
keyPrefix, startKey := keyvalue.ClosestWithPrefix(req, cdxPrefix)
pf := make([]byte, len(keyPrefix))
copy(pf, keyPrefix)
forwardEndKey := append(pf, endDate...)
pb := make([]byte, len(keyPrefix))
copy(pb, keyPrefix)
backwardEndKey := append(pb, startDate...)
forwardStartKey := make([]byte, len(startKey))
copy(forwardStartKey, startKey)
backwardStartKey := make([]byte, len(startKey))
copy(backwardStartKey, startKey)
done := make(chan struct{})
go repeatScan(fScanner, forwardStartKey, forwardEndKey, forwardChannel, done)
go repeatScan(bScanner, backwardStartKey, backwardEndKey, backwardChannel, done)
iter := &closestIter{
cmp: timestamp.CompareClosest(t.Unix()),
forward: forwardChannel,
backward: backwardChannel,
done: done,
prefix: prefix,
}
return iter, iter.Next()
}
func (ci *closestIter) Key() []byte {
return ci.kv.k[len(ci.prefix):]
}
func (ci *closestIter) Value() []byte {
return ci.kv.v
}
func (ci *closestIter) Valid() bool {
return ci.valid
}
func (ci *closestIter) Close() {
close(ci.done)
}
func (ci *closestIter) Next() error {
var ft int64
var bt int64
ci.valid = true
if ci.a == nil {
f, ok := <-ci.forward
if ok {
ci.a = &f
}
}
if ci.b == nil {
b, ok := <-ci.backward
if ok {
ci.b = &b
}
}
if ci.a != nil {
ft = keyvalue.CdxKey(ci.a.k).Unix()
}
if ci.b != nil {
bt = keyvalue.CdxKey(ci.b.k).Unix()
}
var isForward bool
if ft != 0 && bt != 0 {
// find closest of forward and backward
if ci.cmp(ft, bt) {
isForward = true
}
} else if ft != 0 {
isForward = true
} else if bt != 0 {
// pass
} else {
ci.valid = false
ci.kv = nil
return nil
}
if isForward {
ci.kv = ci.a
ci.a = nil
} else {
ci.kv = ci.b
ci.b = nil
}
return nil
}
type iter struct {
key []byte
value []byte
valid bool
next <-chan maybeKV
done chan<- struct{}
prefix string
}
func newIter(ctx context.Context, key []byte, client *rawkv.Client, req index.Request, prefix string) (iterator, error) {
limit := req.Limit()
if limit == 0 || limit > rawkv.MaxRawKVScanLimit {
limit = rawkv.MaxRawKVScanLimit
}
endKey := make([]byte, len(key))
copy(endKey, key)
endKey = append(endKey, 0xff)
var scan scanner
if req.Sort() == index.SortDesc {
scan = func(key []byte, endKey []byte) ([][]byte, [][]byte, error) {
return client.ReverseScan(ctx, endKey, key, limit)
}
} else {
scan = func(key []byte, endKey []byte) ([][]byte, [][]byte, error) {
return client.Scan(ctx, key, endKey, limit)
}
}
result := make(chan maybeKV)
done := make(chan struct{})
go repeatScan(scan, key, endKey, result, done)
is := &iter{
next: result,
done: done,
prefix: prefix,
}
return is, is.Next()
}
// Next updates the next key, value and validity.
func (is *iter) Next() error {
mkv, ok := <-is.next
if !ok {
is.valid = false
return nil
}
is.valid = true
if mkv.error != nil {
return mkv.error
}
is.key = mkv.k
is.value = mkv.v
return nil
}
func (is *iter) Key() []byte {
return is.key[len(is.prefix):]
}
func (is *iter) Value() []byte {
return is.value
}
func (is *iter) Valid() bool {
return is.valid
}
func (is *iter) Close() {
close(is.done)
}