Skip to content

Commit b2207e1

Browse files
committed
internal/blobtest: move blob testutils
Move blob-related testutils into a separate blobtest package. Future work will require a transitive dependency on the sstable package (via internal/manifest).
1 parent cafb179 commit b2207e1

File tree

5 files changed

+28
-25
lines changed

5 files changed

+28
-25
lines changed

data_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ import (
2525
"github.com/cockroachdb/errors"
2626
"github.com/cockroachdb/pebble/bloom"
2727
"github.com/cockroachdb/pebble/internal/base"
28+
"github.com/cockroachdb/pebble/internal/blobtest"
2829
"github.com/cockroachdb/pebble/internal/humanize"
2930
"github.com/cockroachdb/pebble/internal/keyspan"
3031
"github.com/cockroachdb/pebble/internal/rangekey"
3132
"github.com/cockroachdb/pebble/internal/sstableinternal"
3233
"github.com/cockroachdb/pebble/internal/testkeys"
33-
"github.com/cockroachdb/pebble/internal/testutils"
3434
"github.com/cockroachdb/pebble/objstorage/objstorageprovider"
3535
"github.com/cockroachdb/pebble/objstorage/remote"
3636
"github.com/cockroachdb/pebble/sstable"
@@ -672,10 +672,10 @@ func runBuildRemoteCmd(td *datadriven.TestData, d *DB, storage remote.Storage) e
672672
}
673673

674674
type dataDrivenCmdOptions struct {
675-
blobValues *testutils.BlobValues
675+
blobValues *blobtest.Values
676676
}
677677

