Skip to content

Commit b25d04c

Browse files
committed
base: add NoFilterPolicy
This is cleaner than special-casing `nil` and will allow differentiating between an unset value and a value that is specifically set to `NoFilterPolicy`.
1 parent 507d3bd commit b25d04c

File tree

10 files changed

+32
-19
lines changed

10 files changed

+32
-19
lines changed

cmd/pebble/db.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func newPebbleDB(dir string) DB {
105105
}
106106
l.EnsureDefaults()
107107
}
108-
opts.Levels[6].FilterPolicy = nil
108+
opts.Levels[6].FilterPolicy = pebble.NoFilterPolicy
109109
opts.FlushSplitBytes = opts.Levels[0].TargetFileSize
110110

111111
opts.EnsureDefaults()

cmd/pebble/replay.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ func (c *replayConfig) parseHooks() *pebble.ParseHooks {
300300
NewFilterPolicy: func(name string) (pebble.FilterPolicy, error) {
301301
switch name {
302302
case "none":
303-
return nil, nil
303+
return base.NoFilterPolicy, nil
304304
case "rocksdb.BuiltinBloomFilter":
305305
return bloom.FilterPolicy(10), nil
306306
default:

internal/base/options.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ type FilterPolicy interface {
6565
NewWriter(ftype FilterType) FilterWriter
6666
}
6767

68+
// NoFilterPolicy implements the "none" filter policy.
69+
var NoFilterPolicy FilterPolicy = noFilter{}
70+
71+
type noFilter struct{}
72+
73+
func (noFilter) Name() string { return "none" }
74+
func (noFilter) MayContain(ftype FilterType, filter, key []byte) bool { return true }
75+
func (noFilter) NewWriter(ftype FilterType) FilterWriter { panic("not implemented") }
76+
6877
// BlockPropertyFilter is used in an Iterator to filter sstables and blocks
6978
// within the sstable. It should not maintain any per-sstable state, and must
7079
// be thread-safe.

metamorphic/options.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ func RandomOptions(
792792
opts.Filters = nil
793793
switch rng.IntN(3) {
794794
case 0:
795-
lopts.FilterPolicy = nil
795+
lopts.FilterPolicy = pebble.NoFilterPolicy
796796
case 1:
797797
lopts.FilterPolicy = bloom.FilterPolicy(10)
798798
default:
@@ -1030,7 +1030,7 @@ func (t *testingFilterPolicy) Name() string {
10301030
func filterPolicyFromName(name string) (pebble.FilterPolicy, error) {
10311031
switch name {
10321032
case "none":
1033-
return nil, nil
1033+
return base.NoFilterPolicy, nil
10341034
case "rocksdb.BuiltinBloomFilter":
10351035
return bloom.FilterPolicy(10), nil
10361036
}

options.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ type FilterWriter = base.FilterWriter
6969
// FilterPolicy exports the base.FilterPolicy type.
7070
type FilterPolicy = base.FilterPolicy
7171

72+
var NoFilterPolicy = base.NoFilterPolicy
73+
7274
// KeySchema exports the colblk.KeySchema type.
7375
type KeySchema = colblk.KeySchema
7476

@@ -421,7 +423,7 @@ type LevelOptions struct {
421423
// One such implementation is bloom.FilterPolicy(10) from the pebble/bloom
422424
// package.
423425
//
424-
// The default value means to use no filter.
426+
// The default value is NoFilterPolicy.
425427
FilterPolicy FilterPolicy
426428

427429
// FilterType defines whether an existing filter policy is applied at a
@@ -464,6 +466,9 @@ func (o *LevelOptions) EnsureDefaults() {
464466
if o.Compression == nil {
465467
o.Compression = func() Compression { return DefaultCompression }
466468
}
469+
if o.FilterPolicy == nil {
470+
o.FilterPolicy = NoFilterPolicy
471+
}
467472
if o.IndexBlockSize <= 0 {
468473
o.IndexBlockSize = o.BlockSize
469474
}
@@ -1454,7 +1459,7 @@ func (o *Options) AddEventListener(l EventListener) {
14541459
func (o *Options) initMaps() {
14551460
for i := range o.Levels {
14561461
l := &o.Levels[i]
1457-
if l.FilterPolicy != nil {
1462+
if l.FilterPolicy != NoFilterPolicy {
14581463
if o.Filters == nil {
14591464
o.Filters = make(map[string]FilterPolicy)
14601465
}
@@ -1488,13 +1493,6 @@ func (o *Options) Clone() *Options {
14881493
return n
14891494
}
14901495

1491-
func filterPolicyName(p FilterPolicy) string {
1492-
if p == nil {
1493-
return "none"
1494-
}
1495-
return p.Name()
1496-
}
1497-
14981496
func (o *Options) String() string {
14991497
var buf bytes.Buffer
15001498

@@ -1612,7 +1610,7 @@ func (o *Options) String() string {
16121610
fmt.Fprintf(&buf, " block_size=%d\n", l.BlockSize)
16131611
fmt.Fprintf(&buf, " block_size_threshold=%d\n", l.BlockSizeThreshold)
16141612
fmt.Fprintf(&buf, " compression=%s\n", resolveDefaultCompression(l.Compression()))
1615-
fmt.Fprintf(&buf, " filter_policy=%s\n", filterPolicyName(l.FilterPolicy))
1613+
fmt.Fprintf(&buf, " filter_policy=%s\n", l.FilterPolicy.Name())
16161614
fmt.Fprintf(&buf, " filter_type=%s\n", l.FilterType)
16171615
fmt.Fprintf(&buf, " index_block_size=%d\n", l.IndexBlockSize)
16181616
fmt.Fprintf(&buf, " target_file_size=%d\n", l.TargetFileSize)
@@ -2103,6 +2101,8 @@ func (o *Options) Parse(s string, hooks *ParseHooks) error {
21032101
case "filter_policy":
21042102
if hooks != nil && hooks.NewFilterPolicy != nil {
21052103
l.FilterPolicy, err = hooks.NewFilterPolicy(value)
2104+
} else {
2105+
l.FilterPolicy = NoFilterPolicy
21062106
}
21072107
case "filter_type":
21082108
switch value {

sstable/colblk_writer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func newColumnarWriter(
150150
block.MakeFlushGovernor(o.BlockSize, o.BlockSizeThreshold, o.SizeClassAwareThreshold, o.AllocatorSizeClasses),
151151
w.opts.Compression, w.opts.Checksum, func(compressedSize int) {})
152152
}
153-
if o.FilterPolicy != nil {
153+
if o.FilterPolicy != base.NoFilterPolicy {
154154
switch o.FilterType {
155155
case TableFilter:
156156
w.filterBlock = newTableFilterWriter(o.FilterPolicy)

sstable/copier.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99

1010
"github.com/cockroachdb/errors"
11+
"github.com/cockroachdb/pebble/internal/base"
1112
"github.com/cockroachdb/pebble/internal/bytealloc"
1213
"github.com/cockroachdb/pebble/objstorage"
1314
"github.com/cockroachdb/pebble/objstorage/objstorageprovider"
@@ -57,7 +58,7 @@ func CopySpan(
5758

5859
// If our input has not filters, our output cannot have filters either.
5960
if r.tableFilter == nil {
60-
o.FilterPolicy = nil
61+
o.FilterPolicy = base.NoFilterPolicy
6162
}
6263
o.TableFormat = r.tableFormat
6364
// We don't want the writer to attempt to write out block property data in

sstable/options.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ type WriterOptions struct {
174174
// One such implementation is bloom.FilterPolicy(10) from the pebble/bloom
175175
// package.
176176
//
177-
// The default value means to use no filter.
177+
// The default value is NoFilterPolicy.
178178
FilterPolicy FilterPolicy
179179

180180
// FilterType defines whether an existing filter policy is applied at a
@@ -348,5 +348,8 @@ func (o WriterOptions) ensureDefaults() WriterOptions {
348348
(o.Compression == block.MinLZCompression && o.TableFormat < TableFormatPebblev6) {
349349
o.Compression = block.SnappyCompression
350350
}
351+
if o.FilterPolicy == nil {
352+
o.FilterPolicy = base.NoFilterPolicy
353+
}
351354
return o
352355
}

sstable/rowblk_writer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1742,7 +1742,7 @@ func newRowWriter(writable objstorage.Writable, o WriterOptions) *RawRowWriter {
17421742
return w
17431743
}
17441744

1745-
if o.FilterPolicy != nil {
1745+
if o.FilterPolicy != base.NoFilterPolicy {
17461746
switch o.FilterType {
17471747
case TableFilter:
17481748
w.filter = newTableFilterWriter(o.FilterPolicy)

sstable/suffix_rewriter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func rewriteKeySuffixesInBlocks(
9898
case props.ComparerName != o.Comparer.Name:
9999
return nil, TableFormatUnspecified, errors.Errorf("mismatched Comparer %s vs %s, replacement requires same splitter to copy filters",
100100
props.ComparerName, o.Comparer.Name)
101-
case o.FilterPolicy != nil && props.FilterPolicyName != o.FilterPolicy.Name():
101+
case props.FilterPolicyName != o.FilterPolicy.Name():
102102
return nil, TableFormatUnspecified, errors.New("mismatched filters")
103103
}
104104

0 commit comments

Comments
 (0)