oxigdal-edge 0.1.4

Edge computing platform for OxiGDAL with offline-first architecture and minimal footprint
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
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
//! Local-first caching for edge devices
//!
//! Provides efficient caching mechanisms optimized for resource-constrained
//! edge devices with offline-first architecture.

use crate::error::{EdgeError, Result};
use bytes::Bytes;
use chrono::{DateTime, Utc};
use lru::LruCache;
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::num::NonZeroUsize;
use std::path::PathBuf;
use std::sync::Arc;

/// Cache eviction policy
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum CachePolicy {
    /// Least Recently Used
    Lru,
    /// Least Frequently Used
    Lfu,
    /// Time To Live
    Ttl,
    /// Size-based eviction
    SizeBased,
}

/// Cache configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CacheConfig {
    /// Maximum cache size in bytes
    pub max_size: usize,
    /// Cache eviction policy
    pub policy: CachePolicy,
    /// Time to live in seconds
    pub ttl_secs: Option<u64>,
    /// Enable persistent cache
    pub persistent: bool,
    /// Cache directory for persistent storage
    pub cache_dir: Option<PathBuf>,
    /// Maximum number of entries
    pub max_entries: usize,
}

impl Default for CacheConfig {
    fn default() -> Self {
        Self {
            max_size: crate::DEFAULT_CACHE_SIZE,
            policy: CachePolicy::Lru,
            ttl_secs: Some(3600), // 1 hour
            persistent: false,
            cache_dir: None,
            max_entries: 1000,
        }
    }
}

impl CacheConfig {
    /// Create minimal cache config for embedded devices
    pub fn minimal() -> Self {
        Self {
            max_size: 1024 * 1024, // 1 MB
            policy: CachePolicy::Lru,
            ttl_secs: Some(1800), // 30 minutes
            persistent: false,
            cache_dir: None,
            max_entries: 100,
        }
    }

    /// Create config for offline-first mode
    pub fn offline_first() -> Self {
        Self {
            max_size: 50 * 1024 * 1024, // 50 MB
            policy: CachePolicy::Lru,
            ttl_secs: None, // No expiration
            persistent: true,
            cache_dir: Some(PathBuf::from(".oxigdal_cache")),
            max_entries: 5000,
        }
    }
}

/// Cache entry with metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CacheEntry {
    /// Entry key
    pub key: String,
    /// Cached data
    pub data: Bytes,
    /// Creation timestamp
    pub created_at: DateTime<Utc>,
    /// Last access timestamp
    pub accessed_at: DateTime<Utc>,
    /// Access count
    pub access_count: u64,
    /// Entry size in bytes
    pub size: usize,
    /// Optional expiration time
    pub expires_at: Option<DateTime<Utc>>,
}

impl CacheEntry {
    /// Create new cache entry
    pub fn new(key: String, data: Bytes) -> Self {
        let now = Utc::now();
        let size = data.len();
        Self {
            key,
            data,
            created_at: now,
            accessed_at: now,
            access_count: 0,
            size,
            expires_at: None,
        }
    }

    /// Create entry with TTL
    pub fn with_ttl(key: String, data: Bytes, ttl_secs: u64) -> Self {
        let mut entry = Self::new(key, data);
        entry.expires_at = Some(Utc::now() + chrono::Duration::seconds(ttl_secs as i64));
        entry
    }

    /// Check if entry is expired
    pub fn is_expired(&self) -> bool {
        if let Some(expires_at) = self.expires_at {
            Utc::now() > expires_at
        } else {
            false
        }
    }

    /// Mark entry as accessed
    pub fn mark_accessed(&mut self) {
        self.accessed_at = Utc::now();
        self.access_count = self.access_count.saturating_add(1);
    }
}

/// Edge cache implementation
pub struct Cache {
    config: CacheConfig,
    lru_cache: Arc<RwLock<LruCache<String, CacheEntry>>>,
    metadata: Arc<RwLock<HashMap<String, CacheMetadata>>>,
    current_size: Arc<RwLock<usize>>,
    persistent_storage: Option<sled::Db>,
}

/// Cache metadata for tracking entry statistics
#[derive(Debug, Clone)]
struct CacheMetadata {
    /// Size of the cached entry in bytes
    size: usize,
    /// Number of times this entry has been accessed
    access_count: u64,
}

impl Cache {
    /// Create new cache with configuration
    pub fn new(config: CacheConfig) -> Result<Self> {
        let max_entries = NonZeroUsize::new(config.max_entries)
            .ok_or_else(|| EdgeError::invalid_config("max_entries must be greater than 0"))?;

        let lru_cache = Arc::new(RwLock::new(LruCache::new(max_entries)));
        let metadata = Arc::new(RwLock::new(HashMap::new()));
        let current_size = Arc::new(RwLock::new(0));

        let persistent_storage = if config.persistent {
            if let Some(cache_dir) = &config.cache_dir {
                let db = sled::open(cache_dir).map_err(|e| EdgeError::storage(e.to_string()))?;
                Some(db)
            } else {
                None
            }
        } else {
            None
        };

        Ok(Self {
            config,
            lru_cache,
            metadata,
            current_size,
            persistent_storage,
        })
    }

