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 (
8 : "context"
9 :
10 : "github.com/cockroachdb/pebble/internal/base"
11 : "github.com/cockroachdb/pebble/internal/keyspan"
12 : "github.com/cockroachdb/pebble/internal/rangekey"
13 : )
14 :
15 : // VirtualReader wraps Reader. Its purpose is to restrict functionality of the
16 : // Reader which should be inaccessible to virtual sstables, and enforce bounds
17 : // invariants associated with virtual sstables. All reads on virtual sstables
18 : // should go through a VirtualReader.
19 : //
20 : // INVARIANT: Any iterators created through a virtual reader will guarantee that
21 : // they don't expose keys outside the virtual sstable bounds.
22 : type VirtualReader struct {
23 : vState virtualState
24 : reader *Reader
25 : Properties CommonProperties
26 : }
27 :
28 : var _ CommonReader = (*VirtualReader)(nil)
29 :
30 : // Lightweight virtual sstable state which can be passed to sstable iterators.
31 : type virtualState struct {
32 : lower InternalKey
33 : upper InternalKey
34 : fileNum base.FileNum
35 : Compare Compare
36 : isSharedIngested bool
37 : }
38 :
39 : // VirtualReaderParams are the parameters necessary to create a VirtualReader.
40 : type VirtualReaderParams struct {
41 : Lower InternalKey
42 : Upper InternalKey
43 : FileNum base.FileNum
44 : IsSharedIngested bool
45 : // Size is an estimate of the size of the [Lower, Upper) section of the table.
46 : Size uint64
47 : // BackingSize is the total size of the backing table. The ratio between Size
48 : // and BackingSize is used to estimate statistics.
49 : BackingSize uint64
50 : }
51 :
52 : // MakeVirtualReader is used to contruct a reader which can read from virtual
53 : // sstables.
54 1 : func MakeVirtualReader(reader *Reader, p VirtualReaderParams) VirtualReader {
55 1 : vState := virtualState{
56 1 : lower: p.Lower,
57 1 : upper: p.Upper,
58 1 : fileNum: p.FileNum,
59 1 : Compare: reader.Compare,
60 1 : isSharedIngested: p.IsSharedIngested,
61 1 : }
62 1 : v := VirtualReader{
63 1 : vState: vState,
64 1 : reader: reader,
65 1 : }
66 1 :
67 1 : // Scales the given value by the (Size / BackingSize) ratio, rounding up.
68 1 : scale := func(a uint64) uint64 {
69 1 : return (a*p.Size + p.BackingSize - 1) / p.BackingSize
70 1 : }
71 :
72 1 : v.Properties.RawKeySize = scale(reader.Properties.RawKeySize)
73 1 : v.Properties.RawValueSize = scale(reader.Properties.RawValueSize)
74 1 : v.Properties.NumEntries = scale(reader.Properties.NumEntries)
75 1 : v.Properties.NumDeletions = scale(reader.Properties.NumDeletions)
76 1 : v.Properties.NumRangeDeletions = scale(reader.Properties.NumRangeDeletions)
77 1 : v.Properties.NumRangeKeyDels = scale(reader.Properties.NumRangeKeyDels)
78 1 :
79 1 : // Note that we rely on NumRangeKeySets for correctness. If the sstable may
80 1 : // contain range keys, then NumRangeKeySets must be > 0. ceilDiv works because
81 1 : // meta.Size will not be 0 for virtual sstables.
82 1 : v.Properties.NumRangeKeySets = scale(reader.Properties.NumRangeKeySets)
83 1 : v.Properties.ValueBlocksSize = scale(reader.Properties.ValueBlocksSize)
84 1 : v.Properties.NumSizedDeletions = scale(reader.Properties.NumSizedDeletions)
85 1 : v.Properties.RawPointTombstoneKeySize = scale(reader.Properties.RawPointTombstoneKeySize)
86 1 : v.Properties.RawPointTombstoneValueSize = scale(reader.Properties.RawPointTombstoneValueSize)
87 1 :
88 1 : return v
89 : }
90 :
91 : // NewCompactionIter is the compaction iterator function for virtual readers.
92 : func (v *VirtualReader) NewCompactionIter(
93 : transforms IterTransforms,
94 : categoryAndQoS CategoryAndQoS,
95 : statsCollector *CategoryStatsCollector,
96 : rp ReaderProvider,
97 : bufferPool *BufferPool,
98 1 : ) (Iterator, error) {
99 1 : return v.reader.newCompactionIter(
100 1 : transforms, categoryAndQoS, statsCollector, rp, &v.vState, bufferPool)
101 1 : }
102 :
103 : // NewIterWithBlockPropertyFiltersAndContextEtc wraps
104 : // Reader.NewIterWithBlockPropertyFiltersAndContext. We assume that the passed
105 : // in [lower, upper) bounds will have at least some overlap with the virtual
106 : // sstable bounds. No overlap is not currently supported in the iterator.
107 : func (v *VirtualReader) NewIterWithBlockPropertyFiltersAndContextEtc(
108 : ctx context.Context,
109 : transforms IterTransforms,
110 : lower, upper []byte,
111 : filterer *BlockPropertiesFilterer,
112 : useFilterBlock bool,
113 : stats *base.InternalIteratorStats,
114 : categoryAndQoS CategoryAndQoS,
115 : statsCollector *CategoryStatsCollector,
116 : rp ReaderProvider,
117 1 : ) (Iterator, error) {
118 1 : return v.reader.newIterWithBlockPropertyFiltersAndContext(
119 1 : ctx, transforms, lower, upper, filterer, useFilterBlock,
120 1 : stats, categoryAndQoS, statsCollector, rp, &v.vState)
121 1 : }
122 :
123 : // ValidateBlockChecksumsOnBacking will call ValidateBlockChecksumsOnBacking on the underlying reader.
124 : // Note that block checksum validation is NOT restricted to virtual sstable bounds.
125 1 : func (v *VirtualReader) ValidateBlockChecksumsOnBacking() error {
126 1 : return v.reader.ValidateBlockChecksums()
127 1 : }
128 :
129 : // NewRawRangeDelIter wraps Reader.NewRawRangeDelIter.
130 : func (v *VirtualReader) NewRawRangeDelIter(
131 : transforms IterTransforms,
132 1 : ) (keyspan.FragmentIterator, error) {
133 1 : iter, err := v.reader.NewRawRangeDelIter(transforms)
134 1 : if err != nil {
135 0 : return nil, err
136 0 : }
137 1 : if iter == nil {
138 1 : return nil, nil
139 1 : }
140 :
141 : // Note that if upper is not an exclusive sentinel, Truncate will assert that
142 : // there is no span that contains that key.
143 : //
144 : // As an example, if an sstable contains a rangedel a-c and point keys at
145 : // a.SET.2 and b.SET.3, the file bounds [a#2,SET-b#RANGEDELSENTINEL] are
146 : // allowed (as they exclude b.SET.3), or [a#2,SET-c#RANGEDELSENTINEL] (as it
147 : // includes both point keys), but not [a#2,SET-b#3,SET] (as it would truncate
148 : // the rangedel at b and lead to the point being uncovered).
149 1 : return keyspan.Truncate(
150 1 : v.reader.Compare, iter,
151 1 : base.UserKeyBoundsFromInternal(v.vState.lower, v.vState.upper),
152 1 : ), nil
153 : }
154 :
155 : // NewRawRangeKeyIter wraps Reader.NewRawRangeKeyIter.
156 : func (v *VirtualReader) NewRawRangeKeyIter(
157 : transforms IterTransforms,
158 1 : ) (keyspan.FragmentIterator, error) {
159 1 : syntheticSeqNum := transforms.SyntheticSeqNum
160 1 : if v.vState.isSharedIngested {
161 1 : // Don't pass a synthetic sequence number for shared ingested sstables. We
162 1 : // need to know the materialized sequence numbers, and we will set up the
163 1 : // appropriate sequence number substitution below.
164 1 : transforms.SyntheticSeqNum = 0
165 1 : }
166 1 : iter, err := v.reader.NewRawRangeKeyIter(transforms)
167 1 : if err != nil {
168 0 : return nil, err
169 0 : }
170 1 : if iter == nil {
171 1 : return nil, nil
172 1 : }
173 :
174 1 : if v.vState.isSharedIngested {
175 1 : // We need to coalesce range keys within each sstable, and then apply the
176 1 : // synthetic sequence number. For this, we use ForeignSSTTransformer.
177 1 : //
178 1 : // TODO(bilal): Avoid these allocations by hoisting the transformer and
179 1 : // transform iter into VirtualReader.
180 1 : transform := &rangekey.ForeignSSTTransformer{
181 1 : Equal: v.reader.Equal,
182 1 : SeqNum: uint64(syntheticSeqNum),
183 1 : }
184 1 : transformIter := &keyspan.TransformerIter{
185 1 : FragmentIterator: iter,
186 1 : Transformer: transform,
187 1 : Compare: v.reader.Compare,
188 1 : }
189 1 : iter = transformIter
190 1 : }
191 :
192 : // Note that if upper is not an exclusive sentinel, Truncate will assert that
193 : // there is no span that contains that key.
194 : //
195 : // As an example, if an sstable contains a range key a-c and point keys at
196 : // a.SET.2 and b.SET.3, the file bounds [a#2,SET-b#RANGEKEYSENTINEL] are
197 : // allowed (as they exclude b.SET.3), or [a#2,SET-c#RANGEKEYSENTINEL] (as it
198 : // includes both point keys), but not [a#2,SET-b#3,SET] (as it would truncate
199 : // the range key at b and lead to the point being uncovered).
200 1 : return keyspan.Truncate(
201 1 : v.reader.Compare, iter,
202 1 : base.UserKeyBoundsFromInternal(v.vState.lower, v.vState.upper),
203 1 : ), nil
204 : }
205 :
206 : // Constrain bounds will narrow the start, end bounds if they do not fit within
207 : // the virtual sstable. The function will return if the new end key is
208 : // inclusive.
209 : func (v *virtualState) constrainBounds(
210 : start, end []byte, endInclusive bool,
211 1 : ) (lastKeyInclusive bool, first []byte, last []byte) {
212 1 : first = start
213 1 : if start == nil || v.Compare(start, v.lower.UserKey) < 0 {
214 1 : first = v.lower.UserKey
215 1 : }
216 :
217 : // Note that we assume that start, end has some overlap with the virtual
218 : // sstable bounds.
219 1 : last = v.upper.UserKey
220 1 : lastKeyInclusive = !v.upper.IsExclusiveSentinel()
221 1 : if end != nil {
222 1 : cmp := v.Compare(end, v.upper.UserKey)
223 1 : switch {
224 1 : case cmp == 0:
225 1 : lastKeyInclusive = !v.upper.IsExclusiveSentinel() && endInclusive
226 1 : last = v.upper.UserKey
227 1 : case cmp > 0:
228 1 : lastKeyInclusive = !v.upper.IsExclusiveSentinel()
229 1 : last = v.upper.UserKey
230 1 : default:
231 1 : lastKeyInclusive = endInclusive
232 1 : last = end
233 : }
234 : }
235 : // TODO(bananabrick): What if someone passes in bounds completely outside of
236 : // virtual sstable bounds?
237 1 : return lastKeyInclusive, first, last
238 : }
239 :
240 : // EstimateDiskUsage just calls VirtualReader.reader.EstimateDiskUsage after
241 : // enforcing the virtual sstable bounds.
242 1 : func (v *VirtualReader) EstimateDiskUsage(start, end []byte) (uint64, error) {
243 1 : _, f, l := v.vState.constrainBounds(start, end, true /* endInclusive */)
244 1 : return v.reader.EstimateDiskUsage(f, l)
245 1 : }
246 :
247 : // CommonProperties implements the CommonReader interface.
248 1 : func (v *VirtualReader) CommonProperties() *CommonProperties {
249 1 : return &v.Properties
250 1 : }
|