zipora 3.0.1

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
//! Entropy coding blob store implementations
//!
//! This module provides blob store wrappers that use entropy coding for compression.

use crate::blob_store::{BlobStore, BlobStoreStats};
use crate::entropy::{
    DictionaryBuilder, DictionaryCompressor, EntropyStats, HuffmanDecoder, HuffmanEncoder,
    HuffmanTree,
};
use crate::entropy::rans::{Rans64Encoder, ParallelX1};
use crate::error::{Result, ZiporaError};

/// Compression algorithm type for entropy blob store
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EntropyAlgorithm {
    /// Huffman coding
    Huffman,
    /// rANS (range Asymmetric Numeral Systems)
    Rans,
    /// Dictionary-based compression
    Dictionary,
}

/// Statistics for entropy compression
#[derive(Debug, Clone, PartialEq)]
pub struct EntropyCompressionStats {
    /// Basic blob store statistics
    pub blob_stats: BlobStoreStats,
    /// Entropy coding statistics
    pub entropy_stats: EntropyStats,
    /// Compression algorithm used
    pub algorithm: EntropyAlgorithm,
    /// Number of successful compressions
    pub compressions: u64,
    /// Number of successful decompressions
    pub decompressions: u64,
    /// Total time spent compressing (microseconds)
    pub compression_time_us: u64,
    /// Total time spent decompressing (microseconds)
    pub decompression_time_us: u64,
}

impl EntropyCompressionStats {
    /// Create new entropy compression statistics
    pub fn new(algorithm: EntropyAlgorithm) -> Self {
        Self {
            blob_stats: BlobStoreStats::default(),
            entropy_stats: EntropyStats::new(0, 0, 0.0),
            algorithm,
            compressions: 0,
            decompressions: 0,
            compression_time_us: 0,
            decompression_time_us: 0,
        }
    }

    /// Get average compression time per operation
    pub fn avg_compression_time_us(&self) -> f64 {
        if self.compressions > 0 {
            self.compression_time_us as f64 / self.compressions as f64
        } else {
            0.0
        }
    }

    /// Get average decompression time per operation
    pub fn avg_decompression_time_us(&self) -> f64 {
        if self.decompressions > 0 {
            self.decompression_time_us as f64 / self.decompressions as f64
        } else {
            0.0
        }
    }
}

/// Huffman coding blob store wrapper
pub struct HuffmanBlobStore<S: BlobStore> {
    inner: S,
    stats: EntropyCompressionStats,
    training_data: Vec<u8>,
    encoder: Option<HuffmanEncoder>,
    tree: Option<HuffmanTree>,
}

impl<S: BlobStore> HuffmanBlobStore<S> {
    /// Create new Huffman blob store
    pub fn new(inner: S) -> Self {
        Self {
            inner,
            stats: EntropyCompressionStats::new(EntropyAlgorithm::Huffman),
            training_data: Vec::new(),
            encoder: None,
            tree: None,
        }
    }

    /// Add training data for building Huffman tree
    pub fn add_training_data(&mut self, data: &[u8]) {
        self.training_data.extend_from_slice(data);
    }

    /// Build Huffman tree from training data
    pub fn build_tree(&mut self) -> Result<()> {
        if self.training_data.is_empty() {
            return Err(ZiporaError::invalid_data("No training data provided"));
        }

        let tree = HuffmanTree::from_data(&self.training_data)?;
        let encoder = HuffmanEncoder::new(&self.training_data)?;

        self.tree = Some(tree);
        self.encoder = Some(encoder);

        Ok(())
    }

    /// Get compression statistics
    pub fn compression_stats(&self) -> &EntropyCompressionStats {
        &self.stats
    }

    /// Compress data using Huffman coding
    fn compress_data(&mut self, data: &[u8]) -> Result<Vec<u8>> {
        let start = std::time::Instant::now();

        let encoder = self
            .encoder
            .as_ref()
            .ok_or_else(|| ZiporaError::invalid_data("Huffman tree not built"))?;

        let compressed = encoder.encode(data)?;

        self.stats.compression_time_us += start.elapsed().as_micros() as u64;
        self.stats.compressions += 1;

        // Update entropy statistics
        let entropy = EntropyStats::calculate_entropy(data);
        self.stats.entropy_stats = EntropyStats::new(data.len(), compressed.len(), entropy);

        Ok(compressed)
    }

