| 1 | # LTX file format (Version 3) — byte layout |
| 2 | |
| 3 | Derived by reading `reference/ltx-go` @ v0.5.1 (`ltx.go`, `checksum.go`, |
| 4 | `encoder.go`, `decoder.go`). This is the spec `src/ltx.rs` (T2) implements. |
| 5 | All multi-byte integers are **big-endian**. (Produced in T2; not a vendored file.) |
| 6 | |
| 7 | ## File structure |
| 8 | |
| 9 | ``` |
| 10 | +----------------------+ |
| 11 | | Header (100 bytes) | |
| 12 | +----------------------+ |
| 13 | | Page 0 | PageHeader(6) + LZ4-frame(page data) |
| 14 | | Page 1 | |
| 15 | | ... | |
| 16 | +----------------------+ |
| 17 | | Empty PageHeader (6) | pgno=0 — terminates the page block |
| 18 | +----------------------+ |
| 19 | | Page index | varint tuples + end marker + u64 size |
| 20 | +----------------------+ |
| 21 | | Trailer (16 bytes) | |
| 22 | +----------------------+ |
| 23 | ``` |
| 24 | |
| 25 | ## Header (100 bytes) |
| 26 | |
| 27 | | Offset | Size | Field | Notes | |
| 28 | |-------:|-----:|-------|-------| |
| 29 | | 0 | 4 | Magic | ASCII `"LTX1"` | |
| 30 | | 4 | 4 | Flags (u32) | only `HeaderFlagNoChecksum = 1<<1` is valid | |
| 31 | | 8 | 4 | PageSize (u32) | power of two, 512..=65536 | |
| 32 | | 12 | 4 | Commit (u32) | DB size after txn, in pages | |
| 33 | | 16 | 8 | MinTXID (u64) | | |
| 34 | | 24 | 8 | MaxTXID (u64) | | |
| 35 | | 32 | 8 | Timestamp (i64) | ms since unix epoch | |
| 36 | | 40 | 8 | PreApplyChecksum (u64) | 0 on snapshots (MinTXID==1) | |
| 37 | | 48 | 8 | WALOffset (i64) | 0 if journal | |
| 38 | | 56 | 8 | WALSize (i64) | 0 if journal | |
| 39 | | 64 | 4 | WALSalt1 (u32) | | |
| 40 | | 68 | 4 | WALSalt2 (u32) | | |
| 41 | | 72 | 8 | NodeID (u64) | | |
| 42 | | 80 | 20 | (reserved) | zero | |
| 43 | |
| 44 | `IsSnapshot() == (MinTXID == 1)`. Snapshots include all pages and have |
| 45 | `PreApplyChecksum == 0`. |
| 46 | |
| 47 | ## Page block |
| 48 | |
| 49 | Each page: `PageHeader` then the page data as **one independent LZ4 frame** |
| 50 | (pierrec/lz4 v4, 64 KiB block, "Fast" level). The block ends with an **empty |
| 51 | PageHeader** (`pgno == 0`). |
| 52 | |
| 53 | **PageHeader (6 bytes):** `Pgno (u32 @0)`, `Flags (u16 @4)` (must be 0). |
| 54 | |
| 55 | ## Page index |
| 56 | |
| 57 | After the empty page header, for each page in ascending pgno order: |
| 58 | `uvarint(pgno) ++ uvarint(offset) ++ uvarint(size)` where `offset` is the page's |
| 59 | byte offset from file start and `size = 6 + len(compressed page data)`. Terminated |
| 60 | by `uvarint(0)`, then a `u64` big-endian giving the byte length of the index |
| 61 | region (elements + terminator, **excluding** this size field). |
| 62 | |
| 63 | ## Trailer (16 bytes) |
| 64 | |
| 65 | `PostApplyChecksum (u64 @0)`, `FileChecksum (u64 @8)`. |
| 66 | |
| 67 | ## Checksums — CRC64-ISO with the ChecksumFlag |
| 68 | |
| 69 | - Hasher: **CRC64 / ISO polynomial `0xD800000000000000`**, matching Go |
| 70 | `crc64.MakeTable(crc64.ISO)` (reflected; init 0; per-update invert-process-invert). |
| 71 | - `ChecksumFlag = 1 << 63` is OR-ed into **every** stored checksum (so it is never 0). |
| 72 | - `ChecksumPage(pgno, data) = ChecksumFlag | CRC64( BE_u32(pgno) ++ data )`. |
| 73 | - Rolling DB checksum (post-apply): start `ChecksumFlag`, then for each non-lock |
| 74 | page `chksum = ChecksumFlag | (chksum ^ ChecksumPage(pgno, data))`. Verified |
| 75 | against the trailer's PostApplyChecksum **only for snapshots**. |
| 76 | - **FileChecksum** = `ChecksumFlag | CRC64(feed)` where `feed` is, in write order: |
| 77 | `header(100)` ++ for each page `[ pageheader(6) ++ DECOMPRESSED data ]` ++ |
| 78 | `empty pageheader(6)` ++ `page index region (incl. u64 size field)` ++ |
| 79 | `trailer[0..8]` (PostApplyChecksum). The page **data is fed uncompressed**, so a |
| 80 | decoder must decompress and re-feed — it cannot hash the raw file bytes. |
| 81 | |
| 82 | ## Filename |
| 83 | |
| 84 | `<minTXID:016x>-<maxTXID:016x>.ltx`, e.g. `0000000000000001-0000000000000001.ltx`. |