zipora 3.1.2

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
//! In-memory blob store implementation
//!
//! This module provides a simple in-memory blob store primarily for testing
//! and development purposes. Data is stored in memory and will be lost when
//! the program exits.

use std::collections::HashMap;
use std::sync::atomic::{AtomicU32, Ordering};

use crate::RecordId;
use crate::blob_store::traits::{BatchBlobStore, BlobStore, BlobStoreStats, IterableBlobStore};
use crate::error::{Result, ZiporaError};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// In-memory blob store implementation
///
/// This implementation stores all blobs in memory using a HashMap.
/// It's primarily intended for testing and development use cases.
///
/// # Examples
///
/// ```rust
/// use zipora::blob_store::{BlobStore, MemoryBlobStore};
///
/// let mut store = MemoryBlobStore::new();
/// let data = b"hello world";
/// let id = store.put(data).unwrap();
/// let retrieved = store.get(id).unwrap();
/// assert_eq!(data, &retrieved[..]);
/// ```
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct MemoryBlobStore {
    /// Storage for blob data
    data: HashMap<RecordId, Vec<u8>>,
    /// Next available record ID
    next_id: AtomicU32,
    /// Usage statistics
    stats: BlobStoreStats,
}

impl MemoryBlobStore {
    /// Create a new empty memory blob store
    pub fn new() -> Self {
        Self {
            data: HashMap::new(),
            next_id: AtomicU32::new(1), // Start from 1, 0 is reserved for "null"
            stats: BlobStoreStats::new(),
        }
    }

    /// Create a new memory blob store with the specified initial capacity
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            data: HashMap::with_capacity(capacity),
            next_id: AtomicU32::new(1),
            stats: BlobStoreStats::new(),
        }
    }

    /// Create a memory blob store from existing data
    pub fn from_data(data: HashMap<RecordId, Vec<u8>>) -> Self {
        let next_id = data.keys().max().map(|&id| id + 1).unwrap_or(1);
        let mut stats = BlobStoreStats::new();

        // Initialize stats from existing data
        for blob in data.values() {
            stats.record_put(blob.len());
        }

        Self {
            data,
            next_id: AtomicU32::new(next_id),
            stats,
        }
    }

    /// Get the capacity of the underlying HashMap
    #[inline]
    pub fn capacity(&self) -> usize {
        self.data.capacity()
    }

    /// Reserve space for additional blobs
    pub fn reserve(&mut self, additional: usize) {
        self.data.reserve(additional);
    }

    /// Shrink the capacity to fit the current number of blobs
    pub fn shrink_to_fit(&mut self) {
        self.data.shrink_to_fit();
    }

    /// Clear all blobs from the store
    pub fn clear(&mut self) {
        self.data.clear();
        self.next_id.store(1, Ordering::Relaxed);
        self.stats = BlobStoreStats::new();
    }

    /// Get a reference to the internal data (for testing)
    #[cfg(test)]
    #[allow(dead_code)]
    pub(crate) fn internal_data(&self) -> &HashMap<RecordId, Vec<u8>> {
        &self.data
    }

    /// Generate the next record ID
    fn next_record_id(&self) -> RecordId {
        self.next_id.fetch_add(1, Ordering::Relaxed)
    }
}

impl Clone for MemoryBlobStore {
    fn clone(&self) -> Self {
        Self {
            data: self.data.clone(),
            next_id: AtomicU32::new(self.next_id.load(Ordering::Relaxed)),
            stats: self.stats.clone(),
        }
    }
}

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

impl BlobStore for MemoryBlobStore {
    fn get(&self, id: RecordId) -> Result<Vec<u8>> {
        self.data
            .get(&id)
            .cloned()
            .ok_or_else(|| ZiporaError::not_found(format!("Blob with ID {} not found", id)))
    }

    fn put(&mut self, data: &[u8]) -> Result<RecordId> {
        let id = self.next_record_id();
        let blob_data = data.to_vec();
        self.data.insert(id, blob_data);
        self.stats.record_put(data.len());
        Ok(id)
    }

    fn remove(&mut self, id: RecordId) -> Result<()> {
        match self.data.remove(&id) {
            Some(data) => {
                self.stats.record_remove(data.len());
                Ok(())
            }
            None => Err(ZiporaError::not_found(format!(
                "Blob with ID {} not found",
                id
            ))),
        }
    }

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

