Skip to content

Commit a4e831f

Browse files
committed
manifest: use new InternalKeyBounds struct in TableMetadata's range keys
This patch introduces (and uses) a new `InternalKeyBounds` struct to represent both the smallest, largest range keys (if they exist) in our `TableMetadata` struct. These keys are now stored as a single string -- spliced by a separator index for individual access. This allows us to save 24 bytes for our `TableMetadata` struct. Informs: #2047
1 parent 27eaac8 commit a4e831f

File tree

16 files changed

+145
-76
lines changed

16 files changed

+145
-76
lines changed

compaction.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2612,7 +2612,7 @@ func (d *DB) runCopyCompaction(
26122612
newMeta.ExtendPointKeyBounds(c.cmp, inputMeta.SmallestPointKey, inputMeta.LargestPointKey)
26132613
}
26142614
if inputMeta.HasRangeKeys {
2615-
newMeta.ExtendRangeKeyBounds(c.cmp, inputMeta.SmallestRangeKey, inputMeta.LargestRangeKey)
2615+
newMeta.ExtendRangeKeyBounds(c.cmp, inputMeta.RangeKeyBounds.Smallest(), inputMeta.RangeKeyBounds.Largest())
26162616
}
26172617
newMeta.FileNum = d.mu.versions.getNextFileNum()
26182618
if objMeta.IsExternal() {

db.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3043,23 +3043,23 @@ func (d *DB) checkVirtualBounds(m *tableMetadata) {
30433043
// Check that the lower bound is tight.
30443044
if s, err := rangeKeyIter.First(); err != nil {
30453045
panic(err)
3046-
} else if d.cmp(s.SmallestKey().UserKey, m.SmallestRangeKey.UserKey) != 0 {
3046+
} else if d.cmp(s.SmallestKey().UserKey, m.RangeKeyBounds.SmallestUserKey()) != 0 {
30473047
panic(errors.Newf("pebble: virtual sstable %s lower range key bound is not tight", m.FileNum))
30483048
}
30493049

30503050
// Check that upper bound is tight.
30513051
if s, err := rangeKeyIter.Last(); err != nil {
30523052
panic(err)
3053-
} else if d.cmp(s.LargestKey().UserKey, m.LargestRangeKey.UserKey) != 0 {
3053+
} else if d.cmp(s.LargestKey().UserKey, m.RangeKeyBounds.LargestUserKey()) != 0 {
30543054
panic(errors.Newf("pebble: virtual sstable %s upper range key bound is not tight", m.FileNum))
30553055
}
30563056

30573057
s, err := rangeKeyIter.First()
30583058
for ; s != nil; s, err = rangeKeyIter.Next() {
3059-
if d.cmp(s.SmallestKey().UserKey, m.SmallestRangeKey.UserKey) < 0 {
3059+
if d.cmp(s.SmallestKey().UserKey, m.RangeKeyBounds.SmallestUserKey()) < 0 {
30603060
panic(errors.Newf("pebble: virtual sstable %s point key %s is not within bounds", m.FileNum, s.SmallestKey().UserKey))
30613061
}
3062-
if d.cmp(s.LargestKey().UserKey, m.LargestRangeKey.UserKey) > 0 {
3062+
if d.cmp(s.LargestKey().UserKey, m.RangeKeyBounds.LargestUserKey()) > 0 {
30633063
panic(errors.Newf("pebble: virtual sstable %s point key %s is not within bounds", m.FileNum, s.LargestKey().UserKey))
30643064
}
30653065
}

excise.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,11 @@ func looseLeftTableBounds(
266266
leftTable.ExtendPointKeyBounds(cmp, originalTable.SmallestPointKey, largestPointKey)
267267
}
268268
if originalTable.HasRangeKeys {
269-
largestRangeKey := originalTable.LargestRangeKey
269+
largestRangeKey := originalTable.RangeKeyBounds.Largest()
270270
if largestRangeKey.IsUpperBoundFor(cmp, exciseSpanStart) {
271271
largestRangeKey = base.MakeExclusiveSentinelKey(InternalKeyKindRangeKeyMin, exciseSpanStart)
272272
}
273-
leftTable.ExtendRangeKeyBounds(cmp, originalTable.SmallestRangeKey, largestRangeKey)
273+
leftTable.ExtendRangeKeyBounds(cmp, originalTable.RangeKeyBounds.Smallest(), largestRangeKey)
274274
}
275275
}
276276

@@ -294,11 +294,11 @@ func looseRightTableBounds(
294294
rightTable.ExtendPointKeyBounds(cmp, smallestPointKey, originalTable.LargestPointKey)
295295
}
296296
if originalTable.HasRangeKeys {
297-
smallestRangeKey := originalTable.SmallestRangeKey
297+
smallestRangeKey := originalTable.RangeKeyBounds.Smallest()
298298
if !smallestRangeKey.IsUpperBoundFor(cmp, exciseSpanEnd) {
299299
smallestRangeKey = base.MakeInternalKey(exciseSpanEnd, 0, base.InternalKeyKindRangeKeyMax)
300300
}
301-
rightTable.ExtendRangeKeyBounds(cmp, smallestRangeKey, originalTable.LargestRangeKey)
301+
rightTable.ExtendRangeKeyBounds(cmp, smallestRangeKey, originalTable.RangeKeyBounds.Largest())
302302
}
303303
}
304304

@@ -332,7 +332,7 @@ func determineLeftTableBounds(
332332
}
333333
}
334334

335-
if originalTable.HasRangeKeys && cmp(originalTable.SmallestRangeKey.UserKey, exciseSpanStart) < 0 {
335+
if originalTable.HasRangeKeys && cmp(originalTable.RangeKeyBounds.SmallestUserKey(), exciseSpanStart) < 0 {
336336
rkey, err := iters.RangeKey().SeekLT(exciseSpanStart)
337337
if err != nil {
338338
return err
@@ -344,7 +344,7 @@ func determineLeftTableBounds(
344344
// The key is owned by the range key iter, so we need to copy it.
345345
lastRangeKey = slices.Clone(rkey.End)
346346
}
347-
leftTable.ExtendRangeKeyBounds(cmp, originalTable.SmallestRangeKey,
347+
leftTable.ExtendRangeKeyBounds(cmp, originalTable.RangeKeyBounds.Smallest(),
348348
base.MakeExclusiveSentinelKey(rkey.LargestKey().Kind(), lastRangeKey))
349349
}
350350
}
@@ -393,7 +393,7 @@ func determineRightTableBounds(
393393
}, originalTable.LargestPointKey)
394394
}
395395
}
396-
if originalTable.HasRangeKeys && !exciseSpanEnd.IsUpperBoundForInternalKey(cmp, originalTable.LargestRangeKey) {
396+
if originalTable.HasRangeKeys && !exciseSpanEnd.IsUpperBoundForInternalKey(cmp, originalTable.RangeKeyBounds.Largest()) {
397397
rkey, err := iters.RangeKey().SeekGE(exciseSpanEnd.Key)
398398
if err != nil {
399399
return err
@@ -410,7 +410,7 @@ func determineRightTableBounds(
410410
rightTable.ExtendRangeKeyBounds(cmp, base.InternalKey{
411411
UserKey: firstRangeKey,
412412
Trailer: rkey.SmallestKey().Trailer,
413-
}, originalTable.LargestRangeKey)
413+
}, originalTable.RangeKeyBounds.Largest())
414414
}
415415
}
416416
return nil