    /// Decompress data using Huffman coding
    #[allow(dead_code)]
    fn decompress_data(&mut self, compressed: &[u8], original_length: usize) -> Result<Vec<u8>> {
        let start = std::time::Instant::now();

        let tree = self
            .tree
            .as_ref()
            .ok_or_else(|| ZiporaError::invalid_data("Huffman tree not built"))?;

        let decoder = HuffmanDecoder::new(tree.clone());
        let decompressed = decoder.decode(compressed, original_length)?;

        self.stats.decompression_time_us += start.elapsed().as_micros() as u64;
        self.stats.decompressions += 1;

        Ok(decompressed)
    }
}

impl<S: BlobStore> BlobStore for HuffmanBlobStore<S> {
    fn get(&self, id: crate::RecordId) -> Result<Vec<u8>> {
        // For now, delegate to inner store (would need metadata for decompression)
        self.inner.get(id)
    }

    fn put(&mut self, data: &[u8]) -> Result<crate::RecordId> {
        if self.encoder.is_some() && !data.is_empty() {
            match self.compress_data(data) {
                Ok(compressed) => {
                    let id = self.inner.put(&compressed)?;
                    self.stats.blob_stats.put_count += 1;
                    Ok(id)
                }
                Err(_) => {
                    // Fall back to uncompressed
                    self.inner.put(data)
                }
            }
        } else {
            self.inner.put(data)
        }
    }

    fn remove(&mut self, id: crate::RecordId) -> Result<()> {
        self.inner.remove(id)
    }

    fn contains(&self, id: crate::RecordId) -> bool {
        self.inner.contains(id)
    }

    fn size(&self, id: crate::RecordId) -> Result<Option<usize>> {
        self.inner.size(id)
    }

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

    fn flush(&mut self) -> Result<()> {
        self.inner.flush()
    }

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

/// rANS coding blob store wrapper
pub struct RansBlobStore<S: BlobStore> {
    inner: S,
    stats: EntropyCompressionStats,
    encoder: Option<Rans64Encoder<ParallelX1>>,
}

impl<S: BlobStore> RansBlobStore<S> {
    /// Create new rANS blob store
    pub fn new(inner: S) -> Self {
        Self {
            inner,
            stats: EntropyCompressionStats::new(EntropyAlgorithm::Rans),
            encoder: None,
        }
    }

    /// Train rANS encoder with data
    pub fn train(&mut self, data: &[u8]) -> Result<()> {
        let mut frequencies = [0u32; 256];
        for &byte in data {
            frequencies[byte as usize] += 1;
        }

        let encoder = Rans64Encoder::<ParallelX1>::new(&frequencies)?;
        self.encoder = Some(encoder);

        Ok(())
    }

    /// Get compression statistics
    pub fn compression_stats(&self) -> &EntropyCompressionStats {
        &self.stats
    }
}

impl<S: BlobStore> BlobStore for RansBlobStore<S> {
    fn get(&self, id: crate::RecordId) -> Result<Vec<u8>> {
        self.inner.get(id)
    }

    fn put(&mut self, data: &[u8]) -> Result<crate::RecordId> {
        // For now, delegate to inner store (would need full implementation)
        self.inner.put(data)
    }

    fn remove(&mut self, id: crate::RecordId) -> Result<()> {
        self.inner.remove(id)
    }

    fn contains(&self, id: crate::RecordId) -> bool {
        self.inner.contains(id)
    }

    fn size(&self, id: crate::RecordId) -> Result<Option<usize>> {
        self.inner.size(id)
    }

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

    fn flush(&mut self) -> Result<()> {
        self.inner.flush()
    }

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

/// Dictionary compression blob store wrapper
pub struct DictionaryBlobStore<S: BlobStore> {
    inner: S,
    stats: EntropyCompressionStats,
    compressor: Option<DictionaryCompressor>,
}

impl<S: BlobStore> DictionaryBlobStore<S> {
    /// Create new dictionary blob store
    pub fn new(inner: S) -> Self {
        Self {
            inner,
            stats: EntropyCompressionStats::new(EntropyAlgorithm::Dictionary),
            compressor: None,
        }
    }

