Line data Source code
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 : import "sync/atomic" 8 : 9 : // FilterMetrics holds metrics for the filter policy. 10 : type FilterMetrics struct { 11 : // The number of hits for the filter policy. This is the 12 : // number of times the filter policy was successfully used to avoid access 13 : // of a data block. 14 : Hits int64 15 : // The number of misses for the filter policy. This is the number of times 16 : // the filter policy was checked but was unable to filter an access of a data 17 : // block. 18 : Misses int64 19 : } 20 : 21 : // FilterMetricsTracker is used to keep track of filter metrics. It contains the 22 : // same metrics as FilterMetrics, but they can be updated atomically. An 23 : // instance of FilterMetricsTracker can be passed to a Reader as a ReaderOption. 24 : type FilterMetricsTracker struct { 25 : // See FilterMetrics.Hits. 26 : hits atomic.Int64 27 : // See FilterMetrics.Misses. 28 : misses atomic.Int64 29 : } 30 : 31 : var _ ReaderOption = (*FilterMetricsTracker)(nil) 32 : 33 1 : func (m *FilterMetricsTracker) readerApply(r *Reader) { 34 1 : if r.tableFilter != nil { 35 1 : r.tableFilter.metrics = m 36 1 : } 37 : } 38 : 39 : // Load returns the current values as FilterMetrics. 40 1 : func (m *FilterMetricsTracker) Load() FilterMetrics { 41 1 : return FilterMetrics{ 42 1 : Hits: m.hits.Load(), 43 1 : Misses: m.misses.Load(), 44 1 : } 45 1 : } 46 : 47 : // BlockHandle is the file offset and length of a block. 48 : type BlockHandle struct { 49 : Offset, Length uint64 50 : } 51 : 52 : // BlockHandleWithProperties is used for data blocks and first/lower level 53 : // index blocks, since they can be annotated using BlockPropertyCollectors. 54 : type BlockHandleWithProperties struct { 55 : BlockHandle 56 : Props []byte 57 : } 58 : 59 : type filterWriter interface { 60 : addKey(key []byte) 61 : finish() ([]byte, error) 62 : metaName() string 63 : policyName() string 64 : } 65 : 66 : type tableFilterReader struct { 67 : policy FilterPolicy 68 : metrics *FilterMetricsTracker 69 : } 70 : 71 1 : func newTableFilterReader(policy FilterPolicy) *tableFilterReader { 72 1 : return &tableFilterReader{ 73 1 : policy: policy, 74 1 : metrics: nil, 75 1 : } 76 1 : } 77 : 78 1 : func (f *tableFilterReader) mayContain(data, key []byte) bool { 79 1 : mayContain := f.policy.MayContain(TableFilter, data, key) 80 1 : if f.metrics != nil { 81 1 : if mayContain { 82 1 : f.metrics.misses.Add(1) 83 1 : } else { 84 1 : f.metrics.hits.Add(1) 85 1 : } 86 : } 87 1 : return mayContain 88 : } 89 : 90 : type tableFilterWriter struct { 91 : policy FilterPolicy 92 : writer FilterWriter 93 : // count is the count of the number of keys added to the filter. 94 : count int 95 : } 96 : 97 1 : func newTableFilterWriter(policy FilterPolicy) *tableFilterWriter { 98 1 : return &tableFilterWriter{ 99 1 : policy: policy, 100 1 : writer: policy.NewWriter(TableFilter), 101 1 : } 102 1 : } 103 : 104 1 : func (f *tableFilterWriter) addKey(key []byte) { 105 1 : f.count++ 106 1 : f.writer.AddKey(key) 107 1 : } 108 : 109 1 : func (f *tableFilterWriter) finish() ([]byte, error) { 110 1 : if f.count == 0 { 111 1 : return nil, nil 112 1 : } 113 1 : return f.writer.Finish(nil), nil 114 : } 115 : 116 1 : func (f *tableFilterWriter) metaName() string { 117 1 : return "fullfilter." + f.policy.Name() 118 1 : } 119 : 120 1 : func (f *tableFilterWriter) policyName() string { 121 1 : return f.policy.Name() 122 1 : }