This repository was archived by the owner on Mar 19, 2024. It is now read-only.
forked from cockroachdb/pebble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrange_keys_test.go
151 lines (145 loc) · 3.68 KB
/
range_keys_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
// Copyright 2021 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"
"strconv"
"testing"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/pebble/internal/datadriven"
"github.com/cockroachdb/pebble/internal/testkeys"
"github.com/cockroachdb/pebble/vfs"
"github.com/stretchr/testify/require"
)
func TestRangeKeys(t *testing.T) {
var d *DB
var b *Batch
newIter := func(o *IterOptions) *Iterator {
if b != nil {
return b.NewIter(o)
}
return d.NewIter(o)
}
datadriven.RunTest(t, "testdata/rangekeys", func(td *datadriven.TestData) string {
switch td.Cmd {
case "reset":
if b != nil {
require.NoError(t, b.Close())
b = nil
}
if d != nil {
require.NoError(t, d.Close())
}
opts := &Options{
FS: vfs.NewMem(),
Comparer: testkeys.Comparer,
FormatMajorVersion: FormatRangeKeys,
}
opts.Experimental.RangeKeys = new(RangeKeysArena)
for _, cmdArgs := range td.CmdArgs {
if cmdArgs.Key != "format-major-version" {
return fmt.Sprintf("unknown command %s\n", cmdArgs.Key)
}
v, err := strconv.Atoi(cmdArgs.Vals[0])
if err != nil {
return err.Error()
}
// Override the DB version.
opts.FormatMajorVersion = FormatMajorVersion(v)
}
var err error
d, err = Open("", opts)
require.NoError(t, err)
return ""
case "populate":
b := d.NewBatch()
runPopulateCmd(t, td, b)
count := b.Count()
require.NoError(t, b.Commit(nil))
return fmt.Sprintf("wrote %d keys\n", count)
case "batch":
b := d.NewBatch()
require.NoError(t, runBatchDefineCmd(td, b))
var err error
func() {
defer func() {
if r := recover(); r != nil {
err = errors.New(r.(string))
}
}()
err = b.Commit(nil)
}()
if err != nil {
return err.Error()
}
count := b.Count()
return fmt.Sprintf("wrote %d keys\n", count)
case "flush":
err := d.Flush()
if err != nil {
return err.Error()
}
return ""
case "indexed-batch":
b = d.NewIndexedBatch()
require.NoError(t, runBatchDefineCmd(td, b))
count := b.Count()
return fmt.Sprintf("created indexed batch with %d keys\n", count)
case "commit-batch":
if b == nil {
return "no pending batch"
}
count := b.Count()
require.NoError(t, d.Apply(b, nil))
b = nil
return fmt.Sprintf("wrote %d keys\n", count)
case "combined-iter":
o := &IterOptions{KeyTypes: IterKeyTypePointsAndRanges}
for _, arg := range td.CmdArgs {
switch arg.Key {
case "mask-suffix":
o.RangeKeyMasking.Suffix = []byte(arg.Vals[0])
case "lower":
o.LowerBound = []byte(arg.Vals[0])
case "upper":
o.UpperBound = []byte(arg.Vals[0])
}
}
var iter *Iterator
var err error
func() {
defer func() {
if r := recover(); r != nil {
err = errors.New(r.(string))
}
}()
iter = newIter(o)
}()
if err != nil {
return err.Error()
}
return runIterCmd(td, iter, true /* close iter */)
case "rangekey-iter":
iter := newIter(&IterOptions{KeyTypes: IterKeyTypeRangesOnly})
return runIterCmd(td, iter, true /* close iter */)
case "scan-rangekeys":
var buf bytes.Buffer
iter := newIter(&IterOptions{KeyTypes: IterKeyTypeRangesOnly})
defer iter.Close()
for iter.First(); iter.Valid(); iter.Next() {
start, end := iter.RangeBounds()
fmt.Fprintf(&buf, "[%s, %s)\n", start, end)
writeRangeKeys(&buf, iter)
fmt.Fprintln(&buf)
}
return buf.String()
default:
return fmt.Sprintf("unknown command %q", td.Cmd)
}
})
if d != nil {
require.NoError(t, d.Close())
}
}