do-memory-storage-turso 0.1.30

Turso/libSQL storage backend for the do-memory-core episodic learning system
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
//! Adaptive TTL Cache Implementation
//!
//! This module provides a generic adaptive TTL cache that:
//! - Adjusts TTL based on access patterns (hot items get longer TTL, cold items get shorter)
//! - Performs background cleanup of expired entries
//! - Tracks statistics (hit rate, avg TTL, evictions)
//! - Provides thread-safe operations
//!
//! ## Architecture
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────┐
//! │                    AdaptiveTTLCache<K, V>                   │
//! ├─────────────────────────────────────────────────────────────┤
//! │  ┌─────────────┐  ┌─────────────┐  ┌─────────────────────┐  │
//! │  │   entries   │  │    stats    │  │   cleanup_task      │  │
//! │  │  HashMap    │  │ CacheStats  │  │   (background)      │  │
//! │  └─────────────┘  └─────────────┘  └─────────────────────┘  │
//! └─────────────────────────────────────────────────────────────┘
//!//!                    ┌─────────┴─────────┐
//!                    ▼                   ▼
//!           ┌────────────────┐  ┌────────────────┐
//!           │  CacheEntry<V> │  │   TTLConfig    │
//!           │ - value: V     │  │ - base_ttl     │
//!           │ - created_at   │  │ - min/max_ttl  │
//!           │ - access_count │  │ - thresholds   │
//!           │ - last_access  │  │ - adaptation   │
//!           └────────────────┘  └────────────────┘
//! ```

use super::ttl_config::{TTLConfig, TTLConfigError};
#[path = "adaptive_ttl_stats.rs"]
mod stats;
pub use stats::{CacheStats, CacheStatsSnapshot};
use std::collections::HashMap;
use std::hash::Hash;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;
use tokio::task::JoinHandle;
use tokio::time::{Duration as TokioDuration, interval};
use tracing::{debug, info, trace};

/// A cache entry with metadata for adaptive TTL management
#[derive(Debug, Clone)]
pub struct CacheEntry<V> {
    /// The cached value
    pub value: V,
    /// When the entry was created
    pub created_at: Instant,
    /// Number of times this entry has been accessed
    pub access_count: u64,
    /// When the entry was last accessed
    pub last_accessed: Instant,
    /// Current TTL for this entry (adaptive)
    pub current_ttl: Duration,
    /// Access timestamps for pattern analysis (sliding window)
    access_history: Vec<Instant>,
}

impl<V> CacheEntry<V> {
    /// Create a new cache entry with the given value and initial TTL
    pub fn new(value: V, initial_ttl: Duration) -> Self {
        let now = Instant::now();
        Self {
            value,
            created_at: now,
            access_count: 0,
            last_accessed: now,
            current_ttl: initial_ttl,
            access_history: Vec::with_capacity(20),
        }
    }

    /// Record an access to this entry
    pub fn record_access(&mut self, config: &TTLConfig) {
        let now = Instant::now();
        self.access_count += 1;
        self.last_accessed = now;

        // Add to access history
        self.access_history.push(now);

        // Trim access history to window size
        let window_start = now - Duration::from_secs(config.access_window_secs);
        self.access_history.retain(|&t| t >= window_start);

        // Update TTL based on access pattern
        if config.enable_adaptive_ttl {
            let window_accesses = self.access_history.len() as u64;
            self.current_ttl = config.calculate_ttl(self.current_ttl, window_accesses);
        }
    }

    /// Check if this entry has expired
    pub fn is_expired(&self) -> bool {
        Instant::now().duration_since(self.created_at) > self.current_ttl
    }

    /// Get the remaining TTL for this entry
    pub fn remaining_ttl(&self) -> Duration {
        let elapsed = Instant::now().duration_since(self.created_at);
        self.current_ttl.saturating_sub(elapsed)
    }

    /// Get the access frequency (accesses per minute) over the window
    pub fn access_frequency(&self, window_secs: u64) -> f64 {
        if self.access_history.is_empty() {
            return 0.0;
        }

        let window_duration = Duration::from_secs(window_secs);
        let actual_window = Instant::now()
            .duration_since(self.created_at)
            .min(window_duration);

        if actual_window.as_secs() == 0 {
            return self.access_history.len() as f64;
        }

        let accesses = self.access_history.len() as f64;
        let minutes = actual_window.as_secs_f64() / 60.0;
        accesses / minutes
    }

