Skip to content

Storage format of one LSM tree, format 2

A tree lives in one directory:

file content
CURRENT the name of the live manifest, followed by a newline
MANIFEST-<seq:08> log of edit batches
wal-<seq:08>.log write-ahead log of the memtable generation seq
t-<id:08>.sst an immutable table
LOCK the store lock: the process id of the process that holds the tree open, as decimal text and a newline

All integers are little-endian unless stated otherwise. Checksums are CRC-32C (Castagnoli) of the bytes as stored. lp is a length-prefixed byte string: a LEB128 varint length followed by the bytes.

Framing (log and manifest)

record = crc:u32  len:u32  payload:len bytes     ; crc covers len and payload

Reading stops at the first record that does not check out. If no valid record follows it, it is a torn write of something never acknowledged and the file is cut there; if a valid record follows, the file is corrupt and opening fails.

Write-ahead log record payload

payload = ts:u64  count:varint  op*
op      = 0x01 key:lp value:lp     ; put
        | 0x00 key:lp              ; delete (tombstone)

One record is one atomically committed batch with version ts. A commit is acknowledged after its record is synced.

Manifest edit batch payload

payload = count:varint edit*
edit = 0x01 level:u8 id:varint size:varint entries:varint
            smallest:lp smallest_ts:u64 largest:lp largest_ts:u64 max_ts:u64   ; add table
     | 0x02 level:u8 id:varint                                              ; remove table
     | 0x03 seq:varint     ; logs below seq are fully contained in tables
     | 0x04 id:varint      ; next table id
     | 0x05 ts:u64         ; highest version stored in tables
     | 0x06 ts:u64         ; GC watermark
     | 0x07 level:u8 key:lp ; compaction pointer of a level

A batch is applied atomically. A new manifest starts with a batch describing the whole state; CURRENT is replaced by writing CURRENT.tmp and renaming it.

Table

table  = data_block* filter_partition* index_partition* top_index footer   (format 3)
table  = data_block* filter_partition* index_block footer                  (format 2, read only)

Entries are ordered by key ascending, then version descending.

Data block

entry*  restart:u32*  restart_count:u32  crc:u32
entry   = shared:varint unshared:varint vlen:varint ts:u64 key_suffix:unshared bytes value

shared bytes of the key are taken from the previous entry's key (0 at restart points). A writer puts a restart point every 8 entries (every 16 before 2026-12-03); a reader takes the restart points from the restart array, so it reads any interval. vlen = 0 is a tombstone; otherwise the value has vlen - 1 bytes. The CRC covers everything before it.

Filter partition (one per data block): a Bloom filter over the distinct user keys of that block, bits ‖ k:u8 ‖ crc:u32. Key hash: FNV-1a 64 of the key followed by the SplitMix64 finalizer; probe i of k sets bit (h1 + i·h2) mod nbits with h1 the low and h2 = (high | 1) the high 32 bits. A key's version at or below a read version, if there is one, is in the block holding the first entry at or after (key, read version), so that block's partition answers a point lookup.

Index entry (one per data block): last_key:lp last_ts:u64 offset:varint length:varint filter_offset:varint filter_length:varint.

Index partition (format 3): consecutive index entries, closed when it reaches 4096 bytes or at the last block, then entry_offset:u32 per entry (from the partition's start), count:u32 and crc:u32 over everything before it. Offsets allow a binary search without decoding the partition.

Top-level index (format 3): per index partition last_key:lp last_ts:u64 offset:varint length:varint first_block:varint (the last entry of the partition, where the partition is, the number of its first data block), followed by crc:u32. Only the top level is kept in memory; index partitions (at the filters' cache priority), blocks and filter partitions are read through the node's shared cache. A lookup binary-searches the top level, then the partition.

Index block (format 2): every index entry, followed by crc:u32, all kept in memory.

Footer (64 bytes): index_off:u64 index_len:u64 blocks:u64 0:u64 entries:u64 max_ts:u64 magic:u64 crc:u32 padding; index_* locate the top-level index (format 2: the index block, and blocks is 0); magic is "HDBTBL03" (format 2: "HDBTBL02") read as a little-endian u64 (the magic keeps the spelling it was defined with; it names the format, not the product); the CRC covers the first 56 bytes. Format 3 is written; both are read.

The bytes of a table depend only on its entries and the block size and filter options.

Store lock

A tree has one opener at a time, across processes and within one: a DL/I program's library, a server, an operations command. An opener takes an exclusive advisory lock of the operating system on LOCK (created when missing; flock(2) LOCK_EX | LOCK_NB on Unix) before it reads any other file of the directory, writes its process id into the file, and holds the lock until it closes the tree. The operating system releases the lock when the file is closed, so a process that ends without closing (killed, crashed) leaves no lock behind: a LOCK file without a lock on it is stale and the next opener takes it.

An opener that finds the lock held does not open the tree and fails at once with an error that names the directory and, when LOCK holds one, the holder's process id. It does not wait and does not retry. The lock is advisory: it keeps out OliviaDB processes, not other programs that write the directory's files. File systems on which flock does not exclude other hosts (some network file systems) do not give this guarantee across hosts.

Units of work across trees

A DL/I system keeps its databases in one or more trees: the system's own tree (its directory, which also holds the catalog) and a tree per data set a host maps a database to. A unit of work that changed more than one tree commits with the records below, so that after a crash at any point either every tree has the unit's writes or none has. A unit of work that changed one tree commits as one write-ahead log record of that tree and uses none of them.

Both records are ordinary entries in the catalog namespace, the key prefix 00 00 (two zero bytes) that no database uses. txid is 24 ASCII characters: the commit's wall-clock time in milliseconds since 1970 as 16 lowercase hexadecimal digits, then a sequence number of the system as 8 lowercase hexadecimal digits (unique among the units of work of one system).

record tree key value
marker each tree the unit changed 00 00 xtx: txid the undo list
decision the system's tree 00 00 xdc: txid c (one byte, 0x63)

The undo list holds, for each key the unit writes in that tree, the value the key had before the unit (its before-image). Unlike the rest of this document its integers are big-endian:

undo  = count:u32be  entry*
entry = klen:u32be  key:klen bytes  0x00                       ; the key did not exist
      | klen:u32be  key:klen bytes  0x01  vlen:u32be  value:vlen bytes

Commit. The steps, each a durable commit of one tree (one write-ahead log record):

  1. For each tree in turn: the unit's writes to that tree and its marker, together. If a tree's commit fails, the trees already done are rolled back from their undo lists (their writes undone and their markers removed in one commit each), and the unit fails.
  2. The decision, in the system's tree. The unit of work is committed when this record is durable.
  3. Each tree's marker is removed.
  4. The decision is removed.

Recovery. When a tree is opened, every marker in it is looked at. If the system's tree holds the decision with the same txid, the marker is removed: the writes stand. If it does not, the tree is rolled back from the marker's undo list: every key in the list gets its before-image (or is deleted when it had none) and the marker is removed, in one commit. A decision whose markers are all gone (a crash between steps 3 and 4) stays in the system's tree; it is never read again and does no harm.

Readers. The keys of a marker and a decision are not database keys: a reader of the databases never sees them. Between steps 1 and 2 another program reading a tree that is already prepared would see the unit's writes; DL/I program isolation prevents that, because the unit holds the locks on everything it wrote until it has committed.