code-mesh-core 0.1.0

High-performance, WASM-powered distributed swarm intelligence core library for concurrent code execution and neural mesh computing
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
//! High-performance caching system for code-mesh

use std::collections::HashMap;
use std::hash::Hash;
use std::sync::{Arc, RwLock};
use std::time::{Duration, Instant, SystemTime};
use lru::LruCache;
use std::num::NonZeroUsize;

/// Multi-level cache system with intelligent eviction
pub struct MultiLevelCache<K, V> 
where
    K: Hash + Eq + Clone,
    V: Clone,
{
    l1_cache: Arc<RwLock<LruCache<K, CacheEntry<V>>>>, // Hot cache (fast access)
    l2_cache: Arc<RwLock<HashMap<K, CacheEntry<V>>>>,  // Warm cache (larger capacity)
    config: CacheConfig,
    stats: Arc<RwLock<CacheStats>>,
}

impl<K, V> MultiLevelCache<K, V>
where
    K: Hash + Eq + Clone,
    V: Clone,
{
    pub fn new(config: CacheConfig) -> Self {
        let l1_size = NonZeroUsize::new(config.l1_size).unwrap_or(NonZeroUsize::new(100).unwrap());
        
        Self {
            l1_cache: Arc::new(RwLock::new(LruCache::new(l1_size))),
            l2_cache: Arc::new(RwLock::new(HashMap::new())),
            config,
            stats: Arc::new(RwLock::new(CacheStats::new())),
        }
    }

    /// Get a value from the cache
    pub fn get(&self, key: &K) -> Option<V> {
        let start_time = Instant::now();
        
        // Try L1 cache first (hot cache)
        {
            let mut l1 = self.l1_cache.write().unwrap();
            if let Some(entry) = l1.get(key) {
                if !entry.is_expired() {
                    self.record_hit(CacheLevel::L1, start_time.elapsed());
                    return Some(entry.value.clone());
                } else {
                    // Remove expired entry
                    l1.pop(key);
                }
            }
        }

        // Try L2 cache (warm cache)
        {
            let mut l2 = self.l2_cache.write().unwrap();
            if let Some(entry) = l2.get(key) {
                if !entry.is_expired() {
                    // Promote to L1 cache
                    let mut l1 = self.l1_cache.write().unwrap();
                    l1.put(key.clone(), entry.clone());
                    
                    self.record_hit(CacheLevel::L2, start_time.elapsed());
                    return Some(entry.value.clone());
                } else {
                    // Remove expired entry
                    l2.remove(key);
                }
            }
        }

        // Cache miss
        self.record_miss(start_time.elapsed());
        None
    }

    /// Put a value into the cache
    pub fn put(&self, key: K, value: V) {
        self.put_with_ttl(key, value, self.config.default_ttl);
    }

    /// Put a value with custom TTL
    pub fn put_with_ttl(&self, key: K, value: V, ttl: Duration) {
        let entry = CacheEntry {
            value,
            inserted_at: SystemTime::now(),
            ttl,
            access_count: 1,
            last_accessed: SystemTime::now(),
        };

        // Always insert into L1 cache for hot access
        {
            let mut l1 = self.l1_cache.write().unwrap();
            l1.put(key, entry);
        }

        self.record_insert();
    }

    /// Remove a value from the cache
    pub fn remove(&self, key: &K) -> Option<V> {
        // Remove from both levels
        let l1_result = {
            let mut l1 = self.l1_cache.write().unwrap();
            l1.pop(key)
        };

        let l2_result = {
            let mut l2 = self.l2_cache.write().unwrap();
            l2.remove(key)
        };

        l1_result.or(l2_result).map(|entry| entry.value)
    }

    /// Clear all entries
    pub fn clear(&self) {
        {
            let mut l1 = self.l1_cache.write().unwrap();
            l1.clear();
        }
        {
            let mut l2 = self.l2_cache.write().unwrap();
            l2.clear();
        }
        
        {
            let mut stats = self.stats.write().unwrap();
            *stats = CacheStats::new();
        }
    }

    /// Get cache statistics
    pub fn stats(&self) -> CacheStats {
        self.stats.read().unwrap().clone()
    }

    /// Cleanup expired entries
    pub fn cleanup_expired(&self) {
        let now = SystemTime::now();
        
        // Cleanup L1
        {
            let mut l1 = self.l1_cache.write().unwrap();
            let keys_to_remove: Vec<K> = l1.iter()
                .filter(|(_, entry)| entry.is_expired_at(now))
                .map(|(key, _)| key.clone())
                .collect();
            
            for key in keys_to_remove {
                l1.pop(&key);
            }
        }

        // Cleanup L2
        {
            let mut l2 = self.l2_cache.write().unwrap();
            l2.retain(|_, entry| !entry.is_expired_at(now));
        }
    }

    /// Get cache size information
    pub fn size_info(&self) -> CacheSizeInfo {
        let l1_size = self.l1_cache.read().unwrap().len();
        let l2_size = self.l2_cache.read().unwrap().len();
        
        CacheSizeInfo {
            l1_entries: l1_size,
            l2_entries: l2_size,
            total_entries: l1_size + l2_size,
            l1_capacity: self.config.l1_size,
            l2_capacity: self.config.l2_size,
        }
    }

    fn record_hit(&self, level: CacheLevel, duration: Duration) {
        let mut stats = self.stats.write().unwrap();
        stats.hits += 1;
        stats.total_access_time += duration;
        
        match level {
            CacheLevel::L1 => stats.l1_hits += 1,
            CacheLevel::L2 => stats.l2_hits += 1,
        }
    }

    fn record_miss(&self, duration: Duration) {
        let mut stats = self.stats.write().unwrap();
        stats.misses += 1;
        stats.total_access_time += duration;
    }

    fn record_insert(&self) {
        let mut stats = self.stats.write().unwrap();
        stats.inserts += 1;
    }
}