    /// Get entry from cache
    pub fn get(&self, key: &str) -> Result<Option<Bytes>> {
        // Try memory cache first
        let mut cache = self.lru_cache.write();
        if let Some(entry) = cache.get_mut(key) {
            if !entry.is_expired() {
                entry.mark_accessed();
                return Ok(Some(entry.data.clone()));
            } else {
                // Remove expired entry
                cache.pop(key);
                let mut meta = self.metadata.write();
                meta.remove(key);
            }
        }
        drop(cache);

        // Try persistent storage if enabled
        if let Some(db) = &self.persistent_storage {
            if let Some(value) = db.get(key).map_err(|e| EdgeError::storage(e.to_string()))? {
                let entry: CacheEntry = serde_json::from_slice(&value)
                    .map_err(|e| EdgeError::deserialization(e.to_string()))?;

                if !entry.is_expired() {
                    // Restore to memory cache
                    let mut cache = self.lru_cache.write();
                    cache.put(key.to_string(), entry.clone());
                    return Ok(Some(entry.data));
                }
            }
        }

        Ok(None)
    }

    /// Put entry into cache
    pub fn put(&self, key: String, data: Bytes) -> Result<()> {
        let entry_size = data.len();

        // Check size constraint
        if entry_size > self.config.max_size {
            return Err(EdgeError::cache(format!(
                "Entry size {} exceeds max cache size {}",
                entry_size, self.config.max_size
            )));
        }

        // Create cache entry
        let entry = if let Some(ttl) = self.config.ttl_secs {
            CacheEntry::with_ttl(key.clone(), data, ttl)
        } else {
            CacheEntry::new(key.clone(), data)
        };

        // Evict entries if necessary
        self.evict_if_needed(entry_size)?;

        // Insert into memory cache
        let mut cache = self.lru_cache.write();
        cache.put(key.clone(), entry.clone());
        drop(cache);

        // Update metadata
        let mut meta = self.metadata.write();
        meta.insert(
            key.clone(),
            CacheMetadata {
                size: entry_size,
                access_count: 0,
            },
        );
        drop(meta);

        // Update size
        let mut current_size = self.current_size.write();
        *current_size = current_size.saturating_add(entry_size);
        drop(current_size);

        // Persist if enabled
        if let Some(db) = &self.persistent_storage {
            let serialized =
                serde_json::to_vec(&entry).map_err(|e| EdgeError::serialization(e.to_string()))?;
            db.insert(key.as_bytes(), serialized)
                .map_err(|e| EdgeError::storage(e.to_string()))?;
        }

        Ok(())
    }

    /// Remove entry from cache
    pub fn remove(&self, key: &str) -> Result<Option<Bytes>> {
        let mut cache = self.lru_cache.write();
        let entry = cache.pop(key);
        drop(cache);

        if let Some(ref e) = entry {
            // Update metadata
            let mut meta = self.metadata.write();
            meta.remove(key);
            drop(meta);

            // Update size
            let mut current_size = self.current_size.write();
            *current_size = current_size.saturating_sub(e.size);
            drop(current_size);

            // Remove from persistent storage
            if let Some(db) = &self.persistent_storage {
                db.remove(key.as_bytes())
                    .map_err(|e| EdgeError::storage(e.to_string()))?;
            }
        }

        Ok(entry.map(|e| e.data))
    }

    /// Clear all cache entries
    pub fn clear(&self) -> Result<()> {
        let mut cache = self.lru_cache.write();
        cache.clear();
        drop(cache);

        let mut meta = self.metadata.write();
        meta.clear();
        drop(meta);

        let mut current_size = self.current_size.write();
        *current_size = 0;
        drop(current_size);

        if let Some(db) = &self.persistent_storage {
            db.clear().map_err(|e| EdgeError::storage(e.to_string()))?;
        }

        Ok(())
    }

    /// Get current cache size
    pub fn size(&self) -> usize {
        *self.current_size.read()
    }

    /// Get number of cached entries
    pub fn len(&self) -> usize {
        self.lru_cache.read().len()
    }

