# Ensure that a range deletion in a higher level results in a compensated level
# size and a higher level score as a result.

define lbase-max-bytes=65536 enable-table-stats=false
L5
  a.RANGEDEL.2:f
L6
  a.SET.1:<rand-bytes=65536>
  b.SET.1:<rand-bytes=65536>
  c.SET.1:<rand-bytes=65536>
  d.SET.1:<rand-bytes=65536>
  e.SET.1:<rand-bytes=65536>
----
5:
  000004:[a#2,RANGEDEL-f#inf,RANGEDEL]
6:
  000005:[a#1,SET-e#1,SET]

scores
----
L       Size   Score
L0  	0B     0.0
L1  	0B     0.0
L2  	0B     0.0
L3  	0B     0.0
L4  	0B     0.0
L5  	729B   0.0
L6  	321KB  -

enable-table-stats
----

wait-pending-table-stats
000004
----
num-entries: 1
num-deletions: 1
num-range-key-sets: 0
point-deletions-bytes-estimate: 0
range-deletions-bytes-estimate: 328519

scores
----
L       Size   Score
L0  	0B     0.0
L1  	0B     0.0
L2  	0B     0.0
L3  	0B     0.0
L4  	0B     0.0
L5  	729B   4.5
L6  	321KB  -

# Ensure that point deletions in a higher level result in a compensated level
# size and higher level scores as a result.

define lbase-max-bytes=65536  enable-table-stats=false
L5
  a.DEL.2:
  b.DEL.2:
  c.DEL.2:
  d.DEL.2:
  e.DEL.2:
L6
  a.SET.1:<rand-bytes=65536>
  b.SET.1:<rand-bytes=65536>
  c.SET.1:<rand-bytes=65536>
  d.SET.1:<rand-bytes=65536>
  e.SET.1:<rand-bytes=65536>
----
5:
  000004:[a#2,DEL-e#2,DEL]
6:
  000005:[a#1,SET-e#1,SET]

scores
----
L       Size   Score
L0  	0B     0.0
L1  	0B     0.0
L2  	0B     0.0
L3  	0B     0.0
L4  	0B     0.0
L5  	715B   0.0
L6  	321KB  -

enable-table-stats
----

wait-pending-table-stats
000004
----
num-entries: 5
num-deletions: 5
num-range-key-sets: 0
point-deletions-bytes-estimate: 164581
range-deletions-bytes-estimate: 0

scores
----
L       Size   Score
L0  	0B     0.0
L1  	0B     0.0
L2  	0B     0.0
L3  	0B     0.0
L4  	0B     0.0
L5  	715B   2.3
L6  	321KB  -

# Run a similar test as above, but this time the table containing the DELs is
# ingested after the database is initialized. When the ingested sstable's stats
# are loaded and automatic compactions are re-enabled, it should trigger an
# automatic compaction of the ingested sstable on account of the high
# point-deletions-bytes-estimate value.
#
# This a regression test for an issue where the table stats collector wouldn't
# attempt to schedule a compaction if a file only had compensation due to point
# deletions and not range deletions.

define lbase-max-bytes=65536  enable-table-stats=true auto-compactions=off
L6
  a.SET.1:<rand-bytes=65536>
  b.SET.1:<rand-bytes=65536>
  c.SET.1:<rand-bytes=65536>
  d.SET.1:<rand-bytes=65536>
  e.SET.1:<rand-bytes=65536>
----
6:
  000004:[a#1,SET-e#1,SET]

ingest ext1
del a:
del b:
del c:
del d:
del e:
----
5:
  000005:[a:#10,DEL-e:#10,DEL]
6:
  000004:[a#1,SET-e#1,SET]

wait-pending-table-stats
000005
----
num-entries: 5
num-deletions: 5
num-range-key-sets: 0
point-deletions-bytes-estimate: 164616
range-deletions-bytes-estimate: 0

maybe-compact
----
1 compactions in progress:
5: 000005:a:#10,0-e:#10,0
6: 000004:a#1,1-e#1,1

scores
----
L       Size   Score
L0  	0B     0.0
L1  	0B     0.0
L2  	0B     0.0
L3  	0B     0.0
L4  	0B     0.0
L5  	0B     0.0
L6  	321KB  -

lsm
----
6:
  000006:[a#0,SET-e#0,SET]

# Test the adjustment of level sizes to accommodate in-progress compactions. A
# compaction may be "inProgress" if it's already been applied, but is still
# deleting obsolete files. These compactions' effects have already been applied
# to the LSM, so size adjustment should ignore them and not doubly adjust sizes.

define lbase-max-bytes=65536  enable-table-stats=false auto-compactions=on pause-cleaning
L5
  aa.SET.2:<rand-bytes=131072>
  bb.SET.2:<rand-bytes=131072>
  cc.SET.2:<rand-bytes=131072>
  dd.SET.2:<rand-bytes=131072>
L5
  e.SET.2:<rand-bytes=131072>
L6
  a.SET.1:<rand-bytes=65536>
  b.SET.1:<rand-bytes=65536>
  c.SET.1:<rand-bytes=65536>
  d.SET.1:<rand-bytes=65536>
L6
  e.SET.1:<rand-bytes=131072>
----
5:
  000004:[aa#2,SET-dd#2,SET]
  000005:[e#2,SET-e#2,SET]
6:
  000006:[a#1,SET-d#1,SET]
  000007:[e#1,SET-e#1,SET]

scores
----
L       Size   Score
L0  	0B     0.0
L1  	0B     0.0
L2  	0B     0.0
L3  	0B     0.0
L4  	0B     0.0
L5  	641KB  6.3
L6  	385KB  -

lsm verbose
----
5:
  000004:[aa#2,SET-dd#2,SET] seqnums:[2-2] points:[aa#2,SET-dd#2,SET]
  000005:[e#2,SET-e#2,SET] seqnums:[2-2] points:[e#2,SET-e#2,SET]
6:
  000006:[a#1,SET-d#1,SET] seqnums:[1-1] points:[a#1,SET-d#1,SET]
  000007:[e#1,SET-e#1,SET] seqnums:[1-1] points:[e#1,SET-e#1,SET]

# Attempting to schedule a compaction should begin a L5->L6 compaction.

maybe-compact
----
1 compactions in progress:
5: 000004:aa#2,1-dd#2,1
6: 000006:a#1,1-d#1,1

# The scores and sizes should be stable between when the version edit has been
# applied but the compaction has not completed, and when the compaction is
# finally complete.

scores wait-for-compaction=version-edit
----
L       Size   Score
L0  	0B     0.0
L1  	0B     0.0
L2  	0B     0.0
L3  	0B     0.0
L4  	0B     0.0
L5  	129KB  0.5
L6  	898KB  -

lsm
----
5:
  000005:[e#2,SET-e#2,SET]
6:
  000008:[a#0,SET-dd#0,SET]
  000007:[e#1,SET-e#1,SET]

resume-cleaning
----

scores wait-for-compaction=completion
----
L       Size   Score
L0  	0B     0.0
L1  	0B     0.0
L2  	0B     0.0
L3  	0B     0.0
L4  	0B     0.0
L5  	129KB  0.5
L6  	898KB  -
