1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//! Defines the physical binary layout of Parcode V4 files.
//!
//! This module specifies the on-disk representation of Parcode files, including the
//! global header, chunk structure, and metadata encoding. Understanding this format
//! is essential for implementing readers in other languages or debugging file corruption.
//!
//! ## File Format Overview (V4)
//!
//! Parcode V4 uses a "bottom-up" layout strategy where children are written before their
//! parents. This enables streaming writes and allows the root chunk to contain a complete
//! table of contents for the entire file.
//!
//! ### High-Level Structure
//!
//! ```text
//! ┌──────────────────────────────────┐
//! │ Chunk 0 (Leaf) │
//! ├──────────────────────────────────┤
//! │ Chunk 1 (Leaf) │
//! ├──────────────────────────────────┤
//! │ ... │
//! ├──────────────────────────────────┤
//! │ Chunk N (Parent) │
//! ├──────────────────────────────────┤
//! │ Root Chunk │
//! ├──────────────────────────────────┤
//! │ Global Header (26 bytes) │
//! └──────────────────────────────────┘
//! ```
//!
//! ## Chunk Anatomy
//!
//! Each chunk is self-contained and consists of three parts:
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────┐
//! │ Compressed Payload (Variable Length) │
//! │ - Contains the actual data (serialized with bincode) │
//! │ - May be compressed (LZ4, etc.) based on MetaByte │
//! ├─────────────────────────────────────────────────────────┤
//! │ Children Table (Optional, only if is_chunkable = true) │
//! │ - Array of ChildRef structures (16 bytes each) │
//! │ - Count stored as u32 LE (4 bytes) at the end │
//! ├─────────────────────────────────────────────────────────┤
//! │ MetaByte (1 byte) │
//! │ - Bit 0: is_chunkable (has children) │
//! │ - Bits 1-3: compression_method (0-7) │
//! │ - Bits 4-7: Reserved for future use │
//! └─────────────────────────────────────────────────────────┘
//! ```
//!
//! ### Reading a Chunk
//!
//! To read a chunk at offset `O` with length `L`:
//!
//! 1. Read the `MetaByte` at `O + L - 1`
//! 2. If `is_chunkable`, read the child count at `O + L - 5` (u32 LE)
//! 3. Read the children table (if present) working backwards from the count
//! 4. The payload starts at `O` and ends before the children table (or `MetaByte` if no children)
//! 5. Decompress the payload based on the compression method
//!
//! ## Global Header
//!
//! The global header is always located at the end of the file and has a fixed size of 26 bytes:
//!
//! ```text
//! Offset | Size | Field | Description
//! -------|------|---------------|----------------------------------------
//! 0 | 4 | magic | Magic bytes: "PAR4" (0x50 0x41 0x52 0x34)
//! 4 | 2 | version | Format version (u16 LE, currently 4)
//! 6 | 8 | root_offset | Absolute offset of root chunk (u64 LE)
//! 14 | 8 | root_length | Total length of root chunk (u64 LE)
//! 22 | 4 | checksum | Reserved for CRC32 (u32 LE, currently 0)
//! ```
//!
//! ## Design Rationale
//!
//! ### Why Bottom-Up Layout?
//!
//! - **Streaming Writes:** Children can be written as soon as they're ready, without
//! knowing the final file size
//! - **Parallel Execution:** Multiple threads can write chunks concurrently without
//! coordination beyond the sequential writer mutex
//! - **Self-Describing Root:** The root chunk contains all necessary metadata to
//! navigate the entire file
//!
//! ### Why `MetaByte` at the End?
//!
//! - **Backward Reading:** When navigating from a parent to children, we can read
//! the `MetaByte` first to determine the chunk structure
//! - **Alignment:** Placing metadata at the end avoids alignment issues with the payload
//!
//! ### Why Fixed-Size `ChildRef`?
//!
//! - **Random Access:** Fixed-size references enable O(1) indexing into the children array
//! - **Simplicity:** No need for variable-length encoding or delimiters
//!
//! ## Compatibility
//!
//! - **Endianness:** All multi-byte integers use little-endian encoding
//! - **Alignment:** No special alignment requirements (can be read from any offset)
//! - **Version Detection:** Readers should check the magic bytes and version before parsing
use crate;
/// Magic bytes identifying the file format: "PAR4".
pub const MAGIC_BYTES: = *b"PAR4";
/// The fixed size of the Global Header.
/// Magic(4) + Version(2) + RootOffset(8) + RootLength(8) + Checksum(4) = 26
pub const GLOBAL_HEADER_SIZE: usize = 26;
/// Configuration flags for a specific chunk, stored in the last byte.
;
/// Represents a reference to a child chunk stored within a parent chunk.
/// This allows the reader to locate dependencies without deserializing the payload.
/// The Global Header located at the very end of the file (Tail).
/// It points to the Root Chunk, which is the entry point for the graph.