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
//! Concurrent LRU Cache Map with Sharding
//!
//! This module provides a high-performance, thread-safe LRU cache map that uses
//! sharding to reduce contention in multi-threaded environments.

use super::lru_map::{LruMap, LruMapConfig, LruMapStatistics, EvictionCallback, NoOpEvictionCallback};
use crate::error::{Result, ZiporaError};
use std::hash::{Hash, Hasher};
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::collections::hash_map::DefaultHasher;

/// Configuration for concurrent LRU map
#[derive(Debug, Clone)]
pub struct ConcurrentLruMapConfig {
    /// Base configuration for individual shards
    pub base_config: LruMapConfig,
    
    /// Number of shards (should be power of 2)
    pub shard_count: usize,
    
    /// Load balancing strategy
    pub load_balancing: LoadBalancingStrategy,
}

impl Default for ConcurrentLruMapConfig {
    fn default() -> Self {
        Self {
            base_config: LruMapConfig::default(),
            shard_count: 16, // Good default for most workloads
            load_balancing: LoadBalancingStrategy::Hash,
        }
    }
}

impl ConcurrentLruMapConfig {
    /// Create a performance-optimized configuration
    pub fn performance_optimized() -> Self {
        Self {
            base_config: LruMapConfig::performance_optimized(),
            shard_count: std::thread::available_parallelism().map(|n| n.get()).unwrap_or(1) * 2, // 2 shards per CPU core
            load_balancing: LoadBalancingStrategy::Hash,
        }
    }
    
    /// Create a memory-optimized configuration
    pub fn memory_optimized() -> Self {
        Self {
            base_config: LruMapConfig::memory_optimized(),
            shard_count: 4, // Fewer shards to save memory
            load_balancing: LoadBalancingStrategy::Hash,
        }
    }
    
    /// Validate configuration
    pub fn validate(&self) -> Result<()> {
        self.base_config.validate()?;
        
        if self.shard_count == 0 {
            return Err(ZiporaError::invalid_parameter("Shard count must be > 0"));
        }
        
        if !self.shard_count.is_power_of_two() {
            return Err(ZiporaError::invalid_parameter("Shard count must be power of 2"));
        }
        
        Ok(())
    }
}

/// Load balancing strategies for shard selection
#[derive(Debug, Clone, Copy)]
pub enum LoadBalancingStrategy {
    /// Use hash-based sharding (default)
    Hash,
    /// Round-robin assignment (good for sequential keys)
    RoundRobin,
    /// Use thread ID for affinity-based sharding
    ThreadAffinity,
}

/// Aggregated statistics from all shards
#[derive(Debug, Default)]
pub struct ConcurrentLruMapStatistics {
    /// Statistics from individual shards
    pub shard_stats: Vec<Arc<LruMapStatistics>>,
    
    /// Global operation counter for round-robin
    pub global_counter: AtomicU64,
}

impl ConcurrentLruMapStatistics {
    /// Create new concurrent statistics
    pub fn new(shard_stats: Vec<Arc<LruMapStatistics>>) -> Self {
        Self {
            shard_stats,
            global_counter: AtomicU64::new(0),
        }
    }
    
    /// Get aggregated hit ratio
    pub fn hit_ratio(&self) -> f64 {
        let (total_hits, total_gets) = self.shard_stats.iter().fold((0u64, 0u64), |(hits, gets), stats| {
            (
                hits + stats.hit_count.load(Ordering::Relaxed),
                gets + stats.get_count.load(Ordering::Relaxed),
            )
        });
        
        if total_gets > 0 {
            total_hits as f64 / total_gets as f64
        } else {
            0.0
        }
    }
    
    /// Get total entry count across all shards
    pub fn total_entries(&self) -> usize {
        self.shard_stats.iter().map(|stats| stats.entry_count.load(Ordering::Relaxed)).sum()
    }
    
    /// Get total memory usage across all shards
    pub fn total_memory_usage(&self) -> usize {
        self.shard_stats.iter().map(|stats| stats.memory_usage.load(Ordering::Relaxed)).sum()
    }
    
    /// Get shard with minimum load
    pub fn min_load_shard(&self) -> usize {
        self.shard_stats
            .iter()
            .enumerate()
            .min_by_key(|(_, stats)| stats.entry_count.load(Ordering::Relaxed))
            .map(|(idx, _)| idx)
            .unwrap_or(0)
    }
    
    /// Get shard with maximum load
    pub fn max_load_shard(&self) -> usize {
        self.shard_stats
            .iter()
            .enumerate()
            .max_by_key(|(_, stats)| stats.entry_count.load(Ordering::Relaxed))
            .map(|(idx, _)| idx)
            .unwrap_or(0)
    }
    