678-
func withBlobValues(bv *testutils.BlobValues) func(*dataDrivenCmdOptions) {
678+
func withBlobValues(bv *blobtest.Values) func(*dataDrivenCmdOptions) {
679679
return func(o *dataDrivenCmdOptions) { o.blobValues = bv }
680680
}
681681

@@ -725,7 +725,7 @@ func runBuildCmd(
725725
}
726726

727727
writeOpts := d.opts.MakeWriterOptions(0 /* level */, tableFormat)
728-
var blobReferences testutils.BlobReferences
728+
var blobReferences blobtest.References
729729
f, err := fs.Create(path, vfs.WriteCategoryUnspecified)
730730
if err != nil {
731731
return err

external_iterator_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import (
1313

1414
"github.com/cockroachdb/datadriven"
1515
"github.com/cockroachdb/errors"
16+
"github.com/cockroachdb/pebble/internal/blobtest"
1617
"github.com/cockroachdb/pebble/internal/testkeys"
17-
"github.com/cockroachdb/pebble/internal/testutils"
1818
"github.com/cockroachdb/pebble/objstorage/objstorageprovider"
1919
"github.com/cockroachdb/pebble/sstable"
2020
"github.com/cockroachdb/pebble/vfs"
@@ -34,7 +34,7 @@ func TestExternalIterator(t *testing.T) {
3434
d, err := Open("", o)
3535
require.NoError(t, err)
3636
defer func() { require.NoError(t, d.Close()) }()
37-
var bv testutils.BlobValues
37+
var bv blobtest.Values
3838

3939
getOptsAndFiles := func(td *datadriven.TestData) (opts IterOptions, files [][]sstable.ReadableFile) {
4040
opts = IterOptions{KeyTypes: IterKeyTypePointsAndRanges}

ingest_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ import (
2828
"github.com/cockroachdb/errors/oserror"
2929
"github.com/cockroachdb/pebble/bloom"
3030
"github.com/cockroachdb/pebble/internal/base"
31+
"github.com/cockroachdb/pebble/internal/blobtest"
3132
"github.com/cockroachdb/pebble/internal/keyspan"
3233
"github.com/cockroachdb/pebble/internal/manifest"
3334
"github.com/cockroachdb/pebble/internal/rangekey"
3435
"github.com/cockroachdb/pebble/internal/testkeys"
35-
"github.com/cockroachdb/pebble/internal/testutils"
3636
"github.com/cockroachdb/pebble/objstorage"
3737
"github.com/cockroachdb/pebble/objstorage/objstorageprovider"
3838
"github.com/cockroachdb/pebble/objstorage/remote"
@@ -106,8 +106,8 @@ func TestIngestLoad(t *testing.T) {
106106
if err != nil {
107107
return err.Error()
108108
}
109-
var bv testutils.BlobValues
110-
var br testutils.BlobReferences
109+
var bv blobtest.Values
110+
var br blobtest.References
111111
w := sstable.NewRawWriter(objstorageprovider.NewFileWritable(f), writerOpts)
112112
for _, data := range strings.Split(td.Input, "\n") {
113113
if strings.HasPrefix(data, "EncodeSpan: ") {

internal/testutils/blob_handles.go renamed to internal/blobtest/handles.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
// of this source code is governed by a BSD-style license that can be found in
33
// the LICENSE file.
44

5-
package testutils
5+
// Package blobtest contains helpers for interacting with value separation and
6+
// blob files in tests.
7+
package blobtest
68

79
import (
810
"context"
@@ -12,13 +14,14 @@ import (
1214
"github.com/cockroachdb/errors"
1315
"github.com/cockroachdb/pebble/internal/base"
1416
"github.com/cockroachdb/pebble/internal/strparse"
17+
"github.com/cockroachdb/pebble/internal/testutils"
1518
"github.com/cockroachdb/pebble/sstable/blob"
1619
)
1720

18-
// BlobValues is a helper for using blob handles in tests. It supports parsing a
21+
// Values is a helper for using blob handles in tests. It supports parsing a
1922
// human-readable string describing a blob handle, synthesizing unspecified
2023
// fields, and tracking the blob handle to support future fetches.
21-
type BlobValues struct {
24+
type Values struct {
2225
mostRecentHandle blob.Handle
2326
// trackedHandles maps from a blob handle to its value. The value may be nil
2427
// if the value was not specified (in which case Fetch will
@@ -27,7 +30,7 @@ type BlobValues struct {
2730
}
2831

2932
// Fetch returns the value corresponding to the given handle.
30-
func (bv *BlobValues) Fetch(
33+
func (bv *Values) Fetch(
3134
ctx context.Context, handleSuffix []byte, blobFileNum base.DiskFileNum, valLen uint32, _ []byte,
3235
) (val []byte, callerOwned bool, err error) {
3336
if bv.trackedHandles == nil {
@@ -51,14 +54,14 @@ func (bv *BlobValues) Fetch(
5154
// deterministically from the file number, block number and offset in block.
5255
if len(value) == 0 {
5356
rng := rand.New(rand.NewPCG((uint64(decodedHandle.FileNum)<<32)|uint64(decodedHandle.BlockNum), uint64(decodedHandle.OffsetInBlock)))
54-
return RandBytes(rng, int(decodedHandle.ValueLen)), false, nil
57+
return testutils.RandBytes(rng, int(decodedHandle.ValueLen)), false, nil
5558
}
5659
return []byte(value), false, nil
5760
}
5861

5962
// ParseInternalValue parses a debug blob handle from the string, returning the
6063
// handle as an InternalValue and recording the handle's corresponding value.
61-
func (bv *BlobValues) ParseInternalValue(input string) (base.InternalValue, error) {
64+
func (bv *Values) ParseInternalValue(input string) (base.InternalValue, error) {
6265
h, err := bv.Parse(input)
6366
if err != nil {
6467
return base.InternalValue{}, err
@@ -88,13 +91,13 @@ func (bv *BlobValues) ParseInternalValue(input string) (base.InternalValue, erro
8891

8992
// IsBlobHandle returns true if the input string looks like it's a debug blob
9093
// handle.
91-
func (bv *BlobValues) IsBlobHandle(input string) bool {
94+
func (bv *Values) IsBlobHandle(input string) bool {
9295
return strings.HasPrefix(input, "blob{")
9396
}
9497

9598
// Parse parses a debug blob handle from the string, returning the handle and
9699
// recording the handle's corresponding value.
97-
func (bv *BlobValues) Parse(input string) (h blob.Handle, err error) {
100+
func (bv *Values) Parse(input string) (h blob.Handle, err error) {
98101
if bv.trackedHandles == nil {
99102
bv.trackedHandles = make(map[blob.Handle]string)
100103
}
@@ -170,8 +173,8 @@ func (bv *BlobValues) Parse(input string) (h blob.Handle, err error) {
170173
// returning an inline handle.
171174
//
172175
// It's intended for tests that must manually construct inline blob references.
173-
func (bv *BlobValues) ParseInlineHandle(
174-
input string, references *BlobReferences,
176+
func (bv *Values) ParseInlineHandle(
177+
input string, references *References,
175178
) (h blob.InlineHandle, err error) {
176179
fullHandle, err := bv.Parse(input)
177180
if err != nil {
@@ -197,16 +200,16 @@ func errFromPanic(r any) error {
197200
return errors.Errorf("%v", r)
198201
}
199202

200-
// BlobReferences is a helper for tests that manually construct inline blob
203+
// References is a helper for tests that manually construct inline blob
201204
// references. It tracks the set of file numbers used within a sstable, and maps
202205
// each file number to a reference index (encoded within the
203206
// blob.InlineHandlePreface).
204-
type BlobReferences struct {
207+
type References struct {
205208
fileNums []base.DiskFileNum
206209
}
207210

208211
// MapToReferenceIndex maps the given file number to a reference index.
209-
func (b *BlobReferences) MapToReferenceIndex(fileNum base.DiskFileNum) uint32 {
212+
func (b *References) MapToReferenceIndex(fileNum base.DiskFileNum) uint32 {
210213
for i, fn := range b.fileNums {
211214
if fn == fileNum {
212215
return uint32(i)

value_separation_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ import (
1515
"github.com/cockroachdb/crlib/crstrings"
1616
"github.com/cockroachdb/datadriven"
1717
"github.com/cockroachdb/pebble/internal/base"
18+
"github.com/cockroachdb/pebble/internal/blobtest"
1819
"github.com/cockroachdb/pebble/internal/compact"
1920
"github.com/cockroachdb/pebble/internal/manifest"
2021
"github.com/cockroachdb/pebble/internal/testkeys"
21-
"github.com/cockroachdb/pebble/internal/testutils"
2222
"github.com/cockroachdb/pebble/objstorage"
2323
"github.com/cockroachdb/pebble/objstorage/objstorageprovider"
2424
"github.com/cockroachdb/pebble/sstable"
@@ -29,7 +29,7 @@ import (
2929

3030
func TestValueSeparationPolicy(t *testing.T) {
3131
var (
32-
bv testutils.BlobValues
32+
bv blobtest.Values
3333
vs compact.ValueSeparation
3434
tw sstable.RawWriter
3535
fn base.DiskFileNum
@@ -71,7 +71,7 @@ func TestValueSeparationPolicy(t *testing.T) {
7171
if tw != nil {
7272
require.NoError(t, tw.Close())
7373
}
74-
bv = testutils.BlobValues{}
74+
bv = blobtest.Values{}
7575
switch x := d.CmdArgs[0].String(); x {
7676
case "never-separate-values":
7777
vs = compact.NeverSeparateValues{}

0 commit comments

Comments
 (0)