-
Notifications
You must be signed in to change notification settings - Fork 466
/
prefix_replacing_iterator.go
428 lines (381 loc) · 12.6 KB
/
prefix_replacing_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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// Copyright 2023 The LevelDB-Go and Pebble Authors. All rights reserved. Use
// of this source code is governed by a BSD-style license that can be found in
// the LICENSE file.
package sstable
import (
"bytes"
"context"
"encoding/hex"
"fmt"
"slices"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/pebble/internal/base"
"github.com/cockroachdb/pebble/internal/invariants"
"github.com/cockroachdb/pebble/internal/keyspan"
)
type prefixReplacingIterator struct {
i Iterator
cmp base.Compare
contentPrefix []byte
syntheticPrefix []byte
// lower is a valid key that has syntheticPreficx and is a lower bound for all
// keys produced by i. It is used to determine if the argument key is before
// or after the range of keys produced by the iterator.
lower []byte
// arg and arg2 are buffers that are used to avoid allocations when rewriting
// keys that are provided as arguments. They always start with contentPrefix.
arg, arg2 []byte
// res is used to avoid allocations when rewriting result keys. It always
// starts with syntheticPrefix.
res InternalKey
err error
state iteratorState
}
type iteratorState int8
const (
// inSync indicates that the prefix replacing iterator is "in sync" with the
// underlying iterator; any Next/Prev calls can be passed through.
inSync iteratorState = iota
// afterRange indicates that our iterator is positioned after the synthetic
// prefix range. A Prev() call should return the last key/span in the range.
afterRange
// beforeRange indicates that our iterator is positioned after the synthetic
// prefix range. A Next() call should return the first key/span in the range.
beforeRange
empty
)
func errOutputPrefixMismatch() error {
return errors.AssertionFailedf("key returned does not have prefix required for replacement")
}
var _ Iterator = (*prefixReplacingIterator)(nil)
// newPrefixReplacingIterator wraps an iterator over keys that have
// `contentPrefix` in an iterator that will make them appear to have
// `syntheticPrefix`. Every key produced by the underlying iterator must have
// `contentPrefix`.
//
// lower is a valid key that has syntheticPreficx and is a lower bound for all
// keys produced by i. It is used to determine if an argument key is before
// or after the range of keys produced by the iterator.
//
// INVARIANT: len(syntheticPrefix) > 0 && keyInRange stars with syntheticPrefix.
func newPrefixReplacingIterator(
i Iterator, contentPrefix, syntheticPrefix []byte, lower []byte, cmp base.Compare,
) Iterator {
if invariants.Enabled {
if len(syntheticPrefix) == 0 {
panic("newPrefixReplacingIterator called without synthetic prefix")
}
if !bytes.HasPrefix(lower, syntheticPrefix) {
panic(fmt.Sprintf("lower %q does not have synthetic prefix %q", lower, syntheticPrefix))
}
}
return &prefixReplacingIterator{
i: i,
cmp: cmp,
contentPrefix: contentPrefix,
syntheticPrefix: syntheticPrefix,
lower: lower,
arg: slices.Clone(contentPrefix),
arg2: slices.Clone(contentPrefix),
res: InternalKey{UserKey: slices.Clone(syntheticPrefix)},
}
}
func (p *prefixReplacingIterator) SetContext(ctx context.Context) {
p.i.SetContext(ctx)
}
func (p *prefixReplacingIterator) rewriteArg(key []byte) []byte {
p.arg = append(p.arg[:len(p.contentPrefix)], key[len(p.syntheticPrefix):]...)
return p.arg
}
func (p *prefixReplacingIterator) rewriteArg2(key []byte) []byte {
p.arg2 = append(p.arg2[:len(p.contentPrefix)], key[len(p.syntheticPrefix):]...)
return p.arg2
}
func (p *prefixReplacingIterator) rewriteResult(
k *InternalKey, v base.LazyValue,
) (*InternalKey, base.LazyValue) {
if k == nil {
return k, v
}
if !bytes.HasPrefix(k.UserKey, p.contentPrefix) {
p.err = errOutputPrefixMismatch()
if invariants.Enabled {
panic(p.err)
}
return nil, base.LazyValue{}
}
p.res.Trailer = k.Trailer
p.res.UserKey = append(p.res.UserKey[:len(p.syntheticPrefix)], k.UserKey[len(p.contentPrefix):]...)
return &p.res, v
}
// SeekGE implements the Iterator interface.
func (p *prefixReplacingIterator) SeekGE(
key []byte, flags base.SeekGEFlags,
) (*InternalKey, base.LazyValue) {
p.state = inSync
if !bytes.HasPrefix(key, p.syntheticPrefix) {
if p.cmp(key, p.lower) > 0 {
p.state = afterRange
return nil, base.LazyValue{}
}
// Key must be before the range; seek to the lower bound instead.
// We don't use First because we may miss out on optimizations passed
// through SeekEFlags.
key = p.lower
}
return p.rewriteResult(p.i.SeekGE(p.rewriteArg(key), flags))
}
// SeekPrefixGE implements the Iterator interface.
func (p *prefixReplacingIterator) SeekPrefixGE(
prefix, key []byte, flags base.SeekGEFlags,
) (*InternalKey, base.LazyValue) {
p.state = inSync
if !bytes.HasPrefix(prefix, p.syntheticPrefix) || !bytes.HasPrefix(key, p.syntheticPrefix) {
// We never produce keys with this prefix; we can return nil.
if p.cmp(prefix, p.lower) < 0 {
// We still want to seek the underlying iterator to potentially enable
// optimizations passed through flags.
p.i.SeekGE(p.rewriteArg(p.lower), flags)
}
p.state = empty
return nil, base.LazyValue{}
}
return p.rewriteResult(p.i.SeekPrefixGE(p.rewriteArg2(prefix), p.rewriteArg(key), flags))
}
// SeekLT implements the Iterator interface.
func (p *prefixReplacingIterator) SeekLT(
key []byte, flags base.SeekLTFlags,
) (*InternalKey, base.LazyValue) {
p.state = inSync
if !bytes.HasPrefix(key, p.syntheticPrefix) {
if p.cmp(key, p.lower) < 0 {
// Key before the range; no results.
p.state = beforeRange
return nil, base.LazyValue{}
}
// Key must be after the range. Use Last instead.
return p.rewriteResult(p.i.Last())
}
return p.rewriteResult(p.i.SeekLT(p.rewriteArg(key), flags))
}
// First implements the Iterator interface.
func (p *prefixReplacingIterator) First() (*InternalKey, base.LazyValue) {
p.state = inSync
return p.rewriteResult(p.i.First())
}
// Last implements the Iterator interface.
func (p *prefixReplacingIterator) Last() (*InternalKey, base.LazyValue) {
p.state = inSync
return p.rewriteResult(p.i.Last())
}
// Next implements the Iterator interface.
func (p *prefixReplacingIterator) Next() (*InternalKey, base.LazyValue) {
switch p.state {
case empty, afterRange:
return nil, base.LazyValue{}
case beforeRange:
p.state = inSync
return p.rewriteResult(p.i.First())
case inSync:
return p.rewriteResult(p.i.Next())
default:
panic("invalid iterator state")
}
}
// NextPrefix implements the Iterator interface.
func (p *prefixReplacingIterator) NextPrefix(succKey []byte) (*InternalKey, base.LazyValue) {
switch p.state {
case empty, afterRange:
return nil, base.LazyValue{}
case beforeRange:
p.state = inSync
return p.rewriteResult(p.i.First())
case inSync:
return p.rewriteResult(p.i.NextPrefix(succKey))
default:
panic("invalid iterator state")
}
}
// Prev implements the Iterator interface.
func (p *prefixReplacingIterator) Prev() (*InternalKey, base.LazyValue) {
switch p.state {
case empty, beforeRange:
return nil, base.LazyValue{}
case afterRange:
p.state = inSync
return p.rewriteResult(p.i.Last())
case inSync:
return p.rewriteResult(p.i.Prev())
default:
panic("invalid iterator state")
}
}
// Error implements the Iterator interface.
func (p *prefixReplacingIterator) Error() error {
if p.err != nil {
return p.err
}
return p.i.Error()
}
// Close implements the Iterator interface.
func (p *prefixReplacingIterator) Close() error {
return p.i.Close()
}
// SetBounds implements the Iterator interface.
func (p *prefixReplacingIterator) SetBounds(lower, upper []byte) {
// Check if the underlying iterator requires un-rewritten bounds, i.e. if it
// is going to rewrite them itself or pass them to something e.g. vState that
// will rewrite them.
if x, ok := p.i.(interface{ SetBoundsWithSyntheticPrefix() bool }); ok && x.SetBoundsWithSyntheticPrefix() {
p.i.SetBounds(lower, upper)
return
}
if lower != nil {
lower = append([]byte{}, p.rewriteArg(lower)...)
}
if upper != nil {
upper = append([]byte{}, p.rewriteArg(upper)...)
}
p.i.SetBounds(lower, upper)
}
func (p *prefixReplacingIterator) MaybeFilteredKeys() bool {
return p.i.MaybeFilteredKeys()
}
// String implements the Iterator interface.
func (p *prefixReplacingIterator) String() string {
return fmt.Sprintf("%s [%s->%s]", p.i.String(), hex.EncodeToString(p.contentPrefix), hex.EncodeToString(p.syntheticPrefix))
}
func (p *prefixReplacingIterator) SetCloseHook(fn func(i Iterator) error) {
p.i.SetCloseHook(fn)
}
type prefixReplacingFragmentIterator struct {
i keyspan.FragmentIterator
cmp base.Compare
contentPrefix []byte
syntheticPrefix []byte
// keyInRange is a valid key in the logical range that has the syntheticPrefix.
// When an argument to a seek function does not have the syntheticPrefix,
// keyInRange is used to determine if the argument key is before or after the
// range of keys produced by the iterator.
keyInRange []byte
arg []byte
out1, out2 []byte
state iteratorState
}
// newPrefixReplacingFragmentIterator wraps a FragmentIterator over some reader
// that contains range keys in some key span to make those range keys appear to
// be remapped into some other key-span.
func newPrefixReplacingFragmentIterator(
i keyspan.FragmentIterator,
contentPrefix, syntheticPrefix []byte,
keyInRange []byte,
cmp base.Compare,
) keyspan.FragmentIterator {
return &prefixReplacingFragmentIterator{
i: i,
cmp: cmp,
contentPrefix: contentPrefix,
syntheticPrefix: syntheticPrefix,
keyInRange: keyInRange,
arg: slices.Clone(contentPrefix),
out1: slices.Clone(syntheticPrefix),
out2: slices.Clone(syntheticPrefix),
}
}
func (p *prefixReplacingFragmentIterator) rewriteArg(key []byte) ([]byte, error) {
p.arg = append(p.arg[:len(p.contentPrefix)], key[len(p.syntheticPrefix):]...)
return p.arg, nil
}
func (p *prefixReplacingFragmentIterator) rewriteSpan(
sp *keyspan.Span, err error,
) (*keyspan.Span, error) {
if sp == nil {
return sp, err
}
if !bytes.HasPrefix(sp.Start, p.contentPrefix) || !bytes.HasPrefix(sp.End, p.contentPrefix) {
return nil, errOutputPrefixMismatch()
}
sp.Start = append(p.out1[:len(p.syntheticPrefix)], sp.Start[len(p.contentPrefix):]...)
sp.End = append(p.out2[:len(p.syntheticPrefix)], sp.End[len(p.contentPrefix):]...)
return sp, nil
}
// SeekGE implements the FragmentIterator interface.
func (p *prefixReplacingFragmentIterator) SeekGE(key []byte) (*keyspan.Span, error) {
p.state = inSync
if !bytes.HasPrefix(key, p.syntheticPrefix) {
if p.cmp(key, p.keyInRange) > 0 {
p.state = afterRange
return nil, nil
}
// Key must be before the range; use First instead.
return p.First()
}
rewrittenKey, err := p.rewriteArg(key)
if err != nil {
return nil, err
}
return p.rewriteSpan(p.i.SeekGE(rewrittenKey))
}
// SeekLT implements the FragmentIterator interface.
func (p *prefixReplacingFragmentIterator) SeekLT(key []byte) (*keyspan.Span, error) {
p.state = inSync
if !bytes.HasPrefix(key, p.syntheticPrefix) {
if p.cmp(key, p.keyInRange) < 0 {
p.state = beforeRange
return nil, nil
}
// Key must be after the range; use Last instead.
return p.Last()
}
rewrittenKey, err := p.rewriteArg(key)
if err != nil {
return nil, err
}
return p.rewriteSpan(p.i.SeekLT(rewrittenKey))
}
// First implements the FragmentIterator interface.
func (p *prefixReplacingFragmentIterator) First() (*keyspan.Span, error) {
p.state = inSync
return p.rewriteSpan(p.i.First())
}
// Last implements the FragmentIterator interface.
func (p *prefixReplacingFragmentIterator) Last() (*keyspan.Span, error) {
p.state = inSync
return p.rewriteSpan(p.i.Last())
}
// Next implements the FragmentIterator interface.
func (p *prefixReplacingFragmentIterator) Next() (*keyspan.Span, error) {
switch p.state {
case empty, afterRange:
return nil, nil
case beforeRange:
p.state = inSync
return p.rewriteSpan(p.i.First())
case inSync:
return p.rewriteSpan(p.i.Next())
default:
panic("invalid iterator state")
}
}
// Prev implements the FragmentIterator interface.
func (p *prefixReplacingFragmentIterator) Prev() (*keyspan.Span, error) {
switch p.state {
case empty, beforeRange:
return nil, nil
case afterRange:
p.state = inSync
return p.rewriteSpan(p.i.Last())
case inSync:
return p.rewriteSpan(p.i.Prev())
default:
panic("invalid iterator state")
}
}
// Close implements the FragmentIterator interface.
func (p *prefixReplacingFragmentIterator) Close() error {
return p.i.Close()
}
// WrapChildren implements FragmentIterator.
func (p *prefixReplacingFragmentIterator) WrapChildren(wrap keyspan.WrapFn) {
p.i = wrap(p.i)
}