zipora 2.1.5

High-performance Rust implementation providing advanced data structures and compression algorithms with memory safety guarantees. Features LRU page cache, sophisticated caching layer, fiber-based concurrency, real-time compression, secure memory pools, SIMD optimizations, and complete C FFI for migration from C++.
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
//! SimpleZipBlobStore - Fragment-based compression with deduplication
//!
//! # Overview
//!
//! SimpleZipBlobStore provides efficient compression for variable-length records that
//! share common substrings. It fragments records at delimiter boundaries and deduplicates
//! fragments in a shared string pool.
//!
//! # Algorithm
//!
//! 1. **Fragmentation**: Split each record into fragments based on delimiter characters
//!    - Fragments have minimum and maximum length constraints
//!    - Split preferentially at delimiter boundaries (newlines, spaces, tabs)
//!    - If no delimiter found, split at maximum length
//!
//! 2. **Deduplication**: Store unique fragments in a shared string pool
//!    - HashMap-based deduplication during build
//!    - Fragments referenced by (offset, length) pairs
//!
//! 3. **Encoding**: Store record boundaries and fragment references
//!    - `records`: UintVecMin0 storing fragment indices for each record
//!    - `off_len`: ZipIntVec storing packed (offset << len_bits | length)
//!    - `strpool`: Raw fragment data
//!
//! # Example
//!
//! ```rust
//! use zipora::blob_store::{BlobStore, SimpleZipBlobStore, SimpleZipConfig};
//!
//! let data = vec![
//!     b"Hello World\n".to_vec(),
//!     b"Hello Rust\n".to_vec(),
//!     b"World Peace\n".to_vec(),
//! ];
//!
//! let config = SimpleZipConfig::default();
//! let store = SimpleZipBlobStore::build_from(&data, &config).unwrap();
//!
//! // "Hello " and "World\n" fragments shared
//! assert_eq!(store.get(0).unwrap(), b"Hello World\n");
//! assert_eq!(store.get(1).unwrap(), b"Hello Rust\n");
//! # Ok::<(), zipora::error::ZiporaError>(())
//! ```
//!
//! # Use Cases
//!
//! - Log files with repeated prefixes/timestamps
//! - JSON/XML records with common keys
//! - CSV rows with repeated column values
//! - Any text data with high substring overlap
//!
//! # Performance Characteristics
//!
//! - **Build Time**: O(N * M) where N = records, M = avg fragments per record
//! - **Query Time**: O(F) where F = fragments per record
//! - **Space**: Excellent for data with high fragment reuse (50-90% compression typical)
//! - **Read-Only**: No dynamic updates after build

use crate::containers::UintVecMin0;
use crate::blob_store::traits::{BlobStore, BatchBlobStore, IterableBlobStore, BlobStoreStats};
use crate::error::{Result, ZiporaError};
use crate::RecordId;
use std::collections::HashMap;

/// Configuration for SimpleZipBlobStore fragmentation
#[derive(Debug, Clone)]
pub struct SimpleZipConfig {
    /// Minimum fragment length in bytes (default: 8)
    pub min_frag_len: usize,
    /// Maximum fragment length in bytes (default: 256)
    pub max_frag_len: usize,
    /// Delimiter characters for fragment boundaries (default: \n\r\t space)
    pub delimiters: Vec<u8>,
}

impl Default for SimpleZipConfig {
    fn default() -> Self {
        Self {
            min_frag_len: 8,
            max_frag_len: 256,
            delimiters: vec![b'\n', b'\r', b'\t', b' '],
        }
    }
}

impl SimpleZipConfig {
    /// Create a builder (returns Self with Default values for fluent configuration).
    pub fn builder() -> Self { Self::default() }
    /// Set minimum fragment length.
    pub fn min_frag_len(mut self, v: usize) -> Self { self.min_frag_len = v; self }
    /// Set maximum fragment length.
    pub fn max_frag_len(mut self, v: usize) -> Self { self.max_frag_len = v; self }
    /// Set delimiters.
    pub fn delimiters(mut self, v: Vec<u8>) -> Self { self.delimiters = v; self }
    /// Finalize configuration.
    pub fn build(self) -> Result<Self> { self.validate()?; Ok(self) }