    fn size(&self, id: RecordId) -> Result<Option<usize>> {
        Ok(self.data.get(&id).map(|data| data.len()))
    }

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

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

impl IterableBlobStore for MemoryBlobStore {
    type IdIter = std::vec::IntoIter<RecordId>;

    fn iter_ids(&self) -> Self::IdIter {
        let mut ids: Vec<RecordId> = self.data.keys().copied().collect();
        ids.sort_unstable();
        ids.into_iter()
    }
}

impl BatchBlobStore for MemoryBlobStore {
    fn put_batch<I>(&mut self, blobs: I) -> Result<Vec<RecordId>>
    where
        I: IntoIterator<Item = Vec<u8>>,
    {
        let mut ids = Vec::new();
        for blob in blobs {
            let id = self.put(&blob)?;
            ids.push(id);
        }
        Ok(ids)
    }

    fn get_batch<I>(&self, ids: I) -> Result<Vec<Option<Vec<u8>>>>
    where
        I: IntoIterator<Item = RecordId>,
    {
        let mut results = Vec::new();
        for id in ids {
            results.push(self.data.get(&id).cloned());
        }
        Ok(results)
    }

    fn remove_batch<I>(&mut self, ids: I) -> Result<usize>
    where
        I: IntoIterator<Item = RecordId>,
    {
        let mut removed_count = 0;
        for id in ids {
            if let Some(data) = self.data.remove(&id) {
                self.stats.record_remove(data.len());
                removed_count += 1;
            }
        }
        Ok(removed_count)
    }
}

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

    #[test]
    fn test_memory_blob_store_basic_operations() {
        let mut store = MemoryBlobStore::new();

        // Test empty store
        assert_eq!(store.len(), 0);
        assert!(store.is_empty());

        // Test put operation
        let data1 = b"hello world";
        let id1 = store.put(data1).unwrap();
        assert_eq!(store.len(), 1);
        assert!(!store.is_empty());
        assert!(store.contains(id1));

        // Test get operation
        let retrieved = store.get(id1).unwrap();
        assert_eq!(data1, &retrieved[..]);

        // Test size operation
        let size = store.size(id1).unwrap();
        assert_eq!(size, Some(data1.len()));

        // Test put another blob
        let data2 = b"goodbye world";
        let id2 = store.put(data2).unwrap();
        assert_eq!(store.len(), 2);
        assert_ne!(id1, id2);

        // Test remove operation
        store.remove(id1).unwrap();
        assert_eq!(store.len(), 1);
        assert!(!store.contains(id1));
        assert!(store.contains(id2));

        // Test get after remove
        assert!(store.get(id1).is_err());
        let retrieved2 = store.get(id2).unwrap();
        assert_eq!(data2, &retrieved2[..]);
    }

    #[test]
    fn test_memory_blob_store_errors() {
        let mut store = MemoryBlobStore::new();

        // Test get non-existent blob
        let result = store.get(999);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("not found"));