/// Cache entry with metadata
#[derive(Clone, Debug)]
struct CacheEntry<V> {
    value: V,
    inserted_at: SystemTime,
    ttl: Duration,
    access_count: u64,
    last_accessed: SystemTime,
}

impl<V> CacheEntry<V> {
    fn is_expired(&self) -> bool {
        self.is_expired_at(SystemTime::now())
    }

    fn is_expired_at(&self, now: SystemTime) -> bool {
        if let Ok(elapsed) = now.duration_since(self.inserted_at) {
            elapsed > self.ttl
        } else {
            false
        }
    }
}

/// Cache configuration
#[derive(Debug, Clone)]
pub struct CacheConfig {
    pub l1_size: usize,        // Hot cache size
    pub l2_size: usize,        // Warm cache size  
    pub default_ttl: Duration, // Default time-to-live
    pub cleanup_interval: Duration, // How often to cleanup expired entries
}

impl Default for CacheConfig {
    fn default() -> Self {
        Self {
            l1_size: 100,
            l2_size: 1000,
            default_ttl: Duration::from_secs(300), // 5 minutes
            cleanup_interval: Duration::from_secs(60), // 1 minute
        }
    }
}

/// Cache statistics
#[derive(Debug, Clone)]
pub struct CacheStats {
    pub hits: u64,
    pub misses: u64,
    pub l1_hits: u64,
    pub l2_hits: u64,
    pub inserts: u64,
    pub total_access_time: Duration,
}

impl CacheStats {
    fn new() -> Self {
        Self {
            hits: 0,
            misses: 0,
            l1_hits: 0,
            l2_hits: 0,
            inserts: 0,
            total_access_time: Duration::default(),
        }
    }

    pub fn hit_rate(&self) -> f64 {
        let total = self.hits + self.misses;
        if total == 0 {
            0.0
        } else {
            self.hits as f64 / total as f64
        }
    }

