mappy-core 0.3.2

Core maplet data structure implementation
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
//! TTL (Time-To-Live) management for mappy
//!
//! Provides expiration tracking and automatic cleanup of expired keys.

use crate::MapletResult;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
use tokio::sync::RwLock;
use tokio::time::{Duration, interval};

/// TTL configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TTLConfig {
    /// Cleanup interval in seconds
    pub cleanup_interval_secs: u64,
    /// Maximum number of keys to clean up per batch
    pub max_cleanup_batch_size: usize,
    /// Enable background cleanup task
    pub enable_background_cleanup: bool,
}

impl Default for TTLConfig {
    fn default() -> Self {
        Self {
            cleanup_interval_secs: 60, // 1 minute
            max_cleanup_batch_size: 1000,
            enable_background_cleanup: true,
        }
    }
}

/// TTL entry tracking expiration time for a key
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TTLEntry {
    /// The key
    pub key: String,
    /// Expiration timestamp (Unix timestamp in seconds)
    pub expires_at: u64,
    /// Database ID
    pub db_id: u8,
}

impl TTLEntry {
    /// Create a new TTL entry
    #[must_use]
    pub fn new(key: String, db_id: u8, ttl_seconds: u64) -> Self {
        let expires_at = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs()
            + ttl_seconds;

        Self {
            key,
            expires_at,
            db_id,
        }
    }

    /// Check if this entry has expired
    #[must_use]
    pub fn is_expired(&self) -> bool {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();
        now >= self.expires_at
    }

    /// Get remaining TTL in seconds
    #[must_use]
    pub fn remaining_ttl(&self) -> i64 {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();
        #[allow(clippy::cast_possible_wrap)]
        {
            self.expires_at as i64 - now as i64
        }
    }
}

/// TTL manager for tracking and cleaning up expired keys
pub struct TTLManager {
    /// TTL configuration
    config: TTLConfig,
    /// Map of expiration time -> list of keys that expire at that time
    expiration_map: Arc<RwLock<BTreeMap<u64, Vec<TTLEntry>>>>,
    /// Map of key -> expiration time for fast lookups
    key_to_expiration: Arc<RwLock<std::collections::HashMap<String, u64>>>,
    /// Background cleanup task handle
    cleanup_handle: Arc<RwLock<Option<tokio::task::JoinHandle<()>>>>,
    /// Shutdown signal
    shutdown_tx: Arc<RwLock<Option<tokio::sync::oneshot::Sender<()>>>>,
}

impl TTLManager {
    /// Create a new TTL manager
    #[must_use]
    pub fn new(config: TTLConfig) -> Self {
        Self {
            config,
            expiration_map: Arc::new(RwLock::new(BTreeMap::new())),
            key_to_expiration: Arc::new(RwLock::new(std::collections::HashMap::new())),
            cleanup_handle: Arc::new(RwLock::new(None)),
            shutdown_tx: Arc::new(RwLock::new(None)),
        }
    }

    /// Set TTL for a key
    pub async fn set_ttl(&self, key: String, db_id: u8, ttl_seconds: u64) -> MapletResult<()> {
        let entry = TTLEntry::new(key.clone(), db_id, ttl_seconds);
        let expires_at = entry.expires_at;

        // Remove from old expiration time if it exists
        self.remove_ttl(&key).await?;

        // Add to new expiration time
        {
            let mut expiration_map = self.expiration_map.write().await;
            expiration_map
                .entry(expires_at)
                .or_insert_with(Vec::new)
                .push(entry);
        }

        // Update key lookup map
        {
            let mut key_map = self.key_to_expiration.write().await;
            key_map.insert(key, expires_at);
        }

        Ok(())
    }

    /// Get TTL for a key in seconds
    pub async fn get_ttl(&self, key: &str) -> MapletResult<Option<i64>> {
        let key_map = self.key_to_expiration.read().await;
        if let Some(&expires_at) = key_map.get(key) {
            let now = SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs();
            #[allow(clippy::cast_possible_wrap)]
            let remaining = expires_at as i64 - now as i64;
            Ok(Some(remaining.max(0)))
        } else {
            Ok(None)
        }
    }

