filereducelib 0.1.0

High-performance, constant-memory compression library for structured data (JSONL) producing .fra archives.
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
use std::io::{self, Read, Seek, SeekFrom, Write};

use hashbrown::HashMap as HbHashMap;
use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use thiserror::Error;

/// Error type for the library
#[derive(Error, Debug)]
pub enum FileReduceError {
    #[error("IO Error: {0}")]
    Io(#[from] io::Error),
    #[error("Serialization Error: {0}")]
    Bincode(#[from] bincode::Error),
    #[error("JSON Parsing Error: {0}")]
    Json(#[from] serde_json::Error),
    #[error("Decompression Error: {0}")]
    Decompression(String),
    #[error("Format Error: Invalid Magic or Version")]
    FormatError,
    #[error("Seek Error: Record not found")]
    RecordNotFound,
}

pub type Result<T> = std::result::Result<T, FileReduceError>;

const MAGIC: [u8; 4] = [0x46, 0x52, 0x41, 0x02]; // "FRA" + 0x02 (Version 2)
const CHUNK_SIZE: usize = 1000; // Records per block

/// Represents the simplified value types for our binary format.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum BinaryValue {
    Null,
    Bool(bool),
    Int(i64),
    UInt(u64),
    Float(f64),
    String(String),
    Array(Vec<BinaryValue>),
    Object(Vec<(u16, BinaryValue)>), // Keys are compressed to u16 IDs
}

/// A block of compressed records.
/// In V2, we only serialize this, compress it, and write it as a frame.
#[derive(Serialize, Deserialize, Debug)]
struct BlockData {
    records: Vec<BinaryValue>,
}

/// The Header of the .fra file (Start of file)
#[derive(Serialize, Deserialize, Debug)]
pub struct FraHeader {
    pub magic: [u8; 4],
    pub version: u8,
}

/// The Index Map entry
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct IndexEntry {
    pub start_record_id: u64, // The global record ID (0-indexed) this block starts with
    pub byte_offset: u64,     // The absolute byte offset in the file where the block frame starts
}

/// The Footer of the .fra file (End of file)
/// Contains the complete Dictionary and the Index for Random Access.
#[derive(Serialize, Deserialize, Debug)]
pub struct FraFooter {
    pub final_dictionary: HbHashMap<u16, String>,
    pub index: Vec<IndexEntry>,
    pub total_records: u64,
}

/// Main Compressor Struct
pub struct FileReduceCompressor {
    dict: HbHashMap<String, u16>,
    next_id: u16,
    index: Vec<IndexEntry>,
    total_records: u64,
}

/// Main Decompressor Struct
pub struct FileReduceDecompressor {
    dict: HbHashMap<u16, String>,
    index: Vec<IndexEntry>,
    total_records: u64,
}

pub trait Compressor {
    fn compress<W: Write + Seek>(&mut self, input: impl Read, output: W) -> Result<()>;
}

pub trait Decompressor {
    fn decompress<R: Read + Seek, W: Write>(&mut self, input: R, output: W) -> Result<()>;
    fn seek_record<R: Read + Seek>(&mut self, input: &mut R, record_id: u64) -> Result<Value>;
}

impl FileReduceCompressor {
    pub fn new() -> Self {
        Self {
            dict: HbHashMap::new(),
            next_id: 1, // Start IDs at 1
            index: Vec::new(),
            total_records: 0,
        }
    }

    /// Process a chunk of JSON Values into BinaryValues and update local dictionary state.
    fn process_chunk(&mut self, chunk: Vec<Value>) -> Result<Vec<u8>> {
        // 1. Scan for Keys (Sequential or Parallel Reduce)
        let mut keys_to_add = Vec::new();

        // Use a simple scanner for the chunk
        for record in &chunk {
            self.scan_keys(record, &mut keys_to_add);
        }

        // Update Dictionary
        for key in keys_to_add {
            if !self.dict.contains_key(&key) {
                self.dict.insert(key, self.next_id);
                self.next_id += 1;
            }
        }

        // 2. Convert to Binary (Parallel)
        // We need a read-only view of the dict for Rayon
        let dict_ref = &self.dict;
        let binaries: Vec<BinaryValue> = chunk
            .par_iter()
            .map(|v| Self::json_to_binary(v, dict_ref))
            .collect();

        // 3. Serialize BlockData
        let block = BlockData { records: binaries };
        let serialized_block = bincode::serialize(&block)?;

        // 4. Compress Independently (Zstd Frame)
        let compressed_block = zstd::bulk::compress(&serialized_block, 3)?;

        Ok(compressed_block)
    }

    fn scan_keys(&self, v: &Value, sink: &mut Vec<String>) {
        match v {
            Value::Object(map) => {
                for (k, v) in map {
                    if !self.dict.contains_key(k) {
                        sink.push(k.clone());
                    }
                    self.scan_keys(v, sink);
                }
            }
            Value::Array(arr) => {
                for v in arr {
                    self.scan_keys(v, sink);
                }
            }
            _ => {}
        }
    }

    fn json_to_binary(v: &Value, dict: &HbHashMap<String, u16>) -> BinaryValue {
        match v {
            Value::Null => BinaryValue::Null,
            Value::Bool(b) => BinaryValue::Bool(*b),
            Value::Number(n) => {
                if let Some(i) = n.as_i64() {
                    BinaryValue::Int(i)
                } else if let Some(u) = n.as_u64() {
                    BinaryValue::UInt(u)
                } else {
                    BinaryValue::Float(n.as_f64().unwrap_or(0.0))
                }
            }
            Value::String(s) => BinaryValue::String(s.clone()),
            Value::Array(arr) => {
                BinaryValue::Array(arr.iter().map(|v| Self::json_to_binary(v, dict)).collect())
            }
            Value::Object(map) => {
                let fields = map
                    .iter()
                    .map(|(k, v)| {
                        let id = dict.get(k).copied().unwrap_or(0);
                        (id, Self::json_to_binary(v, dict))
                    })
                    .collect();
                BinaryValue::Object(fields)
            }
        }
    }
}

// Inherent methods to maintain compatibility and ease of use
impl FileReduceCompressor {
    pub fn compress<W: Write + Seek>(&mut self, input: impl Read, output: W) -> Result<()> {
        <Self as Compressor>::compress(self, input, output)
    }
}

impl Compressor for FileReduceCompressor {
    fn compress<W: Write + Seek>(&mut self, input: impl Read, mut output: W) -> Result<()> {
        // 1. Write Header
        let header = FraHeader {
            magic: MAGIC,
            version: 2,
        };
        bincode::serialize_into(&mut output, &header)?;
        let mut current_offset = output.stream_position()?;

        // 2. Stream Processing
        let deserializer = serde_json::Deserializer::from_reader(input);
        let iterator = deserializer.into_iter::<Value>();

        let mut chunk_buffer = Vec::with_capacity(CHUNK_SIZE);

        for item in iterator {
            let val = item?;
            chunk_buffer.push(val);

            if chunk_buffer.len() >= CHUNK_SIZE {
                // Process Chunk
                let compressed_data = self.process_chunk(chunk_buffer)?;
                let compressed_len = compressed_data.len() as u32;

                // Record Index
                self.index.push(IndexEntry {
                    start_record_id: self.total_records,
                    byte_offset: current_offset,
                });
                self.total_records += CHUNK_SIZE as u64;

                // Write Frame: [Length u32] [Data]
                output.write_all(&compressed_len.to_le_bytes())?;
                output.write_all(&compressed_data)?;

                current_offset = output.stream_position()?;
                chunk_buffer = Vec::with_capacity(CHUNK_SIZE);
            }
        }

        // Flush remaining
        if !chunk_buffer.is_empty() {
            let processed_count = chunk_buffer.len() as u64;
            let compressed_data = self.process_chunk(chunk_buffer)?;
            let compressed_len = compressed_data.len() as u32;

            self.index.push(IndexEntry {
                start_record_id: self.total_records,
                byte_offset: current_offset,
            });
            self.total_records += processed_count;

            output.write_all(&compressed_len.to_le_bytes())?;
            output.write_all(&compressed_data)?;
            current_offset = output.stream_position()?;
        }

        // 3. Write Footer
        let footer_start = current_offset;

        let mut final_dict_rev = HbHashMap::new();
        for (k, v) in &self.dict {
            final_dict_rev.insert(*v, k.clone());
        }

        let footer = FraFooter {
            final_dictionary: final_dict_rev,
            index: self.index.clone(),
            total_records: self.total_records,
        };

        let footer_bytes = bincode::serialize(&footer)?;
        output.write_all(&footer_bytes)?;

        // 4. Write Footer Metadata
        // [Footer Start Offset u64] [Magic u32]
        output.write_all(&footer_start.to_le_bytes())?;
        output.write_all(&MAGIC)?;

        Ok(())
    }
}

// Inherent methods for Decompressor
impl FileReduceDecompressor {
    pub fn new() -> Self {
        Self {
            dict: HbHashMap::new(),
            index: Vec::new(),
            total_records: 0,
        }
    }

    pub fn decompress<R: Read + Seek, W: Write>(&mut self, input: R, output: W) -> Result<()> {
        <Self as Decompressor>::decompress(self, input, output)
    }

    pub fn seek_record<R: Read + Seek>(&mut self, input: &mut R, record_id: u64) -> Result<Value> {
        <Self as Decompressor>::seek_record(self, input, record_id)
    }

    fn read_metadata<R: Read + Seek>(&mut self, reader: &mut R) -> Result<u64> {
        let file_len = reader.seek(SeekFrom::End(0))?;
        if file_len < 16 {
            return Err(FileReduceError::FormatError);
        }

        reader.seek(SeekFrom::End(-12))?;

        let mut buf_u64 = [0u8; 8];
        let mut buf_magic = [0u8; 4];

        reader.read_exact(&mut buf_u64)?;
        reader.read_exact(&mut buf_magic)?;

        if buf_magic != MAGIC {
            return Err(FileReduceError::FormatError);
        }

        let footer_start = u64::from_le_bytes(buf_u64);

        reader.seek(SeekFrom::Start(footer_start))?;
        let footer: FraFooter = bincode::deserialize_from(reader)?;

        self.dict = footer.final_dictionary;
        self.index = footer.index;
        self.total_records = footer.total_records;

        Ok(footer_start)
    }

    fn binary_to_json(&self, val: &BinaryValue) -> Value {
        match val {
            BinaryValue::Null => Value::Null,
            BinaryValue::Bool(b) => Value::Bool(*b),
            BinaryValue::Int(i) => Value::Number((*i).into()),
            BinaryValue::UInt(u) => Value::Number((*u).into()),
            BinaryValue::Float(f) => serde_json::Number::from_f64(*f)
                .map(Value::Number)
                .unwrap_or(Value::Null),
            BinaryValue::String(s) => Value::String(s.clone()),
            BinaryValue::Array(arr) => {
                Value::Array(arr.iter().map(|v| self.binary_to_json(v)).collect())
            }
            BinaryValue::Object(fields) => {
                let mut map = serde_json::Map::new();
                for (id, v) in fields {
                    if let Some(key) = self.dict.get(id) {
                        map.insert(key.clone(), self.binary_to_json(v));
                    }
                }
                Value::Object(map)
            }
        }
    }
}

impl Decompressor for FileReduceDecompressor {
    fn decompress<R: Read + Seek, W: Write>(&mut self, mut input: R, mut output: W) -> Result<()> {
        let footer_start = self.read_metadata(&mut input)?;

        input.seek(SeekFrom::Start(0))?;
        let header: FraHeader = bincode::deserialize_from(&mut input)?;
        if header.magic != MAGIC {
            return Err(FileReduceError::FormatError);
        }

        let mut current_pos = input.stream_position()?;

        while current_pos < footer_start {
            let mut len_buf = [0u8; 4];
            input.read_exact(&mut len_buf)?;
            let len = u32::from_le_bytes(len_buf) as usize;

            let mut compressed = vec![0u8; len];
            input.read_exact(&mut compressed)?;

            let decompressed = zstd::bulk::decompress(&compressed, 10_000_000)
                .map_err(|e| FileReduceError::Decompression(e.to_string()))?;

            let block: BlockData = bincode::deserialize(&decompressed)?;

            for record in block.records {
                let v = self.binary_to_json(&record);
                serde_json::to_writer(&mut output, &v)?;
                output.write_all(b"\n")?;
            }

            current_pos = input.stream_position()?;
        }

        Ok(())
    }

    fn seek_record<R: Read + Seek>(&mut self, input: &mut R, record_id: u64) -> Result<Value> {
        if self.index.is_empty() {
            self.read_metadata(input)?;
        }

        if record_id >= self.total_records {
            return Err(FileReduceError::RecordNotFound);
        }

        let block_idx = match self
            .index
            .binary_search_by_key(&record_id, |entry| entry.start_record_id)
        {
            Ok(idx) => idx,
            Err(idx) => idx - 1,
        };

        let entry = &self.index[block_idx];

        input.seek(SeekFrom::Start(entry.byte_offset))?;

        let mut len_buf = [0u8; 4];
        input.read_exact(&mut len_buf)?;
        let len = u32::from_le_bytes(len_buf) as usize;

        let mut compressed = vec![0u8; len];
        input.read_exact(&mut compressed)?;

        let decompressed = zstd::bulk::decompress(&compressed, 10_000_000)
            .map_err(|e| FileReduceError::Decompression(e.to_string()))?;

        let block: BlockData = bincode::deserialize(&decompressed)?;

        let relative_index = (record_id - entry.start_record_id) as usize;
        if relative_index >= block.records.len() {
            return Err(FileReduceError::RecordNotFound);
        }

        let record_bin = &block.records[relative_index];
        Ok(self.binary_to_json(record_bin))
    }
}

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

    #[test]
    fn test_compress_decompress_cycle() {
        let jsonl_input = r#"{"name": "Alice", "age": 30, "meta": {"score": 100}}
{"name": "Bob", "age": 25, "meta": {"score": 90}}
{"name": "Alice", "age": 31, "meta": {"score": 101}}"#;

        let mut compressor = FileReduceCompressor::new();
        let mut compressed_data = Cursor::new(Vec::new());

        compressor
            .compress(Cursor::new(jsonl_input), &mut compressed_data)
            .expect("Compression failed");

        let compressed_vec = compressed_data.into_inner();
        assert!(!compressed_vec.is_empty());
        println!("Compressed size: {}", compressed_vec.len());

        let mut decompressor = FileReduceDecompressor::new();
        let mut output_data = Vec::new();
        decompressor
            .decompress(Cursor::new(compressed_vec), &mut output_data)
            .expect("Decompression failed");

        let output_str = String::from_utf8(output_data).expect("Invalid UTF8");

        let input_lines: Vec<Value> = serde_json::Deserializer::from_str(jsonl_input)
            .into_iter::<Value>()
            .map(|x| x.unwrap())
            .collect();
        let output_lines: Vec<Value> = serde_json::Deserializer::from_str(&output_str)
            .into_iter::<Value>()
            .map(|x| x.unwrap())
            .collect();

        assert_eq!(input_lines, output_lines);
    }

    #[test]
    fn test_seek_random_access() {
        let mut jsonl_input = String::new();
        for i in 0..5000 {
            jsonl_input.push_str(&format!(r#"{{"id": {}, "data": "record_{}"}}"#, i, i));
            jsonl_input.push('\n');
        }

        let mut compressor = FileReduceCompressor::new();
        let mut compressed_data = Cursor::new(Vec::new());
        compressor
            .compress(Cursor::new(&jsonl_input), &mut compressed_data)
            .expect("Compression failed");

        let compressed_vec = compressed_data.into_inner();
        let mut decompressor = FileReduceDecompressor::new();
        let mut cursor = Cursor::new(compressed_vec);

        // Seek Record 2500 (middle)
        let record_val = decompressor
            .seek_record(&mut cursor, 2500)
            .expect("Seek failed");
        assert_eq!(record_val["id"], 2500);
        assert_eq!(record_val["data"], "record_2500");

        // Seek Record 0
        let record_val = decompressor
            .seek_record(&mut cursor, 0)
            .expect("Seek failed");
        assert_eq!(record_val["id"], 0);

        // Seek Last Record
        let record_val = decompressor
            .seek_record(&mut cursor, 4999)
            .expect("Seek failed");
        assert_eq!(record_val["id"], 4999);
    }
}