    /// Validate configuration parameters
    pub fn validate(&self) -> Result<()> {
        if self.min_frag_len == 0 {
            return Err(ZiporaError::invalid_parameter("min_frag_len must be > 0"));
        }
        if self.max_frag_len < self.min_frag_len {
            return Err(ZiporaError::invalid_parameter(
                "max_frag_len must be >= min_frag_len"
            ));
        }
        if self.max_frag_len > 1024 * 1024 {
            return Err(ZiporaError::invalid_parameter(
                "max_frag_len must be <= 1MB (fragmentation efficiency)"
            ));
        }
        Ok(())
    }
}
/// SimpleZipBlobStore - Fragment-based compression with deduplication
///
/// Read-only blob store optimized for records with shared substrings.
pub struct SimpleZipBlobStore {
    /// Deduplicated string pool storing all unique fragments
    strpool: Vec<u8>,
    /// Packed (offset << len_bits | length) for each fragment reference.
    /// Packed offset|length pairs for each fragment.
    off_len: Vec<u64>,
    /// Number of bits used for length field in packed off_len
    len_bits: u32,
    /// Record boundaries: records[i]..records[i+1] = fragment range for record i
    records: UintVecMin0,
    /// Number of records stored
    num_records: usize,
    /// Total uncompressed size in bytes
    unzip_size: usize,
    /// Statistics
    stats: BlobStoreStats,
}

impl SimpleZipBlobStore {
    /// Build from data with configuration
    ///
    /// # Arguments
    ///
    /// * `data` - Records to compress
    /// * `config` - Fragmentation configuration
    ///
    /// # Example
    ///
    /// ```rust
    /// use zipora::blob_store::{SimpleZipBlobStore, SimpleZipConfig};
    ///
    /// let data = vec![b"Hello World".to_vec(), b"Hello Rust".to_vec()];
    /// let config = SimpleZipConfig::default();
    /// let store = SimpleZipBlobStore::build_from(&data, &config).unwrap();
    /// # Ok::<(), zipora::error::ZiporaError>(())
    /// ```
    pub fn build_from(data: &[Vec<u8>], config: &SimpleZipConfig) -> Result<Self> {
        config.validate()?;

        if data.is_empty() {
            return Ok(Self::default());
        }

        let mut unzip_size = 0;
        let mut all_fragments = Vec::new();
        let mut record_boundaries = vec![0usize];

        // Step 1: Fragment each record
        for record in data {
            unzip_size += record.len();
            let record_frags = Self::fragment_record(record, config);
            all_fragments.extend(record_frags.into_iter().map(|s| s.to_vec()));
            record_boundaries.push(all_fragments.len());
        }

        // Step 2: Build deduplicated string pool and collect offsets/lengths
        let (strpool, offsets, lengths) = Self::build_strpool(&all_fragments)?;

        // Step 3: Pack offset|length into single u64
        let max_len = lengths.iter().copied().max().unwrap_or(0);
        let len_bits = if max_len == 0 { 0 } else { (usize::BITS - max_len.leading_zeros()) as u32 };
        let off_len: Vec<u64> = offsets.iter().zip(lengths.iter())
            .map(|(&offset, &length)| ((offset as u64) << len_bits) | (length as u64))
            .collect();

        // Step 4: Build record boundaries
        let records = UintVecMin0::build_from_usize(&record_boundaries).0;

        let mut stats = BlobStoreStats::default();
        stats.blob_count = data.len();
        stats.total_size = unzip_size;
        stats.average_size = if data.is_empty() {
            0.0
        } else {
            unzip_size as f64 / data.len() as f64
        };

        Ok(Self {
            strpool,
            off_len,
            len_bits,
            records,
            num_records: data.len(),
            unzip_size,
            stats,
        })
    }

