forked from cockroachdb/pebble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
external_iterator_test.go
196 lines (182 loc) · 5.48 KB
/
external_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
// Copyright 2022 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 (
"bytes"
"fmt"
"testing"
"github.com/cockroachdb/datadriven"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/pebble/internal/itertest"
"github.com/cockroachdb/pebble/internal/testkeys"
"github.com/cockroachdb/pebble/objstorage/objstorageprovider"
"github.com/cockroachdb/pebble/sstable"
"github.com/cockroachdb/pebble/vfs"
"github.com/stretchr/testify/require"
)
func TestExternalIterator(t *testing.T) {
mem := vfs.NewMem()
o := &Options{
FS: mem,
Comparer: testkeys.Comparer,
}
o.testingRandomized(t)
o.EnsureDefaults()
d, err := Open("", o)
require.NoError(t, err)
defer func() { require.NoError(t, d.Close()) }()
datadriven.RunTest(t, "testdata/external_iterator", func(t *testing.T, td *datadriven.TestData) string {
switch td.Cmd {
case "reset":
mem = vfs.NewMem()
return ""
case "build":
if err := runBuildCmd(td, d, mem); err != nil {
return err.Error()
}
return ""
case "iter":
opts := IterOptions{KeyTypes: IterKeyTypePointsAndRanges}
var externalIterOpts []ExternalIterOption
var files [][]sstable.ReadableFile
for _, arg := range td.CmdArgs {
switch arg.Key {
case "mask-suffix":
opts.RangeKeyMasking.Suffix = []byte(arg.Vals[0])
case "lower":
opts.LowerBound = []byte(arg.Vals[0])
case "upper":
opts.UpperBound = []byte(arg.Vals[0])
case "files":
for _, v := range arg.Vals {
f, err := mem.Open(v)
require.NoError(t, err)
files = append(files, []sstable.ReadableFile{f})
}
}
}
it, err := NewExternalIter(o, &opts, files, externalIterOpts...)
require.NoError(t, err)
return runIterCmd(td, it, true /* close iter */)
default:
return fmt.Sprintf("unknown command: %s", td.Cmd)
}
})
}
func TestSimpleLevelIter(t *testing.T) {
mem := vfs.NewMem()
o := &Options{
FS: mem,
Comparer: testkeys.Comparer,
}
o.testingRandomized(t)
o.EnsureDefaults()
d, err := Open("", o)
require.NoError(t, err)
defer func() { require.NoError(t, d.Close()) }()
datadriven.RunTest(t, "testdata/simple_level_iter", func(t *testing.T, td *datadriven.TestData) string {
switch td.Cmd {
case "reset":
mem = vfs.NewMem()
return ""
case "build":
if err := runBuildCmd(td, d, mem); err != nil {
return err.Error()
}
return ""
case "iter":
var files []sstable.ReadableFile
var filenames []string
td.ScanArgs(t, "files", &filenames)
for _, name := range filenames {
f, err := mem.Open(name)
require.NoError(t, err)
files = append(files, f)
}
readers, err := openExternalTables(o, files, 0, o.MakeReaderOptions())
require.NoError(t, err)
defer func() {
for i := range readers {
_ = readers[i].Close()
}
}()
var internalIters []internalIterator
for i := range readers {
iter, err := readers[i].NewIter(sstable.NoTransforms, nil, nil)
require.NoError(t, err)
internalIters = append(internalIters, iter)
}
it := &simpleLevelIter{cmp: o.Comparer.Compare, iters: internalIters}
it.init(IterOptions{})
response := itertest.RunInternalIterCmd(t, td, it)
require.NoError(t, it.Close())
return response
default:
return fmt.Sprintf("unknown command: %s", td.Cmd)
}
})
}
func TestSimpleIterError(t *testing.T) {
s := simpleLevelIter{cmp: DefaultComparer.Compare, iters: []internalIterator{&errorIter{err: errors.New("injected")}}}
s.init(IterOptions{})
defer s.Close()
require.Nil(t, s.First())
require.Error(t, s.Error())
}
func BenchmarkExternalIter_NonOverlapping_Scan(b *testing.B) {
ks := testkeys.Alpha(6)
opts := (&Options{Comparer: testkeys.Comparer}).EnsureDefaults()
iterOpts := &IterOptions{
KeyTypes: IterKeyTypePointsAndRanges,
}
writeOpts := opts.MakeWriterOptions(6, sstable.TableFormatPebblev2)
for _, keyCount := range []int{100, 10_000, 100_000} {
b.Run(fmt.Sprintf("keys=%d", keyCount), func(b *testing.B) {
for _, fileCount := range []int{1, 10, 100} {
b.Run(fmt.Sprintf("files=%d", fileCount), func(b *testing.B) {
var fs vfs.FS = vfs.NewMem()
filenames := make([]string, fileCount)
var keys [][]byte
for i := 0; i < fileCount; i++ {
filename := fmt.Sprintf("%03d.sst", i)
wf, err := fs.Create(filename, vfs.WriteCategoryUnspecified)
require.NoError(b, err)
w := sstable.NewWriter(objstorageprovider.NewFileWritable(wf), writeOpts)
for j := 0; j < keyCount/fileCount; j++ {
key := testkeys.Key(ks, int64(len(keys)))
keys = append(keys, key)
require.NoError(b, w.Set(key, key))
}
require.NoError(b, w.Close())
filenames[i] = filename
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
func() {
files := make([][]sstable.ReadableFile, fileCount)
for i := 0; i < fileCount; i++ {
f, err := fs.Open(filenames[i])
require.NoError(b, err)
files[i] = []sstable.ReadableFile{f}
}
it, err := NewExternalIter(opts, iterOpts, files)
require.NoError(b, err)
defer it.Close()
k := 0
for valid := it.First(); valid; valid = it.NextPrefix() {
if !bytes.Equal(it.Key(), keys[k]) {
b.Fatalf("expected key %q, found %q", keys[k+1], it.Key())
}
k++
}
if k != len(keys) {
b.Fatalf("k=%d, expected %d\n", k, len(keys))
}
}()
}
})
}
})
}
}