    /// Reset the entry with a new value and TTL
    pub fn reset(&mut self, value: V, ttl: Duration) {
        let now = Instant::now();
        self.value = value;
        self.created_at = now;
        self.access_count = 0;
        self.last_accessed = now;
        self.current_ttl = ttl;
        self.access_history.clear();
    }
}

/// Adaptive TTL cache with generic key and value types
///
/// This cache automatically adjusts the TTL of entries based on their access patterns:
/// - Frequently accessed items (hot) get extended TTL
/// - Rarely accessed items (cold) get reduced TTL
/// - Expired entries are cleaned up in the background
pub struct AdaptiveTTLCache<K, V> {
    /// Cache entries
    entries: Arc<RwLock<HashMap<K, CacheEntry<V>>>>,
    /// Cache configuration
    config: TTLConfig,
    /// Cache statistics
    stats: Arc<CacheStats>,
    /// Background cleanup task handle
    cleanup_task: Option<JoinHandle<()>>,
}

impl<K, V> AdaptiveTTLCache<K, V>
where
    K: Eq + Hash + Clone + Send + Sync + 'static,
    V: Clone + Send + Sync + 'static,
{
    /// Create a new adaptive TTL cache with the given configuration
    ///
    /// # Errors
    ///
    /// Returns `TTLConfigError` if the configuration is invalid
    pub fn new(config: TTLConfig) -> Result<Self, TTLConfigError> {
        config.validate()?;

        let entries = Arc::new(RwLock::new(HashMap::new()));
        let stats = Arc::new(CacheStats::new());

        // Start background cleanup task if enabled
        let cleanup_task = if config.enable_background_cleanup {
            Some(Self::start_cleanup_task(
                Arc::clone(&entries),
                Arc::clone(&stats),
                config.cleanup_interval,
                config.max_entries,
            ))
        } else {
            None
        };

        info!(
            "AdaptiveTTLCache initialized: max_entries={}, base_ttl={:?}, cleanup_interval={:?}",
            config.max_entries, config.base_ttl, config.cleanup_interval
        );

        Ok(Self {
            entries,
            config,
            stats,
            cleanup_task,
        })
    }

    /// Create a new cache with default configuration
    pub fn default_config() -> Result<Self, TTLConfigError> {
        Self::new(TTLConfig::default())
    }

    /// Get a value from the cache
    ///
    /// Returns `Some(value)` if the key exists and hasn't expired,
    /// `None` otherwise.
    pub async fn get(&self, key: &K) -> Option<V> {
        let mut entries = self.entries.write().await;

        if let Some(entry) = entries.get_mut(key) {
            if entry.is_expired() {
                // Entry has expired, remove it
                trace!("Entry expired for key, removing");
                self.stats.record_ttl_expiration();
                entries.remove(key);
                self.stats.update_entry_count(entries.len());
                return None;
            }

            // Record the access and update TTL
            entry.record_access(&self.config);
            self.stats.record_hit();
            self.stats.record_ttl_adaptation(entry.current_ttl);

            Some(entry.value.clone())
        } else {
            self.stats.record_miss();
            None
        }
    }

    /// Insert a value into the cache
    ///
    /// If the key already exists, the value is updated and the entry is reset.
    pub async fn insert(&self, key: K, value: V) {
        let mut entries = self.entries.write().await;

        // Check if we need to evict entries
        if entries.len() >= self.config.max_entries && !entries.contains_key(&key) {
            self.evict_oldest(&mut entries).await;
        }

        let entry = CacheEntry::new(value, self.config.base_ttl);
        entries.insert(key, entry);
        self.stats.update_entry_count(entries.len());

        debug!(
            "Inserted entry, cache size: {}/{}",
            entries.len(),
            self.config.max_entries
        );
    }

    /// Remove a value from the cache
    ///
    /// Returns `true` if the key existed and was removed, `false` otherwise.
    pub async fn remove(&self, key: &K) -> bool {
        let mut entries = self.entries.write().await;

        if entries.remove(key).is_some() {
            self.stats.record_removal();
            self.stats.update_entry_count(entries.len());
            true
        } else {
            false
        }
    }

    /// Check if a key exists in the cache (and hasn't expired)
    pub async fn contains(&self, key: &K) -> bool {
        let entries = self.entries.read().await;

        if let Some(entry) = entries.get(key) {
            !entry.is_expired()
        } else {
            false
        }
    }

    /// Get the current TTL for an entry
    pub async fn ttl(&self, key: &K) -> Option<Duration> {
        let entries = self.entries.read().await;
        entries.get(key).map(|e| e.current_ttl)
    }

    /// Get the remaining TTL for an entry
    pub async fn remaining_ttl(&self, key: &K) -> Option<Duration> {
        let entries = self.entries.read().await;
        entries.get(key).map(|e| e.remaining_ttl())
    }

    #[cfg(test)]
    pub async fn force_set_entry_created_at(&self, key: &K, created_at: Instant) {
        let mut entries = self.entries.write().await;
        if let Some(entry) = entries.get_mut(key) {
            entry.created_at = created_at;
            entry.last_accessed = created_at;
            entry.access_history.clear();
        }
    }

    /// Get the access count for an entry
    pub async fn access_count(&self, key: &K) -> Option<u64> {
        let entries = self.entries.read().await;
        entries.get(key).map(|e| e.access_count)
    }

    /// Get the number of entries in the cache
    pub async fn len(&self) -> usize {
        let entries = self.entries.read().await;
        entries.len()
    }

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

    /// Clear all entries from the cache
    pub async fn clear(&self) {
        let mut entries = self.entries.write().await;
        let count = entries.len();
        entries.clear();
        self.stats.update_entry_count(0);
        info!("Cleared {} entries from cache", count);
    }

    /// Get a snapshot of the current statistics
    pub fn stats(&self) -> CacheStatsSnapshot {
        self.stats.snapshot()
    }

    /// Get the cache configuration
    pub fn config(&self) -> &TTLConfig {
        &self.config
    }

    /// Manually trigger cleanup of expired entries
    ///
    /// Returns the number of entries removed.
    pub async fn cleanup_expired(&self) -> usize {
        let mut entries = self.entries.write().await;
        let before_count = entries.len();

        entries.retain(|_key, entry| {
            if entry.is_expired() {
                self.stats.record_ttl_expiration();
                false
            } else {
                true
            }
        });

        let removed = before_count - entries.len();
        self.stats.update_entry_count(entries.len());
        self.stats.record_cleanup();

        if removed > 0 {
            debug!("Cleaned up {} expired entries", removed);
        }

        removed
    }

    /// Get all keys in the cache (that haven't expired)
    pub async fn keys(&self) -> Vec<K> {
        let entries = self.entries.read().await;
        entries
            .iter()
            .filter(|(_, entry)| !entry.is_expired())
            .map(|(key, _)| key.clone())
            .collect()
    }

    /// Evict the oldest entry (LRU eviction)
    async fn evict_oldest(&self, entries: &mut HashMap<K, CacheEntry<V>>) {
        if let Some(oldest_key) = entries
            .iter()
            .min_by_key(|(_, entry)| entry.last_accessed)
            .map(|(key, _)| key.clone())
        {
            if let Some(entry) = entries.remove(&oldest_key) {
                // Estimate bytes (simplified - just use size of value)
                let estimated_bytes = std::mem::size_of_val(&entry.value) as u64;
                self.stats.record_eviction(estimated_bytes);
                debug!("Evicted oldest entry");
            }
        }
    }

    /// Start the background cleanup task
    fn start_cleanup_task(
        entries: Arc<RwLock<HashMap<K, CacheEntry<V>>>>,
        stats: Arc<CacheStats>,
        interval_duration: Duration,
        _max_entries: usize,
    ) -> JoinHandle<()> {
        tokio::spawn(async move {
            let mut ticker = interval(TokioDuration::from_secs(interval_duration.as_secs()));

            loop {
                ticker.tick().await;

                let mut entries_guard = entries.write().await;
                let before_count = entries_guard.len();

                // Remove expired entries
                entries_guard.retain(|_key, entry| !entry.is_expired());
                let expired_count = before_count - entries_guard.len();
                for _ in 0..expired_count {
                    stats.record_ttl_expiration();
                }

                let removed = before_count - entries_guard.len();
                stats.update_entry_count(entries_guard.len());
                stats.record_cleanup();

                if removed > 0 {
                    debug!("Background cleanup removed {} expired entries", removed);
                }

                drop(entries_guard);
            }
        })
    }

    /// Stop the background cleanup task
    pub fn stop_cleanup(&mut self) {
        if let Some(task) = self.cleanup_task.take() {
            task.abort();
            info!("Background cleanup task stopped");
        }
    }
}

impl<K, V> Drop for AdaptiveTTLCache<K, V> {
    fn drop(&mut self) {
        if let Some(task) = self.cleanup_task.take() {
            task.abort();
        }
    }
}

#[cfg(test)]
#[path = "adaptive_ttl_tests.rs"]
mod tests;