Diffinery

homeflare/celldpublic

celld/crates/ltx/reference/ltx-format.md
93 lines · 3.7 KBRaw History
1# LTX file format (Version 3) — byte layout
2
3Derived from `superfly/ltx` v0.5.2 (`ltx.go`, `checksum.go`, `encoder.go`, and
4`decoder.go`). The implementation is in `src/ltx.rs`. All multi-byte integers
5are **big-endian**.
6
7## File structure
8
9```
10+----------------------+
11| Header (100 bytes) |
12+----------------------+
13| Page 0 | PageHeader(6) + Size(4) + LZ4-block(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
49Each v0.5.2 page contains a `PageHeader`, a four-byte compressed size, and one
50independent raw LZ4 block. The writer uses the fast compressor from
51`pierrec/lz4` v4.1.23. The block ends with an **empty PageHeader**
52(`pgno == 0`).
53
54**PageHeader (6 bytes):** `Pgno (u32 @0)`, `Flags (u16 @4)`. The
55`PageHeaderFlagSize` bit (`1<<0`) marks the size-prefixed block representation.
56No other flag is valid.
57
58Files written through LTX v0.5.1 have a zero page flag and one independent LZ4
59frame after each page header. A v0.5.2 decoder accepts both representations,
60so existing Litestream objects remain readable.
61
62## Page index
63
64After the empty page header, for each page in ascending pgno order:
65`uvarint(pgno) ++ uvarint(offset) ++ uvarint(size)` where `offset` is the page's
66byte offset from the file start. A v0.5.2 page uses
67`size = 6 + 4 + len(compressed page data)`. A legacy page omits the four-byte
68size. The index ends with `uvarint(0)` and a big-endian `u64` index length. The
69length includes the elements and terminator, but it excludes its own field.
70
71## Trailer (16 bytes)
72
73`PostApplyChecksum (u64 @0)`, `FileChecksum (u64 @8)`.
74
75## Checksums — CRC64-ISO with the ChecksumFlag
76
77- Hasher: **CRC64 / ISO polynomial `0xD800000000000000`**, matching Go
78 `crc64.MakeTable(crc64.ISO)` (reflected; init 0; per-update invert-process-invert).
79- `ChecksumFlag = 1 << 63` is OR-ed into **every** stored checksum (so it is never 0).
80- `ChecksumPage(pgno, data) = ChecksumFlag | CRC64( BE_u32(pgno) ++ data )`.
81- Rolling DB checksum (post-apply): start `ChecksumFlag`, then for each non-lock
82 page `chksum = ChecksumFlag | (chksum ^ ChecksumPage(pgno, data))`. Verified
83 against the trailer's PostApplyChecksum **only for snapshots**.
84- **FileChecksum** = `ChecksumFlag | CRC64(feed)`. The v0.5.2 feed contains the
85 header and each page header. It also contains each compressed-size field and
86 the corresponding uncompressed page data. It then contains the empty page
87 header, the page index with its size field, and the post-apply checksum. The
88 compressed page bytes are not part of the feed. A legacy feed omits each
89 compressed-size field.
90
91## Filename
92
93`<minTXID:016x>-<maxTXID:016x>.ltx`, e.g. `0000000000000001-0000000000000001.ltx`.