    /// Get load balance ratio (perfect balance = 1.0, higher = more imbalanced)
    pub fn load_balance_ratio(&self) -> f64 {
        if self.shard_stats.is_empty() {
            return 1.0;
        }
        
        let loads: Vec<usize> = self.shard_stats
            .iter()
            .map(|stats| stats.entry_count.load(Ordering::Relaxed))
            .collect();
        
        let max_load = *loads.iter().max().unwrap_or(&0);
        let min_load = *loads.iter().min().unwrap_or(&0);
        
        if min_load == 0 {
            if max_load == 0 { 1.0 } else { f64::INFINITY }
        } else {
            max_load as f64 / min_load as f64
        }
    }
}

/// High-performance concurrent LRU cache map with sharding
///
/// This implementation distributes entries across multiple LRU map shards
/// to reduce lock contention and improve scalability in multi-threaded environments.
///
/// # Examples
///
/// ```rust
/// use zipora::containers::specialized::ConcurrentLruMap;
/// 
/// let cache = ConcurrentLruMap::new(1024, 8).unwrap(); // 1024 capacity, 8 shards
/// 
/// cache.put("key1".to_string(), "value1".to_string()).unwrap();
/// cache.put("key2".to_string(), "value2".to_string()).unwrap();
/// 
/// assert_eq!(cache.get(&"key1".to_string()), Some("value1".to_string()));
/// assert_eq!(cache.len(), 2);
/// ```
pub struct ConcurrentLruMap<K, V, E = NoOpEvictionCallback>
where
    K: Hash + Eq + Clone + Send + Sync + Default,
    V: Clone + Send + Sync + Default,
    E: EvictionCallback<K, V> + Send + Sync + Clone,
{
    /// Individual LRU map shards
    shards: Vec<Arc<LruMap<K, V, E>>>,
    
    /// Configuration
    config: ConcurrentLruMapConfig,
    
    /// Shard selection mask for efficient modulo
    shard_mask: usize,
    
    /// Aggregated statistics
    stats: ConcurrentLruMapStatistics,
}

impl<K, V> ConcurrentLruMap<K, V, NoOpEvictionCallback>
where
    K: Hash + Eq + Clone + Send + Sync + Default,
    V: Clone + Send + Sync + Default,
{
    /// Create a new concurrent LRU map
    pub fn new(total_capacity: usize, shard_count: usize) -> Result<Self> {
        let config = ConcurrentLruMapConfig {
            base_config: LruMapConfig {
                capacity: total_capacity / shard_count,
                ..Default::default()
            },
            shard_count,
            ..Default::default()
        };
        
        Self::with_config(config)
    }
    
    /// Create with configuration
    pub fn with_config(config: ConcurrentLruMapConfig) -> Result<Self> {
        config.validate()?;
        
        let mut shards = Vec::with_capacity(config.shard_count);
        let mut shard_stats = Vec::with_capacity(config.shard_count);
        
        for _ in 0..config.shard_count {
            let shard = Arc::new(LruMap::with_config(config.base_config.clone())?);
            shard_stats.push(shard.stats_arc());
            shards.push(shard);
        }
        
        Ok(Self {
            shards,
            shard_mask: config.shard_count - 1,
            config,
            stats: ConcurrentLruMapStatistics::new(shard_stats),
        })
    }
}