    /// Remove TTL for a key
    pub async fn remove_ttl(&self, key: &str) -> MapletResult<()> {
        let mut key_map = self.key_to_expiration.write().await;
        if let Some(expires_at) = key_map.remove(key) {
            drop(key_map);

            let mut expiration_map = self.expiration_map.write().await;
            if let Some(entries) = expiration_map.get_mut(&expires_at) {
                entries.retain(|entry| entry.key != key);
                if entries.is_empty() {
                    expiration_map.remove(&expires_at);
                }
            }
        }

        Ok(())
    }

    /// Check if a key has expired
    pub async fn is_expired(&self, key: &str) -> MapletResult<bool> {
        let key_map = self.key_to_expiration.read().await;
        if let Some(&expires_at) = key_map.get(key) {
            let now = SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs();
            Ok(now >= expires_at)
        } else {
            Ok(false)
        }
    }

    /// Get all expired keys
    pub async fn get_expired_keys(&self) -> MapletResult<Vec<TTLEntry>> {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        let mut expired_entries = Vec::new();
        let mut expiration_map = self.expiration_map.write().await;

        // Get all entries that have expired
        let expired_times: Vec<u64> = expiration_map
            .range(..=now)
            .map(|(&time, _)| time)
            .collect();

        for time in expired_times {
            if let Some(entries) = expiration_map.remove(&time) {
                expired_entries.extend(entries);
            }
        }

        // Update key lookup map
        let mut key_map = self.key_to_expiration.write().await;
        for entry in &expired_entries {
            key_map.remove(&entry.key);
        }

        Ok(expired_entries)
    }

    /// Start background cleanup task
    pub async fn start_cleanup<F>(&self, mut cleanup_callback: F) -> MapletResult<()>
    where
        F: FnMut(Vec<TTLEntry>) -> MapletResult<()> + Send + Sync + 'static,
    {
        if !self.config.enable_background_cleanup {
            return Ok(());
        }

        let (shutdown_tx, mut shutdown_rx) = tokio::sync::oneshot::channel();
        let expiration_map = Arc::clone(&self.expiration_map);
        let key_to_expiration = Arc::clone(&self.key_to_expiration);
        let config = self.config.clone();

        let handle = tokio::spawn(async move {
            let mut interval = interval(Duration::from_secs(config.cleanup_interval_secs));

            loop {
                tokio::select! {
                    _ = interval.tick() => {
                        // Perform cleanup
                        let now = SystemTime::now()
                            .duration_since(UNIX_EPOCH)
                            .unwrap_or_default()
                            .as_secs();

                        let mut expired_entries = Vec::new();
                        {
                            let mut expiration_map = expiration_map.write().await;
                            let expired_times: Vec<u64> = expiration_map
                                .range(..=now)
                                .take(config.max_cleanup_batch_size)
                                .map(|(&time, _)| time)
                                .collect();

                            for time in expired_times {
                                if let Some(entries) = expiration_map.remove(&time) {
                                    expired_entries.extend(entries);
                                }
                            }
                        }

                        if !expired_entries.is_empty() {
                            // Update key lookup map
                            {
                                let mut key_map = key_to_expiration.write().await;
                                for entry in &expired_entries {
                                    key_map.remove(&entry.key);
                                }
                            }

                            // Call cleanup callback
                            if let Err(e) = cleanup_callback(expired_entries) {
                                eprintln!("TTL cleanup callback error: {e}");
                            }
                        }
                    }
                    _ = &mut shutdown_rx => {
                        break;
                    }
                }
            }
        });

        // Store cleanup handle and shutdown sender
        {
            let mut cleanup_handle = self.cleanup_handle.write().await;
            *cleanup_handle = Some(handle);
        }
        {
            let mut shutdown_tx_guard = self.shutdown_tx.write().await;
            *shutdown_tx_guard = Some(shutdown_tx);
        }

        Ok(())
    }