    pub fn l1_hit_rate(&self) -> f64 {
        if self.hits == 0 {
            0.0
        } else {
            self.l1_hits as f64 / self.hits as f64
        }
    }

    pub fn average_access_time(&self) -> Duration {
        let total_accesses = self.hits + self.misses;
        if total_accesses == 0 {
            Duration::default()
        } else {
            self.total_access_time / total_accesses as u32
        }
    }
}

/// Cache size information
#[derive(Debug, Clone)]
pub struct CacheSizeInfo {
    pub l1_entries: usize,
    pub l2_entries: usize,
    pub total_entries: usize,
    pub l1_capacity: usize,
    pub l2_capacity: usize,
}

/// Cache level enum
enum CacheLevel {
    L1,
    L2,
}

/// Specialized cache implementations
pub type ResponseCache = MultiLevelCache<String, String>;
pub type FileCache = MultiLevelCache<std::path::PathBuf, Vec<u8>>;
pub type TokenCache = MultiLevelCache<String, Vec<String>>;

/// Factory for creating optimized caches
pub struct CacheFactory;

impl CacheFactory {
    /// Create a cache optimized for API responses
    pub fn create_response_cache() -> ResponseCache {
        let config = CacheConfig {
            l1_size: 50,
            l2_size: 500,
            default_ttl: Duration::from_secs(300), // 5 minutes
            cleanup_interval: Duration::from_secs(60),
        };
        MultiLevelCache::new(config)
    }

    /// Create a cache optimized for file content
    pub fn create_file_cache() -> FileCache {
        let config = CacheConfig {
            l1_size: 20,
            l2_size: 200,
            default_ttl: Duration::from_secs(600), // 10 minutes
            cleanup_interval: Duration::from_secs(120),
        };
        MultiLevelCache::new(config)
    }

    /// Create a cache optimized for tokens
    pub fn create_token_cache() -> TokenCache {
        let config = CacheConfig {
            l1_size: 100,
            l2_size: 1000,
            default_ttl: Duration::from_secs(1800), // 30 minutes
            cleanup_interval: Duration::from_secs(300),
        };
        MultiLevelCache::new(config)
    }
}

/// Cache manager for coordinating multiple caches
pub struct CacheManager {
    response_cache: ResponseCache,
    file_cache: FileCache,
    token_cache: TokenCache,
}

impl CacheManager {
    pub fn new() -> Self {
        Self {
            response_cache: CacheFactory::create_response_cache(),
            file_cache: CacheFactory::create_file_cache(),
            token_cache: CacheFactory::create_token_cache(),
        }
    }

    pub fn response_cache(&self) -> &ResponseCache {
        &self.response_cache
    }

    pub fn file_cache(&self) -> &FileCache {
        &self.file_cache
    }

    pub fn token_cache(&self) -> &TokenCache {
        &self.token_cache
    }

    /// Get overall cache statistics
    pub fn overall_stats(&self) -> OverallCacheStats {
        OverallCacheStats {
            response_cache: self.response_cache.stats(),
            file_cache: self.file_cache.stats(),
            token_cache: self.token_cache.stats(),
        }
    }

    /// Cleanup all caches
    pub fn cleanup_all(&self) {
        self.response_cache.cleanup_expired();
        self.file_cache.cleanup_expired();
        self.token_cache.cleanup_expired();
    }
}

/// Overall cache statistics
#[derive(Debug, Clone)]
pub struct OverallCacheStats {
    pub response_cache: CacheStats,
    pub file_cache: CacheStats,
    pub token_cache: CacheStats,
}

impl OverallCacheStats {
    pub fn total_hit_rate(&self) -> f64 {
        let total_hits = self.response_cache.hits + self.file_cache.hits + self.token_cache.hits;
        let total_accesses = total_hits + 
            self.response_cache.misses + self.file_cache.misses + self.token_cache.misses;
        
        if total_accesses == 0 {
            0.0
        } else {
            total_hits as f64 / total_accesses as f64
        }
    }
}