    /// Fragment a record into chunks at delimiter boundaries
    ///
    /// Follows C++ algorithm from simple_zip_blob_store.cpp lines 86-98
    fn fragment_record<'a>(record: &'a [u8], config: &SimpleZipConfig) -> Vec<&'a [u8]> {
        let mut fragments = Vec::new();
        let mut pos = 0;

        while pos < record.len() {
            let max_end = (pos + config.max_frag_len).min(record.len());
            let min_end = (pos + config.min_frag_len).min(record.len());

            // Find delimiter boundary between min_end and max_end
            let mut end = min_end;
            for i in min_end..max_end {
                if config.delimiters.contains(&record[i]) {
                    end = i + 1; // Include delimiter
                    break;
                }
            }

            // If no delimiter found, use max_end
            if end == min_end {
                end = max_end;
            }

            fragments.push(&record[pos..end]);
            pos = end;
        }

        fragments
    }

    /// Build deduplicated string pool
    ///
    /// Returns: (strpool, offsets, lengths)
    fn build_strpool(fragments: &[Vec<u8>]) -> Result<(Vec<u8>, Vec<usize>, Vec<usize>)> {
        let mut strpool = Vec::new();
        let mut frag_map: HashMap<Vec<u8>, usize> = HashMap::new();
        let mut offsets = Vec::new();
        let mut lengths = Vec::new();

        for frag in fragments {
            // Get or insert fragment in pool
            let offset = *frag_map.entry(frag.clone()).or_insert_with(|| {
                let offset = strpool.len();
                strpool.extend_from_slice(frag);
                offset
            });

            offsets.push(offset);
            lengths.push(frag.len());
        }

        Ok((strpool, offsets, lengths))
    }

    /// Get record by reassembling fragments.
    ///
    /// Fast path for record retrieval:
    /// pre-compute data pointer, bit width, and mask, then loop with
    /// shift+mask to unpack each (offset, length) pair.
    fn get_record_append_imp(&self, rec_id: usize, rec_data: &mut Vec<u8>) -> Result<()> {
        if rec_id >= self.num_records {
            return Err(ZiporaError::not_found(format!(
                "Record {} not found (max {})", rec_id, self.num_records - 1
            )));
        }

        let beg = self.records.get(rec_id);
        let end = self.records.get(rec_id + 1);

        // Pre-compute constants for hot loop
        let strpool = self.strpool.as_ptr();
        let ol_data = self.off_len.as_ptr();
        let len_bits = self.len_bits;
        let len_mask = (1u64 << len_bits) - 1;

        for i in beg..end {
            // SAFETY: i is in bounds [beg, end) which are from valid UintVecMin0 entries
            let packed = unsafe { *ol_data.add(i) };
            let offset = (packed >> len_bits) as usize;
            let length = (packed & len_mask) as usize;

            // SAFETY: offset and length were packed from valid strpool indices during build
            debug_assert!(offset + length <= self.strpool.len());
            unsafe {
                let src = strpool.add(offset);
                rec_data.extend_from_slice(std::slice::from_raw_parts(src, length));
            }
        }

        Ok(())
    }

    /// Get memory usage statistics.
    pub fn memory_stats(&self) -> MemoryStats {
        let off_len_size = self.off_len.len() * std::mem::size_of::<u64>();
        let metadata_size = off_len_size + self.records.mem_size();

        MemoryStats {
            strpool_size: self.strpool.len(),
            off_len_size,
            records_size: self.records.mem_size(),
            total_size: self.strpool.len() + metadata_size,
            uncompressed_size: self.unzip_size,
            compression_ratio: if self.unzip_size > 0 {
                (self.strpool.len() + metadata_size) as f64 / self.unzip_size as f64
            } else {
                1.0
            },
        }
    }

    /// Get number of fragment references.
    pub fn num_unique_fragments(&self) -> usize {
        self.off_len.len()
    }
}

impl Default for SimpleZipBlobStore {
    fn default() -> Self {
        Self {
            strpool: Vec::new(),
            off_len: Vec::new(),
            len_bits: 0,
            records: UintVecMin0::new_empty(),
            num_records: 0,
            unzip_size: 0,
            stats: BlobStoreStats::default(),
        }
    }
}

impl BlobStore for SimpleZipBlobStore {
    fn get(&self, id: RecordId) -> Result<Vec<u8>> {
        let mut data = Vec::new();
        self.get_record_append_imp(id as usize, &mut data)?;
        Ok(data)
    }

    fn put(&mut self, _data: &[u8]) -> Result<RecordId> {
        Err(ZiporaError::not_supported(
            "SimpleZipBlobStore is read-only after build"
        ))
    }

    fn remove(&mut self, _id: RecordId) -> Result<()> {
        Err(ZiporaError::not_supported(
            "SimpleZipBlobStore is read-only"
        ))
    }

    fn contains(&self, id: RecordId) -> bool {
        (id as usize) < self.num_records
    }

    fn size(&self, id: RecordId) -> Result<Option<usize>> {
        if id as usize >= self.num_records {
            return Ok(None);
        }

        // Calculate size by decompressing (could be optimized with size cache)
        let mut data = Vec::new();
        self.get_record_append_imp(id as usize, &mut data)?;
        Ok(Some(data.len()))
    }

    fn len(&self) -> usize {
        self.num_records
    }

    fn stats(&self) -> BlobStoreStats {
        self.stats.clone()
    }
}

