Skip to content

Commit

Permalink
db: switch from table to block property collector in iterator test
Browse files Browse the repository at this point in the history
Informs #1887.
  • Loading branch information
RaduBerinde committed Jan 9, 2024
1 parent 8782c55 commit 407904c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 46 deletions.
11 changes: 1 addition & 10 deletions data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func runIterCmd(d *datadriven.TestData, iter *Iterator, closeIter bool) string {
func parseIterOptions(
opts *IterOptions, ref *IterOptions, parts []string,
) (foundAny bool, err error) {
const usageString = "[lower=<lower>] [upper=<upper>] [key-types=point|range|both] [mask-suffix=<suffix>] [mask-filter=<bool>] [only-durable=<bool>] [table-filter=reuse|none] [point-filters=reuse|none]\n"
const usageString = "[lower=<lower>] [upper=<upper>] [key-types=point|range|both] [mask-suffix=<suffix>] [mask-filter=<bool>] [only-durable=<bool>] point-filters=reuse|none]\n"
for _, part := range parts {
arg := strings.SplitN(part, "=", 2)
if len(arg) != 2 {
Expand Down Expand Up @@ -300,15 +300,6 @@ func parseIterOptions(
opts.RangeKeyMasking.Filter = func() BlockPropertyFilterMask {
return sstable.NewTestKeysMaskingFilter()
}
case "table-filter":
switch arg[1] {
case "reuse":
opts.TableFilter = ref.TableFilter
case "none":
opts.TableFilter = nil
default:
return false, errors.Newf("unknown arg table-filter=%q:\n%s", arg[1], usageString)
}
case "only-durable":
var err error
opts.OnlyReadGuaranteedDurable, err = strconv.ParseBool(arg[1])
Expand Down
75 changes: 55 additions & 20 deletions iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package pebble
import (
"bytes"
"context"
"encoding/binary"
"flag"
"fmt"
"io"
Expand Down Expand Up @@ -458,20 +459,55 @@ type minSeqNumPropertyCollector struct {
minSeqNum uint64
}

var _ BlockPropertyCollector = (*minSeqNumPropertyCollector)(nil)

func (c *minSeqNumPropertyCollector) Name() string {
return "minSeqNumPropertyCollector"
}

func (c *minSeqNumPropertyCollector) Add(key InternalKey, value []byte) error {
if c.minSeqNum == 0 || c.minSeqNum > key.SeqNum() {
c.minSeqNum = key.SeqNum()
}
return nil
}

func (c *minSeqNumPropertyCollector) Finish(userProps map[string]string) error {
userProps["test.min-seq-num"] = fmt.Sprint(c.minSeqNum)
return nil
func (c *minSeqNumPropertyCollector) FinishDataBlock(buf []byte) ([]byte, error) {
return nil, nil
}

func (c *minSeqNumPropertyCollector) Name() string {
return "minSeqNumPropertyCollector"
func (c *minSeqNumPropertyCollector) AddPrevDataBlockToIndexBlock() {}

func (c *minSeqNumPropertyCollector) FinishIndexBlock(buf []byte) ([]byte, error) {
return nil, nil
}

func (c *minSeqNumPropertyCollector) FinishTable(buf []byte) ([]byte, error) {
return binary.AppendUvarint(buf, c.minSeqNum), nil
}

// minSeqNumFilter is a BlockPropertyFilter that uses the
// minSeqNumPropertyCollector data to filter out entire tables.
type minSeqNumFilter struct {
seqNumUpperBound uint64
}

var _ BlockPropertyFilter = (*minSeqNumFilter)(nil)

func (*minSeqNumFilter) Name() string {
return (&minSeqNumPropertyCollector{}).Name()
}

func (f *minSeqNumFilter) Intersects(prop []byte) (bool, error) {
// Blocks will have no data.
if len(prop) == 0 {
return true, nil
}
minSeqNum, n := binary.Uvarint(prop)
if n <= 0 {
return false, errors.Errorf("invalid block property data %v", prop)
}
return minSeqNum < f.seqNumUpperBound, nil
}

func TestReadSampling(t *testing.T) {
Expand Down Expand Up @@ -504,10 +540,11 @@ func TestReadSampling(t *testing.T) {
}

opts := &Options{}
opts.TablePropertyCollectors = append(opts.TablePropertyCollectors,
func() TablePropertyCollector {
opts.BlockPropertyCollectors = []func() BlockPropertyCollector{
func() BlockPropertyCollector {
return &minSeqNumPropertyCollector{}
})
},
}

var err error
if d, err = runDBDefineCmd(td, opts); err != nil {
Expand Down Expand Up @@ -653,10 +690,11 @@ func TestIteratorTableFilter(t *testing.T) {
}

opts := &Options{}
opts.TablePropertyCollectors = append(opts.TablePropertyCollectors,
func() TablePropertyCollector {
opts.BlockPropertyCollectors = []func() BlockPropertyCollector{
func() BlockPropertyCollector {
return &minSeqNumPropertyCollector{}
})
},
}

var err error
if d, err = runDBDefineCmd(td, opts); err != nil {
Expand All @@ -676,12 +714,8 @@ func TestIteratorTableFilter(t *testing.T) {
iterOpts := &IterOptions{}
var filterSeqNum uint64
if td.MaybeScanArgs(t, "filter", &filterSeqNum) {
iterOpts.TableFilter = func(userProps map[string]string) bool {
minSeqNum, err := strconv.ParseUint(userProps["test.min-seq-num"], 10, 64)
if err != nil {
return true
}
return minSeqNum < filterSeqNum
iterOpts.PointKeyFilters = []BlockPropertyFilter{
&minSeqNumFilter{seqNumUpperBound: filterSeqNum},
}
}

Expand Down Expand Up @@ -868,10 +902,11 @@ func TestIteratorSeekOpt(t *testing.T) {
seekPrefixGEUsingNext = 0

opts := &Options{}
opts.TablePropertyCollectors = append(opts.TablePropertyCollectors,
func() TablePropertyCollector {
opts.BlockPropertyCollectors = []func() BlockPropertyCollector{
func() BlockPropertyCollector {
return &minSeqNumPropertyCollector{}
})
},
}

var err error
if d, err = runDBDefineCmd(td, opts); err != nil {
Expand Down
16 changes: 0 additions & 16 deletions testdata/iterator_table_filter
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,3 @@ iter filter=1
first
----
.

# Set-options that reuses the filter should still see the filter apply.
# Set-options that removes the filter should not.

iter filter=4
first
set-options table-filter=reuse
first
set-options table-filter=none
first
----
a: (3, .)
.
a: (3, .)
.
a: (4, .)

0 comments on commit 407904c

Please sign in to comment.