Skip to main content

qualia_cli/ingest/
writer.rs

1use qualia_core_db::NQuin;
2use std::fs::File;
3use std::io::{BufWriter, Write}; // Using NQuin
4
5pub struct SuperBlockWriter {
6    writer: BufWriter<File>,
7    buffer: [NQuin; 850],
8    cursor: usize,
9    blocks_written: u32,
10}
11
12impl SuperBlockWriter {
13    /// Creates a new instance.
14    pub fn new(output_path: &std::path::Path) -> std::io::Result<Self> {
15        let file = File::create(output_path)?;
16        // Use a large system buffer to prevent constant OS context switches
17        let writer = BufWriter::with_capacity(1024 * 1024, file);
18
19        Ok(Self {
20            writer,
21            buffer: [NQuin::default(); 850],
22            cursor: 0,
23            blocks_written: 0,
24        })
25    }
26
27    #[inline(always)]
28    /// Pushes a record.
29    pub fn push(&mut self, quin: NQuin) -> std::io::Result<()> {
30        self.buffer[self.cursor] = quin;
31        self.cursor += 1;
32
33        if self.cursor == 850 {
34            self.flush_block()?;
35        }
36        Ok(())
37    }
38
39    /// Flushes a block.
40    pub fn flush_block(&mut self) -> std::io::Result<()> {
41        if self.cursor == 0 {
42            return Ok(());
43        }
44
45        // 1. Build the 160-byte Header
46        // [4 bytes Magic Number "Q42B"] + [4 bytes block index] + [152 bytes reserved/padding]
47        let mut header = [0u8; 160];
48        header[0..4].copy_from_slice(b"Q42B");
49        header[4..8].copy_from_slice(&self.blocks_written.to_le_bytes());
50
51        // 2. Write Header
52        self.writer.write_all(&header)?;
53
54        // 3. Write Data (Zero-cost slice cast via bytemuck)
55        // Note: If cursor < 850 (e.g. final block), we still write the full 850
56        // to maintain strict 40KB alignment, leaving trailing structs as Zero/Default.
57        let bytes: &[u8] = bytemuck::cast_slice(&self.buffer);
58        self.writer.write_all(bytes)?;
59
60        // 4. Reset Buffer
61        self.cursor = 0;
62        // Zero out buffer so leftover old records aren't written if this is the last block
63        self.buffer = [NQuin::default(); 850];
64        self.blocks_written += 1;
65
66        Ok(())
67    }
68}
69
70impl Drop for SuperBlockWriter {
71    fn drop(&mut self) {
72        // Ensure final partial block is flushed when the writer goes out of scope
73        let _ = self.flush_block();
74        let _ = self.writer.flush();
75    }
76}