parx-rs 0.1.0

Parx format Rust library
Documentation
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
/*
 * Copyright 2026 PARX Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

use crate::error::{ParxError, Result};
use crate::format::{BundleHeader, Trailer, BUNDLE_HEADER_SIZE, BUNDLE_MAGIC, TRAILER_SIZE};
use crate::proto::{BundleEntry, ParxBundle};
use bytes::Bytes;
use prost::Message;
use std::collections::HashMap;
use std::time::{SystemTime, UNIX_EPOCH};

/// Default bundle filename.
pub const BUNDLE_FILENAME: &str = "_parx_bundle.parx";

/// Data for a single entry in the bundle.
#[derive(Debug, Clone)]
pub struct BundleEntryData {
    /// Relative path to the Parquet file.
    pub parquet_path: String,
    /// Source file size for staleness detection.
    pub source_size: u64,
    /// Raw footer bytes.
    pub footer_bytes: Bytes,
    /// Optional raw page index bytes.
    pub page_index_bytes: Bytes,
}

/// Writer for PARX bundle files.
///
/// Combines multiple Parquet file footers into a single bundle file.
#[derive(Debug)]
pub struct ParxBundleWriter {
    entries: Vec<BundleEntryData>,
}

impl ParxBundleWriter {
    /// Create a new bundle writer.
    pub const fn new() -> Self {
        Self {
            entries: Vec::new(),
        }
    }

    /// Add an entry to the bundle.
    pub fn add_entry(
        &mut self,
        parquet_path: &str,
        source_size: u64,
        footer_bytes: impl Into<Bytes>,
    ) {
        self.add_entry_with_page_indexes(parquet_path, source_size, footer_bytes, Bytes::new());
    }

    /// Add an entry to the bundle with optional page indexes.
    pub fn add_entry_with_page_indexes(
        &mut self,
        parquet_path: &str,
        source_size: u64,
        footer_bytes: impl Into<Bytes>,
        page_index_bytes: impl Into<Bytes>,
    ) {
        self.entries.push(BundleEntryData {
            parquet_path: parquet_path.to_string(),
            source_size,
            footer_bytes: footer_bytes.into(),
            page_index_bytes: page_index_bytes.into(),
        });
    }

    /// Get the number of entries in the bundle.
    #[inline]
    pub fn entry_count(&self) -> usize {
        self.entries.len()
    }

    /// Build the bundle file bytes
    ///
    /// # Panics
    /// Panics if manifest exceeds 4GB.
    pub fn finish(self) -> Vec<u8> {
        let header = BundleHeader::new(self.entries.len() as u64);
        let header_bytes = header.to_bytes();

        let mut current_offset = BUNDLE_HEADER_SIZE as u64;
        let mut payload = Vec::new();
        let mut bundle_entries = Vec::new();

        for entry in &self.entries {
            // Footer section
            let footer_offset = current_offset;
            let footer_length = entry.footer_bytes.len() as u64;
            let footer_checksum = crc32c::crc32c(&entry.footer_bytes).to_le_bytes().to_vec();

            payload.extend_from_slice(&entry.footer_bytes);
            current_offset += footer_length;

            // Optional page index section
            let (page_index_offset, page_index_length, page_index_checksum) =
                if entry.page_index_bytes.is_empty() {
                    (0, 0, Vec::new())
                } else {
                    let page_index_offset = current_offset;
                    let page_index_length = entry.page_index_bytes.len() as u64;
                    let page_index_checksum = crc32c::crc32c(&entry.page_index_bytes)
                        .to_le_bytes()
                        .to_vec();
                    payload.extend_from_slice(&entry.page_index_bytes);
                    current_offset += page_index_length;
                    (page_index_offset, page_index_length, page_index_checksum)
                };

            bundle_entries.push(BundleEntry {
                parquet_path: entry.parquet_path.clone(),
                source_size: entry.source_size,
                footer_offset,
                footer_length,
                footer_checksum,
                page_index_offset,
                page_index_length,
                page_index_checksum,
            });
        }

        // Get current timestamp (safe cast: won't overflow until year 2554)
        #[allow(clippy::cast_possible_truncation)]
        let created_at_ms = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_millis() as u64)
            .unwrap_or(0);

        // Build bundle manifest
        let bundle = ParxBundle {
            version: 1,
            created_at_ms,
            entries: bundle_entries,
        };

        let manifest_bytes = bundle.encode_to_vec();
        let manifest_crc = crc32c::crc32c(&manifest_bytes);

        // Build trailer with bundle magic
        let manifest_len = u32::try_from(manifest_bytes.len()).expect("manifest too large (>4GB)");
        let trailer = Trailer::new(manifest_len, manifest_crc, BUNDLE_MAGIC);
        let trailer_bytes = trailer.to_bytes();

        // Assemble file
        let total_size =
            header_bytes.len() + payload.len() + manifest_bytes.len() + trailer_bytes.len();

        let mut output = Vec::with_capacity(total_size);
        output.extend_from_slice(&header_bytes);
        output.extend_from_slice(&payload);
        output.extend_from_slice(&manifest_bytes);
        output.extend_from_slice(&trailer_bytes);

        output
    }
}

impl Default for ParxBundleWriter {
    fn default() -> Self {
        Self::new()
    }
}

/// Reference to a single entry in a bundle.
#[derive(Debug, Clone)]
pub struct BundleEntryRef<'a> {
    /// Relative path to the Parquet file.
    pub parquet_path: &'a str,
    /// Source file size.
    pub source_size: u64,
    /// Footer bytes.
    pub footer_bytes: &'a [u8],
    /// Optional page index bytes.
    pub page_index_bytes: Option<&'a [u8]>,
}

/// Reader for PARX bundle files.
///
/// Provides access to all cached footers in the bundle.
#[derive(Debug, Clone)]
pub struct ParxBundleReader {
    header: BundleHeader,
    bundle: ParxBundle,
    data: Bytes,
    /// Index for fast path lookup.
    path_index: HashMap<String, usize>,
}

impl ParxBundleReader {
    /// Open a bundle from bytes.
    ///
    /// # Errors
    /// Returns error if file is invalid or corrupted.
    ///
    /// # Panics
    /// Panics if file size is less than minimum header size.
    pub fn open(data: Bytes) -> Result<Self> {
        let file_size = data.len();
        let min_size = BUNDLE_HEADER_SIZE + TRAILER_SIZE;

        if file_size < min_size {
            return Err(ParxError::FileTooSmall {
                size: file_size,
                minimum: min_size,
            });
        }

        // Parse header
        let header_bytes: [u8; BUNDLE_HEADER_SIZE] = data[..BUNDLE_HEADER_SIZE]
            .try_into()
            .expect("header slice length verified above");
        let header = BundleHeader::from_bytes(&header_bytes);

        // Validate header magic
        if !header.is_magic_valid() {
            return Err(ParxError::InvalidBundleMagic(header.magic));
        }

        // Validate version
        if !header.is_version_supported() {
            return Err(ParxError::UnsupportedVersion {
                major: header.version_major,
                minor: header.version_minor,
            });
        }

        // Parse trailer
        let trailer_bytes: [u8; TRAILER_SIZE] = data[file_size - TRAILER_SIZE..]
            .try_into()
            .expect("trailer slice length verified above");
        let trailer = Trailer::from_bytes(&trailer_bytes);

        // Validate trailer magic
        if !trailer.is_magic_valid(BUNDLE_MAGIC) {
            return Err(ParxError::InvalidBundleMagic(trailer.magic));
        }

        // Extract and validate manifest
        let manifest_end = file_size - TRAILER_SIZE;
        let manifest_start = manifest_end
            .checked_sub(trailer.manifest_len as usize)
            .ok_or(ParxError::FileTooSmall {
                size: file_size,
                minimum: min_size + trailer.manifest_len as usize,
            })?;

        let manifest_bytes = &data[manifest_start..manifest_end];

        // Verify manifest CRC
        let actual_crc = crc32c::crc32c(manifest_bytes);
        if actual_crc != trailer.manifest_crc32c {
            return Err(ParxError::ManifestChecksumMismatch {
                expected: trailer.manifest_crc32c,
                actual: actual_crc,
            });
        }

        // Decode bundle manifest
        let bundle = ParxBundle::decode(manifest_bytes)?;

        // Build path index
        let path_index: HashMap<String, usize> = bundle
            .entries
            .iter()
            .enumerate()
            .map(|(i, e)| (e.parquet_path.clone(), i))
            .collect();

        Ok(Self {
            header,
            bundle,
            data,
            path_index,
        })
    }

    /// Get the bundle header.
    #[inline]
    pub const fn header(&self) -> &BundleHeader {
        &self.header
    }

    /// Get the number of entries in the bundle.
    #[inline]
    pub fn entry_count(&self) -> usize {
        self.bundle.entries.len()
    }

    /// Get the creation timestamp in milliseconds.
    #[inline]
    pub const fn created_at_ms(&self) -> u64 {
        self.bundle.created_at_ms
    }

    /// Check if a parquet path exists in the bundle.
    #[inline]
    pub fn contains(&self, parquet_path: &str) -> bool {
        self.path_index.contains_key(parquet_path)
    }

    /// Get the list of parquet paths in this bundle.
    pub fn parquet_paths(&self) -> Vec<&str> {
        self.bundle
            .entries
            .iter()
            .map(|e| e.parquet_path.as_str())
            .collect()
    }

    /// Get footer bytes for a specific parquet file.
    pub fn get_footer(&self, parquet_path: &str) -> Option<&[u8]> {
        let idx = *self.path_index.get(parquet_path)?;
        let entry = &self.bundle.entries[idx];

        let start = usize::try_from(entry.footer_offset).ok()?;
        let length = usize::try_from(entry.footer_length).ok()?;
        let end = start
            .checked_add(length)
            .filter(|&end| end <= self.data.len())?;

        Some(&self.data[start..end])
    }

    /// Get source size for a specific parquet file.
    pub fn get_source_size(&self, parquet_path: &str) -> Option<u64> {
        let idx = *self.path_index.get(parquet_path)?;
        Some(self.bundle.entries[idx].source_size)
    }

    /// Validate that a parquet file matches the expected size.
    pub fn validate_source_size(&self, parquet_path: &str, actual_size: u64) -> bool {
        self.get_source_size(parquet_path)
            .is_some_and(|expected| expected == actual_size)
    }

    /// Get a full entry reference for a parquet file.
    pub fn get_entry(&self, parquet_path: &str) -> Option<BundleEntryRef<'_>> {
        let idx = *self.path_index.get(parquet_path)?;
        let entry = &self.bundle.entries[idx];

        let footer_start = usize::try_from(entry.footer_offset).ok()?;
        let footer_length = usize::try_from(entry.footer_length).ok()?;
        let footer_end = footer_start
            .checked_add(footer_length)
            .filter(|&end| end <= self.data.len())?;

        let footer_bytes = &self.data[footer_start..footer_end];
        let page_index_bytes = self.resolve_page_index_bytes(entry)?;

        Some(BundleEntryRef {
            parquet_path: &entry.parquet_path,
            source_size: entry.source_size,
            footer_bytes,
            page_index_bytes,
        })
    }

    /// Iterate over all entries in the bundle.
    pub fn iter_entries(&self) -> impl Iterator<Item = BundleEntryRef<'_>> {
        self.bundle.entries.iter().filter_map(|entry| {
            let footer_start = usize::try_from(entry.footer_offset).ok()?;
            let footer_length = usize::try_from(entry.footer_length).ok()?;
            let footer_end = footer_start
                .checked_add(footer_length)
                .filter(|&end| end <= self.data.len())?;

            let footer_bytes = &self.data[footer_start..footer_end];
            let page_index_bytes = self.resolve_page_index_bytes(entry)?;

            Some(BundleEntryRef {
                parquet_path: &entry.parquet_path,
                source_size: entry.source_size,
                footer_bytes,
                page_index_bytes,
            })
        })
    }

    fn resolve_page_index_bytes<'a>(&'a self, entry: &BundleEntry) -> Option<Option<&'a [u8]>> {
        if entry.page_index_length == 0 {
            return Some(None);
        }

        let start = usize::try_from(entry.page_index_offset).ok()?;
        let length = usize::try_from(entry.page_index_length).ok()?;
        let end = start
            .checked_add(length)
            .filter(|&end| end <= self.data.len())?;
        Some(Some(&self.data[start..end]))
    }

    /// Validate all entry checksums.
    ///
    /// # Errors
    /// Returns error if any checksum fails or bounds are invalid.
    pub fn validate_all(&self) -> Result<()> {
        for entry in &self.bundle.entries {
            // Validate footer checksum
            let footer_start = usize::try_from(entry.footer_offset).map_err(|_| {
                ParxError::InvalidPayloadBounds {
                    offset: entry.footer_offset,
                    length: entry.footer_length,
                    file_size: self.data.len() as u64,
                }
            })?;
            let footer_length = usize::try_from(entry.footer_length).map_err(|_| {
                ParxError::InvalidPayloadBounds {
                    offset: entry.footer_offset,
                    length: entry.footer_length,
                    file_size: self.data.len() as u64,
                }
            })?;
            let footer_end = footer_start.checked_add(footer_length).ok_or_else(|| {
                ParxError::InvalidPayloadBounds {
                    offset: entry.footer_offset,
                    length: entry.footer_length,
                    file_size: self.data.len() as u64,
                }
            })?;

            if footer_end > self.data.len() {
                return Err(ParxError::InvalidPayloadBounds {
                    offset: entry.footer_offset,
                    length: entry.footer_length,
                    file_size: self.data.len() as u64,
                });
            }

            let footer_bytes = &self.data[footer_start..footer_end];
            let footer_crc = crc32c::crc32c(footer_bytes);

            if footer_crc.to_le_bytes().as_slice() != entry.footer_checksum.as_slice() {
                return Err(ParxError::FooterChecksumMismatch);
            }

            // Validate optional page index checksum
            if entry.page_index_length > 0 {
                let page_index_start = usize::try_from(entry.page_index_offset).map_err(|_| {
                    ParxError::InvalidPayloadBounds {
                        offset: entry.page_index_offset,
                        length: entry.page_index_length,
                        file_size: self.data.len() as u64,
                    }
                })?;
                let page_index_length = usize::try_from(entry.page_index_length).map_err(|_| {
                    ParxError::InvalidPayloadBounds {
                        offset: entry.page_index_offset,
                        length: entry.page_index_length,
                        file_size: self.data.len() as u64,
                    }
                })?;
                let page_index_end =
                    page_index_start
                        .checked_add(page_index_length)
                        .ok_or_else(|| ParxError::InvalidPayloadBounds {
                            offset: entry.page_index_offset,
                            length: entry.page_index_length,
                            file_size: self.data.len() as u64,
                        })?;

                if page_index_end > self.data.len() {
                    return Err(ParxError::InvalidPayloadBounds {
                        offset: entry.page_index_offset,
                        length: entry.page_index_length,
                        file_size: self.data.len() as u64,
                    });
                }

                let page_index_bytes = &self.data[page_index_start..page_index_end];
                let page_index_crc = crc32c::crc32c(page_index_bytes);
                if page_index_crc.to_le_bytes().as_slice() != entry.page_index_checksum.as_slice() {
                    return Err(ParxError::PageIndexChecksumMismatch);
                }
            }
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_bundle_roundtrip() {
        let mut writer = ParxBundleWriter::new();
        writer.add_entry("part-00000.parquet", 1000, b"footer0".to_vec());
        writer.add_entry("part-00001.parquet", 2000, b"footer1".to_vec());
        writer.add_entry("part-00002.parquet", 3000, b"footer2".to_vec());

        let bundle_bytes = writer.finish();
        let reader = ParxBundleReader::open(Bytes::from(bundle_bytes)).expect("failed to open");

        assert_eq!(reader.entry_count(), 3);
        assert!(reader.contains("part-00000.parquet"));
        assert!(reader.contains("part-00001.parquet"));
        assert!(reader.contains("part-00002.parquet"));
        assert!(!reader.contains("nonexistent.parquet"));

        assert_eq!(
            reader.get_footer("part-00000.parquet"),
            Some(b"footer0".as_slice())
        );
        assert_eq!(
            reader.get_footer("part-00001.parquet"),
            Some(b"footer1".as_slice())
        );
        assert_eq!(
            reader.get_footer("part-00002.parquet"),
            Some(b"footer2".as_slice())
        );

        assert_eq!(reader.get_source_size("part-00000.parquet"), Some(1000));
        assert_eq!(reader.get_source_size("part-00001.parquet"), Some(2000));

        assert!(reader.validate_source_size("part-00000.parquet", 1000));
        assert!(!reader.validate_source_size("part-00000.parquet", 9999));
    }

    #[test]
    fn test_bundle_iter_entries() {
        let mut writer = ParxBundleWriter::new();
        writer.add_entry("a.parquet", 100, b"fa".to_vec());
        writer.add_entry("b.parquet", 200, b"fb".to_vec());

        let bundle_bytes = writer.finish();
        let reader = ParxBundleReader::open(Bytes::from(bundle_bytes)).expect("failed to open");

        let entries: Vec<_> = reader.iter_entries().collect();
        assert_eq!(entries.len(), 2);
        assert_eq!(entries[0].parquet_path, "a.parquet");
        assert_eq!(entries[0].footer_bytes, b"fa");
        assert!(entries[0].page_index_bytes.is_none());
        assert_eq!(entries[1].parquet_path, "b.parquet");
        assert_eq!(entries[1].footer_bytes, b"fb");
        assert!(entries[1].page_index_bytes.is_none());
    }

    #[test]
    fn test_bundle_validate_all() {
        let mut writer = ParxBundleWriter::new();
        writer.add_entry("test.parquet", 1000, b"footer".to_vec());

        let bundle_bytes = writer.finish();
        let reader = ParxBundleReader::open(Bytes::from(bundle_bytes)).expect("failed to open");

        // Should pass validation
        reader.validate_all().expect("validation should pass");
    }

    #[test]
    fn test_bundle_parquet_paths() {
        let mut writer = ParxBundleWriter::new();
        writer.add_entry("z.parquet", 100, b"f".to_vec());
        writer.add_entry("a.parquet", 200, b"f".to_vec());
        writer.add_entry("m.parquet", 300, b"f".to_vec());

        let bundle_bytes = writer.finish();
        let reader = ParxBundleReader::open(Bytes::from(bundle_bytes)).expect("failed to open");

        let paths = reader.parquet_paths();
        assert_eq!(paths, vec!["z.parquet", "a.parquet", "m.parquet"]);
    }

    #[test]
    fn test_invalid_bundle_magic() {
        let mut data = vec![0u8; 100];
        data[0..4].copy_from_slice(b"NOPE");

        let result = ParxBundleReader::open(Bytes::from(data));
        assert!(matches!(result, Err(ParxError::InvalidBundleMagic(_))));
    }

    #[test]
    fn test_bundle_manifest_crc_mismatch() {
        let mut writer = ParxBundleWriter::new();
        writer.add_entry("test.parquet", 1000, b"footer".to_vec());

        let mut bundle_bytes = writer.finish();
        // Corrupt manifest CRC (bytes -8 to -5 from end)
        let len = bundle_bytes.len();
        bundle_bytes[len - 8] ^= 0xFF;

        let result = ParxBundleReader::open(Bytes::from(bundle_bytes));
        assert!(matches!(
            result,
            Err(ParxError::ManifestChecksumMismatch { .. })
        ));
    }

    #[test]
    fn test_bundle_reader_handles_normal_case() {
        // Create a simple valid bundle as baseline
        let mut writer = ParxBundleWriter::new();

        // Add a normal entry
        let footer = vec![1, 2, 3, 4];
        writer.add_entry("test.parquet", 1000, footer.clone());

        let bundle_bytes = writer.finish();

        // Should open and read successfully
        let reader = ParxBundleReader::open(bundle_bytes.into()).expect("Should open valid bundle");
        let retrieved = reader
            .get_footer("test.parquet")
            .expect("Should find footer");
        assert_eq!(retrieved, &footer[..]);
    }

    #[test]
    fn test_bundle_with_page_indexes_roundtrip() {
        let mut writer = ParxBundleWriter::new();
        writer.add_entry_with_page_indexes(
            "test.parquet",
            1000,
            b"footer".to_vec(),
            b"pi".to_vec(),
        );

        let bundle_bytes = writer.finish();
        let reader = ParxBundleReader::open(bundle_bytes.into()).expect("Should open valid bundle");
        let entry = reader
            .get_entry("test.parquet")
            .expect("Should find bundle entry");

        assert_eq!(entry.footer_bytes, b"footer");
        assert_eq!(entry.page_index_bytes, Some(b"pi".as_slice()));
        reader.validate_all().expect("validation should pass");
    }
}