synadb 1.2.0

An AI-native embedded database
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
//! FreshnessIndexV2: Scalable staleness queries with Forward Decay
//!
//! Addresses the O(N) scan problem by using a deadline-based secondary index.
//!
//! # The Problem
//!
//! In a standard HashMap, querying "all items where freshness < threshold" requires
//! O(N) iteration because freshness changes continuously as time advances.
//!
//! # The Solution: Forward Decay
//!
//! Instead of computing freshness at query time, we precompute the **deadline**
//! when each entry becomes stale:
//!
//! ```text
//! freshness(t) = e^(-λ × (t - stored_at)) < threshold
//! deadline = stored_at + (-ln(threshold) / λ)
//! ```
//!
//! For threshold = 0.5 (half-life):
//! ```text
//! deadline = stored_at + ln(2) / λ
//! ```
//!
//! # Complexity
//!
//! | Operation | HashMap Only | With Deadline Index |
//! |-----------|--------------|---------------------|
//! | Insert | O(1) | O(log N) |
//! | Point query | O(1) | O(1) |
//! | Staleness scan | O(N) | O(k + log N) |
//! | Eviction | O(N) | O(k) |
//!
//! Where k = number of stale entries.

use std::collections::{BTreeMap, HashMap, HashSet};

/// Scalable freshness index with deadline-based staleness queries
#[derive(Debug)]
pub struct FreshnessIndexV2 {
    /// Key -> (stored_at, decay_rate) for point queries
    entries: HashMap<String, FreshnessEntry>,

    /// Deadline -> Keys (sorted by when they become stale)
    /// BTreeMap enables O(log N) range queries
    deadline_index: BTreeMap<u64, HashSet<String>>,

    /// Staleness threshold for deadline computation (default: 0.5)
    threshold: f32,
}

#[derive(Debug, Clone)]
struct FreshnessEntry {
    stored_at: u64,
    decay_rate: f32,
    deadline: u64,
}

impl FreshnessIndexV2 {
    /// Create a new freshness index with default threshold (0.5)
    pub fn new() -> Self {
        Self::with_threshold(0.5)
    }

    /// Create with custom staleness threshold
    pub fn with_threshold(threshold: f32) -> Self {
        Self {
            entries: HashMap::new(),
            deadline_index: BTreeMap::new(),
            threshold: threshold.clamp(0.001, 0.999),
        }
    }

    /// Insert or update a key's freshness info
    /// Complexity: O(log N)
    pub fn insert(&mut self, key: &str, decay_rate: f32) {
        let now = now_micros();
        let deadline = self.compute_deadline(now, decay_rate);

        // Remove old deadline if key exists
        if let Some(old) = self.entries.get(key) {
            self.remove_from_deadline_index(key, old.deadline);
        }

        // Insert new entry
        self.entries.insert(
            key.to_string(),
            FreshnessEntry {
                stored_at: now,
                decay_rate,
                deadline,
            },
        );

        // Add to deadline index
        self.deadline_index
            .entry(deadline)
            .or_default()
            .insert(key.to_string());
    }

    /// Get freshness of a key (0.0 to 1.0)
    /// Complexity: O(1)
    pub fn get_freshness(&self, key: &str) -> Option<f32> {
        self.entries.get(key).map(|e| {
            let age = now_micros().saturating_sub(e.stored_at);
            let age_secs = age as f32 / 1_000_000.0;
            (-e.decay_rate * age_secs).exp()
        })
    }

    /// Check if a key is stale (below threshold)
    /// Complexity: O(1)
    pub fn is_stale(&self, key: &str) -> Option<bool> {
        self.entries.get(key).map(|e| now_micros() >= e.deadline)
    }

    /// Query all stale keys (freshness < threshold)
    /// Complexity: O(k + log N) where k = number of stale entries
    pub fn query_stale(&self) -> Vec<String> {
        let now = now_micros();

        self.deadline_index
            .range(..=now)
            .flat_map(|(_, keys)| keys.iter().cloned())
            .collect()
    }

