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 : bytesIterated *uint64,
95 : categoryAndQoS CategoryAndQoS,
96 : statsCollector *CategoryStatsCollector,
97 : rp ReaderProvider,
98 : bufferPool *BufferPool,
99 1 : ) (Iterator, error) {
100 1 : return v.reader.newCompactionIter(
101 1 : transforms, bytesIterated, categoryAndQoS, statsCollector, rp, &v.vState, bufferPool)
102 1 : }
103 :
104 : // NewIterWithBlockPropertyFiltersAndContextEtc wraps
105 : // Reader.NewIterWithBlockPropertyFiltersAndContext. We assume that the passed
106 : // in [lower, upper) bounds will have at least some overlap with the virtual
107 : // sstable bounds. No overlap is not currently supported in the iterator.
108 : func (v *VirtualReader) NewIterWithBlockPropertyFiltersAndContextEtc(
109 : ctx context.Context,
110 : transforms IterTransforms,
111 : lower, upper []byte,
112 : filterer *BlockPropertiesFilterer,
113 : useFilterBlock bool,
114 : stats *base.InternalIteratorStats,
115 : categoryAndQoS CategoryAndQoS,
116 : statsCollector *CategoryStatsCollector,
117 : rp ReaderProvider,
118 1 : ) (Iterator, error) {
119 1 : return v.reader.newIterWithBlockPropertyFiltersAndContext(
120 1 : ctx, transforms, lower, upper, filterer, useFilterBlock,
121 1 : stats, categoryAndQoS, statsCollector, rp, &v.vState)
122 1 : }
123 :
124 : // ValidateBlockChecksumsOnBacking will call ValidateBlockChecksumsOnBacking on the underlying reader.
125 : // Note that block checksum validation is NOT restricted to virtual sstable bounds.
126 1 : func (v *VirtualReader) ValidateBlockChecksumsOnBacking() error {
127 1 : return v.reader.ValidateBlockChecksums()
128 1 : }
129 :
130 : // NewRawRangeDelIter wraps Reader.NewRawRangeDelIter.
131 : func (v *VirtualReader) NewRawRangeDelIter(
132 : transforms IterTransforms,
133 1 : ) (keyspan.FragmentIterator, error) {
134 1 : iter, err := v.reader.NewRawRangeDelIter(transforms)
135 1 : if err != nil {
136 0 : return nil, err
137 0 : }
138 1 : if iter == nil {
139 1 : return nil, nil
140 1 : }
141 :
142 : // Note that if upper is not an exclusive sentinel, Truncate will assert that
143 : // there is no span that contains that key.
144 : //
145 : // As an example, if an sstable contains a rangedel a-c and point keys at
146 : // a.SET.2 and b.SET.3, the file bounds [a#2,SET-b#RANGEDELSENTINEL] are
147 : // allowed (as they exclude b.SET.3), or [a#2,SET-c#RANGEDELSENTINEL] (as it
148 : // includes both point keys), but not [a#2,SET-b#3,SET] (as it would truncate
149 : // the rangedel at b and lead to the point being uncovered).
150 1 : return keyspan.Truncate(
151 1 : v.reader.Compare, iter,
152 1 : base.UserKeyBoundsFromInternal(v.vState.lower, v.vState.upper),
153 1 : ), nil
154 : }
155 :
156 : // NewRawRangeKeyIter wraps Reader.NewRawRangeKeyIter.
157 : func (v *VirtualReader) NewRawRangeKeyIter(
158 : transforms IterTransforms,
159 1 : ) (keyspan.FragmentIterator, error) {
160 1 : syntheticSeqNum := transforms.SyntheticSeqNum
161 1 : if v.vState.isSharedIngested {
162 1 : // Don't pass a synthetic sequence number for shared ingested sstables. We
163 1 : // need to know the materialized sequence numbers, and we will set up the
164 1 : // appropriate sequence number substitution below.
165 1 : transforms.SyntheticSeqNum = 0
166 1 : }
167 1 : iter, err := v.reader.NewRawRangeKeyIter(transforms)
168 1 : if err != nil {
169 0 : return nil, err
170 0 : }
171 1 : if iter == nil {
172 1 : return nil, nil
173 1 : }
174 :
175 1 : if v.vState.isSharedIngested {
176 1 : // We need to coalesce range keys within each sstable, and then apply the
177 1 : // synthetic sequence number. For this, we use ForeignSSTTransformer.
178 1 : //
179 1 : // TODO(bilal): Avoid these allocations by hoisting the transformer and
180 1 : // transform iter into VirtualReader.
181 1 : transform := &rangekey.ForeignSSTTransformer{
182 1 : Equal: v.reader.Equal,
183 1 : SeqNum: uint64(syntheticSeqNum),
184 1 : }
185 1 : transformIter := &keyspan.TransformerIter{
186 1 : FragmentIterator: iter,
187 1 : Transformer: transform,
188 1 : Compare: v.reader.Compare,
189 1 : }
190 1 : iter = transformIter
191 1 : }
192 :
193 : // Note that if upper is not an exclusive sentinel, Truncate will assert that
194 : // there is no span that contains that key.
195 : //
196 : // As an example, if an sstable contains a range key a-c and point keys at
197 : // a.SET.2 and b.SET.3, the file bounds [a#2,SET-b#RANGEKEYSENTINEL] are
198 : // allowed (as they exclude b.SET.3), or [a#2,SET-c#RANGEKEYSENTINEL] (as it
199 : // includes both point keys), but not [a#2,SET-b#3,SET] (as it would truncate
200 : // the range key at b and lead to the point being uncovered).
201 1 : return keyspan.Truncate(
202 1 : v.reader.Compare, iter,
203 1 : base.UserKeyBoundsFromInternal(v.vState.lower, v.vState.upper),
204 1 : ), nil
205 : }
206 :
207 : // Constrain bounds will narrow the start, end bounds if they do not fit within
208 : // the virtual sstable. The function will return if the new end key is
209 : // inclusive.
210 : func (v *virtualState) constrainBounds(
211 : start, end []byte, endInclusive bool,
212 1 : ) (lastKeyInclusive bool, first []byte, last []byte) {
213 1 : first = start
214 1 : if start == nil || v.Compare(start, v.lower.UserKey) < 0 {
215 1 : first = v.lower.UserKey
216 1 : }
217 :
218 : // Note that we assume that start, end has some overlap with the virtual
219 : // sstable bounds.
220 1 : last = v.upper.UserKey
221 1 : lastKeyInclusive = !v.upper.IsExclusiveSentinel()
222 1 : if end != nil {
223 1 : cmp := v.Compare(end, v.upper.UserKey)
224 1 : switch {
225 1 : case cmp == 0:
226 1 : lastKeyInclusive = !v.upper.IsExclusiveSentinel() && endInclusive
227 1 : last = v.upper.UserKey
228 1 : case cmp > 0:
229 1 : lastKeyInclusive = !v.upper.IsExclusiveSentinel()
230 1 : last = v.upper.UserKey
231 1 : default:
232 1 : lastKeyInclusive = endInclusive
233 1 : last = end
234 : }
235 : }
236 : // TODO(bananabrick): What if someone passes in bounds completely outside of
237 : // virtual sstable bounds?
238 1 : return lastKeyInclusive, first, last
239 : }
240 :
241 : // EstimateDiskUsage just calls VirtualReader.reader.EstimateDiskUsage after
242 : // enforcing the virtual sstable bounds.
243 1 : func (v *VirtualReader) EstimateDiskUsage(start, end []byte) (uint64, error) {
244 1 : _, f, l := v.vState.constrainBounds(start, end, true /* endInclusive */)
245 1 : return v.reader.EstimateDiskUsage(f, l)
246 1 : }
247 :
248 : // CommonProperties implements the CommonReader interface.
249 1 : func (v *VirtualReader) CommonProperties() *CommonProperties {
250 1 : return &v.Properties
251 1 : }
|