    /// Train dictionary with data
    pub fn train(&mut self, data: &[u8]) -> Result<()> {
        let builder = DictionaryBuilder::new();
        let dictionary = builder.build(data);
        let compressor = DictionaryCompressor::new(dictionary);

        self.compressor = Some(compressor);

        Ok(())
    }

    /// Get compression statistics
    pub fn compression_stats(&self) -> &EntropyCompressionStats {
        &self.stats
    }
}

impl<S: BlobStore> BlobStore for DictionaryBlobStore<S> {
    fn get(&self, id: crate::RecordId) -> Result<Vec<u8>> {
        self.inner.get(id)
    }

    fn put(&mut self, data: &[u8]) -> Result<crate::RecordId> {
        // For now, delegate to inner store (would need full implementation)
        self.inner.put(data)
    }

    fn remove(&mut self, id: crate::RecordId) -> Result<()> {
        self.inner.remove(id)
    }

    fn contains(&self, id: crate::RecordId) -> bool {
        self.inner.contains(id)
    }

    fn size(&self, id: crate::RecordId) -> Result<Option<usize>> {
        self.inner.size(id)
    }

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

    fn flush(&mut self) -> Result<()> {
        self.inner.flush()
    }

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

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

    #[test]
    fn test_huffman_blob_store_creation() {
        let inner = MemoryBlobStore::new();
        let huffman_store = HuffmanBlobStore::new(inner);

        assert_eq!(
            huffman_store.compression_stats().algorithm,
            EntropyAlgorithm::Huffman
        );
        assert_eq!(huffman_store.compression_stats().compressions, 0);
    }

    #[test]
    fn test_huffman_blob_store_training() {
        let inner = MemoryBlobStore::new();
        let mut huffman_store = HuffmanBlobStore::new(inner);

        huffman_store.add_training_data(b"hello world hello world");
        let result = huffman_store.build_tree();
        assert!(result.is_ok());
    }

    #[test]
    fn test_rans_blob_store_creation() {
        let inner = MemoryBlobStore::new();
        let rans_store = RansBlobStore::new(inner);

        assert_eq!(
            rans_store.compression_stats().algorithm,
            EntropyAlgorithm::Rans
        );
    }

    #[test]
    fn test_dictionary_blob_store_creation() {
        let inner = MemoryBlobStore::new();
        let dict_store = DictionaryBlobStore::new(inner);

        assert_eq!(
            dict_store.compression_stats().algorithm,
            EntropyAlgorithm::Dictionary
        );
    }

    #[test]
    fn test_entropy_compression_stats() {
        let mut stats = EntropyCompressionStats::new(EntropyAlgorithm::Huffman);

        stats.compressions = 10;
        stats.compression_time_us = 1000;

        assert_eq!(stats.avg_compression_time_us(), 100.0);

        stats.decompressions = 5;
        stats.decompression_time_us = 500;

        assert_eq!(stats.avg_decompression_time_us(), 100.0);
    }

    #[test]
    fn test_huffman_blob_store_basic_operations() {
        let inner = MemoryBlobStore::new();
        let mut huffman_store = HuffmanBlobStore::new(inner);

        // Test basic blob store operations
        let data = b"test data";
        let id = huffman_store.put(data).unwrap();

        assert!(huffman_store.contains(id));
        assert_eq!(huffman_store.len(), 1);

        let retrieved = huffman_store.get(id).unwrap();
        assert_eq!(retrieved, data);
    }

    #[test]
    fn test_rans_blob_store_training() {
        let inner = MemoryBlobStore::new();
        let mut rans_store = RansBlobStore::new(inner);

        let training_data = b"hello world hello world hello";
        let result = rans_store.train(training_data);
        assert!(result.is_ok());
    }

    #[test]
    fn test_dictionary_blob_store_training() {
        let inner = MemoryBlobStore::new();
        let mut dict_store = DictionaryBlobStore::new(inner);

        let training_data = b"hello world hello world hello";
        let result = dict_store.train(training_data);
        assert!(result.is_ok());
    }
}