# Test the file-size grandparent boundary alignment heuristic. This test sets up
# L3 with a file at each of 'a', 'b', ..., 'z'. It also creates a single file in
# L2 spanning a-z. Then, it commits, flushes and compacts into L1 keys 'a@1',
# 'aa@1', 'ab@1', ..., 'zz@1'. Finally, it tests compacting L1 into L2.
#
# With L3 as the grandparent level, the alignment heuristic should attempt to
# align the output files with grandparent's boundaries. Each output file should
# have a key range formed by the prefix of a single letter.

define target-file-sizes=(5000, 5000, 5000, 5000)
L2
  a.SET.101:<rand-bytes=1000>
  z.SET.102:<rand-bytes=1000>
L3
  a.SET.001:<rand-bytes=10000>
L3
  b.SET.002:<rand-bytes=10000>
L3
  c.SET.003:<rand-bytes=10000>
L3
  d.SET.004:<rand-bytes=10000>
L3
  e.SET.005:<rand-bytes=10000>
L3
  f.SET.006:<rand-bytes=10000>
L3
  g.SET.007:<rand-bytes=10000>
L3
  h.SET.008:<rand-bytes=10000>
L3
  i.SET.009:<rand-bytes=10000>
L3
  j.SET.010:<rand-bytes=10000>
L3
  k.SET.011:<rand-bytes=10000>
L3
  l.SET.012:<rand-bytes=10000>
L3
  m.SET.013:<rand-bytes=10000>
L3
  n.SET.014:<rand-bytes=10000>
L3
  o.SET.015:<rand-bytes=10000>
L3
  p.SET.016:<rand-bytes=10000>
L3
  q.SET.017:<rand-bytes=10000>
L3
  r.SET.018:<rand-bytes=10000>
L3
  s.SET.019:<rand-bytes=10000>
L3
  t.SET.020:<rand-bytes=10000>
L3
  u.SET.021:<rand-bytes=10000>
L3
  v.SET.022:<rand-bytes=10000>
L3
  w.SET.023:<rand-bytes=10000>
L3
  x.SET.024:<rand-bytes=10000>
L3
  y.SET.025:<rand-bytes=10000>
L3
  z.SET.026:<rand-bytes=10000>
