# Init a multi-level compaction, because multi level write amp is lower
setup-inputs a a
L1
  a.SET.1-b.SET.2 size=1
L2
  a.SET.3-c.SET.4 size=1
L3
  c.SET.3-d.SET.2 size=1
----
L1
  000001:[a#1,1-b#2,1]
L2
  000002:[a#3,1-c#4,1]
L3
  000003:[c#3,1-d#2,1]
init-multi-level(1,2,3)
Original WriteAmp 2.00; ML WriteAmp 1.50
Original OverlappingRatio 1.00; ML OverlappingRatio 0.50

# Verify that the input level size should not affect the decision to conduct a multi
# level compaction.
setup-inputs a a
L1
  a.SET.1-b.SET.2 size=10
L2
  a.SET.3-c.SET.4 size=1
L3
  c.SET.3-d.SET.2 size=1
----
L1
  000001:[a#1,1-b#2,1]
L2
  000002:[a#3,1-c#4,1]
L3
  000003:[c#3,1-d#2,1]
init-multi-level(1,2,3)
Original WriteAmp 1.10; ML WriteAmp 1.09
Original OverlappingRatio 0.10; ML OverlappingRatio 0.09

# Don't init a multi-level compaction because write amp from multi level compaction is larger
setup-inputs a a
L1
  a.SET.1-b.SET.2 size=1
L2
  a.SET.3-c.SET.4 size=1
L3
  c.SET.3-d.SET.2 size=3
----
L1
  000001:[a#1,1-b#2,1]
L2
  000002:[a#3,1-c#4,1]

# Init a multi-level compaction, but note that the second files in L2 and L3 do not get
# chosen, as they don't overlap with the original compaction.
setup-inputs a a
L1
  a.SET.1-b.SET.2 size=6
L2
  a.SET.3-c.SET.4 size=5
  e.SET.1-h.SET.4 size=4
L3
  c.SET.3-d.SET.2 size=6
  e.SET.2-h.SET.4 size=4
----
L1
  000001:[a#1,1-b#2,1]
L2
  000002:[a#3,1-c#4,1]
L3
  000004:[c#3,1-d#2,1]
init-multi-level(1,2,3)
Original WriteAmp 1.83; ML WriteAmp 1.55
Original OverlappingRatio 0.83; ML OverlappingRatio 0.55

# Init a multi-level compaction without an overlapping file in the lowest level.
setup-inputs a a
L1
  a.SET.1-b.SET.2 size=6
L2 max-size=5
  a.SET.3-c.SET.4 size=5
L3
  e.SET.3-f.SET.2 size=100
----
L1
  000001:[a#1,1-b#2,1]
L2
  000002:[a#3,1-c#4,1]
init-multi-level(1,2,3)
Original WriteAmp 1.83; ML WriteAmp 1.00
Original OverlappingRatio 0.83; ML OverlappingRatio 0.00

# Init a multi-level compaction with no file in the lowest level.
setup-inputs a a
L1
  a.SET.1-b.SET.2 size=6
L2
  a.SET.3-c.SET.4 size=5
----
L1
  000001:[a#1,1-b#2,1]
L2
  000002:[a#3,1-c#4,1]
init-multi-level(1,2,3)
Original WriteAmp 1.83; ML WriteAmp 1.00
Original OverlappingRatio 0.83; ML OverlappingRatio 0.00


# Don't init a multi-level compaction, as the single level compaction results in a move (
# write amp is 1) while the multi level compaction results in a Write Amp greater than 1
setup-inputs a a
L1
  a.SET.1-b.SET.2 size=6
L2
  e.SET.3-f.SET.2 size=100
L3
  a.SET.3-c.SET.4 size=5
----
L1
  000001:[a#1,1-b#2,1]

# Init a multi-level compaction, without an overlapping file in the (tie goes to the ML compaction!)
# intermediate and output levels
setup-inputs a a
L1
  a.SET.1-b.SET.2 size=6
L2
  e.SET.3-f.SET.2 size=1
L3
  e.SET.4-f.SET.5 size=5
----
L1
  000001:[a#1,1-b#2,1]
init-multi-level(1,2,3)
Original WriteAmp 1.00; ML WriteAmp 1.00
Original OverlappingRatio 0.00; ML OverlappingRatio 0.00


# Init a multi-level compaction which expands the intermediate level with a file that only
# overlaps with the lowest level. (I.e. it gets included during second setupInputs call)
setup-inputs a a
L1
  a.SET.1-b.SET.2 size=3
L2
  a.SET.2-b.SET.3 size=5
  c.SET.2-d.SET.3 size=3
L3
  a.SET.3-c.SET.4 size=3
----
L1
  000001:[a#1,1-b#2,1]
L2
  000002:[a#2,1-b#3,1]
  000003:[c#2,1-d#3,1]
L3
  000004:[a#3,1-c#4,1]
init-multi-level(1,2,3)
Original WriteAmp 2.67; ML WriteAmp 1.27
Original OverlappingRatio 1.67; ML OverlappingRatio 0.27

# Init a multi-level compaction which DOES NOT expand the input level with a file that
# only overlaps with the lowest level, even if it doesn't expand the output level keyspan.
# TODO(msbutler): include this file in the compaction
setup-inputs a a
L1
  a.SET.1-b.SET.2 size=1
  c.SET.2-d.SET.3 size=10
L2
  a.SET.2-b.SET.3 size=1
L3
  a.SET.3-c.SET.4 size=1
----
L1
  000001:[a#1,1-b#2,1]
L2
  000003:[a#2,1-b#3,1]
L3
  000004:[a#3,1-c#4,1]
init-multi-level(1,2,3)
Original WriteAmp 2.00; ML WriteAmp 1.50
Original OverlappingRatio 1.00; ML OverlappingRatio 0.50

# Verify an expansion of the output level in the initial setupInputs will init a multi-level
# compaction. i.e. without the initial expansion, the multil level compaction would not have
# occurred.
setup-inputs a a
L1
  a.SET.5-b.SET.6 size=1
L2
  a.SET.3-c.SET.4 size=1
  c.SET.3-d.SET.2 size=1
L3
  c.SET.4-d.SET.4 size=3
----
L1
  000001:[a#5,1-b#6,1]
L2
  000002:[a#3,1-c#4,1]
  000003:[c#3,1-d#2,1]
L3
  000004:[c#4,1-d#4,1]
init-multi-level(1,2,3)
Original WriteAmp 3.00; ML WriteAmp 2.00
Original OverlappingRatio 2.00; ML OverlappingRatio 1.00

setup-inputs a a
L1
  a.SET.5-b.SET.6 size=1
L2
  a.SET.3-c.SET.4 size=1
L3
  c.SET.4-d.SET.4 size=3
----
L1
  000001:[a#5,1-b#6,1]
L2
  000002:[a#3,1-c#4,1]


# Verify the second setupInputs call does not add an intermediate file if doing so would expand the
# output level (i.e. the pc.grow logic).
setup-inputs a a
L1
  a.SET.1-b.SET.2 size=1
L2
  a.SET.2-b.SET.3 size=1
  d.SET.2-f.SET.2 size=1
L3
  b.SET.1-d.SET.1 size=1
  e.SET.4-f.SET.5 size=1
----
L1
  000001:[a#1,1-b#2,1]
L2
  000002:[a#2,1-b#3,1]
L3
  000004:[b#1,1-d#1,1]
init-multi-level(1,2,3)
Original WriteAmp 2.00; ML WriteAmp 1.50
Original OverlappingRatio 1.00; ML OverlappingRatio 0.50

# Verify the max number of input levels equals 2.
setup-inputs a a
L1
  a.SET.1-b.SET.2 size=6
L2
  a.SET.3-c.SET.4 size=5
L3
  c.SET.3-d.SET.2 size=2
L4
  c.SET.4-d.SET.3 size=1
----
L1
  000001:[a#1,1-b#2,1]
L2
  000002:[a#3,1-c#4,1]
L3
  000003:[c#3,1-d#2,1]
init-multi-level(1,2,3)
Original WriteAmp 1.83; ML WriteAmp 1.18
Original OverlappingRatio 0.83; ML OverlappingRatio 0.18

# Don't init multi-level compaction if the max size limit exceeded by initial setupInputs.
setup-inputs avail-bytes=10 a a
L1
  a.SET.1-b.SET.2 size=6
L2
  a.SET.5-b.SET.6 size=5
L3
  a.SET.3-d.SET.4 size=3
----
L1
  000001:[a#1,1-b#2,1]
L2
  000002:[a#5,1-b#6,1]

# During second setupInputs call, allow output level expansion even if max size
# limit is exceeded, but not conduct intermediate level expansion.
#
# TODO(msbutler): If second setup inputs exceeds maxSize limits, should the first compaction get
# returned?
setup-inputs avail-bytes=20 a a
L1
  a.SET.1-b.SET.2 size=4
L2
  a.SET.5-b.SET.6 size=5
  c.SET.4-e.SET.3 size=8
L3
  a.SET.3-d.SET.4 size=2
  d.SET.2-e.SET.2 size=2
----
L1
  000001:[a#1,1-b#2,1]
L2
  000002:[a#5,1-b#6,1]
L3
  000004:[a#3,1-d#4,1]
  000005:[d#2,1-e#2,1]
init-multi-level(1,2,3)
Original WriteAmp 2.25; ML WriteAmp 1.44
Original OverlappingRatio 1.25; ML OverlappingRatio 0.44

# Don't init a multi-level compaction if the start level is L5.
setup-inputs a a
L5
  a.SET.1-b.SET.2 size=6
L6
  a.SET.3-c.SET.4 size=5
----
L5
  000001:[a#1,1-b#2,1]
L6
  000002:[a#3,1-c#4,1]

# Don't init a multi-level compaction if the start level is L0.
setup-inputs a a
L0
  a.SET.1-b.SET.2 size=6
L1
  a.SET.3-c.SET.4 size=5
----
L0
  000001:[a#1,1-b#2,1]
L1
  000002:[a#3,1-c#4,1]

# Verify a multi level compaction will not init on a compacting file.
setup-inputs a a
L1
  a.SET.1-b.SET.2 size=1
L2
  a.SET.3-c.SET.4 size=1
L3
  c.SET.3-d.SET.2 size=1 compacting
----
L1
  000001:[a#1,1-b#2,1]
L2
  000002:[a#3,1-c#4,1]
