Compaction -- Cleaning Up
What is Compaction?
After a while, your data can get messy. You've got: - Tombstones (crossed-out data waiting to be cleaned up) - Expired columns (sticky notes whose time has run out)
Compaction is like going through your messy notebook and rewriting everything on a fresh page, leaving out all the crossed-out stuff and expired sticky notes.
Why Compact?
- Save space -- Tombstones and expired data take up room
- Speed up reads -- Less clutter means faster lookups
- Keep things tidy -- A clean database is a happy database
from pycoldb import ColumnFamily, Column
from pycoldb.compaction import compact_column_family
grades = ColumnFamily(name="grades")
# Add some data
grades.put("alice", Column(name="math", value="A+"))
grades.put("alice", Column(name="science", value="A"))
grades.put("bob", Column(name="math", value="B"))
# Delete some data (creates tombstones)
grades.delete_column("alice", "science")
grades.delete_column("bob", "math")
# Compact! Returns how many entries were cleaned up
cleaned = compact_column_family(grades)
print(f"Cleaned up {cleaned} entries") # Cleaned up 2 entries
Compaction Never Changes an Answer
Compaction is tidying, not editing. Whatever a read returned just before
compaction, it returns just after. That's why a row whose grades have all
been deleted or have expired already counts as missing before compaction:
get_row raises RowNotFoundError, exactly as it will once compaction has
thrown the empty record card away.
When Does Compaction Happen?
In real databases like Cassandra, compaction runs automatically in the background. In our educational version, you call it manually whenever you want to clean up. This lets you see exactly what's happening and learn how it works!