-
Notifications
You must be signed in to change notification settings - Fork 478
/
Copy pathfilter.go
103 lines (87 loc) · 2.49 KB
/
filter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2011 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"
"sync/atomic"
)
// FilterMetrics holds metrics for the filter policy.
type FilterMetrics struct {
// The number of hits for the filter policy. This is the
// number of times the filter policy was successfully used to avoid access
// of a data block.
Hits int64
// The number of misses for the filter policy. This is the number of times
// the filter policy was checked but was unable to filter an access of a data
// block.
Misses int64
}
var dummyFilterMetrics FilterMetrics
func (m *FilterMetrics) readerApply(r *Reader) {
if r.tableFilter != nil {
r.tableFilter.metrics = m
}
}
// BlockHandle is the file offset and length of a block. BlockInterval may be
// non-zero if the block was annotated when writing. See
// BlockIntervalAnnotator. The zero values are used for blocks that never have
// annotation, or to represent the universal set for data blocks and first
// level index blocks.
type BlockHandle struct {
Offset, Length uint64
BlockInterval base.BlockInterval
}
type filterWriter interface {
addKey(key []byte)
finish() ([]byte, error)
metaName() string
policyName() string
}
type tableFilterReader struct {
policy FilterPolicy
metrics *FilterMetrics
}
func newTableFilterReader(policy FilterPolicy) *tableFilterReader {
return &tableFilterReader{
policy: policy,
metrics: &dummyFilterMetrics,
}
}
func (f *tableFilterReader) mayContain(data, key []byte) bool {
mayContain := f.policy.MayContain(TableFilter, data, key)
if mayContain {
atomic.AddInt64(&f.metrics.Misses, 1)
} else {
atomic.AddInt64(&f.metrics.Hits, 1)
}
return mayContain
}
type tableFilterWriter struct {
policy FilterPolicy
writer FilterWriter
// count is the count of the number of keys added to the filter.
count int
}
func newTableFilterWriter(policy FilterPolicy) *tableFilterWriter {
return &tableFilterWriter{
policy: policy,
writer: policy.NewWriter(TableFilter),
}
}
func (f *tableFilterWriter) addKey(key []byte) {
f.count++
f.writer.AddKey(key)
}
func (f *tableFilterWriter) finish() ([]byte, error) {
if f.count == 0 {
return nil, nil
}
return f.writer.Finish(nil), nil
}
func (f *tableFilterWriter) metaName() string {
return "fullfilter." + f.policy.Name()
}
func (f *tableFilterWriter) policyName() string {
return f.policy.Name()
}