github.com/petermattis/pebble@v0.0.0-20190905164901-ab51a2166067/sstable/filter.go (about)

     1  // Copyright 2011 The LevelDB-Go and Pebble Authors. All rights reserved. Use
     2  // of this source code is governed by a BSD-style license that can be found in
     3  // the LICENSE file.
     4  
     5  package sstable
     6  
     7  type filterWriter interface {
     8  	addKey(key []byte)
     9  	finishBlock(blockOffset uint64) error
    10  	finish() ([]byte, error)
    11  	metaName() string
    12  	policyName() string
    13  }
    14  
    15  type tableFilterReader struct {
    16  	policy FilterPolicy
    17  }
    18  
    19  func newTableFilterReader(policy FilterPolicy) *tableFilterReader {
    20  	return &tableFilterReader{
    21  		policy: policy,
    22  	}
    23  }
    24  
    25  func (f *tableFilterReader) mayContain(data, key []byte) bool {
    26  	return f.policy.MayContain(TableFilter, data, key)
    27  }
    28  
    29  type tableFilterWriter struct {
    30  	policy FilterPolicy
    31  	writer FilterWriter
    32  	// count is the count of the number of keys added to the filter.
    33  	count int
    34  }
    35  
    36  func newTableFilterWriter(policy FilterPolicy) *tableFilterWriter {
    37  	return &tableFilterWriter{
    38  		policy: policy,
    39  		writer: policy.NewWriter(TableFilter),
    40  	}
    41  }
    42  
    43  func (f *tableFilterWriter) addKey(key []byte) {
    44  	f.count++
    45  	f.writer.AddKey(key)
    46  }
    47  
    48  func (f *tableFilterWriter) finishBlock(blockOffset uint64) error {
    49  	// NB: table-level filters have nothing to do when a block is finished.
    50  	return nil
    51  }
    52  
    53  func (f *tableFilterWriter) finish() ([]byte, error) {
    54  	if f.count == 0 {
    55  		return nil, nil
    56  	}
    57  	return f.writer.Finish(nil), nil
    58  }
    59  
    60  func (f *tableFilterWriter) metaName() string {
    61  	return "fullfilter." + f.policy.Name()
    62  }
    63  
    64  func (f *tableFilterWriter) policyName() string {
    65  	return f.policy.Name()
    66  }