    /// Check if cache is empty
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Evict entries based on policy
    fn evict_if_needed(&self, new_entry_size: usize) -> Result<()> {
        let current_size = *self.current_size.read();
        let target_size = self.config.max_size.saturating_sub(new_entry_size);

        if current_size <= target_size {
            return Ok(());
        }

        let mut to_evict = Vec::new();
        let mut freed_size = 0;

        match self.config.policy {
            CachePolicy::Lru => {
                // LRU eviction is handled by LruCache automatically
                let mut cache = self.lru_cache.write();
                while freed_size < current_size.saturating_sub(target_size) && !cache.is_empty() {
                    if let Some((key, entry)) = cache.pop_lru() {
                        freed_size = freed_size.saturating_add(entry.size);
                        to_evict.push(key);
                    }
                }
            }
            CachePolicy::Lfu => {
                // Evict least frequently used
                let meta = self.metadata.read();
                let mut entries: Vec<_> = meta.iter().collect();
                entries.sort_by_key(|(_, m)| m.access_count);

                for (key, metadata) in entries {
                    if freed_size >= current_size.saturating_sub(target_size) {
                        break;
                    }
                    freed_size = freed_size.saturating_add(metadata.size);
                    to_evict.push(key.clone());
                }
            }
            CachePolicy::Ttl => {
                // Evict expired entries first
                let cache = self.lru_cache.read();
                for (key, entry) in cache.iter() {
                    if entry.is_expired() {
                        freed_size = freed_size.saturating_add(entry.size);
                        to_evict.push(key.clone());
                    }
                }
            }
            CachePolicy::SizeBased => {
                // Evict largest entries first
                let meta = self.metadata.read();
                let mut entries: Vec<_> = meta.iter().collect();
                entries.sort_by_key(|(_, m)| std::cmp::Reverse(m.size));

                for (key, metadata) in entries {
                    if freed_size >= current_size.saturating_sub(target_size) {
                        break;
                    }
                    freed_size = freed_size.saturating_add(metadata.size);
                    to_evict.push(key.clone());
                }
            }
        }

        // Remove evicted entries
        for key in to_evict {
            self.remove(&key)?;
        }

        Ok(())
    }

    /// Get cache statistics
    pub fn stats(&self) -> CacheStats {
        CacheStats {
            entries: self.len(),
            size_bytes: self.size(),
            max_size_bytes: self.config.max_size,
            max_entries: self.config.max_entries,
            policy: self.config.policy,
        }
    }
}

/// Cache statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CacheStats {
    /// Number of entries
    pub entries: usize,
    /// Current size in bytes
    pub size_bytes: usize,
    /// Maximum size in bytes
    pub max_size_bytes: usize,
    /// Maximum number of entries
    pub max_entries: usize,
    /// Cache policy
    pub policy: CachePolicy,
}

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

    #[test]
    fn test_cache_creation() {
        let config = CacheConfig::default();
        let cache = Cache::new(config);
        assert!(cache.is_ok());
    }

    #[test]
    fn test_cache_put_get() -> Result<()> {
        let config = CacheConfig::minimal();
        let cache = Cache::new(config)?;

        let key = "test_key".to_string();
        let data = Bytes::from("test_data");

        cache.put(key.clone(), data.clone())?;
        let retrieved = cache.get(&key)?;

        assert_eq!(retrieved, Some(data));
        Ok(())
    }

    #[test]
    fn test_cache_eviction() -> Result<()> {
        let mut config = CacheConfig::minimal();
        config.max_size = 100;
        config.max_entries = 10;

        let cache = Cache::new(config)?;

        // Fill cache
        for i in 0..5 {
            let key = format!("key_{}", i);
            let data = Bytes::from(vec![0u8; 25]);
            cache.put(key, data)?;
        }

        // Should trigger eviction
        let key = "new_key".to_string();
        let data = Bytes::from(vec![0u8; 25]);
        cache.put(key.clone(), data.clone())?;

        let retrieved = cache.get(&key)?;
        assert_eq!(retrieved, Some(data));

        Ok(())
    }

    #[test]
    fn test_cache_remove() -> Result<()> {
        let config = CacheConfig::minimal();
        let cache = Cache::new(config)?;

        let key = "test_key".to_string();
        let data = Bytes::from("test_data");

        cache.put(key.clone(), data.clone())?;
        let removed = cache.remove(&key)?;

        assert_eq!(removed, Some(data));
        assert_eq!(cache.get(&key)?, None);

        Ok(())
    }

    #[test]
    fn test_cache_clear() -> Result<()> {
        let config = CacheConfig::minimal();
        let cache = Cache::new(config)?;

        for i in 0..5 {
            let key = format!("key_{}", i);
            let data = Bytes::from(format!("data_{}", i));
            cache.put(key, data)?;
        }

        assert_eq!(cache.len(), 5);

        cache.clear()?;
        assert_eq!(cache.len(), 0);
        assert!(cache.is_empty());

        Ok(())
    }

    #[test]
    fn test_entry_expiration() {
        let key = "test".to_string();
        let data = Bytes::from("data");

        let entry = CacheEntry::with_ttl(key, data, 0);
        std::thread::sleep(std::time::Duration::from_millis(10));
        assert!(entry.is_expired());
    }
}