Skip to content

Commit

Permalink
sstable: add ReadAll test util
Browse files Browse the repository at this point in the history
Add a test routine to allow tests to easily read all
points/rangedels/rangekeys from an sstable.
  • Loading branch information
RaduBerinde committed May 6, 2024
1 parent 1210811 commit bc2d51c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
37 changes: 37 additions & 0 deletions internal/testutils/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2024 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 testutils

// CheckErr can be used to simplify test code that expects no errors.
// Instead of:
//
// v, err := SomeFunc()
// if err != nil { .. }
//
// we can use:
//
// v := testutils.CheckErr(someFunc())
func CheckErr[V any](v V, err error) V {
if err != nil {
panic(err)
}
return v
}

// CheckErr2 can be used to simplify test code that expects no errors.
// Instead of:
//
// v, w, err := SomeFunc()
// if err != nil { .. }
//
// we can use:
//
// v, w := testutils.CheckErr2(someFunc())
func CheckErr2[V any, W any](v V, w W, err error) (V, W) {
if err != nil {
panic(err)
}
return v, w
}
2 changes: 1 addition & 1 deletion sstable/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ type Reader struct {

var _ CommonReader = (*Reader)(nil)

// Close implements DB.Close, as documented in the pebble package.
// Close the reader and the underlying objstorage.Readable.
func (r *Reader) Close() error {
r.opts.Cache.Unref()

Expand Down
46 changes: 46 additions & 0 deletions sstable/test_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2024 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 sstable

import (
"github.com/cockroachdb/pebble/internal/base"
"github.com/cockroachdb/pebble/internal/keyspan"
"github.com/cockroachdb/pebble/internal/testutils"
"github.com/cockroachdb/pebble/objstorage"
)

// ReadAll returns all point keys, range del spans, and range key spans from an
// sstable. Closes the Readable. Panics on errors.
func ReadAll(
r objstorage.Readable,
) (points []base.InternalKV, rangeDels, rangeKeys []keyspan.Span) {
reader := testutils.CheckErr(NewReader(r, ReaderOptions{}))
defer reader.Close()
pointIter := testutils.CheckErr(reader.NewIter(NoTransforms, nil /* lower */, nil /* upper */))
defer pointIter.Close()

for kv := pointIter.First(); kv != nil; kv = pointIter.Next() {
val, _ := testutils.CheckErr2(kv.Value(nil))
points = append(points, base.InternalKV{
K: kv.K.Clone(),
V: base.MakeInPlaceValue(val),
})
}

if rangeDelIter := testutils.CheckErr(reader.NewRawRangeDelIter(NoTransforms)); rangeDelIter != nil {
defer rangeDelIter.Close()
for s := testutils.CheckErr(rangeDelIter.First()); s != nil; s = testutils.CheckErr(rangeDelIter.Next()) {
rangeDels = append(rangeDels, s.DeepClone())
}
}

if rangeKeyIter := testutils.CheckErr(reader.NewRawRangeKeyIter(NoTransforms)); rangeKeyIter != nil {
defer rangeKeyIter.Close()
for s := testutils.CheckErr(rangeKeyIter.First()); s != nil; s = testutils.CheckErr(rangeKeyIter.Next()) {
rangeKeys = append(rangeKeys, s.DeepClone())
}
}
return points, rangeDels, rangeKeys
}

0 comments on commit bc2d51c

Please sign in to comment.