impl BatchBlobStore for SimpleZipBlobStore {
    fn put_batch<I>(&mut self, _blobs: I) -> Result<Vec<RecordId>>
    where
        I: IntoIterator<Item = Vec<u8>>,
    {
        Err(ZiporaError::not_supported(
            "SimpleZipBlobStore is read-only"
        ))
    }

    fn get_batch<I>(&self, ids: I) -> Result<Vec<Option<Vec<u8>>>>
    where
        I: IntoIterator<Item = RecordId>,
    {
        ids.into_iter()
            .map(|id| {
                if self.contains(id) {
                    self.get(id).map(Some)
                } else {
                    Ok(None)
                }
            })
            .collect()
    }

    fn remove_batch<I>(&mut self, _ids: I) -> Result<usize>
    where
        I: IntoIterator<Item = RecordId>,
    {
        Err(ZiporaError::not_supported(
            "SimpleZipBlobStore is read-only"
        ))
    }
}

impl IterableBlobStore for SimpleZipBlobStore {
    type IdIter = std::ops::Range<RecordId>;

    fn iter_ids(&self) -> Self::IdIter {
        0..self.num_records as RecordId
    }
}

/// Memory usage statistics for SimpleZipBlobStore
#[derive(Debug, Clone)]
pub struct MemoryStats {
    /// Size of deduplicated string pool in bytes
    pub strpool_size: usize,
    /// Size of offset-length array in bytes
    pub off_len_size: usize,
    /// Size of record boundaries array in bytes
    pub records_size: usize,
    /// Total compressed size in bytes
    pub total_size: usize,
    /// Total uncompressed size in bytes
    pub uncompressed_size: usize,
    /// Compression ratio (compressed / uncompressed)
    pub compression_ratio: f64,
}

impl MemoryStats {
    /// Calculate space saved as percentage
    pub fn space_saved_percent(&self) -> f64 {
        (1.0 - self.compression_ratio) * 100.0
    }