        // Test remove non-existent blob
        let result = store.remove(999);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("not found"));

        // Test size of non-existent blob
        let size = store.size(999).unwrap();
        assert_eq!(size, None);
    }

    #[test]
    fn test_memory_blob_store_iteration() {
        let mut store = MemoryBlobStore::new();

        // Add some blobs
        let data1 = b"blob1";
        let data2 = b"blob2";
        let data3 = b"blob3";

        let id1 = store.put(data1).unwrap();
        let id2 = store.put(data2).unwrap();
        let id3 = store.put(data3).unwrap();

        // Test ID iteration
        let ids: Vec<RecordId> = store.iter_ids().collect();
        assert_eq!(ids.len(), 3);
        assert!(ids.contains(&id1));
        assert!(ids.contains(&id2));
        assert!(ids.contains(&id3));

        // Test blob iteration
        let blobs: Result<Vec<(RecordId, Vec<u8>)>> = store.iter_blobs().collect();
        let blobs = blobs.unwrap();
        assert_eq!(blobs.len(), 3);

        // Verify blob contents
        for (id, data) in blobs {
            match id {
                _ if id == id1 => assert_eq!(&data, data1),
                _ if id == id2 => assert_eq!(&data, data2),
                _ if id == id3 => assert_eq!(&data, data3),
                _ => panic!("Unexpected blob ID: {}", id),
            }
        }
    }

    #[test]
    fn test_memory_blob_store_batch_operations() {
        let mut store = MemoryBlobStore::new();

        // Test batch put
        let blobs = vec![b"blob1".to_vec(), b"blob2".to_vec(), b"blob3".to_vec()];
        let ids = store.put_batch(blobs.clone()).unwrap();
        assert_eq!(ids.len(), 3);
        assert_eq!(store.len(), 3);

        // Test batch get
        let retrieved = store.get_batch(ids.clone()).unwrap();
        assert_eq!(retrieved.len(), 3);
        for (i, blob_opt) in retrieved.iter().enumerate() {
            assert!(blob_opt.is_some());
            assert_eq!(blob_opt.as_ref().unwrap(), &blobs[i]);
        }

        // Test batch get with some missing IDs
        let mut test_ids = ids.clone();
        test_ids.push(999); // Non-existent ID
        let retrieved = store.get_batch(test_ids).unwrap();
        assert_eq!(retrieved.len(), 4);
        assert!(retrieved[3].is_none());

        // Test batch remove
        let removed_count = store.remove_batch(ids).unwrap();
        assert_eq!(removed_count, 3);
        assert_eq!(store.len(), 0);
    }

    #[test]
    fn test_memory_blob_store_capacity_management() {
        let mut store = MemoryBlobStore::with_capacity(10);
        assert!(store.capacity() >= 10);

        // Test reserve
        store.reserve(100);
        assert!(store.capacity() >= 100);

        // Add some data and test shrink
        for i in 0..5 {
            store.put(format!("blob{}", i).as_bytes()).unwrap();
        }

        store.shrink_to_fit();
        assert!(store.capacity() >= store.len());

        // Test clear
        store.clear();
        assert_eq!(store.len(), 0);
        assert!(store.is_empty());
    }

    #[test]
    fn test_memory_blob_store_from_data() {
        let mut data = HashMap::new();
        data.insert(5, b"blob1".to_vec());
        data.insert(10, b"blob2".to_vec());
        data.insert(15, b"blob3".to_vec());

        let store = MemoryBlobStore::from_data(data.clone());
        assert_eq!(store.len(), 3);

        // Test that next ID is generated correctly
        let next_id = store.next_id.load(Ordering::Relaxed);
        assert_eq!(next_id, 16);

        // Test that all data is accessible
        for (id, expected_data) in data {
            let retrieved = store.get(id).unwrap();
            assert_eq!(retrieved, expected_data);
        }
    }

    #[test]
    fn test_memory_blob_store_stats() {
        let mut store = MemoryBlobStore::new();
        let initial_stats = store.stats();
        assert_eq!(initial_stats.blob_count, 0);
        assert_eq!(initial_stats.total_size, 0);

        // Add some blobs
        store.put(b"blob1").unwrap();
        store.put(b"blob22").unwrap();

        let stats = store.stats();
        assert_eq!(stats.blob_count, 2);
        assert_eq!(stats.total_size, 11); // 5 + 6 bytes
        assert_eq!(stats.put_count, 2);
    }

    #[test]
    fn test_record_id_generation() {
        let store = MemoryBlobStore::new();

        // Test sequential ID generation
        let id1 = store.next_record_id();
        let id2 = store.next_record_id();
        let id3 = store.next_record_id();

        assert_eq!(id1, 1);
        assert_eq!(id2, 2);
        assert_eq!(id3, 3);

        // Test that IDs are unique even in concurrent access
        use std::sync::Arc;
        use std::thread;

        let store = Arc::new(MemoryBlobStore::new());
        let mut handles = vec![];

        for _ in 0..10 {
            let store_clone = Arc::clone(&store);
            let handle = thread::spawn(move || {
                (0..100)
                    .map(|_| store_clone.next_record_id())
                    .collect::<Vec<_>>()
            });
            handles.push(handle);
        }

        let mut all_ids = Vec::new();
        for handle in handles {
            all_ids.extend(handle.join().unwrap());
        }

        // Check that all IDs are unique
        all_ids.sort_unstable();
        for window in all_ids.windows(2) {
            assert_ne!(window[0], window[1], "Found duplicate ID: {}", window[0]);
        }
    }
}