impl<K, V, E> ConcurrentLruMap<K, V, E>
where
    K: Hash + Eq + Clone + Send + Sync + Default,
    V: Clone + Send + Sync + Default,
    E: EvictionCallback<K, V> + Send + Sync + Clone,
{
    /// Create with eviction callback
    pub fn with_eviction_callback(total_capacity: usize, shard_count: usize, callback: E) -> Result<Self> {
        let config = ConcurrentLruMapConfig {
            base_config: LruMapConfig {
                capacity: total_capacity / shard_count,
                ..Default::default()
            },
            shard_count,
            ..Default::default()
        };
        
        Self::with_config_and_callback(config, callback)
    }
    
    /// Create with configuration and callback
    pub fn with_config_and_callback(config: ConcurrentLruMapConfig, callback: E) -> Result<Self> {
        config.validate()?;
        
        let mut shards = Vec::with_capacity(config.shard_count);
        let mut shard_stats = Vec::with_capacity(config.shard_count);
        
        for _ in 0..config.shard_count {
            let shard = Arc::new(LruMap::with_config_and_callback(
                config.base_config.clone(),
                callback.clone(),
            )?);
            shard_stats.push(shard.stats_arc());
            shards.push(shard);
        }
        
        Ok(Self {
            shards,
            shard_mask: config.shard_count - 1,
            config,
            stats: ConcurrentLruMapStatistics::new(shard_stats),
        })
    }
    
    /// Get a value by key
    #[inline]
    pub fn get(&self, key: &K) -> Option<V> {
        let shard_idx = self.select_shard(key);
        self.shards[shard_idx].get(key)
    }
    
    /// Insert or update a key-value pair
    pub fn put(&self, key: K, value: V) -> Result<Option<V>> {
        let shard_idx = self.select_shard(&key);
        self.shards[shard_idx].put(key, value)
    }
    
    /// Remove a key-value pair
    pub fn remove(&self, key: &K) -> Option<V> {
        let shard_idx = self.select_shard(key);
        self.shards[shard_idx].remove(key)
    }
    
    /// Check if the cache contains a key
    pub fn contains_key(&self, key: &K) -> bool {
        let shard_idx = self.select_shard(key);
        self.shards[shard_idx].contains_key(key)
    }
    
    /// Get the current number of entries across all shards
    #[inline]
    pub fn len(&self) -> usize {
        self.shards.iter().map(|shard| shard.len()).sum()
    }
    
    /// Check if the cache is empty
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
    
    /// Get total capacity across all shards
    #[inline]
    pub fn capacity(&self) -> usize {
        self.shards.iter().map(|shard| shard.capacity()).sum()
    }
    
    /// Clear all entries in all shards
    pub fn clear(&self) -> Result<()> {
        for shard in &self.shards {
            shard.clear()?;
        }
        Ok(())
    }
    
    /// Get aggregated statistics
    pub fn stats(&self) -> &ConcurrentLruMapStatistics {
        &self.stats
    }
    
    /// Get configuration
    pub fn config(&self) -> &ConcurrentLruMapConfig {
        &self.config
    }
    
    /// Get number of shards
    pub fn shard_count(&self) -> usize {
        self.shards.len()
    }
    
    /// Get statistics for a specific shard
    pub fn shard_stats(&self, shard_idx: usize) -> Option<&LruMapStatistics> {
        self.shards.get(shard_idx).map(|shard| shard.stats())
    }
    
    /// Select shard for a given key
    fn select_shard(&self, key: &K) -> usize {
        match self.config.load_balancing {
            LoadBalancingStrategy::Hash => {
                self.hash_key(key) & self.shard_mask
            }
            LoadBalancingStrategy::RoundRobin => {
                let counter = self.stats.global_counter.fetch_add(1, Ordering::Relaxed);
                (counter as usize) & self.shard_mask
            }
            LoadBalancingStrategy::ThreadAffinity => {
                // Use thread ID for affinity
                let thread_id = std::thread::current().id();
                let hash = self.hash_thread_id(thread_id);
                hash & self.shard_mask
            }
        }
    }
    
    /// Hash a key for shard selection
    fn hash_key(&self, key: &K) -> usize {
        let mut hasher = DefaultHasher::new();
        key.hash(&mut hasher);
        let hash = hasher.finish();
        
        // Use upper bits for better distribution
        ((hash >> 32) ^ hash) as usize
    }
    
    /// Hash thread ID for affinity-based sharding
    fn hash_thread_id(&self, thread_id: std::thread::ThreadId) -> usize {
        let mut hasher = DefaultHasher::new();
        thread_id.hash(&mut hasher);
        hasher.finish() as usize
    }
    
    /// Execute a function on all shards in parallel
    pub fn for_each_shard<F>(&self, mut f: F) -> Result<()>
    where
        F: FnMut(&LruMap<K, V, E>) -> Result<()> + Send + Clone + 'static,
        K: 'static,
        V: 'static,
        E: 'static,
    {
        use std::thread;
        
        let handles: Vec<_> = self.shards
            .iter()
            .enumerate()
            .map(|(idx, shard)| {
                let shard = shard.clone();
                let mut f = f.clone();
                thread::spawn(move || f(&shard))
            })
            .collect();
        
        for handle in handles {
            handle.join().map_err(|_| ZiporaError::out_of_memory(0))??;
        }
        
        Ok(())
    }
    
    /// Get keys from all shards (expensive operation)
    pub fn keys(&self) -> Vec<K>
    where
        K: Clone,
    {
        // This is an expensive operation that requires coordination
        // In practice, you might want to implement this differently
        // or provide warnings about its cost
        
        let mut all_keys = Vec::new();
        
        // Note: This is not atomic across shards, so the snapshot
        // might not be perfectly consistent
        for shard in &self.shards {
            // We'd need to add a keys() method to LruMap for this to work
            // For now, this is a placeholder showing the interface
        }
        
        all_keys
    }
    
    /// Get an approximate count of entries in each shard
    pub fn shard_sizes(&self) -> Vec<usize> {
        self.shards.iter().map(|shard| shard.len()).collect()
    }
    
    /// Rebalance load across shards (advanced operation)
    pub fn rebalance(&self) -> Result<()> {
        // This would be a complex operation that moves entries between
        // shards to achieve better load balance. Implementation would
        // depend on specific requirements and constraints.
        
        // For now, this is a placeholder
        Ok(())
    }
}