    /// Query all fresh keys (freshness >= threshold)
    /// Complexity: O(k + log N) where k = number of fresh entries
    pub fn query_fresh(&self) -> Vec<String> {
        let now = now_micros();

        self.deadline_index
            .range((now + 1)..)
            .flat_map(|(_, keys)| keys.iter().cloned())
            .collect()
    }

    /// Query fresh keys matching pattern
    /// Complexity: O(k) where k = fresh entries matching pattern
    pub fn query_fresh_pattern(&self, pattern: &str, min_freshness: f32) -> Vec<String> {
        let now = now_micros();

        self.entries
            .iter()
            .filter(|(key, entry)| {
                // Check pattern match
                if !Self::matches_pattern(key, pattern) {
                    return false;
                }

                // Check freshness
                let age = now.saturating_sub(entry.stored_at);
                let age_secs = age as f32 / 1_000_000.0;
                let freshness = (-entry.decay_rate * age_secs).exp();

                freshness >= min_freshness
            })
            .map(|(key, _)| key.clone())
            .collect()
    }

    /// Evict all stale entries and return their keys
    /// Complexity: O(k) where k = number of stale entries
    pub fn evict_stale(&mut self) -> Vec<String> {
        let now = now_micros();
        let mut evicted = Vec::new();

        // Collect all stale deadlines
        let stale_deadlines: Vec<u64> =
            self.deadline_index.range(..=now).map(|(d, _)| *d).collect();

        // Remove from both indexes
        for deadline in stale_deadlines {
            if let Some(keys) = self.deadline_index.remove(&deadline) {
                for key in keys {
                    self.entries.remove(&key);
                    evicted.push(key);
                }
            }
        }

        evicted
    }

    /// Count stale keys
    /// Complexity: O(log N + k) where k = stale entries
    pub fn count_stale(&self) -> usize {
        let now = now_micros();

        self.deadline_index
            .range(..=now)
            .map(|(_, keys)| keys.len())
            .sum()
    }

    /// Count fresh keys
    /// Complexity: O(log N + k) where k = fresh entries
    pub fn count_fresh(&self) -> usize {
        self.entries.len() - self.count_stale()
    }

    /// Average freshness across all keys
    /// Complexity: O(N) - must compute freshness for each entry
    pub fn average_freshness(&self) -> f32 {
        if self.entries.is_empty() {
            return 1.0;
        }

        let now = now_micros();
        let total: f32 = self
            .entries
            .values()
            .map(|entry| {
                let age = now.saturating_sub(entry.stored_at);
                let age_secs = age as f32 / 1_000_000.0;
                (-entry.decay_rate * age_secs).exp()
            })
            .sum();

        total / self.entries.len() as f32
    }

    /// Number of tracked keys
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Check if empty
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Get the staleness threshold
    pub fn threshold(&self) -> f32 {
        self.threshold
    }

    /// Remove a key from the index
    pub fn remove(&mut self, key: &str) -> bool {
        if let Some(entry) = self.entries.remove(key) {
            self.remove_from_deadline_index(key, entry.deadline);
            true
        } else {
            false
        }
    }

    /// Compute deadline when entry becomes stale
    fn compute_deadline(&self, stored_at: u64, decay_rate: f32) -> u64 {
        if decay_rate <= 0.0 {
            return u64::MAX; // Never stale (static data)
        }

        // deadline = stored_at + (-ln(threshold) / λ)
        // For threshold = 0.5: deadline = stored_at + ln(2) / λ
        let time_to_stale_secs = -self.threshold.ln() / decay_rate;
        let time_to_stale_micros = (time_to_stale_secs * 1_000_000.0) as u64;

        stored_at.saturating_add(time_to_stale_micros)
    }

    /// Remove key from deadline index
    fn remove_from_deadline_index(&mut self, key: &str, deadline: u64) {
        if let Some(keys) = self.deadline_index.get_mut(&deadline) {
            keys.remove(key);
            if keys.is_empty() {
                self.deadline_index.remove(&deadline);
            }
        }
    }

