Skip to content

Commit e6b971f

Browse files
committed
sstable, compact: move IsLikelyMVCCGarbage to RawWriter interface
Make this a function on the RawWriter. We can now unexport `IsPrefixEqualPrev`.
1 parent 495be96 commit e6b971f

File tree

6 files changed

+26
-73
lines changed

6 files changed

+26
-73
lines changed

internal/base/internal.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,11 @@ func (k InternalKeyKind) SafeFormat(w redact.SafePrinter, _ rune) {
228228
w.Print(redact.SafeString(k.String()))
229229
}
230230

231+
// IsSet returns true if the key kind is a SET type.
232+
func (k InternalKeyKind) IsSet() bool {
233+
return k == InternalKeyKindSet || k == InternalKeyKindSetWithDelete
234+
}
235+
231236
// InternalKeyTrailer encodes a SeqNum and an InternalKeyKind.
232237
type InternalKeyTrailer uint64
233238

internal/compact/run.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,8 @@ func (r *Runner) writeKeysToTable(
258258
equalPrev := func(k []byte) bool {
259259
return tw.ComparePrev(k) == 0
260260
}
261-
prefixEqual := func(k []byte) bool {
262-
return tw.IsPrefixEqualPrev(k)
263-
}
264261
var pinnedKeySize, pinnedValueSize, pinnedCount uint64
265262
var iteratedKeys uint64
266-
var prevKeyKind base.InternalKeyKind
267263
kv := r.kv
268264
for ; kv != nil; kv = r.iter.Next() {
269265
iteratedKeys++
@@ -302,7 +298,7 @@ func (r *Runner) writeKeysToTable(
302298

303299
valueLen := kv.V.Len()
304300
isLikelyMVCCGarbage := valueLen > valueSeparation.OutputConfig().MinimumMVCCGarbageSize &&
305-
sstable.IsLikelyMVCCGarbage(kv.K.UserKey, prevKeyKind, kv.K.Kind(), valueLen, prefixEqual)
301+
tw.IsLikelyMVCCGarbage(kv.K.UserKey, kv.K.Kind())
306302
// Add the value to the sstable, possibly separating its value into a
307303
// blob file. The ValueSeparation implementation is responsible for
308304
// writing the KV to the sstable.
@@ -312,7 +308,6 @@ func (r *Runner) writeKeysToTable(
312308
if err := valueSeparation.Add(tw, kv, r.iter.ForceObsoleteDueToRangeDel(), isLikelyMVCCGarbage); err != nil {
313309
return nil, err
314310
}
315-
prevKeyKind = kv.K.Kind()
316311
if r.iter.SnapshotPinned() {
317312
// The kv pair we just added to the sstable was only surfaced by
318313
// the compaction iterator because an open snapshot prevented

sstable/colblk_writer.go

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -255,25 +255,16 @@ func (w *RawColumnWriter) ComparePrev(k []byte) int {
255255
return int(w.dataBlock.KeyWriter.ComparePrev(k).UserKeyComparison)
256256
}
257257

258-
// IsPrefixEqualPrev compares the provided user key's prefix to the key
259-
// prefix of the last point key written to the writer.
258+
// IsLikelyMVCCGarbage implements the RawWriter interface.
260259
//
261-
// If no key has been written yet, IsPrefixEqualPrev returns false.
262-
//
263-
// Must not be called after Writer is closed.
264-
func (w *RawColumnWriter) IsPrefixEqualPrev(k []byte) bool {
260+
// If no key has been written yet, IsLikelyMVCCGarbage returns false.
261+
func (w *RawColumnWriter) IsLikelyMVCCGarbage(k []byte, keyKind base.InternalKeyKind) bool {
265262
if w == nil || w.dataBlock.Rows() == 0 {
266263
return false
267264
}
268-
return w.dataBlock.KeyWriter.ComparePrev(k).PrefixEqual()
269-
}
270-
271-
// PrevPointKeyKind implements the RawWriter interface.
272-
func (w *RawColumnWriter) PrevPointKeyKind() base.InternalKeyKind {
273-
if w == nil || w.dataBlock.Rows() == 0 {
274-
return base.InternalKeyKindInvalid
275-
}
276-
return w.prevPointKey.trailer.Kind()
265+
return w.prevPointKey.trailer.Kind().IsSet() &&
266+
keyKind.IsSet() &&
267+
w.dataBlock.KeyWriter.ComparePrev(k).PrefixEqual()
277268
}
278269

279270
// SetSnapshotPinnedProperties sets the properties for pinned keys. Should only
@@ -641,9 +632,6 @@ func (w *RawColumnWriter) evaluatePoint(
641632
key.Pretty(w.comparer.FormatKey))
642633
}
643634

644-
prefixEqual := func(k []byte) bool {
645-
return w.IsPrefixEqualPrev(k)
646-
}
647635
// We might want to write this key's value to a value block if it has the
648636
// same prefix.
649637
//
@@ -660,7 +648,7 @@ func (w *RawColumnWriter) evaluatePoint(
660648
useValueBlock := !w.opts.DisableValueBlocks &&
661649
w.valueBlock != nil &&
662650
valueLen > 0 &&
663-
IsLikelyMVCCGarbage(key.UserKey, prevKeyKind, keyKind, valueLen, prefixEqual)
651+
w.IsLikelyMVCCGarbage(key.UserKey, key.Kind())
664652
if !useValueBlock {
665653
return eval, nil
666654
}
@@ -1293,25 +1281,3 @@ func (w *RawColumnWriter) SetValueSeparationProps(
12931281
w.props.ValueSeparationMinSize = minValueSize
12941282
w.props.ValueSeparationBySuffixDisabled = disableSeparationBySuffix
12951283
}
1296-
1297-
// IsLikelyMVCCGarbage determines whether the given user key is likely MVCC
1298-
// garbage.
1299-
//
1300-
// We require:
1301-
//
1302-
// . The previous key to be a SET/SETWITHDEL.
1303-
// . The current key to be a SET/SETWITHDEL.
1304-
// . The current key to have the same prefix as the previous key.
1305-
func IsLikelyMVCCGarbage(
1306-
k []byte,
1307-
prevKeyKind, keyKind base.InternalKeyKind,
1308-
valueLen int,
1309-
prefixEqual func(k []byte) bool,
1310-
) bool {
1311-
isSetStarKind := func(k base.InternalKeyKind) bool {
1312-
return k == InternalKeyKindSet || k == InternalKeyKindSetWithDelete
1313-
}
1314-
return isSetStarKind(prevKeyKind) &&
1315-
isSetStarKind(keyKind) &&
1316-
prefixEqual(k)
1317-
}

sstable/rowblk_writer.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,25 +1395,15 @@ func (w *RawRowWriter) ComparePrev(k []byte) int {
13951395
return w.compare(k, w.dataBlockBuf.dataBlock.CurUserKey())
13961396
}
13971397

1398-
// IsPrefixEqualPrev compares the provided user key's prefix to the key
1399-
// prefix of the last point key written to the writer.
1400-
//
1401-
// If no key has been written yet, IsPrefixEqualPrev returns false.
1402-
//
1403-
// Must not be called after Writer is closed.
1404-
func (w *RawRowWriter) IsPrefixEqualPrev(k []byte) bool {
1398+
// IsLikelyMVCCGarbage implements the RawWriter interface.
1399+
func (w *RawRowWriter) IsLikelyMVCCGarbage(k []byte, keyKind base.InternalKeyKind) bool {
14051400
if w == nil || w.dataBlockBuf.dataBlock.EntryCount() == 0 {
14061401
return false
14071402
}
1408-
return bytes.Equal(w.split.Prefix(k), w.split.Prefix(w.dataBlockBuf.dataBlock.CurUserKey()))
1409-
}
1403+
return w.dataBlockBuf.dataBlock.CurKey().Kind().IsSet() &&
1404+
keyKind.IsSet() &&
1405+
bytes.Equal(w.split.Prefix(k), w.split.Prefix(w.dataBlockBuf.dataBlock.CurUserKey()))
14101406

1411-
// PrevPointKeyKind implements the RawWriter interface.
1412-
func (w *RawRowWriter) PrevPointKeyKind() base.InternalKeyKind {
1413-
if w == nil || w.dataBlockBuf.dataBlock.EntryCount() == 0 {
1414-
return base.InternalKeyKindInvalid
1415-
}
1416-
return w.dataBlockBuf.dataBlock.CurKey().Kind()
14171407
}
14181408

14191409
// EncodeSpan encodes the keys in the given span. The span can contain either

sstable/writer.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -356,16 +356,14 @@ type RawWriter interface {
356356
// Metadata returns the metadata for the finished sstable. Only valid to
357357
// call after the sstable has been finished.
358358
Metadata() (*WriterMetadata, error)
359-
// IsPrefixEqualPrev compares the provided user key's prefix to the key
360-
// prefix of the last point key written to the writer.
359+
// IsLikelyMVCCGarbage determines whether the given user key is likely MVCC
360+
// garbage according to the previous key written to the writer.
361361
//
362-
// If no key has been written yet, IsPrefixEqualPrev returns false.
363-
//
364-
// Must not be called after Writer is closed.
365-
IsPrefixEqualPrev(k []byte) bool
366-
// PrevPointKeyKind returns the InternalKeyKind of the last point key written
367-
// to the writer. Must not be called after Writer is closed.
368-
PrevPointKeyKind() base.InternalKeyKind
362+
// We require:
363+
// * The previous key to be a SET/SETWITHDEL.
364+
// * The current key to be a SET/SETWITHDEL.
365+
// * The current key to have the same prefix as the previous key.
366+
IsLikelyMVCCGarbage(key []byte, kind base.InternalKeyKind) bool
369367

370368
// SetValueSeparationProps sets the value separation props that were used when
371369
// writing this sstable. This is recorded in the sstable properties.

valsep/sst_blob_writer.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,7 @@ func (w *SSTBlobWriter) Set(key, value []byte) error {
142142

143143
w.kvScratch.K = base.MakeInternalKey(key, 0, sstable.InternalKeyKindSet)
144144
w.kvScratch.V = base.MakeInPlaceValue(value)
145-
isLikelyMVCCGarbage := sstable.IsLikelyMVCCGarbage(
146-
key, w.Raw().PrevPointKeyKind(), sstable.InternalKeyKindSet, len(value), w.Writer.Raw().IsPrefixEqualPrev)
145+
isLikelyMVCCGarbage := w.Raw().IsLikelyMVCCGarbage(w.kvScratch.K.UserKey, w.kvScratch.Kind())
147146
return w.valSep.Add(w.Raw(), &w.kvScratch, false, isLikelyMVCCGarbage)
148147
}
149148

0 commit comments

Comments
 (0)