Skip to content

Commit bb173d2

Browse files
committed
db: remove pickedCompaction.cmp
Remove the base.Compare field from pickedCompaction. This is a step towards further simplifications.
1 parent a50aabc commit bb173d2

File tree

3 files changed

+33
-31
lines changed

3 files changed

+33
-31
lines changed

compaction.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ func newCompaction(
385385
) *compaction {
386386
c := &compaction{
387387
kind: compactionKindDefault,
388-
cmp: pc.cmp,
388+
cmp: opts.Comparer.Compare,
389389
equal: opts.Comparer.Equal,
390390
comparer: opts.Comparer,
391391
formatKey: opts.Comparer.FormatKey,

compaction_picker.go

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ type pickedCompactionMetrics struct {
164164
// this struct, and is copied over into the compaction struct when that's
165165
// created.
166166
type pickedCompaction struct {
167-
cmp Compare
168167
// score of the chosen compaction (candidateLevelInfo.score).
169168
score float64
170169
// kind indicates the kind of compaction.
@@ -239,7 +238,6 @@ func newPickedCompaction(
239238

240239
adjustedLevel := adjustedOutputLevel(outputLevel, baseLevel)
241240
pc := &pickedCompaction{
242-
cmp: opts.Comparer.Compare,
243241
version: cur,
244242
l0Organizer: l0Organizer,
245243
baseLevel: baseLevel,
@@ -319,7 +317,6 @@ func (pc *pickedCompaction) clone() *pickedCompaction {
319317
// Quickly copy over fields that do not require special deep copy care, and
320318
// set all fields that will require a deep copy to nil.
321319
newPC := &pickedCompaction{
322-
cmp: pc.cmp,
323320
score: pc.score,
324321
kind: pc.kind,
325322
baseLevel: pc.baseLevel,
@@ -365,7 +362,9 @@ func (pc *pickedCompaction) clone() *pickedCompaction {
365362
// the candidate keys expand the key span. This avoids a bug for multi-level
366363
// compactions: during the second call to setupInputs, the picked compaction's
367364
// smallest and largest keys should not decrease the key span.
368-
func (pc *pickedCompaction) maybeExpandBounds(smallest InternalKey, largest InternalKey) {
365+
func (pc *pickedCompaction) maybeExpandBounds(
366+
cmp base.Compare, smallest InternalKey, largest InternalKey,
367+
) {
369368
if len(smallest.UserKey) == 0 && len(largest.UserKey) == 0 {
370369
return
371370
}
@@ -374,10 +373,10 @@ func (pc *pickedCompaction) maybeExpandBounds(smallest InternalKey, largest Inte
374373
pc.largest = largest
375374
return
376375
}
377-
if base.InternalCompare(pc.cmp, pc.smallest, smallest) >= 0 {
376+
if base.InternalCompare(cmp, pc.smallest, smallest) >= 0 {
378377
pc.smallest = smallest
379378
}
380-
if base.InternalCompare(pc.cmp, pc.largest, largest) <= 0 {
379+
if base.InternalCompare(cmp, pc.largest, largest) <= 0 {
381380
pc.largest = largest
382381
}
383382
}
@@ -394,11 +393,13 @@ func (pc *pickedCompaction) setupInputs(
394393
inputLevel *compactionLevel,
395394
problemSpans *problemspans.ByLevel,
396395
) bool {
396+
cmp := opts.Comparer.Compare
397397
if !canCompactTables(inputLevel.files, inputLevel.level, problemSpans) {
398398
return false
399399
}
400400

401-
pc.maybeExpandBounds(manifest.KeyRange(pc.cmp, inputLevel.files.All()))
401+
sm, la := manifest.KeyRange(cmp, inputLevel.files.All())
402+
pc.maybeExpandBounds(cmp, sm, la)
402403

403404
// Setup output files and attempt to grow the inputLevel files with
404405
// the expanded key range. No need to do this for intra-L0 compactions;
@@ -411,7 +412,8 @@ func (pc *pickedCompaction) setupInputs(
411412
return false
412413
}
413414

414-
pc.maybeExpandBounds(manifest.KeyRange(pc.cmp, pc.outputLevel.files.All()))
415+
sm, la = manifest.KeyRange(cmp, pc.outputLevel.files.All())
416+
pc.maybeExpandBounds(cmp, sm, la)
415417

416418
// maxExpandedBytes is the maximum size of an expanded compaction. If
417419
// growing a compaction results in a larger size, the original compaction
@@ -423,16 +425,17 @@ func (pc *pickedCompaction) setupInputs(
423425
// Grow the sstables in inputLevel.level as long as it doesn't affect the number
424426
// of sstables included from pc.outputLevel.level.
425427
if pc.lcf != nil && inputLevel.level == 0 {
426-
pc.growL0ForBase(maxExpandedBytes)
427-
} else if pc.grow(pc.smallest, pc.largest, maxExpandedBytes, inputLevel, problemSpans) {
428+
pc.growL0ForBase(cmp, maxExpandedBytes)
429+
} else if pc.grow(cmp, pc.smallest, pc.largest, maxExpandedBytes, inputLevel, problemSpans) {
428430
// inputLevel was expanded, adjust key range if necessary.
429-
pc.maybeExpandBounds(manifest.KeyRange(pc.cmp, inputLevel.files.All()))
431+
sm, la = manifest.KeyRange(cmp, inputLevel.files.All())
432+
pc.maybeExpandBounds(cmp, sm, la)
430433
}
431434
}
432435

433436
if inputLevel.level == 0 {
434437
// If L0 is involved, it should always be the startLevel of the compaction.
435-
pc.startLevel.l0SublevelInfo = generateSublevelInfo(pc.cmp, pc.startLevel.files)
438+
pc.startLevel.l0SublevelInfo = generateSublevelInfo(cmp, pc.startLevel.files)
436439
}
437440

438441
return true
@@ -442,6 +445,7 @@ func (pc *pickedCompaction) setupInputs(
442445
// pc.outputLevel files in the compaction, and returns whether the inputs grew. sm
443446
// and la are the smallest and largest InternalKeys in all of the inputs.
444447
func (pc *pickedCompaction) grow(
448+
cmp base.Compare,
445449
sm, la InternalKey,
446450
maxExpandedBytes uint64,
447451
inputLevel *compactionLevel,
@@ -465,7 +469,7 @@ func (pc *pickedCompaction) grow(
465469
// expandedInputLevel's key range not fully cover all files currently in pc.outputLevel,
466470
// since pc.outputLevel was created using the entire key range which includes higher levels.
467471
expandedOutputLevel := pc.version.Overlaps(pc.outputLevel.level,
468-
base.UserKeyBoundsFromInternal(manifest.KeyRange(pc.cmp, expandedInputLevel.All(), pc.outputLevel.files.All())))
472+
base.UserKeyBoundsFromInternal(manifest.KeyRange(cmp, expandedInputLevel.All(), pc.outputLevel.files.All())))
469473
if expandedOutputLevel.Len() != pc.outputLevel.files.Len() {
470474
return false
471475
}
@@ -493,7 +497,7 @@ func (pc *pickedCompaction) grow(
493497
// will expand the compaction to include c-d and g-h from L0. The
494498
// bounds passed in are exclusive; the compaction cannot be expanded
495499
// to include files that "touch" it.
496-
func (pc *pickedCompaction) growL0ForBase(maxExpandedBytes uint64) bool {
500+
func (pc *pickedCompaction) growL0ForBase(cmp base.Compare, maxExpandedBytes uint64) bool {
497501
if invariants.Enabled {
498502
if pc.startLevel.level != 0 {
499503
panic(fmt.Sprintf("pc.startLevel.level is %d, expected 0", pc.startLevel.level))
@@ -503,10 +507,10 @@ func (pc *pickedCompaction) growL0ForBase(maxExpandedBytes uint64) bool {
503507
largestBaseKey := base.InvalidInternalKey
504508
if pc.outputLevel.files.Empty() {
505509
baseIter := pc.version.Levels[pc.outputLevel.level].Iter()
506-
if sm := baseIter.SeekLT(pc.cmp, pc.smallest.UserKey); sm != nil {
510+
if sm := baseIter.SeekLT(cmp, pc.smallest.UserKey); sm != nil {
507511
smallestBaseKey = sm.Largest()
508512
}
509-
if la := baseIter.SeekGE(pc.cmp, pc.largest.UserKey); la != nil {
513+
if la := baseIter.SeekGE(cmp, pc.largest.UserKey); la != nil {
510514
largestBaseKey = la.Smallest()
511515
}
512516
} else {
@@ -543,7 +547,7 @@ func (pc *pickedCompaction) growL0ForBase(maxExpandedBytes uint64) bool {
543547
}
544548

545549
pc.startLevel.files = manifest.NewLevelSliceSeqSorted(newStartLevelFiles)
546-
pc.smallest, pc.largest = manifest.KeyRange(pc.cmp,
550+
pc.smallest, pc.largest = manifest.KeyRange(cmp,
547551
pc.startLevel.files.All(), pc.outputLevel.files.All())
548552
return true
549553
}
@@ -1373,7 +1377,7 @@ func (p *compactionPickerByScore) pickAutoScore(env compactionEnv) (pc *pickedCo
13731377
pc = pickL0(env, p.opts, p.vers, p.latestVersionState.l0Organizer, p.baseLevel)
13741378
// Fail-safe to protect against compacting the same sstable
13751379
// concurrently.
1376-
if pc != nil && !inputRangeAlreadyCompacting(env, pc) {
1380+
if pc != nil && !inputRangeAlreadyCompacting(p.opts.Comparer.Compare, env, pc) {
13771381
p.addScoresToPickedCompactionMetrics(pc, scores)
13781382
pc.score = info.score
13791383
if false {
@@ -1393,7 +1397,7 @@ func (p *compactionPickerByScore) pickAutoScore(env compactionEnv) (pc *pickedCo
13931397

13941398
pc := pickAutoLPositive(env, p.opts, p.vers, p.latestVersionState.l0Organizer, *info, p.baseLevel)
13951399
// Fail-safe to protect against compacting the same sstable concurrently.
1396-
if pc != nil && !inputRangeAlreadyCompacting(env, pc) {
1400+
if pc != nil && !inputRangeAlreadyCompacting(p.opts.Comparer.Compare, env, pc) {
13971401
p.addScoresToPickedCompactionMetrics(pc, scores)
13981402
pc.score = info.score
13991403
if false {
@@ -1579,10 +1583,10 @@ func (p *compactionPickerByScore) pickedCompactionFromCandidateFile(
15791583
startLevel, outputLevel, p.baseLevel)
15801584
pc.kind = kind
15811585
pc.startLevel.files = inputs
1582-
pc.smallest, pc.largest = manifest.KeyRange(pc.cmp, pc.startLevel.files.All())
1586+
pc.smallest, pc.largest = manifest.KeyRange(p.opts.Comparer.Compare, pc.startLevel.files.All())
15831587

15841588
// Fail-safe to protect against compacting the same sstable concurrently.
1585-
if inputRangeAlreadyCompacting(env, pc) {
1589+
if inputRangeAlreadyCompacting(p.opts.Comparer.Compare, env, pc) {
15861590
return nil
15871591
}
15881592

@@ -1881,7 +1885,7 @@ func pickL0(
18811885
}
18821886
// A single-file intra-L0 compaction is unproductive.
18831887
if iter := pc.startLevel.files.Iter(); iter.First() != nil && iter.Next() != nil {
1884-
pc.smallest, pc.largest = manifest.KeyRange(pc.cmp, pc.startLevel.files.All())
1888+
pc.smallest, pc.largest = manifest.KeyRange(opts.Comparer.Compare, pc.startLevel.files.All())
18851889
return pc
18861890
}
18871891
} else {
@@ -1946,7 +1950,7 @@ func newPickedManualCompaction(
19461950
}
19471951
}
19481952
// Fail-safe to protect against compacting the same sstable concurrently.
1949-
if inputRangeAlreadyCompacting(env, pc) {
1953+
if inputRangeAlreadyCompacting(opts.Comparer.Compare, env, pc) {
19501954
return nil, true
19511955
}
19521956
return pc, false
@@ -1984,7 +1988,7 @@ func pickDownloadCompaction(
19841988
panic("pebble: download compaction picked unexpected output level")
19851989
}
19861990
// Fail-safe to protect against compacting the same sstable concurrently.
1987-
if inputRangeAlreadyCompacting(env, pc) {
1991+
if inputRangeAlreadyCompacting(opts.Comparer.Compare, env, pc) {
19881992
return nil
19891993
}
19901994
return pc
@@ -2030,7 +2034,7 @@ func pickReadTriggeredCompactionHelper(
20302034
if !pc.setupInputs(p.opts, env.diskAvailBytes, pc.startLevel, env.problemSpans) {
20312035
return nil
20322036
}
2033-
if inputRangeAlreadyCompacting(env, pc) {
2037+
if inputRangeAlreadyCompacting(p.opts.Comparer.Compare, env, pc) {
20342038
return nil
20352039
}
20362040
pc.kind = compactionKindRead
@@ -2055,7 +2059,7 @@ func (p *compactionPickerByScore) forceBaseLevel1() {
20552059
p.baseLevel = 1
20562060
}
20572061

2058-
func inputRangeAlreadyCompacting(env compactionEnv, pc *pickedCompaction) bool {
2062+
func inputRangeAlreadyCompacting(cmp base.Compare, env compactionEnv, pc *pickedCompaction) bool {
20592063
for _, cl := range pc.inputs {
20602064
for f := range cl.files.All() {
20612065
if f.IsCompacting() {
@@ -2097,8 +2101,8 @@ func inputRangeAlreadyCompacting(env compactionEnv, pc *pickedCompaction) bool {
20972101
if pc.outputLevel.level != c.outputLevel {
20982102
continue
20992103
}
2100-
if base.InternalCompare(pc.cmp, c.largest, pc.smallest) < 0 ||
2101-
base.InternalCompare(pc.cmp, c.smallest, pc.largest) > 0 {
2104+
if base.InternalCompare(cmp, c.largest, pc.smallest) < 0 ||
2105+
base.InternalCompare(cmp, c.smallest, pc.largest) > 0 {
21022106
continue
21032107
}
21042108

compaction_picker_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,6 @@ func TestPickedCompactionSetupInputs(t *testing.T) {
10581058
}
10591059

10601060
pc := &pickedCompaction{
1061-
cmp: DefaultComparer.Compare,
10621061
baseLevel: 1,
10631062
inputs: []compactionLevel{{level: -1}, {level: -1}},
10641063
}
@@ -1218,7 +1217,6 @@ func TestPickedCompactionExpandInputs(t *testing.T) {
12181217

12191218
case "expand-inputs":
12201219
pc := &pickedCompaction{
1221-
cmp: cmp,
12221220
inputs: []compactionLevel{{level: 1}},
12231221
}
12241222
pc.startLevel = &pc.inputs[0]

0 commit comments

Comments
 (0)