forked from cockroachdb/pebble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinternal_iterator_test.go
257 lines (249 loc) · 6.31 KB
/
internal_iterator_test.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
// 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 pebble
import (
"fmt"
"strconv"
"strings"
"testing"
"github.com/cockroachdb/datadriven"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/pebble/bloom"
"github.com/cockroachdb/pebble/internal/base"
"github.com/cockroachdb/pebble/internal/keyspan"
"github.com/cockroachdb/pebble/internal/testkeys"
"github.com/cockroachdb/pebble/sstable"
"github.com/cockroachdb/pebble/vfs"
"github.com/stretchr/testify/require"
)
func TestScanInternal(t *testing.T) {
var d *DB
type scanInternalReader interface {
ScanInternal(
lower, upper []byte, visitPointKey func(key *InternalKey, value LazyValue) error,
visitRangeDel func(start, end []byte, seqNum uint64) error,
visitRangeKey func(start, end []byte, keys []keyspan.Key) error) error
}
batches := map[string]*Batch{}
snaps := map[string]*Snapshot{}
parseOpts := func(td *datadriven.TestData) (*Options, error) {
opts := &Options{
FS: vfs.NewMem(),
Comparer: testkeys.Comparer,
FormatMajorVersion: FormatRangeKeys,
BlockPropertyCollectors: []func() BlockPropertyCollector{
sstable.NewTestKeysBlockPropertyCollector,
},
}
opts.DisableAutomaticCompactions = true
opts.EnsureDefaults()
opts.WithFSDefaults()
for _, cmdArg := range td.CmdArgs {
switch cmdArg.Key {
case "format-major-version":
v, err := strconv.Atoi(cmdArg.Vals[0])
if err != nil {
return nil, err
}
// Override the DB version.
opts.FormatMajorVersion = FormatMajorVersion(v)
case "block-size":
v, err := strconv.Atoi(cmdArg.Vals[0])
if err != nil {
return nil, err
}
for i := range opts.Levels {
opts.Levels[i].BlockSize = v
}
case "index-block-size":
v, err := strconv.Atoi(cmdArg.Vals[0])
if err != nil {
return nil, err
}
for i := range opts.Levels {
opts.Levels[i].IndexBlockSize = v
}
case "target-file-size":
v, err := strconv.Atoi(cmdArg.Vals[0])
if err != nil {
return nil, err
}
for i := range opts.Levels {
opts.Levels[i].TargetFileSize = int64(v)
}
case "bloom-bits-per-key":
v, err := strconv.Atoi(cmdArg.Vals[0])
if err != nil {
return nil, err
}
fp := bloom.FilterPolicy(v)
opts.Filters = map[string]FilterPolicy{fp.Name(): fp}
for i := range opts.Levels {
opts.Levels[i].FilterPolicy = fp
}
case "merger":
switch cmdArg.Vals[0] {
case "appender":
opts.Merger = base.DefaultMerger
default:
return nil, errors.Newf("unrecognized Merger %q\n", cmdArg.Vals[0])
}
}
}
return opts, nil
}
cleanup := func() (err error) {
for key, batch := range batches {
err = firstError(err, batch.Close())
delete(batches, key)
}
for key, snap := range snaps {
err = firstError(err, snap.Close())
delete(snaps, key)
}
if d != nil {
err = firstError(err, d.Close())
d = nil
}
return err
}
defer cleanup()
datadriven.RunTest(t, "testdata/scan_internal", func(t *testing.T, td *datadriven.TestData) string {
switch td.Cmd {
case "define":
if err := cleanup(); err != nil {
return err.Error()
}
opts, err := parseOpts(td)
if err != nil {
return err.Error()
}
d, err = runDBDefineCmd(td, opts)
if err != nil {
return err.Error()
}
return runLSMCmd(td, d)
case "reset":
if err := cleanup(); err != nil {
t.Fatal(err)
return err.Error()
}
opts, err := parseOpts(td)
if err != nil {
t.Fatal(err)
return err.Error()
}
d, err = Open("", opts)
require.NoError(t, err)
return ""
case "snapshot":
s := d.NewSnapshot()
var name string
for _, arg := range td.CmdArgs {
switch arg.Key {
case "name":
name = arg.Vals[0]
default:
return fmt.Sprintf("unrecognized command argument %q\n", arg.Key)
}
}
snaps[name] = s
return ""
case "batch":
var commit bool
var name string
for _, arg := range td.CmdArgs {
switch arg.Key {
case "name":
name = arg.Vals[0]
case "commit":
commit = true
default:
return fmt.Sprintf("unrecognized command argument %q\n", arg.Key)
}
}
b := d.NewIndexedBatch()
require.NoError(t, runBatchDefineCmd(td, b))
var err error
if commit {
func() {
defer func() {
if r := recover(); r != nil {
err = errors.New(r.(string))
}
}()
err = b.Commit(nil)
}()
} else if name != "" {
batches[name] = b
}
if err != nil {
return err.Error()
}
count := b.Count()
if commit {
return fmt.Sprintf("committed %d keys\n", count)
}
return fmt.Sprintf("wrote %d keys to batch %q\n", count, name)
case "compact":
if err := runCompactCmd(td, d); err != nil {
return err.Error()
}
return runLSMCmd(td, d)
case "flush":
err := d.Flush()
if err != nil {
return err.Error()
}
return ""
case "lsm":
return runLSMCmd(td, d)
case "commit":
name := pluckStringCmdArg(td, "batch")
b := batches[name]
defer b.Close()
count := b.Count()
require.NoError(t, d.Apply(b, nil))
delete(batches, name)
return fmt.Sprintf("committed %d keys\n", count)
case "scan-internal":
var lower, upper []byte
var reader scanInternalReader = d
for _, arg := range td.CmdArgs {
switch arg.Key {
case "lower":
lower = []byte(arg.Vals[0])
case "upper":
upper = []byte(arg.Vals[0])
case "snapshot":
name := arg.Vals[0]
snap, ok := snaps[name]
if !ok {
return fmt.Sprintf("no snapshot found for name %s", name)
}
reader = snap
}
}
var b strings.Builder
err := reader.ScanInternal(lower, upper, func(key *InternalKey, value LazyValue) error {
v := value.InPlaceValue()
fmt.Fprintf(&b, "%s (%s)\n", key, v)
return nil
}, func(start, end []byte, seqNum uint64) error {
fmt.Fprintf(&b, "%s-%s#%d,RANGEDEL\n", start, end, seqNum)
return nil
}, func(start, end []byte, keys []keyspan.Key) error {
s := keyspan.Span{Start: start, End: end, Keys: keys}
fmt.Fprintf(&b, "%s\n", s.String())
return nil
})
if err != nil {
return err.Error()
}
return b.String()
default:
return fmt.Sprintf("unknown command %q", td.Cmd)
}
})
}