    /// Stop background cleanup task
    pub async fn stop_cleanup(&self) -> MapletResult<()> {
        // Send shutdown signal
        {
            let mut shutdown_tx = self.shutdown_tx.write().await;
            if let Some(tx) = shutdown_tx.take() {
                let _ = tx.send(());
            }
        }

        // Wait for cleanup task to finish
        {
            let mut cleanup_handle = self.cleanup_handle.write().await;
            if let Some(handle) = cleanup_handle.take() {
                let _ = handle.await;
            }
        }

        Ok(())
    }

    /// Get TTL statistics
    pub async fn get_stats(&self) -> MapletResult<TTLStats> {
        #[allow(clippy::significant_drop_in_scrutinee)] // Guards needed for entire operation
        let expiration_map = self.expiration_map.read().await;
        #[allow(clippy::significant_drop_in_scrutinee)] // Guards needed for entire operation
        let key_map = self.key_to_expiration.read().await;

        let total_keys = key_map.len();
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        let expired_count: usize = expiration_map
            .range(..=now)
            .map(|(_, entries)| entries.len())
            .sum();

        Ok(TTLStats {
            total_keys_with_ttl: total_keys as u64,
            expired_keys: expired_count as u64,
            next_expiration: expiration_map.range(now..).next().map(|(&time, _)| time),
        })
    }

    /// Clear all TTL entries
    pub async fn clear_all(&self) -> MapletResult<()> {
        {
            let mut expiration_map = self.expiration_map.write().await;
            expiration_map.clear();
        }
        {
            let mut key_map = self.key_to_expiration.write().await;
            key_map.clear();
        }
        Ok(())
    }
}

/// TTL statistics
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct TTLStats {
    /// Total number of keys with TTL set
    pub total_keys_with_ttl: u64,
    /// Number of expired keys waiting for cleanup
    pub expired_keys: u64,
    /// Next expiration timestamp (if any)
    pub next_expiration: Option<u64>,
}

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

    #[tokio::test]
    async fn test_ttl_manager_basic_operations() {
        let config = TTLConfig::default();
        let manager = TTLManager::new(config);

        // Set TTL for a key
        manager.set_ttl("key1".to_string(), 0, 60).await.unwrap();

        // Check TTL
        let ttl = manager.get_ttl("key1").await.unwrap();
        assert!(ttl.is_some());
        assert!(ttl.unwrap() <= 60);

        // Check if not expired
        assert!(!manager.is_expired("key1").await.unwrap());

        // Remove TTL
        manager.remove_ttl("key1").await.unwrap();
        assert!(manager.get_ttl("key1").await.unwrap().is_none());
    }

    #[tokio::test]
    async fn test_ttl_expiration() {
        let config = TTLConfig::default();
        let manager = TTLManager::new(config);

        // Set very short TTL
        manager.set_ttl("key1".to_string(), 0, 1).await.unwrap();

        // Wait for expiration
        tokio::time::sleep(Duration::from_millis(1100)).await;

        // Check if expired
        assert!(manager.is_expired("key1").await.unwrap());

        // Get expired keys
        let expired = manager.get_expired_keys().await.unwrap();
        assert!(!expired.is_empty());
        assert_eq!(expired[0].key, "key1");
    }

    #[tokio::test]
    async fn test_ttl_stats() {
        let config = TTLConfig::default();
        let manager = TTLManager::new(config);

        // Set some TTLs
        manager.set_ttl("key1".to_string(), 0, 60).await.unwrap();
        manager.set_ttl("key2".to_string(), 0, 120).await.unwrap();

        let stats = manager.get_stats().await.unwrap();
        assert_eq!(stats.total_keys_with_ttl, 2);
        assert_eq!(stats.expired_keys, 0);
        assert!(stats.next_expiration.is_some());
    }

    #[tokio::test]
    async fn test_ttl_clear_all() {
        let config = TTLConfig::default();
        let manager = TTLManager::new(config);

        // Set some TTLs
        manager.set_ttl("key1".to_string(), 0, 60).await.unwrap();
        manager.set_ttl("key2".to_string(), 0, 120).await.unwrap();

        // Clear all
        manager.clear_all().await.unwrap();

        let stats = manager.get_stats().await.unwrap();
        assert_eq!(stats.total_keys_with_ttl, 0);
    }
}