Skip to content

Commit 3618106

Browse files
committed
manifest: use InternalKeyBounds struct in TableMetadata's point keys
This patch uses our `InternalKeyBounds` struct to represent both the smallest, largest point keys 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 a4e831f commit 3618106

19 files changed

+101
-99
lines changed

compaction.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2609,7 +2609,7 @@ func (d *DB) runCopyCompaction(
26092609
SyntheticPrefixAndSuffix: inputMeta.SyntheticPrefixAndSuffix,
26102610
}
26112611
if inputMeta.HasPointKeys {
2612-
newMeta.ExtendPointKeyBounds(c.cmp, inputMeta.SmallestPointKey, inputMeta.LargestPointKey)
2612+
newMeta.ExtendPointKeyBounds(c.cmp, inputMeta.PointKeyBounds.Smallest(), inputMeta.PointKeyBounds.Largest())
26132613
}
26142614
if inputMeta.HasRangeKeys {
26152615
newMeta.ExtendRangeKeyBounds(c.cmp, inputMeta.RangeKeyBounds.Smallest(), inputMeta.RangeKeyBounds.Largest())

compaction_picker_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,6 +1716,6 @@ func checkClone(t *testing.T, pc *pickedCompaction) {
17161716
}
17171717

17181718
func checkTableBoundary(a, b *tableMetadata, cmp base.Compare) (ok bool) {
1719-
c := cmp(a.LargestPointKey.UserKey, b.SmallestPointKey.UserKey)
1720-
return c < 0 || (c == 0 && a.LargestPointKey.IsExclusiveSentinel())
1719+
c := cmp(a.PointKeyBounds.LargestUserKey(), b.PointKeyBounds.SmallestUserKey())
1720+
return c < 0 || (c == 0 && a.PointKeyBounds.Largest().IsExclusiveSentinel())
17211721
}

data_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -942,12 +942,14 @@ func runDBDefineCmdReuseFS(td *datadriven.TestData, opts *Options) (*DB, error)
942942
largestSeqNum := d.mu.versions.logSeqNum.Load()
943943
ve.NewBlobFiles = append(ve.NewBlobFiles, newVE.NewBlobFiles...)
944944
for _, f := range newVE.NewTables {
945-
if start != nil {
946-
f.Meta.SmallestPointKey = *start
945+
if start != nil && end != nil {
946+
f.Meta.PointKeyBounds.SetInternalKeyBounds(*start, *end)
947+
f.Meta.ExtendPointKeyBounds(DefaultComparer.Compare, *start, *end)
948+
} else if start != nil {
949+
f.Meta.PointKeyBounds.SetSmallest(*start)
947950
f.Meta.ExtendPointKeyBounds(DefaultComparer.Compare, *start, *start)
948-
}
949-
if end != nil {
950-
f.Meta.LargestPointKey = *end
951+
} else if end != nil {
952+
f.Meta.PointKeyBounds.SetLargest(*end)
951953
f.Meta.ExtendPointKeyBounds(DefaultComparer.Compare, *end, *end)
952954
}
953955
if blobDepth > 0 {

db.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2999,8 +2999,8 @@ func (d *DB) checkVirtualBounds(m *tableMetadata) {
29992999
if err != nil {
30003000
panic(err)
30013001
}
3002-
if (rangeDel == nil || d.cmp(rangeDel.SmallestKey().UserKey, m.SmallestPointKey.UserKey) != 0) &&
3003-
(pointKV == nil || d.cmp(pointKV.K.UserKey, m.SmallestPointKey.UserKey) != 0) {
3002+
if (rangeDel == nil || d.cmp(rangeDel.SmallestKey().UserKey, m.PointKeyBounds.Smallest().UserKey) != 0) &&
3003+
(pointKV == nil || d.cmp(pointKV.K.UserKey, m.PointKeyBounds.Smallest().UserKey) != 0) {
30043004
panic(errors.Newf("pebble: virtual sstable %s lower point key bound is not tight", m.FileNum))
30053005
}
30063006

@@ -3010,23 +3010,23 @@ func (d *DB) checkVirtualBounds(m *tableMetadata) {
30103010
if err != nil {
30113011
panic(err)
30123012
}
3013-
if (rangeDel == nil || d.cmp(rangeDel.LargestKey().UserKey, m.LargestPointKey.UserKey) != 0) &&
3014-
(pointKV == nil || d.cmp(pointKV.K.UserKey, m.LargestPointKey.UserKey) != 0) {
3013+
if (rangeDel == nil || d.cmp(rangeDel.LargestKey().UserKey, m.PointKeyBounds.LargestUserKey()) != 0) &&
3014+
(pointKV == nil || d.cmp(pointKV.K.UserKey, m.PointKeyBounds.Largest().UserKey) != 0) {
30153015
panic(errors.Newf("pebble: virtual sstable %s upper point key bound is not tight", m.FileNum))
30163016
}
30173017

30183018
// Check that iterator keys are within bounds.
30193019
for kv := pointIter.First(); kv != nil; kv = pointIter.Next() {
3020-
if d.cmp(kv.K.UserKey, m.SmallestPointKey.UserKey) < 0 || d.cmp(kv.K.UserKey, m.LargestPointKey.UserKey) > 0 {
3020+
if d.cmp(kv.K.UserKey, m.PointKeyBounds.Smallest().UserKey) < 0 || d.cmp(kv.K.UserKey, m.PointKeyBounds.LargestUserKey()) > 0 {
30213021
panic(errors.Newf("pebble: virtual sstable %s point key %s is not within bounds", m.FileNum, kv.K.UserKey))
30223022
}
30233023
}
30243024
s, err := rangeDelIter.First()
30253025
for ; s != nil; s, err = rangeDelIter.Next() {
3026-
if d.cmp(s.SmallestKey().UserKey, m.SmallestPointKey.UserKey) < 0 {
3026+
if d.cmp(s.SmallestKey().UserKey, m.PointKeyBounds.Smallest().UserKey) < 0 {
30273027
panic(errors.Newf("pebble: virtual sstable %s point key %s is not within bounds", m.FileNum, s.SmallestKey().UserKey))
30283028
}
3029-
if d.cmp(s.LargestKey().UserKey, m.LargestPointKey.UserKey) > 0 {
3029+
if d.cmp(s.LargestKey().UserKey, m.PointKeyBounds.Largest().UserKey) > 0 {
30303030
panic(errors.Newf("pebble: virtual sstable %s point key %s is not within bounds", m.FileNum, s.LargestKey().UserKey))
30313031
}
30323032
}

excise.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,11 @@ func looseLeftTableBounds(
259259
cmp Compare, originalTable, leftTable *tableMetadata, exciseSpanStart []byte,
260260
) {
261261
if originalTable.HasPointKeys {
262-
largestPointKey := originalTable.LargestPointKey
262+
largestPointKey := originalTable.PointKeyBounds.Largest()
263263
if largestPointKey.IsUpperBoundFor(cmp, exciseSpanStart) {
264264
largestPointKey = base.MakeRangeDeleteSentinelKey(exciseSpanStart)
265265
}
266-
leftTable.ExtendPointKeyBounds(cmp, originalTable.SmallestPointKey, largestPointKey)
266+
leftTable.ExtendPointKeyBounds(cmp, originalTable.PointKeyBounds.Smallest(), largestPointKey)
267267
}
268268
if originalTable.HasRangeKeys {
269269
largestRangeKey := originalTable.RangeKeyBounds.Largest()
@@ -287,11 +287,11 @@ func looseRightTableBounds(
287287
cmp Compare, originalTable, rightTable *tableMetadata, exciseSpanEnd []byte,
288288
) {
289289
if originalTable.HasPointKeys {
290-
smallestPointKey := originalTable.SmallestPointKey
290+
smallestPointKey := originalTable.PointKeyBounds.Smallest()
291291
if !smallestPointKey.IsUpperBoundFor(cmp, exciseSpanEnd) {
292292
smallestPointKey = base.MakeInternalKey(exciseSpanEnd, 0, base.InternalKeyKindMaxForSSTable)
293293
}
294-
rightTable.ExtendPointKeyBounds(cmp, smallestPointKey, originalTable.LargestPointKey)
294+
rightTable.ExtendPointKeyBounds(cmp, smallestPointKey, originalTable.PointKeyBounds.Largest())
295295
}
296296
if originalTable.HasRangeKeys {
297297
smallestRangeKey := originalTable.RangeKeyBounds.Smallest()
@@ -311,10 +311,10 @@ func looseRightTableBounds(
311311
func determineLeftTableBounds(
312312
cmp Compare, originalTable, leftTable *tableMetadata, exciseSpanStart []byte, iters iterSet,
313313
) error {
314-
if originalTable.HasPointKeys && cmp(originalTable.SmallestPointKey.UserKey, exciseSpanStart) < 0 {
314+
if originalTable.HasPointKeys && cmp(originalTable.PointKeyBounds.Smallest().UserKey, exciseSpanStart) < 0 {
315315
// This file will probably contain point keys.
316316
if kv := iters.Point().SeekLT(exciseSpanStart, base.SeekLTFlagsNone); kv != nil {
317-
leftTable.ExtendPointKeyBounds(cmp, originalTable.SmallestPointKey, kv.K.Clone())
317+
leftTable.ExtendPointKeyBounds(cmp, originalTable.PointKeyBounds.Smallest(), kv.K.Clone())
318318
}
319319
rdel, err := iters.RangeDeletion().SeekLT(exciseSpanStart)
320320
if err != nil {
@@ -327,7 +327,7 @@ func determineLeftTableBounds(
327327
// The key is owned by the range del iter, so we need to copy it.
328328
lastRangeDel = slices.Clone(rdel.End)
329329
}
330-
leftTable.ExtendPointKeyBounds(cmp, originalTable.SmallestPointKey,
330+
leftTable.ExtendPointKeyBounds(cmp, originalTable.PointKeyBounds.Smallest(),
331331
base.MakeExclusiveSentinelKey(InternalKeyKindRangeDelete, lastRangeDel))
332332
}
333333
}
@@ -367,12 +367,12 @@ func determineRightTableBounds(
367367
exciseSpanEnd base.UserKeyBoundary,
368368
iters iterSet,
369369
) error {
370-
if originalTable.HasPointKeys && !exciseSpanEnd.IsUpperBoundForInternalKey(cmp, originalTable.LargestPointKey) {
370+
if originalTable.HasPointKeys && !exciseSpanEnd.IsUpperBoundForInternalKey(cmp, originalTable.PointKeyBounds.Largest()) {
371371
if kv := iters.Point().SeekGE(exciseSpanEnd.Key, base.SeekGEFlagsNone); kv != nil {
372372
if exciseSpanEnd.Kind == base.Inclusive && cmp(exciseSpanEnd.Key, kv.K.UserKey) == 0 {
373373
return base.AssertionFailedf("cannot excise with an inclusive end key and data overlap at end key")
374374
}
375-
rightTable.ExtendPointKeyBounds(cmp, kv.K.Clone(), originalTable.LargestPointKey)
375+
rightTable.ExtendPointKeyBounds(cmp, kv.K.Clone(), originalTable.PointKeyBounds.Largest())
376376
}
377377
rdel, err := iters.RangeDeletion().SeekGE(exciseSpanEnd.Key)
378378
if err != nil {
@@ -390,7 +390,7 @@ func determineRightTableBounds(
390390
rightTable.ExtendPointKeyBounds(cmp, base.InternalKey{
391391
UserKey: firstRangeDel,
392392
Trailer: rdel.SmallestKey().Trailer,
393-
}, originalTable.LargestPointKey)
393+
}, originalTable.PointKeyBounds.Largest())
394394
}
395395
}
396396
if originalTable.HasRangeKeys && !exciseSpanEnd.IsUpperBoundForInternalKey(cmp, originalTable.RangeKeyBounds.Largest()) {

excise_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ func TestExciseBounds(t *testing.T) {
801801
fmt.Fprintf(&buf, "%s:\n", title)
802802
fmt.Fprintf(&buf, " overall: %v - %v\n", m.Smallest(), m.Largest())
803803
if m.HasPointKeys {
804-
fmt.Fprintf(&buf, " point: %v - %v\n", m.SmallestPointKey, m.LargestPointKey)
804+
fmt.Fprintf(&buf, " point: %v - %v\n", m.PointKeyBounds.Smallest(), m.PointKeyBounds.Largest())
805805
}
806806
if m.HasRangeKeys {
807807
fmt.Fprintf(&buf, " range: %v - %v\n", m.RangeKeyBounds.Smallest(), m.RangeKeyBounds.Largest())

file_cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ func newRangeDelIter(
737737
cmp = handle.readerOpts.Comparer.Compare
738738
}
739739
rangeDelIter = keyspan.AssertBounds(
740-
rangeDelIter, file.SmallestPointKey, file.LargestPointKey.UserKey, cmp,
740+
rangeDelIter, file.PointKeyBounds.Smallest(), file.PointKeyBounds.LargestUserKey(), cmp,
741741
)
742742
}
743743
return rangeDelIter, nil

file_cache_test.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -351,12 +351,12 @@ func TestVirtualReadsWiring(t *testing.T) {
351351
SmallestSeqNum: parentFile.SmallestSeqNum,
352352
LargestSeqNum: parentFile.LargestSeqNum,
353353
LargestSeqNumAbsolute: parentFile.LargestSeqNumAbsolute,
354-
SmallestPointKey: base.MakeInternalKey([]byte{'a'}, seqNumA, InternalKeyKindSet),
355-
LargestPointKey: base.MakeInternalKey([]byte{'a'}, seqNumA, InternalKeyKindSet),
356354
HasPointKeys: true,
357355
Virtual: true,
358356
}
359-
v1.ExtendPointKeyBounds(DefaultComparer.Compare, v1.SmallestPointKey, v1.LargestPointKey)
357+
v1.PointKeyBounds.SetInternalKeyBounds(base.MakeInternalKey([]byte{'a'}, seqNumA, InternalKeyKindSet),
358+
base.MakeInternalKey([]byte{'a'}, seqNumA, InternalKeyKindSet))
359+
v1.ExtendPointKeyBounds(DefaultComparer.Compare, v1.PointKeyBounds.Smallest(), v1.PointKeyBounds.Largest())
360360
v1.AttachVirtualBacking(parentFile.FileBacking)
361361
v1.Stats.NumEntries = 1
362362

@@ -367,23 +367,21 @@ func TestVirtualReadsWiring(t *testing.T) {
367367
SmallestSeqNum: parentFile.SmallestSeqNum,
368368
LargestSeqNum: parentFile.LargestSeqNum,
369369
LargestSeqNumAbsolute: parentFile.LargestSeqNumAbsolute,
370-
SmallestPointKey: base.MakeInternalKey([]byte{'d'}, seqNumCEDel, InternalKeyKindRangeDelete),
371-
LargestPointKey: base.MakeInternalKey([]byte{'z'}, seqNumZ, InternalKeyKindSet),
372370
HasPointKeys: true,
373371
Virtual: true,
374372
}
373+
v2.PointKeyBounds.SetInternalKeyBounds(base.MakeInternalKey([]byte{'d'}, seqNumCEDel, InternalKeyKindRangeDelete),
374+
base.MakeInternalKey([]byte{'z'}, seqNumZ, InternalKeyKindSet))
375375
v2.RangeKeyBounds.SetInternalKeyBounds(
376376
base.MakeInternalKey([]byte{'f'}, seqNumRangeSet, InternalKeyKindRangeKeySet),
377377
base.MakeInternalKey([]byte{'k'}, seqNumRangeUnset, InternalKeyKindRangeKeyUnset))
378-
v2.ExtendPointKeyBounds(DefaultComparer.Compare, v2.SmallestPointKey, v2.LargestPointKey)
378+
v2.ExtendPointKeyBounds(DefaultComparer.Compare, v2.PointKeyBounds.Smallest(), v2.PointKeyBounds.Largest())
379379
v2.AttachVirtualBacking(parentFile.FileBacking)
380380
v2.Stats.NumEntries = 6
381381

382-
v1.LargestPointKey = v1.Largest()
383-
v1.SmallestPointKey = v1.Smallest()
382+
v1.PointKeyBounds.SetInternalKeyBounds(v1.Smallest(), v1.Largest())
384383

385-
v2.LargestPointKey = v2.Largest()
386-
v2.SmallestPointKey = v2.Smallest()
384+
v2.PointKeyBounds.SetInternalKeyBounds(v2.Smallest(), v2.Largest())
387385

388386
v1.ValidateVirtual(parentFile)
389387
d.checkVirtualBounds(v1)

get_iter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ func (g *getIter) getSSTableIterators(
267267
}
268268
// m is now positioned at the file containing the first point key ≥ `g.key`.
269269
// Does it exist and possibly contain point keys with the user key 'g.key'?
270-
if m == nil || !m.HasPointKeys || g.comparer.Compare(m.SmallestPointKey.UserKey, g.key) > 0 {
270+
if m == nil || !m.HasPointKeys || g.comparer.Compare(m.PointKeyBounds.SmallestUserKey(), g.key) > 0 {
271271
return emptyIter, nil, nil
272272
}
273273
// m may possibly contain point (or range deletion) keys relevant to g.key.

ingest.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ func setSeqNumInMetadata(
878878
// NB: we set the fields directly here, rather than via their Extend*
879879
// methods, as we are updating sequence numbers.
880880
if m.HasPointKeys {
881-
m.SmallestPointKey = setSeqFn(m.SmallestPointKey)
881+
m.PointKeyBounds.SetSmallest(setSeqFn(m.PointKeyBounds.Smallest()))
882882
}
883883
if m.HasRangeKeys {
884884
m.RangeKeyBounds.SetSmallest(setSeqFn(m.RangeKeyBounds.Smallest()))
@@ -890,8 +890,8 @@ func setSeqNumInMetadata(
890890
// table.
891891
// NB: as the largest range key is always an exclusive sentinel, it is never
892892
// updated.
893-
if m.HasPointKeys && !m.LargestPointKey.IsExclusiveSentinel() {
894-
m.LargestPointKey = setSeqFn(m.LargestPointKey)
893+
if m.HasPointKeys && !m.PointKeyBounds.Largest().IsExclusiveSentinel() {
894+
m.PointKeyBounds.SetLargest(setSeqFn(m.PointKeyBounds.Largest()))
895895
}
896896
// Setting smallestSeqNum == largestSeqNum triggers the setting of
897897
// Properties.GlobalSeqNum when an sstable is loaded.

0 commit comments

Comments
 (0)