    /// Calculate overhead of metadata (off_len + records) vs data
    pub fn metadata_overhead_percent(&self) -> f64 {
        if self.strpool_size == 0 {
            0.0
        } else {
            ((self.off_len_size + self.records_size) as f64 / self.strpool_size as f64) * 100.0
        }
    }
}

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

    #[test]
    fn test_config_default() {
        let config = SimpleZipConfig::default();
        assert_eq!(config.min_frag_len, 8);
        assert_eq!(config.max_frag_len, 256);
        assert_eq!(config.delimiters, vec![b'\n', b'\r', b'\t', b' ']);
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_config_builder() {
        let config = SimpleZipConfig::builder()
            .min_frag_len(16)
            .max_frag_len(512)
            .delimiters(vec![b',', b';'])
            .build()
            .unwrap();

        assert_eq!(config.min_frag_len, 16);
        assert_eq!(config.max_frag_len, 512);
        assert_eq!(config.delimiters, vec![b',', b';']);
    }

    #[test]
    fn test_config_validation() {
        // min_frag_len = 0 should fail
        let result = SimpleZipConfig::builder()
            .min_frag_len(0)
            .build();
        assert!(result.is_err());

        // max < min should fail
        let result = SimpleZipConfig::builder()
            .min_frag_len(100)
            .max_frag_len(50)
            .build();
        assert!(result.is_err());
    }

    #[test]
    fn test_empty_data() {
        let data: Vec<Vec<u8>> = vec![];
        let config = SimpleZipConfig::default();
        let store = SimpleZipBlobStore::build_from(&data, &config).unwrap();

        assert_eq!(store.len(), 0);
        assert!(store.is_empty());
        assert_eq!(store.unzip_size, 0);
    }

    #[test]
    fn test_single_record() {
        let data = vec![b"Hello World\n".to_vec()];
        let config = SimpleZipConfig::default();
        let store = SimpleZipBlobStore::build_from(&data, &config).unwrap();

        assert_eq!(store.len(), 1);
        assert_eq!(store.get(0).unwrap(), b"Hello World\n");
        assert!(store.contains(0));
        assert!(!store.contains(1));
    }

    #[test]
    fn test_fragment_deduplication() {
        let data = vec![
            b"Hello World\n".to_vec(),
            b"Hello Rust\n".to_vec(),
            b"World Peace\n".to_vec(),
        ];

        let config = SimpleZipConfig::default();
        let store = SimpleZipBlobStore::build_from(&data, &config).unwrap();

        // Verify all records retrievable
        assert_eq!(store.get(0).unwrap(), b"Hello World\n");
        assert_eq!(store.get(1).unwrap(), b"Hello Rust\n");
        assert_eq!(store.get(2).unwrap(), b"World Peace\n");

        // Check that deduplication works
        let stats = store.memory_stats();
        // With small datasets, the Vec<usize> metadata overhead may exceed savings
        // Production implementation should use ZipIntVec for better compression
        // Just verify deduplication occurred (unique fragments stored once)
        assert!(stats.strpool_size > 0);
        println!("Strpool: {} bytes", stats.strpool_size);
        println!("Uncompressed: {} bytes", store.unzip_size);
        println!("Compression ratio: {:.2}%", stats.compression_ratio * 100.0);
    }

    #[test]
    fn test_delimiter_fragmentation() {
        let data = vec![
            b"line1\nline2\nline3\n".to_vec(),
            b"line1\nline4\nline5\n".to_vec(),
        ];

        let config = SimpleZipConfig {
            min_frag_len: 1,
            max_frag_len: 20,
            delimiters: vec![b'\n'],
        };

        let store = SimpleZipBlobStore::build_from(&data, &config).unwrap();

        assert_eq!(store.get(0).unwrap(), b"line1\nline2\nline3\n");
        assert_eq!(store.get(1).unwrap(), b"line1\nline4\nline5\n");

        // "line1\n" should be shared - strpool smaller than uncompressed
        let stats = store.memory_stats();
        assert!(stats.strpool_size < store.unzip_size, "Fragment deduplication should reduce strpool size");
    }

    #[test]
    fn test_no_delimiters_found() {
        let data = vec![
            b"AAAAAAAAAABBBBBBBBBBCCCCCCCCCC".to_vec(),
            b"DDDDDDDDDDEEEEEEEEEEFFFFFFFFFF".to_vec(),
        ];

        let config = SimpleZipConfig {
            min_frag_len: 8,
            max_frag_len: 10,
            delimiters: vec![b'\n'],
        };

        let store = SimpleZipBlobStore::build_from(&data, &config).unwrap();

        assert_eq!(store.get(0).unwrap(), b"AAAAAAAAAABBBBBBBBBBCCCCCCCCCC");
        assert_eq!(store.get(1).unwrap(), b"DDDDDDDDDDEEEEEEEEEEFFFFFFFFFF");
    }

    #[test]
    fn test_shared_substrings() {
        let timestamp = b"2024-01-15 12:34:56 ";
        let mut data = Vec::new();
        for i in 0..100 {
            let mut record = timestamp.to_vec();
            record.extend_from_slice(format!("Event {}\n", i).as_bytes());
            data.push(record);
        }

        let config = SimpleZipConfig::default();
        let store = SimpleZipBlobStore::build_from(&data, &config).unwrap();

        // Verify correctness
        for i in 0..100 {
            let expected = format!("2024-01-15 12:34:56 Event {}\n", i);
            assert_eq!(store.get(i).unwrap(), expected.as_bytes());
        }

        // Check that timestamp is shared (strpool much smaller than uncompressed)
        let stats = store.memory_stats();
        // Timestamp appears 100 times but stored once - strpool should be much smaller
        assert!(stats.strpool_size < store.unzip_size / 2, "Timestamp deduplication should save space");
        println!("Space saved: {:.2}%", stats.space_saved_percent());
    }

    #[test]
    fn test_size_method() {
        let data = vec![
            b"short".to_vec(),
            b"medium length".to_vec(),
            b"a much longer string with more content".to_vec(),
        ];

        let config = SimpleZipConfig::default();
        let store = SimpleZipBlobStore::build_from(&data, &config).unwrap();

        assert_eq!(store.size(0).unwrap(), Some(5));
        assert_eq!(store.size(1).unwrap(), Some(13));
        assert_eq!(store.size(2).unwrap(), Some(b"a much longer string with more content".len()));
        assert_eq!(store.size(999).unwrap(), None);
    }

    #[test]
    fn test_batch_operations() {
        let data = vec![
            b"record0".to_vec(),
            b"record1".to_vec(),
            b"record2".to_vec(),
        ];

        let config = SimpleZipConfig::default();
        let store = SimpleZipBlobStore::build_from(&data, &config).unwrap();

        let results = store.get_batch(vec![0, 2, 999]).unwrap();
        assert_eq!(results[0], Some(b"record0".to_vec()));
        assert_eq!(results[1], Some(b"record2".to_vec()));
        assert_eq!(results[2], None);
    }

    #[test]
    fn test_iteration() {
        let data = vec![
            b"A".to_vec(),
            b"B".to_vec(),
            b"C".to_vec(),
        ];

        let config = SimpleZipConfig::default();
        let store = SimpleZipBlobStore::build_from(&data, &config).unwrap();

        let ids: Vec<_> = store.iter_ids().collect();
        assert_eq!(ids, vec![0, 1, 2]);

        let blobs: Vec<_> = store.iter_blobs().collect::<Result<Vec<_>>>().unwrap();
        assert_eq!(blobs.len(), 3);
        assert_eq!(blobs[0].1, b"A");
        assert_eq!(blobs[1].1, b"B");
        assert_eq!(blobs[2].1, b"C");
    }

    #[test]
    fn test_read_only_operations() {
        let data = vec![b"test".to_vec()];
        let config = SimpleZipConfig::default();
        let mut store = SimpleZipBlobStore::build_from(&data, &config).unwrap();

        assert!(store.put(b"new").is_err());
        assert!(store.remove(0).is_err());
        assert!(store.put_batch(vec![b"a".to_vec()]).is_err());
        assert!(store.remove_batch(vec![0]).is_err());
    }

    #[test]
    fn test_record_not_found() {
        let data = vec![b"test".to_vec()];
        let config = SimpleZipConfig::default();
        let store = SimpleZipBlobStore::build_from(&data, &config).unwrap();

        assert!(store.get(999).is_err());
    }

    #[test]
    fn test_memory_stats() {
        let data = vec![
            b"Hello World\n".to_vec(),
            b"Hello Rust\n".to_vec(),
        ];

        let config = SimpleZipConfig::default();
        let store = SimpleZipBlobStore::build_from(&data, &config).unwrap();

        let stats = store.memory_stats();
        assert!(stats.strpool_size > 0);
        assert!(stats.off_len_size > 0);
        assert!(stats.records_size > 0);
        assert_eq!(stats.total_size, stats.strpool_size + stats.off_len_size + stats.records_size);
        assert!(stats.compression_ratio > 0.0);
        // NOTE: With Vec<usize> metadata, compression ratio may exceed 1.0 for small data

        println!("Strpool: {} bytes", stats.strpool_size);
        println!("Off-len: {} bytes", stats.off_len_size);
        println!("Records: {} bytes", stats.records_size);
        println!("Total: {} bytes", stats.total_size);
        println!("Uncompressed: {} bytes", stats.uncompressed_size);
        println!("Ratio: {:.2}%", stats.compression_ratio * 100.0);
        println!("Metadata overhead: {:.2}%", stats.metadata_overhead_percent());
    }

    #[test]
    fn test_large_dataset() {
        let mut data = Vec::new();
        for i in 0..1000 {
            let record = format!("Record {}: Some common prefix data\n", i);
            data.push(record.into_bytes());
        }

        let config = SimpleZipConfig::default();
        let store = SimpleZipBlobStore::build_from(&data, &config).unwrap();

        // Verify random samples
        assert_eq!(store.get(0).unwrap(), b"Record 0: Some common prefix data\n");
        assert_eq!(store.get(500).unwrap(), b"Record 500: Some common prefix data\n");
        assert_eq!(store.get(999).unwrap(), b"Record 999: Some common prefix data\n");

        let stats = store.memory_stats();
        println!("Large dataset compression: {:.2}%", stats.compression_ratio * 100.0);
        // Check that common prefix is deduplicated (strpool much smaller)
        assert!(stats.strpool_size < store.unzip_size / 2, "Common prefix should be deduplicated");
    }

    #[test]
    fn test_various_delimiters() {
        let data = vec![
            b"a,b,c,d".to_vec(),
            b"a,e,f,g".to_vec(),
            b"h,b,i,d".to_vec(),
        ];

        let config = SimpleZipConfig {
            min_frag_len: 1,
            max_frag_len: 10,
            delimiters: vec![b','],
        };

        let store = SimpleZipBlobStore::build_from(&data, &config).unwrap();

        assert_eq!(store.get(0).unwrap(), b"a,b,c,d");
        assert_eq!(store.get(1).unwrap(), b"a,e,f,g");
        assert_eq!(store.get(2).unwrap(), b"h,b,i,d");

        // "a,", "b,", and other fragments should be shared
        let stats = store.memory_stats();
        assert!(stats.strpool_size < store.unzip_size, "Fragment deduplication should work");
    }
}