----
2:
  000004:[a#101,SET-z#102,SET]
3:
  000005:[a#1,SET-a#1,SET]
  000006:[b#2,SET-b#2,SET]
  000007:[c#3,SET-c#3,SET]
  000008:[d#4,SET-d#4,SET]
  000009:[e#5,SET-e#5,SET]
  000010:[f#6,SET-f#6,SET]
  000011:[g#7,SET-g#7,SET]
  000012:[h#8,SET-h#8,SET]
  000013:[i#9,SET-i#9,SET]
  000014:[j#10,SET-j#10,SET]
  000015:[k#11,SET-k#11,SET]
  000016:[l#12,SET-l#12,SET]
  000017:[m#13,SET-m#13,SET]
  000018:[n#14,SET-n#14,SET]
  000019:[o#15,SET-o#15,SET]
  000020:[p#16,SET-p#16,SET]
  000021:[q#17,SET-q#17,SET]
  000022:[r#18,SET-r#18,SET]
  000023:[s#19,SET-s#19,SET]
  000024:[t#20,SET-t#20,SET]
  000025:[u#21,SET-u#21,SET]
  000026:[v#22,SET-v#22,SET]
  000027:[w#23,SET-w#23,SET]
  000028:[x#24,SET-x#24,SET]
  000029:[y#25,SET-y#25,SET]
  000030:[z#26,SET-z#26,SET]

populate keylen=2 vallen=200 timestamps=(1)
----
wrote 702 keys

flush
----
0.0:
  000033:[a@1#103,SET-av@1#125,SET]
  000034:[aw@1#126,SET-br@1#148,SET]
  000035:[bs@1#149,SET-cn@1#171,SET]
  000036:[co@1#172,SET-dj@1#194,SET]
  000037:[dk@1#195,SET-ef@1#217,SET]
  000038:[eg@1#218,SET-fb@1#240,SET]
  000039:[fc@1#241,SET-fy@1#263,SET]
  000040:[fz@1#264,SET-gu@1#286,SET]
  000041:[gv@1#287,SET-hq@1#309,SET]
  000042:[hr@1#310,SET-im@1#332,SET]
  000043:[in@1#333,SET-ji@1#355,SET]
  000044:[jj@1#356,SET-ke@1#378,SET]
  000045:[kf@1#379,SET-la@1#401,SET]
  000046:[lb@1#402,SET-lx@1#424,SET]
  000047:[ly@1#425,SET-mt@1#447,SET]
  000048:[mu@1#448,SET-np@1#470,SET]
  000049:[nq@1#471,SET-ol@1#493,SET]
  000050:[om@1#494,SET-ph@1#516,SET]
  000051:[pi@1#517,SET-qd@1#539,SET]
  000052:[qe@1#540,SET-r@1#562,SET]
  000053:[ra@1#563,SET-rw@1#585,SET]
  000054:[rx@1#586,SET-ss@1#608,SET]
  000055:[st@1#609,SET-to@1#631,SET]
  000056:[tp@1#632,SET-uk@1#654,SET]
  000057:[ul@1#655,SET-vg@1#677,SET]
  000058:[vh@1#678,SET-wc@1#700,SET]
  000059:[wd@1#701,SET-wz@1#723,SET]
  000060:[x@1#724,SET-xv@1#746,SET]
  000061:[xw@1#747,SET-yr@1#769,SET]
  000062:[ys@1#770,SET-zn@1#792,SET]
  000063:[zo@1#793,SET-zz@1#804,SET]
2:
  000004:[a#101,SET-z#102,SET]
3:
  000005:[a#1,SET-a#1,SET]
  000006:[b#2,SET-b#2,SET]
  000007:[c#3,SET-c#3,SET]
  000008:[d#4,SET-d#4,SET]
  000009:[e#5,SET-e#5,SET]
  000010:[f#6,SET-f#6,SET]
  000011:[g#7,SET-g#7,SET]
  000012:[h#8,SET-h#8,SET]
  000013:[i#9,SET-i#9,SET]
  000014:[j#10,SET-j#10,SET]
  000015:[k#11,SET-k#11,SET]
  000016:[l#12,SET-l#12,SET]
  000017:[m#13,SET-m#13,SET]
  000018:[n#14,SET-n#14,SET]
  000019:[o#15,SET-o#15,SET]
  000020:[p#16,SET-p#16,SET]
  000021:[q#17,SET-q#17,SET]
  000022:[r#18,SET-r#18,SET]
  000023:[s#19,SET-s#19,SET]
  000024:[t#20,SET-t#20,SET]
  000025:[u#21,SET-u#21,SET]
  000026:[v#22,SET-v#22,SET]
  000027:[w#23,SET-w#23,SET]
  000028:[x#24,SET-x#24,SET]
  000029:[y#25,SET-y#25,SET]
  000030:[z#26,SET-z#26,SET]

compact a-zz L0
----
1:
  000064:[a@1#103,SET-av@1#125,SET]
  000065:[aw@1#126,SET-br@1#148,SET]
  000066:[bs@1#149,SET-cn@1#171,SET]
  000067:[co@1#172,SET-dj@1#194,SET]
  000068:[dk@1#195,SET-ef@1#217,SET]
  000069:[eg@1#218,SET-fb@1#240,SET]
  000070:[fc@1#241,SET-fy@1#263,SET]
  000071:[fz@1#264,SET-gu@1#286,SET]
  000072:[gv@1#287,SET-hq@1#309,SET]
  000073:[hr@1#310,SET-im@1#332,SET]
  000074:[in@1#333,SET-ji@1#355,SET]
  000075:[jj@1#356,SET-ke@1#378,SET]
  000076:[kf@1#379,SET-la@1#401,SET]
  000077:[lb@1#402,SET-lx@1#424,SET]
  000078:[ly@1#425,SET-mt@1#447,SET]
  000079:[mu@1#448,SET-np@1#470,SET]
  000080:[nq@1#471,SET-ol@1#493,SET]
  000081:[om@1#494,SET-ph@1#516,SET]
  000082:[pi@1#517,SET-qd@1#539,SET]
  000083:[qe@1#540,SET-r@1#562,SET]
  000084:[ra@1#563,SET-rw@1#585,SET]
  000085:[rx@1#586,SET-ss@1#608,SET]
  000086:[st@1#609,SET-to@1#631,SET]
  000087:[tp@1#632,SET-uk@1#654,SET]
  000088:[ul@1#655,SET-vg@1#677,SET]
  000089:[vh@1#678,SET-wc@1#700,SET]
  000090:[wd@1#701,SET-wz@1#723,SET]
  000091:[x@1#724,SET-xv@1#746,SET]
  000092:[xw@1#747,SET-yr@1#769,SET]
  000093:[ys@1#770,SET-zn@1#792,SET]
  000094:[zo@1#793,SET-zz@1#804,SET]
2:
  000004:[a#101,SET-z#102,SET]
3:
  000005:[a#1,SET-a#1,SET]
  000006:[b#2,SET-b#2,SET]
  000007:[c#3,SET-c#3,SET]
  000008:[d#4,SET-d#4,SET]
  000009:[e#5,SET-e#5,SET]
  000010:[f#6,SET-f#6,SET]
  000011:[g#7,SET-g#7,SET]
  000012:[h#8,SET-h#8,SET]
  000013:[i#9,SET-i#9,SET]
  000014:[j#10,SET-j#10,SET]
  000015:[k#11,SET-k#11,SET]
  000016:[l#12,SET-l#12,SET]
  000017:[m#13,SET-m#13,SET]
  000018:[n#14,SET-n#14,SET]
  000019:[o#15,SET-o#15,SET]
  000020:[p#16,SET-p#16,SET]
  000021:[q#17,SET-q#17,SET]
  000022:[r#18,SET-r#18,SET]
  000023:[s#19,SET-s#19,SET]
  000024:[t#20,SET-t#20,SET]
  000025:[u#21,SET-u#21,SET]
  000026:[v#22,SET-v#22,SET]
  000027:[w#23,SET-w#23,SET]
  000028:[x#24,SET-x#24,SET]
  000029:[y#25,SET-y#25,SET]
  000030:[z#26,SET-z#26,SET]

# Perform the actual test. Compacting L1 into L2 should use L3's boundaries to
# inform compaction output splitting.
#
compact a-zz L1
----
2:
  000095:[a#101,SET-az@1#129,SET]
  000096:[b@1#130,SET-bz@1#156,SET]
  000097:[c@1#157,SET-cz@1#183,SET]
  000098:[d@1#184,SET-dz@1#210,SET]
  000099:[e@1#211,SET-ez@1#237,SET]
  000100:[f@1#238,SET-fz@1#264,SET]
  000101:[g@1#265,SET-gz@1#291,SET]
  000102:[h@1#292,SET-hz@1#318,SET]
  000103:[i@1#319,SET-iz@1#345,SET]
  000104:[j@1#346,SET-jz@1#372,SET]
  000105:[k@1#373,SET-kz@1#399,SET]
  000106:[l@1#400,SET-lz@1#426,SET]
  000107:[m@1#427,SET-mz@1#453,SET]
  000108:[n@1#454,SET-nz@1#480,SET]
  000109:[o@1#481,SET-oz@1#507,SET]
  000110:[p@1#508,SET-pz@1#534,SET]
  000111:[q@1#535,SET-qz@1#561,SET]
  000112:[r@1#562,SET-rz@1#588,SET]
  000113:[s@1#589,SET-sz@1#615,SET]
  000114:[t@1#616,SET-tz@1#642,SET]
  000115:[u@1#643,SET-uz@1#669,SET]
  000116:[v@1#670,SET-vz@1#696,SET]
  000117:[w@1#697,SET-wz@1#723,SET]
  000118:[x@1#724,SET-xz@1#750,SET]
  000119:[y@1#751,SET-yz@1#777,SET]
  000120:[z#102,SET-zr@1#796,SET]
  000121:[zs@1#797,SET-zz@1#804,SET]
3:
  000005:[a#1,SET-a#1,SET]
  000006:[b#2,SET-b#2,SET]
  000007:[c#3,SET-c#3,SET]
  000008:[d#4,SET-d#4,SET]
  000009:[e#5,SET-e#5,SET]
  000010:[f#6,SET-f#6,SET]
  000011:[g#7,SET-g#7,SET]
  000012:[h#8,SET-h#8,SET]
  000013:[i#9,SET-i#9,SET]
  000014:[j#10,SET-j#10,SET]
  000015:[k#11,SET-k#11,SET]
  000016:[l#12,SET-l#12,SET]
  000017:[m#13,SET-m#13,SET]
  000018:[n#14,SET-n#14,SET]
  000019:[o#15,SET-o#15,SET]
  000020:[p#16,SET-p#16,SET]
  000021:[q#17,SET-q#17,SET]
  000022:[r#18,SET-r#18,SET]
  000023:[s#19,SET-s#19,SET]
  000024:[t#20,SET-t#20,SET]
  000025:[u#21,SET-u#21,SET]
  000026:[v#22,SET-v#22,SET]
  000027:[w#23,SET-w#23,SET]
  000028:[x#24,SET-x#24,SET]
  000029:[y#25,SET-y#25,SET]
  000030:[z#26,SET-z#26,SET]

file-sizes
----
L2:
  000095:[a#101,1-az@1#129,1]: 7528 bytes (7.4KB)
  000096:[b@1#130,1-bz@1#156,1]: 6520 bytes (6.4KB)
  000097:[c@1#157,1-cz@1#183,1]: 6520 bytes (6.4KB)
  000098:[d@1#184,1-dz@1#210,1]: 6520 bytes (6.4KB)
  000099:[e@1#211,1-ez@1#237,1]: 6520 bytes (6.4KB)
  000100:[f@1#238,1-fz@1#264,1]: 6520 bytes (6.4KB)
  000101:[g@1#265,1-gz@1#291,1]: 6520 bytes (6.4KB)
  000102:[h@1#292,1-hz@1#318,1]: 6520 bytes (6.4KB)
  000103:[i@1#319,1-iz@1#345,1]: 6520 bytes (6.4KB)
  000104:[j@1#346,1-jz@1#372,1]: 6520 bytes (6.4KB)
  000105:[k@1#373,1-kz@1#399,1]: 6520 bytes (6.4KB)
  000106:[l@1#400,1-lz@1#426,1]: 6520 bytes (6.4KB)
  000107:[m@1#427,1-mz@1#453,1]: 6520 bytes (6.4KB)
  000108:[n@1#454,1-nz@1#480,1]: 6520 bytes (6.4KB)
  000109:[o@1#481,1-oz@1#507,1]: 6520 bytes (6.4KB)
  000110:[p@1#508,1-pz@1#534,1]: 6520 bytes (6.4KB)
  000111:[q@1#535,1-qz@1#561,1]: 6519 bytes (6.4KB)
  000112:[r@1#562,1-rz@1#588,1]: 6520 bytes (6.4KB)
  000113:[s@1#589,1-sz@1#615,1]: 6520 bytes (6.4KB)
  000114:[t@1#616,1-tz@1#642,1]: 6520 bytes (6.4KB)
  000115:[u@1#643,1-uz@1#669,1]: 6520 bytes (6.4KB)
  000116:[v@1#670,1-vz@1#696,1]: 6520 bytes (6.4KB)
  000117:[w@1#697,1-wz@1#723,1]: 6520 bytes (6.4KB)
  000118:[x@1#724,1-xz@1#750,1]: 6520 bytes (6.4KB)
  000119:[y@1#751,1-yz@1#777,1]: 6520 bytes (6.4KB)
  000120:[z#102,1-zr@1#796,1]: 5800 bytes (5.7KB)
  000121:[zs@1#797,1-zz@1#804,1]: 2382 bytes (2.3KB)
L3:
  000005:[a#1,1-a#1,1]: 10667 bytes (10KB)
  000006:[b#2,1-b#2,1]: 10667 bytes (10KB)
  000007:[c#3,1-c#3,1]: 10667 bytes (10KB)
  000008:[d#4,1-d#4,1]: 10667 bytes (10KB)
  000009:[e#5,1-e#5,1]: 10667 bytes (10KB)
  000010:[f#6,1-f#6,1]: 10667 bytes (10KB)
  000011:[g#7,1-g#7,1]: 10667 bytes (10KB)
  000012:[h#8,1-h#8,1]: 10667 bytes (10KB)
  000013:[i#9,1-i#9,1]: 10667 bytes (10KB)
  000014:[j#10,1-j#10,1]: 10667 bytes (10KB)
  000015:[k#11,1-k#11,1]: 10667 bytes (10KB)
  000016:[l#12,1-l#12,1]: 10667 bytes (10KB)
  000017:[m#13,1-m#13,1]: 10667 bytes (10KB)
  000018:[n#14,1-n#14,1]: 10667 bytes (10KB)
  000019:[o#15,1-o#15,1]: 10667 bytes (10KB)
  000020:[p#16,1-p#16,1]: 10667 bytes (10KB)
  000021:[q#17,1-q#17,1]: 10667 bytes (10KB)
  000022:[r#18,1-r#18,1]: 10667 bytes (10KB)
  000023:[s#19,1-s#19,1]: 10667 bytes (10KB)
  000024:[t#20,1-t#20,1]: 10667 bytes (10KB)
  000025:[u#21,1-u#21,1]: 10667 bytes (10KB)
  000026:[v#22,1-v#22,1]: 10667 bytes (10KB)
  000027:[w#23,1-w#23,1]: 10667 bytes (10KB)
  000028:[x#24,1-x#24,1]: 10667 bytes (10KB)
  000029:[y#25,1-y#25,1]: 10667 bytes (10KB)
  000030:[z#26,1-z#26,1]: 10667 bytes (10KB)

# Test a scenario where there exists a grandparent file (in L3), but the L1->L2
# compaction doesn't reach it until late in the compaction. The output file
# should be split at 2x the target file size (~10K), despite not being aligned
# with a grandparent.
#
# Additionally, when the compaction does reach the grandparent's start bound,
# the compaction should NOT split the output if the current output is less than
# 0.5x the target file size (~2.5K).
#
# Lastly, once past the final grandparent, the compaction should optimize for
# cutting as close to file size as possible, resulting in an output file ~5K.

define target-file-sizes=(5000, 5000, 5000, 5000)
L1
  a.SET.201:<rand-bytes=1000>
  b.SET.202:<rand-bytes=1000>
  c.SET.203:<rand-bytes=1000>
  d.SET.204:<rand-bytes=1000>
  e.SET.205:<rand-bytes=1000>
  f.SET.206:<rand-bytes=1000>
  g.SET.207:<rand-bytes=1000>
  h.SET.208:<rand-bytes=1000>
  i.SET.209:<rand-bytes=1000>
  j.SET.210:<rand-bytes=1000>
  k.SET.211:<rand-bytes=1000>
  l.SET.212:<rand-bytes=1000>
  m.SET.213:<rand-bytes=1000>
  n.SET.214:<rand-bytes=1000>
  o.SET.215:<rand-bytes=1000>
L2
  a.SET.101:<rand-bytes=10>
  z.SET.102:<rand-bytes=10>
L3
  m.SET.001:<rand-bytes=10000>
----
1:
  000004:[a#201,SET-o#215,SET]
2:
  000005:[a#101,SET-z#102,SET]
3:
  000006:[m#1,SET-m#1,SET]

compact a-zz L1
----
2:
  000007:[a#201,SET-j#210,SET]
  000008:[k#211,SET-o#215,SET]
  000009:[z#102,SET-z#102,SET]
3:
  000006:[m#1,SET-m#1,SET]

file-sizes
----
L2:
  000007:[a#201,1-j#210,1]: 10849 bytes (11KB)
  000008:[k#211,1-o#215,1]: 5756 bytes (5.6KB)
  000009:[z#102,1-z#102,1]: 672 bytes (672B)
L3:
  000006:[m#1,1-m#1,1]: 10667 bytes (10KB)

# Test the file-size splitter's adaptive tolerance for early-splitting at a
# grandparent boundary. The L1->L2 compaction has many opportunities to split at
# a grandparent boundary at file sizes ≥ 2.5K. Because it's seen more than 8
# grandparent boundaries, waits until file size is ≥ 90% of the target file size
# (eg, ~4.5K).

define target-file-sizes=(5000, 5000, 5000, 5000)
L1
  a.SET.201:<rand-bytes=1000>
  b.SET.202:<rand-bytes=1000>
  c.SET.203:<rand-bytes=1000>
  d.SET.204:<rand-bytes=1000>
  e.SET.205:<rand-bytes=1000>
  f.SET.206:<rand-bytes=1000>
  g.SET.207:<rand-bytes=1000>
  h.SET.208:<rand-bytes=1000>
  i.SET.209:<rand-bytes=1000>
  j.SET.210:<rand-bytes=1000>
  k.SET.211:<rand-bytes=1000>
  l.SET.212:<rand-bytes=1000>
  m.SET.213:<rand-bytes=1000>
  n.SET.214:<rand-bytes=1000>
  o.SET.215:<rand-bytes=1000>
L2
  a.SET.101:<rand-bytes=10>
  z.SET.102:<rand-bytes=10>
L3
  a.SET.001:<rand-bytes=1000>
L3
  ab.SET.002:<rand-bytes=1000>
L3
  ac.SET.003:<rand-bytes=1000>
L3
  ad.SET.004:<rand-bytes=1000>
L3
  ae.SET.005:<rand-bytes=1000>
L3
  af.SET.006:<rand-bytes=1000>
L3
  ag.SET.007:<rand-bytes=1000>
L3
  ah.SET.008:<rand-bytes=1000>
L3
  c.SET.009:<rand-bytes=1000>
L3
  d.SET.010:<rand-bytes=1000>
L3
  e.SET.011:<rand-bytes=1000>
L3
  f.SET.012:<rand-bytes=1000>
L3
  m.SET.013:<rand-bytes=1000>
----
1:
  000004:[a#201,SET-o#215,SET]
2:
  000005:[a#101,SET-z#102,SET]
3:
  000006:[a#1,SET-a#1,SET]
  000007:[ab#2,SET-ab#2,SET]
  000008:[ac#3,SET-ac#3,SET]
  000009:[ad#4,SET-ad#4,SET]
  000010:[ae#5,SET-ae#5,SET]
  000011:[af#6,SET-af#6,SET]
  000012:[ag#7,SET-ag#7,SET]
  000013:[ah#8,SET-ah#8,SET]
  000014:[c#9,SET-c#9,SET]
  000015:[d#10,SET-d#10,SET]
  000016:[e#11,SET-e#11,SET]
  000017:[f#12,SET-f#12,SET]
  000018:[m#13,SET-m#13,SET]

compact a-zz L1
----
2:
  000019:[a#201,SET-e#205,SET]
  000020:[f#206,SET-l#212,SET]
  000021:[m#213,SET-z#102,SET]
3:
  000006:[a#1,SET-a#1,SET]
  000007:[ab#2,SET-ab#2,SET]
  000008:[ac#3,SET-ac#3,SET]
  000009:[ad#4,SET-ad#4,SET]
  000010:[ae#5,SET-ae#5,SET]
  000011:[af#6,SET-af#6,SET]
  000012:[ag#7,SET-ag#7,SET]
  000013:[ah#8,SET-ah#8,SET]
  000014:[c#9,SET-c#9,SET]
  000015:[d#10,SET-d#10,SET]
  000016:[e#11,SET-e#11,SET]
  000017:[f#12,SET-f#12,SET]
  000018:[m#13,SET-m#13,SET]

file-sizes
----
L2:
  000019:[a#201,1-e#205,1]: 5756 bytes (5.6KB)
  000020:[f#206,1-l#212,1]: 7784 bytes (7.6KB)
  000021:[m#213,1-z#102,1]: 3718 bytes (3.6KB)
L3:
  000006:[a#1,1-a#1,1]: 1667 bytes (1.6KB)
  000007:[ab#2,1-ab#2,1]: 1668 bytes (1.6KB)
  000008:[ac#3,1-ac#3,1]: 1668 bytes (1.6KB)
  000009:[ad#4,1-ad#4,1]: 1668 bytes (1.6KB)
  000010:[ae#5,1-ae#5,1]: 1668 bytes (1.6KB)
  000011:[af#6,1-af#6,1]: 1668 bytes (1.6KB)
  000012:[ag#7,1-ag#7,1]: 1668 bytes (1.6KB)
  000013:[ah#8,1-ah#8,1]: 1668 bytes (1.6KB)
  000014:[c#9,1-c#9,1]: 1667 bytes (1.6KB)
  000015:[d#10,1-d#10,1]: 1667 bytes (1.6KB)
  000016:[e#11,1-e#11,1]: 1667 bytes (1.6KB)
  000017:[f#12,1-f#12,1]: 1667 bytes (1.6KB)
  000018:[m#13,1-m#13,1]: 1667 bytes (1.6KB)
