Skip to content

Commit d6b8e14

Browse files
committed
db: initialize baseLevel in compaction tests
1 parent b25d04c commit d6b8e14

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

compaction_picker.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ type pickedCompaction struct {
185185
// levels that get compacted in multi level compactions
186186
extraLevels []*compactionLevel
187187
inputs []compactionLevel
188-
// LBase at the time of compaction picking.
188+
// LBase at the time of compaction picking. Might be uninitialized for
189+
// intra-L0 compactions.
189190
baseLevel int
190191
// L0-specific compaction info. Set to a non-nil value for all compactions
191192
// where startLevel == 0 that were generated by L0Sublevels.
@@ -230,6 +231,9 @@ func newPickedCompaction(
230231
l0Organizer *manifest.L0Organizer,
231232
startLevel, outputLevel, baseLevel int,
232233
) *pickedCompaction {
234+
if outputLevel > 0 && baseLevel == 0 {
235+
panic("base level cannot be 0")
236+
}
233237
if startLevel > 0 && startLevel < baseLevel {
234238
panic(fmt.Sprintf("invalid compaction: start level %d should not be empty (base level %d)",
235239
startLevel, baseLevel))
@@ -255,14 +259,16 @@ func newPickedCompaction(
255259
// determining the target output file size, overlap bytes, and expanded
256260
// bytes, taking into account the base level.
257261
func adjustedOutputLevel(outputLevel int, baseLevel int) int {
258-
adjustedOutputLevel := outputLevel
259-
if adjustedOutputLevel > 0 {
260-
// Output level is in the range [baseLevel, numLevels]. For the purpose of
261-
// determining the target output file size, overlap bytes, and expanded
262-
// bytes, we want to adjust the range to [1,numLevels].
263-
adjustedOutputLevel = 1 + outputLevel - baseLevel
264-
}
265-
return adjustedOutputLevel
262+
if outputLevel == 0 {
263+
return 0
264+
}
265+
if baseLevel == 0 {
266+
panic("base level cannot be 0")
267+
}
268+
// Output level is in the range [baseLevel, numLevels). For the purpose of
269+
// determining the target output file size, overlap bytes, and expanded
270+
// bytes, we want to adjust the range to [1, numLevels).
271+
return 1 + outputLevel - baseLevel
266272
}
267273

268274
func newPickedCompactionFromL0(

compaction_picker_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,8 +1063,9 @@ func TestPickedCompactionSetupInputs(t *testing.T) {
10631063
}
10641064

10651065
pc := &pickedCompaction{
1066-
cmp: DefaultComparer.Compare,
1067-
inputs: []compactionLevel{{level: -1}, {level: -1}},
1066+
cmp: DefaultComparer.Compare,
1067+
baseLevel: 1,
1068+
inputs: []compactionLevel{{level: -1}, {level: -1}},
10681069
}
10691070
pc.startLevel, pc.outputLevel = &pc.inputs[0], &pc.inputs[1]
10701071
var currentLevel int

0 commit comments

Comments
 (0)