// Thread-safe implementation
unsafe impl<K, V, E> Send for ConcurrentLruMap<K, V, E>
where
    K: Hash + Eq + Clone + Send + Sync + Default,
    V: Clone + Send + Sync + Default,
    E: EvictionCallback<K, V> + Send + Sync + Clone,
{}

unsafe impl<K, V, E> Sync for ConcurrentLruMap<K, V, E>
where
    K: Hash + Eq + Clone + Send + Sync + Default,
    V: Clone + Send + Sync + Default,
    E: EvictionCallback<K, V> + Send + Sync + Clone,
{}

#[cfg(test)]
mod tests {
    use super::*;
    // Test-specific atomic utilities
    // use std::sync::atomic::{AtomicUsize, Ordering};
    use std::sync::Arc;
    use std::thread;
    
    #[test]
    fn test_concurrent_basic_operations() {
        let cache = ConcurrentLruMap::new(64, 4).unwrap();
        
        cache.put(1, "one".to_string()).unwrap();
        cache.put(2, "two".to_string()).unwrap();
        
        assert_eq!(cache.get(&1), Some("one".to_string()));
        assert_eq!(cache.get(&2), Some("two".to_string()));
        assert_eq!(cache.len(), 2);
    }
    
    #[test]
    fn test_concurrent_access() {
        let cache = Arc::new(ConcurrentLruMap::new(1000, 8).unwrap());
        let num_threads = 4;
        let operations_per_thread = 100;
        
        let handles: Vec<_> = (0..num_threads)
            .map(|thread_id| {
                let cache = cache.clone();
                thread::spawn(move || {
                    for i in 0..operations_per_thread {
                        let key = thread_id * operations_per_thread + i;
                        let value = format!("value_{}", key);
                        
                        cache.put(key, value.clone()).unwrap();
                        assert_eq!(cache.get(&key), Some(value));
                    }
                })
            })
            .collect();
        
        for handle in handles {
            handle.join().unwrap();
        }
        
        assert_eq!(cache.len(), num_threads * operations_per_thread);
    }
    
    #[test]
    fn test_shard_distribution() {
        let cache = ConcurrentLruMap::new(64, 4).unwrap();
        
        // Insert many items
        for i in 0..64 {
            cache.put(i, format!("value_{}", i)).unwrap();
        }
        
        let shard_sizes = cache.shard_sizes();
        
        // Check that items are distributed across shards
        assert_eq!(shard_sizes.len(), 4);
        assert!(shard_sizes.iter().all(|&size| size > 0));
        
        // Total should equal cache size
        let total: usize = shard_sizes.iter().sum();
        assert_eq!(total, cache.len());
    }
    
    #[test]
    fn test_statistics() {
        let cache = ConcurrentLruMap::new(32, 2).unwrap();
        
        // Perform operations
        cache.put(1, "one".to_string()).unwrap();
        cache.put(2, "two".to_string()).unwrap();
        
        cache.get(&1); // Hit
        cache.get(&3); // Miss
        
        let stats = cache.stats();
        assert!(stats.hit_ratio() > 0.0);
        assert_eq!(stats.total_entries(), 2);
    }
    
    #[test]
    fn test_load_balancing_strategies() {
        // Test different load balancing strategies
        let configs = vec![
            ConcurrentLruMapConfig {
                base_config: LruMapConfig { capacity: 16, ..Default::default() },
                shard_count: 4,
                load_balancing: LoadBalancingStrategy::Hash,
            },
            ConcurrentLruMapConfig {
                base_config: LruMapConfig { capacity: 16, ..Default::default() },
                shard_count: 4,
                load_balancing: LoadBalancingStrategy::RoundRobin,
            },
            ConcurrentLruMapConfig {
                base_config: LruMapConfig { capacity: 16, ..Default::default() },
                shard_count: 4,
                load_balancing: LoadBalancingStrategy::ThreadAffinity,
            },
        ];
        
        for config in configs {
            let cache = ConcurrentLruMap::with_config(config).unwrap();
            
            // Insert items and verify distribution
            for i in 0..16 {
                cache.put(i, format!("value_{}", i)).unwrap();
            }
            
            assert_eq!(cache.len(), 16);
            
            // All items should be retrievable
            for i in 0..16 {
                assert!(cache.get(&i).is_some());
            }
        }
    }
}