forked from cockroachdb/pebble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compaction_iter_test.go
150 lines (140 loc) · 3.57 KB
/
compaction_iter_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
// Copyright 2018 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"
"sort"
"strconv"
"strings"
"testing"
"github.com/cockroachdb/pebble/internal/base"
"github.com/cockroachdb/pebble/internal/datadriven"
)
func TestSnapshotIndex(t *testing.T) {
testCases := []struct {
snapshots []uint64
seq uint64
expectedIndex int
expectedSeqNum uint64
}{
{[]uint64{}, 1, 0, InternalKeySeqNumMax},
{[]uint64{1}, 0, 0, 1},
{[]uint64{1}, 1, 1, InternalKeySeqNumMax},
{[]uint64{1}, 2, 1, InternalKeySeqNumMax},
{[]uint64{1, 3}, 1, 1, 3},
{[]uint64{1, 3}, 2, 1, 3},
{[]uint64{1, 3}, 3, 2, InternalKeySeqNumMax},
{[]uint64{1, 3}, 4, 2, InternalKeySeqNumMax},
{[]uint64{1, 3, 3}, 2, 1, 3},
}
for _, c := range testCases {
t.Run("", func(t *testing.T) {
idx, seqNum := snapshotIndex(c.seq, c.snapshots)
if c.expectedIndex != idx {
t.Fatalf("expected %d, but got %d", c.expectedIndex, idx)
}
if c.expectedSeqNum != seqNum {
t.Fatalf("expected %d, but got %d", c.expectedSeqNum, seqNum)
}
})
}
}
func TestCompactionIter(t *testing.T) {
var keys []InternalKey
var vals [][]byte
var snapshots []uint64
var elideTombstones bool
newIter := func() *compactionIter {
return newCompactionIter(
DefaultComparer.Compare,
DefaultMerger.Merge,
&fakeIter{keys: keys, vals: vals},
snapshots,
false, /* allowZeroSeqNum */
func([]byte) bool {
return elideTombstones
},
func(_, _ []byte) bool {
return elideTombstones
},
)
}
datadriven.RunTest(t, "testdata/compaction_iter", func(d *datadriven.TestData) string {
switch d.Cmd {
case "define":
keys = keys[:0]
vals = vals[:0]
for _, key := range strings.Split(d.Input, "\n") {
j := strings.Index(key, ":")
keys = append(keys, base.ParseInternalKey(key[:j]))
vals = append(vals, []byte(key[j+1:]))
}
return ""
case "iter":
snapshots = snapshots[:0]
elideTombstones = false
for _, arg := range d.CmdArgs {
switch arg.Key {
case "snapshots":
for _, val := range arg.Vals {
seqNum, err := strconv.Atoi(val)
if err != nil {
return err.Error()
}
snapshots = append(snapshots, uint64(seqNum))
}
case "elide-tombstones":
var err error
elideTombstones, err = strconv.ParseBool(arg.Vals[0])
if err != nil {
return err.Error()
}
default:
return fmt.Sprintf("%s: unknown arg: %s", d.Cmd, arg.Key)
}
}
sort.Slice(snapshots, func(i, j int) bool {
return snapshots[i] < snapshots[j]
})
iter := newIter()
var b bytes.Buffer
for _, line := range strings.Split(d.Input, "\n") {
parts := strings.Fields(line)
if len(parts) == 0 {
continue
}
switch parts[0] {
case "first":
iter.First()
case "next":
iter.Next()
case "tombstones":
var key []byte
if len(parts) == 2 {
key = []byte(parts[1])
}
for _, v := range iter.Tombstones(key) {
fmt.Fprintf(&b, "%s-%s#%d\n",
v.Start.UserKey, v.End, v.Start.SeqNum())
}
fmt.Fprintf(&b, ".\n")
continue
default:
return fmt.Sprintf("unknown op: %s", parts[0])
}
if iter.Valid() {
fmt.Fprintf(&b, "%s:%s\n", iter.Key(), iter.Value())
} else if err := iter.Error(); err != nil {
fmt.Fprintf(&b, "err=%v\n", err)
} else {
fmt.Fprintf(&b, ".\n")
}
}
return b.String()
default:
return fmt.Sprintf("unknown command: %s", d.Cmd)
}
})
}