Tombstones cannot live forever, but dropping one too early resurrects a deleted key. Today you let compaction discard a tombstone only when it is compacting the oldest data, where nothing older can survive it.
Drop tombstones during a bottom-level compaction, but keep them otherwise.
A tombstone’s job is to hide older values, so it must outlive every value it hides. If compaction drops a tombstone while some older SSTable it didn’t include still holds that key, the old value resurrects - a deleted key comes back from the dead, one of the classic LSM bugs. So a tombstone can only be discarded when the compaction is processing the oldest data in the store, where no older table survives to leak the value.
That is why compaction takes a drop-tombstones flag: set it only for a bottom-level compaction that includes the oldest tables. There, once newest-wins has collapsed a key to a tombstone, both the tombstone and the value beneath it can be thrown away - the key is genuinely, permanently gone and its space reclaimed. Everywhere else the tombstone is preserved so it keeps doing its shadowing job. This flag is the safety valve the leveling in the next lesson will set correctly.
func Compact(inputs []*SSTable, outPath string, dropTombstones bool) error {// after newest-wins collapse: if the surviving record is a// tombstone AND dropTombstones is true, skip writing it.// only safe when NO older table (outside this compaction) could// still hold that key - i.e. the bottom level.}