excise_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ func TestExciseBounds(t *testing.T) {
804804
fmt.Fprintf(&buf, " point: %v - %v\n", m.SmallestPointKey, m.LargestPointKey)
805805
}
806806
if m.HasRangeKeys {
807-
fmt.Fprintf(&buf, " range: %v - %v\n", m.SmallestRangeKey, m.LargestRangeKey)
807+
fmt.Fprintf(&buf, " range: %v - %v\n", m.RangeKeyBounds.Smallest(), m.RangeKeyBounds.Largest())
808808
}
809809
}
810810
switch td.Cmd {

file_cache_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,11 +369,12 @@ func TestVirtualReadsWiring(t *testing.T) {
369369
LargestSeqNumAbsolute: parentFile.LargestSeqNumAbsolute,
370370
SmallestPointKey: base.MakeInternalKey([]byte{'d'}, seqNumCEDel, InternalKeyKindRangeDelete),
371371
LargestPointKey: base.MakeInternalKey([]byte{'z'}, seqNumZ, InternalKeyKindSet),
372-
SmallestRangeKey: base.MakeInternalKey([]byte{'f'}, seqNumRangeSet, InternalKeyKindRangeKeySet),
373-
LargestRangeKey: base.MakeInternalKey([]byte{'k'}, seqNumRangeUnset, InternalKeyKindRangeKeyUnset),
374372
HasPointKeys: true,
375373
Virtual: true,
376374
}
375+
v2.RangeKeyBounds.SetInternalKeyBounds(
376+
base.MakeInternalKey([]byte{'f'}, seqNumRangeSet, InternalKeyKindRangeKeySet),
377+
base.MakeInternalKey([]byte{'k'}, seqNumRangeUnset, InternalKeyKindRangeKeyUnset))
377378
v2.ExtendPointKeyBounds(DefaultComparer.Compare, v2.SmallestPointKey, v2.LargestPointKey)
378379
v2.AttachVirtualBacking(parentFile.FileBacking)
379380
v2.Stats.NumEntries = 6

ingest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ func setSeqNumInMetadata(
881881
m.SmallestPointKey = setSeqFn(m.SmallestPointKey)
882882
}
883883
if m.HasRangeKeys {
884-
m.SmallestRangeKey = setSeqFn(m.SmallestRangeKey)
884+
m.RangeKeyBounds.SetSmallest(setSeqFn(m.RangeKeyBounds.Smallest()))
885885
}
886886
// Only update the seqnum for the largest key if that key is not an
887887
// "exclusive sentinel" (i.e. a range deletion sentinel or a range key

ingest_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func TestIngestLoad(t *testing.T) {
155155
for _, m := range lr.local {
156156
fmt.Fprintf(&buf, "%d: %s-%s\n", m.FileNum, m.Smallest(), m.Largest())
157157
fmt.Fprintf(&buf, " points: %s-%s\n", m.SmallestPointKey, m.LargestPointKey)
158-
fmt.Fprintf(&buf, " ranges: %s-%s\n", m.SmallestRangeKey, m.LargestRangeKey)
158+
fmt.Fprintf(&buf, " ranges: %s-%s\n", m.RangeKeyBounds.Smallest(), m.RangeKeyBounds.Largest())
159159
}
160160
return buf.String()
161161

@@ -2660,7 +2660,7 @@ func TestIngest_UpdateSequenceNumber(t *testing.T) {
26602660
fmt.Fprintf(&buf, "file %d:\n", i)
26612661
fmt.Fprintf(&buf, " combined: %s-%s\n", m.Smallest(), m.Largest())
26622662
fmt.Fprintf(&buf, " points: %s-%s\n", m.SmallestPointKey, m.LargestPointKey)
2663-
fmt.Fprintf(&buf, " ranges: %s-%s\n", m.SmallestRangeKey, m.LargestRangeKey)
2663+
fmt.Fprintf(&buf, " ranges: %s-%s\n", m.RangeKeyBounds.Smallest(), m.RangeKeyBounds.Largest())
26642664
}
26652665

26662666
return buf.String()

internal/keyspan/keyspanimpl/level_iter.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,14 @@ func (l *LevelIter) SeekGE(key []byte) (*keyspan.Span, error) {
182182
l.setPosBeforeFile(nil)
183183
return nil, nil
184184
}
185-
if l.straddleSpansEnabled() && l.cmp(key, file.SmallestRangeKey.UserKey) < 0 {
185+
if l.straddleSpansEnabled() && l.cmp(key, file.RangeKeyBounds.SmallestUserKey()) < 0 {
186186
// Peek at the previous file.
187187
if prevFile := l.files.Prev(); prevFile != nil {
188-
// We could unconditionally return an empty span between the seek key and
189-
// f.SmallestRangeKey, however if this span is to the left of all range
190-
// keys on this level, it could lead to inconsistent behaviour in relative
191-
// positioning operations. Consider this example, with a b-c range key:
188+
// We could unconditionally return an empty span between the seek
189+
// key and f.RangeKeyBounds.Smallest(), however if this span is to
190+
// the left of all range keys on this level, it could lead to
191+
// inconsistent behaviour in relative positioning operations.
192+
// Consider this example, with a b-c range key:
192193
// SeekGE(a) -> a-b:{}
193194
// Next() -> b-c{(#5,RANGEKEYSET,@4,foo)}
194195
// Prev() -> nil
@@ -221,7 +222,7 @@ func (l *LevelIter) SeekLT(key []byte) (*keyspan.Span, error) {
221222
l.setPosAfterFile(nil)
222223
return nil, nil
223224
}
224-
if l.straddleSpansEnabled() && l.cmp(file.LargestRangeKey.UserKey, key) < 0 {
225+
if l.straddleSpansEnabled() && l.cmp(file.RangeKeyBounds.LargestUserKey(), key) < 0 {
225226
// Peek at the next file.
226227
if nextFile := l.files.Next(); nextFile != nil {
227228
// We could unconditionally return an empty span between f.LargestRangeKey
@@ -488,15 +489,15 @@ func (l *LevelIter) straddleSpansEnabled() bool {
488489
func (l *LevelIter) needStraddleSpan(file, nextFile *manifest.TableMetadata) bool {
489490
// We directly use range key bounds because that is the current condition for
490491
// straddleSpansEnabled.
491-
return l.straddleSpansEnabled() && l.cmp(file.LargestRangeKey.UserKey, nextFile.SmallestRangeKey.UserKey) < 0
492+
return l.straddleSpansEnabled() && l.cmp(file.RangeKeyBounds.LargestUserKey(), nextFile.RangeKeyBounds.SmallestUserKey()) < 0
492493
}
493494

494495
// makeStraddleSpan returns a straddle span that covers the gap between file and
495496
// nextFile.
496497
func (l *LevelIter) makeStraddleSpan(file, nextFile *manifest.TableMetadata) *keyspan.Span {
497498
l.straddleSpan = keyspan.Span{
498-
Start: file.LargestRangeKey.UserKey,
499-
End: nextFile.SmallestRangeKey.UserKey,
499+
Start: file.RangeKeyBounds.LargestUserKey(),
500+
End: nextFile.RangeKeyBounds.SmallestUserKey(),
500501
Keys: nil,
501502
}
502503
return &l.straddleSpan

internal/keyspan/keyspanimpl/level_iter_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,13 +292,14 @@ func TestLevelIterEquivalence(t *testing.T) {
292292
SmallestSeqNum: 2,
293293
LargestSeqNum: 2,
294294
LargestSeqNumAbsolute: 2,
295-
SmallestRangeKey: base.MakeInternalKey(file[0].Start, file[0].SmallestKey().SeqNum(), file[0].SmallestKey().Kind()),
296-
LargestRangeKey: base.MakeExclusiveSentinelKey(file[len(file)-1].LargestKey().Kind(), file[len(file)-1].End),
297295
HasPointKeys: false,
298296
HasRangeKeys: true,
299297
}
300298
meta.InitPhysicalBacking()
301-
meta.ExtendRangeKeyBounds(base.DefaultComparer.Compare, meta.SmallestRangeKey, meta.LargestRangeKey)
299+
meta.RangeKeyBounds.SetInternalKeyBounds(
300+
base.MakeInternalKey(file[0].Start, file[0].SmallestKey().SeqNum(), file[0].SmallestKey().Kind()),
301+
base.MakeExclusiveSentinelKey(file[len(file)-1].LargestKey().Kind(), file[len(file)-1].End))
302+
meta.ExtendRangeKeyBounds(base.DefaultComparer.Compare, meta.RangeKeyBounds.Smallest(), meta.RangeKeyBounds.Largest())
302303
metas = append(metas, meta)
303304
}
304305

0 commit comments

Comments
 (0)