    /// Simple pattern matching (supports * wildcard)
    fn matches_pattern(key: &str, pattern: &str) -> bool {
        if pattern == "*" {
            return true;
        }

        if let Some(prefix) = pattern.strip_suffix("/*") {
            return key.starts_with(prefix);
        }

        if let Some(prefix) = pattern.strip_suffix('*') {
            return key.starts_with(prefix);
        }

        key == pattern
    }
}

impl Default for FreshnessIndexV2 {
    fn default() -> Self {
        Self::new()
    }
}

/// Helper to get current time in microseconds
fn now_micros() -> u64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_micros() as u64)
        .unwrap_or(0)
}

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

    #[test]
    fn test_insert_and_query() {
        let mut index = FreshnessIndexV2::new();

        // Insert with fast decay (will be stale quickly)
        index.insert("fast", 10.0); // ~70ms half-life

        // Insert with slow decay
        index.insert("slow", 0.0001); // ~2 hour half-life

        // Both should be fresh immediately
        assert!(index.get_freshness("fast").unwrap() > 0.99);
        assert!(index.get_freshness("slow").unwrap() > 0.99);
    }

    #[test]
    fn test_deadline_computation() {
        let index = FreshnessIndexV2::with_threshold(0.5);

        // For λ = 1.0, half-life = ln(2) ≈ 0.693 seconds
        let now = 1_000_000; // 1 second in micros
        let deadline = index.compute_deadline(now, 1.0);

        // Deadline should be ~693,147 microseconds after stored_at
        let expected = now + 693_147;
        assert!((deadline as i64 - expected as i64).abs() < 1000);
    }

    #[test]
    fn test_stale_query_efficiency() {
        let mut index = FreshnessIndexV2::new();

        // Insert 1000 entries with varying decay rates
        for i in 0..1000 {
            let decay = if i < 100 { 100.0 } else { 0.00001 }; // 100 fast, 900 slow
            index.insert(&format!("key_{}", i), decay);
        }

        // Wait a tiny bit for fast entries to become stale
        std::thread::sleep(std::time::Duration::from_millis(10));

        // Query stale - should be O(k) not O(N)
        let stale = index.query_stale();

        // Fast entries should be stale
        assert!(stale.len() >= 50); // At least some should be stale
        assert!(stale.len() <= 200); // Not all should be stale
    }

    #[test]
    fn test_eviction() {
        let mut index = FreshnessIndexV2::new();

        // Insert with immediate staleness
        index.insert("stale1", 1000.0); // Very fast decay
        index.insert("stale2", 1000.0);
        index.insert("fresh", 0.00001); // Very slow decay

        // Wait for fast entries to become stale
        std::thread::sleep(std::time::Duration::from_millis(5));

        let evicted = index.evict_stale();

        assert!(evicted.contains(&"stale1".to_string()));
        assert!(evicted.contains(&"stale2".to_string()));
        assert!(!evicted.contains(&"fresh".to_string()));
        assert_eq!(index.len(), 1);
    }

    #[test]
    fn test_static_data_never_stale() {
        let mut index = FreshnessIndexV2::new();

        // Insert with zero decay (static)
        index.insert("static", 0.0);

        // Should never be stale
        assert!(!index.is_stale("static").unwrap());

        // Freshness should always be 1.0
        assert!((index.get_freshness("static").unwrap() - 1.0).abs() < 0.001);
    }

    #[test]
    fn test_pattern_matching() {
        assert!(FreshnessIndexV2::matches_pattern(
            "gradient/layer_0",
            "gradient/*"
        ));
        assert!(FreshnessIndexV2::matches_pattern(
            "gradient/layer_0/weights",
            "gradient/*"
        ));
        assert!(!FreshnessIndexV2::matches_pattern(
            "model/weights",
            "gradient/*"
        ));
        assert!(FreshnessIndexV2::matches_pattern("anything", "*"));
    }

    #[test]
    fn test_remove() {
        let mut index = FreshnessIndexV2::new();

        index.insert("key1", 0.001);
        index.insert("key2", 0.001);

        assert_eq!(index.len(), 2);

        assert!(index.remove("key1"));
        assert_eq!(index.len(), 1);

        assert!(!index.remove("key1")); // Already removed
        assert!(index.get_freshness("key1").is_none());
    }
}