
build ext0 table-format=Pebble,v2
set a 1
set l 2
----

ingest ext0
----

lsm
----
L6:
  000004:[a#10,SET-l#10,SET]


batch
set d foo
set f bar
----

flush
----

lsm
----
L0.0:
  000006:[d#11,SET-f#12,SET]
L6:
  000004:[a#10,SET-l#10,SET]

excise-dryrun c k
----
would excise 2 files, use ingest-and-excise to excise.
  del-table:     L0 000006
  del-table:     L6 000004
  add-table:     L6 000007(000004):[a#10,SET-a#10,SET] seqnums:[10-10] points:[a#10,SET-a#10,SET] size:33(654)
  add-table:     L6 000008(000004):[l#10,SET-l#10,SET] seqnums:[10-10] points:[l#10,SET-l#10,SET] size:33(654)
  add-backing:   000004


excise-dryrun a e
----
would excise 2 files, use ingest-and-excise to excise.
  del-table:     L0 000006
  del-table:     L6 000004
  add-table:     L0 000009(000006):[f#12,SET-f#12,SET] seqnums:[11-12] points:[f#12,SET-f#12,SET] size:45(696)
  add-table:     L6 000010(000004):[l#10,SET-l#10,SET] seqnums:[10-10] points:[l#10,SET-l#10,SET] size:33(654)
  add-backing:   000006
  add-backing:   000004

excise-dryrun e z
----
would excise 2 files, use ingest-and-excise to excise.
  del-table:     L0 000006
  del-table:     L6 000004
  add-table:     L0 000011(000006):[d#11,SET-d#11,SET] seqnums:[11-12] points:[d#11,SET-d#11,SET] size:45(696)
  add-table:     L6 000012(000004):[a#10,SET-a#10,SET] seqnums:[10-10] points:[a#10,SET-a#10,SET] size:33(654)
  add-backing:   000006
  add-backing:   000004

excise-dryrun f l
----
would excise 2 files, use ingest-and-excise to excise.
  del-table:     L0 000006
  del-table:     L6 000004
  add-table:     L0 000013(000006):[d#11,SET-d#11,SET] seqnums:[11-12] points:[d#11,SET-d#11,SET] size:45(696)
  add-table:     L6 000014(000004):[a#10,SET-a#10,SET] seqnums:[10-10] points:[a#10,SET-a#10,SET] size:33(654)
  add-table:     L6 000015(000004):[l#10,SET-l#10,SET] seqnums:[10-10] points:[l#10,SET-l#10,SET] size:33(654)
  add-backing:   000006
  add-backing:   000004

excise-dryrun f ll
----
would excise 2 files, use ingest-and-excise to excise.
  del-table:     L0 000006
  del-table:     L6 000004
  add-table:     L0 000016(000006):[d#11,SET-d#11,SET] seqnums:[11-12] points:[d#11,SET-d#11,SET] size:45(696)
  add-table:     L6 000017(000004):[a#10,SET-a#10,SET] seqnums:[10-10] points:[a#10,SET-a#10,SET] size:33(654)
  add-backing:   000006
  add-backing:   000004

excise-dryrun p q
----
would excise 0 files, use ingest-and-excise to excise.

lsm
----
L0.0:
  000006:[d#11,SET-f#12,SET]
L6:
  000004:[a#10,SET-l#10,SET]

iter
first
next
next
next
next
----
a: (1, .)
d: (foo, .)
f: (bar, .)
l: (2, .)
.

build ext1 table-format=Pebble,v2
set d foo3
set e bar2
----

ingest-and-excise ext1 excise=c-k
----

lsm
----
L6:
  000019(000004):[a#10,SET-a#10,SET]
  000018:[d#14,SET-e#14,SET]
  000020(000004):[l#10,SET-l#10,SET]

iter
first
next
next
next
next
----
a: (1, .)
d: (foo3, .)
e: (bar2, .)
l: (2, .)
.

# More complex cases, with the truncation of file bounds happening at rangedel
# and rangekey bounds.

reset
----

build ext3 table-format=Pebble,v2
range-key-set c f @4 foobar
----

ingest ext3
----

build ext4 table-format=Pebble,v2
set b bar
del-range g i
----

ingest ext4
----

lsm
----
L0.0:
  000005:[b#11,SET-i#inf,RANGEDEL]
L6:
  000004:[c#10,RANGEKEYSET-f#inf,RANGEKEYSET]

excise-dryrun f g
----
would excise 1 files, use ingest-and-excise to excise.
  del-table:     L0 000005
  add-table:     L0 000006(000005):[b#11,SET-b#11,SET] seqnums:[11-11] points:[b#11,SET-b#11,SET] size:28(702)
  add-table:     L0 000007(000005):[g#11,RANGEDEL-i#inf,RANGEDEL] seqnums:[11-11] points:[g#11,RANGEDEL-i#inf,RANGEDEL] size:1(702)
  add-backing:   000005

excise-dryrun b c
----
would excise 1 files, use ingest-and-excise to excise.
  del-table:     L0 000005
  add-table:     L0 000008(000005):[g#11,RANGEDEL-i#inf,RANGEDEL] seqnums:[11-11] points:[g#11,RANGEDEL-i#inf,RANGEDEL] size:1(702)
  add-backing:   000005

excise-dryrun i j
----
would excise 0 files, use ingest-and-excise to excise.

# Excise mid range key. This will not happen in practice, but excise()
# supports it.

excise-dryrun c d
----
would excise 2 files, use ingest-and-excise to excise.
  del-table:     L0 000005
  del-table:     L6 000004
  add-table:     L0 000009(000005):[b#11,SET-b#11,SET] seqnums:[11-11] points:[b#11,SET-b#11,SET] size:28(702)
  add-table:     L0 000010(000005):[g#11,RANGEDEL-i#inf,RANGEDEL] seqnums:[11-11] points:[g#11,RANGEDEL-i#inf,RANGEDEL] size:1(702)
  add-table:     L6 000011(000004):[d#10,RANGEKEYSET-f#inf,RANGEKEYSET] seqnums:[10-10] ranges:[d#10,RANGEKEYSET-f#inf,RANGEKEYSET] size:1(760)
  add-backing:   000005
  add-backing:   000004

reset
----

# Create an sstable with a range key set.
batch
set a a
set b b
set d d
range-key-set e ee @1 foo
----

flush
----

lsm
----
L0.0:
  000005:[a#10,SET-ee#inf,RANGEKEYSET]

build ext2
set z z
----

ingest-and-excise ext2 excise=b-c
----

lsm
----
L0.0:
  000007(000005):[a#10,SET-a#10,SET]
  000008(000005):[d#12,SET-ee#inf,RANGEKEYSET]
L6:
  000006:[z#15,SET-z#15,SET]

# Regression test for https://github.com/cockroachdb/pebble/v2/issues/2947.
reset
----

batch
set a a
set b b
set c c
set d d
set e e
set f f
set g g
set h h
set i i
set j j
----

flush
----

lsm
----
L0.0:
  000005:[a#10,SET-j#19,SET]

build ext2
set z z
----

ingest-and-excise ext2 excise=d-e
----

lsm
----
L0.0:
  000007(000005):[a#10,SET-c#12,SET]
  000008(000005):[e#14,SET-j#19,SET]
L6:
  000006:[z#21,SET-z#21,SET]

build ext3
set zz zz
----

ingest-and-excise ext3 excise=g-h
----

# 7, 10, 11 should have the same file backing struct.
lsm
----
L0.0:
  000007(000005):[a#10,SET-c#12,SET]
  000010(000005):[e#14,SET-f#15,SET]
  000011(000005):[h#17,SET-j#19,SET]
L6:
  000006:[z#21,SET-z#21,SET]
  000009:[zz#23,SET-zz#23,SET]

confirm-backing 7 10 11
----
file backings are the same

reopen
----

# 7, 10, 11 should still have the same file backing struct even after manifest
# replay.
lsm
----
L0.0:
  000007(000005):[a#10,SET-c#12,SET]
  000010(000005):[e#14,SET-f#15,SET]
  000011(000005):[h#17,SET-j#19,SET]
L6:
  000006:[z#21,SET-z#21,SET]
  000009:[zz#23,SET-zz#23,SET]

confirm-backing 7 10 11
----
file backings are the same

# Excise one boundary, the file backing should still be set.
reset
----

batch
set a a
set b b
set c c
set d d
set e e
----

flush
----

lsm
----
L0.0:
  000005:[a#10,SET-e#14,SET]

build ext2
set z z
----

ingest-and-excise ext2 excise=d-f
----

lsm
----
L0.0:
  000007(000005):[a#10,SET-c#12,SET]
L6:
  000006:[z#16,SET-z#16,SET]

reopen
----

lsm
----
L0.0:
  000007(000005):[a#10,SET-c#12,SET]
L6:
  000006:[z#16,SET-z#16,SET]

# Regression test for #3128. A key at the upper bound of a virtual sstable
# should not be skipped in reverse iteration with range key masking enabled.
# bbsomethinglong@4 is not covered by the range key [bd-f) (which gets truncated
# into a [cc-f) with an excise) and so should not be range key masked in both
# iteration directions at the bottom.

reset tiny-blocks
----

batch
set a@3 foo
merge b@5 bar
merge bbsomethinglong@4 bazz
merge bg@3 something
merge d@6 baz
merge z something
----

flush
----

batch
set c foobar
----

flush
----

lsm
----
L0.1:
  000007:[c#16,SET-c#16,SET]
L0.0:
  000005:[a@3#10,SET-z#15,MERGE]

compact a z
----

build ext4
range-key-set bd f @7 foo
merge f something
----

ingest ext4
----

lsm
----
L0.0:
  000009:[bd#17,RANGEKEYSET-f#17,MERGE]
L6:
  000008:[a@3#0,SET-z#0,SET]

build ext3
set z updated
----

ingest-and-excise ext3 excise=bd-cc
----

lsm
----
L0.0:
  000011(000009):[cc#17,RANGEKEYSET-f#17,MERGE]
  000010:[z#19,SET-z#19,SET]
L6:
  000012(000008):[a@3#0,SET-bbsomethinglong@4#0,SET]
  000013(000008):[d@6#0,SET-z#0,SET]

iter range-key-masking=@10
first
next
next
next
next
----
a@3: (foo, .)
b@5: (bar, .)
bbsomethinglong@4: (bazz, .)
cc: (., [cc-f) @7=foo UPDATED)
f: (something, . UPDATED)


iter range-key-masking=@10
set-bounds lower=a upper=f
seek-lt f
prev
prev
----
.
cc: (., [cc-f) @7=foo UPDATED)
bbsomethinglong@4: (bazz, . UPDATED)
b@5: (bar, .)

# Regression test for #3236. Lower bounds are enforced in virtualLastSeekLE()
# in the case where the actual virtual sstable last key is invisible due to
# being obsolete. We create an EFOS to ensure that we create a virtual
# sstable consisting only of obsolete keys with an excise, then do an iterator
# operation that does a virtualLastSeekLE() on it.

reset
----

batch
set a foo
set b bar
----

batch
set d@6 baz
----

flush
----

compact a z
----

batch
set d@6 something
----

flush
----

batch
set x something
----

file-only-snapshot s1
a z
----
ok

build ext7
del d@6
----

ingest ext7
----

lsm
----
L0.1:
  000008:[d@6#15,DEL-d@6#15,DEL]
L0.0:
  000007:[d@6#13,SET-d@6#13,SET]
L6:
  000005:[a#10,SET-d@6#12,SET]

compact c e
----

lsm
----
L6:
  000009:[a#0,SET-d@6#0,SET]

build ext5
set c something
----

ingest-and-excise ext5 excise=c-cc
----
flushable ingest

lsm
----
L0.0:
  000013:[x#14,SET-x#14,SET]
L6:
  000014(000009):[a#0,SET-b#0,SET]
  000010:[c#17,SET-c#17,SET]
  000015(000009):[d@6#15,DEL-d@6#0,SET]

iter lower=c upper=e
last
prev
prev
seek-lt dd
prev
prev
----
c: (something, .)
.
.
c: (something, .)
.
.


# Test to verify that IngestAndExcise now uses flushableIngest.

reset
----

batch
set a foo
set b bar
----

batch
set d@6 baz
----

flush
----

compact a z
----

batch
set d@6 something
set g something
----

flush
----

lsm
----
L0.0:
  000007:[d@6#13,SET-g#14,SET]
L6:
  000005:[a#10,SET-d@6#12,SET]

batch
set x something
----

file-only-snapshot s1
a z
----
ok

lsm
----
L0.0:
  000007:[d@6#13,SET-g#14,SET]
L6:
  000005:[a#10,SET-d@6#12,SET]

build ext7
del d@6
----

ingest ext7
----

lsm
----
L0.1:
  000008:[d@6#16,DEL-d@6#16,DEL]
L0.0:
  000007:[d@6#13,SET-g#14,SET]
L6:
  000005:[a#10,SET-d@6#12,SET]

compact c e
----

lsm
----
L6:
  000009:[a#0,SET-g#0,SET]

build ext5
set c something
set b something
set f something
del b-e
----

ingest-and-excise ext5 excise=b-e
----
flushable ingest

lsm
----
L0.0:
  000010:[b#18,SET-f#18,SET]
  000013:[x#15,SET-x#15,SET]
L6:
  000014(000009):[a#0,SET-a#0,SET]
  000015(000009):[g#0,SET-g#0,SET]

iter lower=c upper=e
last
prev
prev
seek-lt dd
prev
prev
----
c: (something, .)
.
.
c: (something, .)
.
.

batch
set a old
set aa old
----

flush
----

compact a z
----

lsm
----
L6:
  000018:[a#0,SET-x#0,SET]

batch commit
----

build ext5
set a outside-span
----

build ext6
set b somethingElse
set c somethingElse
set f somethingElse
del b-e
----

ingest-and-excise ext5 ext6 excise=b-e
----

lsm
----
L0.0:
  000019:[a#22,SET-a#22,SET]
  000020:[b#23,SET-f#23,SET]
L6:
  000021(000018):[a#0,SET-aa#0,SET]
  000022(000018):[f#0,SET-x#0,SET]

iter lower=a upper=f
first
next
next
next
next
next
----
a: (outside-span, .)
aa: (old, .)
b: (somethingElse, .)
c: (somethingElse, .)
.
.

excise aa ab
----

lsm
----
L0.0:
  000019:[a#22,SET-a#22,SET]
  000020:[b#23,SET-f#23,SET]
L6:
  000023(000018):[a#0,SET-a#0,SET]
  000022(000018):[f#0,SET-x#0,SET]

excise g h
----

lsm
----
L0.0:
  000019:[a#22,SET-a#22,SET]
  000020:[b#23,SET-f#23,SET]
L6:
  000023(000018):[a#0,SET-a#0,SET]
  000024(000018):[f#0,SET-f#0,SET]
  000025(000018):[x#0,SET-x#0,SET]

# We set the block size to prevent randomization of block sizes (which affects
# the size).
build-remote remote1 block-size=1024 index-block-size=1024
g#0,SET = foo
h#0,SET = bar
i#0,SET = foobar
----

ingest-external
remote1 bounds=(g,j)
----

lsm
----
L0.0:
  000019:[a#22,SET-a#22,SET]
  000020:[b#23,SET-f#23,SET]
L6:
  000023(000018):[a#0,SET-a#0,SET]
  000024(000018):[f#0,SET-f#0,SET]
  000026(000026):[g#26,DELSIZED-j#inf,RANGEDEL]
  000025(000018):[x#0,SET-x#0,SET]

# Non-local tables are excised with loose bounds.
excise ga ha
----

lsm verbose
----
L0.0:
  000019:[a#22,SET-a#22,SET] seqnums:[22-22] points:[a#22,SET-a#22,SET] size:688
  000020:[b#23,SET-f#23,SET] seqnums:[23-23] points:[b#23,SET-f#23,SET] size:743
L6:
  000023(000018):[a#0,SET-a#0,SET] seqnums:[0-0] points:[a#0,SET-a#0,SET] size:71(721)
  000024(000018):[f#0,SET-f#0,SET] seqnums:[0-0] points:[f#0,SET-f#0,SET] size:71(721)
  000027(000026):[g#26,DELSIZED-ga#inf,RANGEDEL] seqnums:[26-26] points:[g#26,DELSIZED-ga#inf,RANGEDEL] size:350(700)
  000028(000026):[ha#0,DELSIZED-j#inf,RANGEDEL] seqnums:[26-26] points:[ha#0,DELSIZED-j#inf,RANGEDEL] size:350(700)
  000025(000018):[x#0,SET-x#0,SET] seqnums:[0-0] points:[x#0,SET-x#0,SET] size:71(721)

# Non-local tables are excised without data access.

build-remote remote2
u#0,SET = foo
v#0,SET = bar
w#0,SET = foobar
----

ingest-external
remote1 bounds=(u,w)
----

move-remote-object remote2 remote2-moved
----
remote2 -> remote2-moved

excise uk vk
----

lsm verbose
----
L0.0:
  000019:[a#22,SET-a#22,SET] seqnums:[22-22] points:[a#22,SET-a#22,SET] size:688
  000020:[b#23,SET-f#23,SET] seqnums:[23-23] points:[b#23,SET-f#23,SET] size:743
L6:
  000023(000018):[a#0,SET-a#0,SET] seqnums:[0-0] points:[a#0,SET-a#0,SET] size:71(721)
  000024(000018):[f#0,SET-f#0,SET] seqnums:[0-0] points:[f#0,SET-f#0,SET] size:71(721)
  000027(000026):[g#26,DELSIZED-ga#inf,RANGEDEL] seqnums:[26-26] points:[g#26,DELSIZED-ga#inf,RANGEDEL] size:350(700)
  000028(000026):[ha#0,DELSIZED-j#inf,RANGEDEL] seqnums:[26-26] points:[ha#0,DELSIZED-j#inf,RANGEDEL] size:350(700)
  000030(000026):[u#28,DELSIZED-uk#inf,RANGEDEL] seqnums:[28-28] points:[u#28,DELSIZED-uk#inf,RANGEDEL] size:350(700)
  000031(000026):[vk#0,DELSIZED-w#inf,RANGEDEL] seqnums:[28-28] points:[vk#0,DELSIZED-w#inf,RANGEDEL] size:350(700)
  000025(000018):[x#0,SET-x#0,SET] seqnums:[0-0] points:[x#0,SET-x#0,SET] size:71(721)

excise a z
----

lsm
----

# Two overlapping ingestions wait on one another even if
# the overlap is only on the excise span.

reset
----

batch
set a foo
set b bar
set bb neverseen
set c baz
----

build ext7
set b foo
set c bar
----

ingest-and-excise ext7 excise=b-g no-wait
----

build ext8
set d gee
set e fee
----

ingest ext8 no-wait
----

iter
first
next
next
next
next
next
----
a: (foo, .)
b: (foo, .)
c: (bar, .)
d: (gee, .)
e: (fee, .)
.

flush
----


iter
first
next
next
next
next
next
----
a: (foo, .)
b: (foo, .)
c: (bar, .)
d: (gee, .)
e: (fee, .)
.

# Regression test for #3963. Ensure that flushable ingests with excises
# correctly compute boundary overlaps with eventually file-only snapshots.

reset
----

batch
set a foo
set b bar
set c baz
set e something
----

flush
----

block-flush
----

batch
set b foobar
set d barbaz
----

build ext8
set d foobar
set e somethingelse
----

ingest-and-excise ext8 excise=b-g no-wait
----

file-only-snapshot s1
b c
----
ok

batch
set b invisible
----

allow-flush
----

flush
----

# This iterator should not return any keys.

iter snapshot=s1 lower=b upper=c
first
next
next
----
.
.
.
