bader-db 0.1.5

Key-value cache RESP server with support for key expirations
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
pub mod expiry;
mod entry;

use std::collections::{BTreeMap, BTreeSet};
use std::sync::RwLock;
use std::time::{Duration, Instant};
use anyhow::{Error, Result};
use std::cmp;
use rand::prelude::*;
use async_timer::Interval;
use crate::cache::entry::Entry;
use crate::cache::expiry::Expiry;

#[derive(Debug)]
pub struct Cache {
    store: RwLock<BTreeMap<String, Entry>>,
    sample: usize,
    threshold: f64,
    frequency: Duration
}

impl Cache {
    pub fn new(sample: usize, threshold: f64, frequency: Duration) -> Self {
        Cache {
            store: RwLock::new(BTreeMap::new()),
            sample,
            threshold,
            frequency,
        }
    }

    pub async fn set(&self, key: String, value: String) {
        let expiry = Expiry::none();
        let entry = Entry::new(value, expiry);

        log::debug!("inserting key {} and value {:?}", key.clone(), entry);

        let mut store = self.store.write().unwrap();
        store.insert(key, entry);
    }

    pub async fn set_with_expiry<E>(&self, key: String, value: String, e: E)
        where
            E: Into<Expiry>,
    {
        let entry = Entry::new(value, e.into());

        log::debug!("inserting key {} and value {:?}", key.clone(), entry);

        let mut store = self.store.write().unwrap();
        store.insert(key, entry);
    }

    pub async fn get(&self, key: String) -> Option<String> {
        let store = self.store.read().unwrap();
        match store.get(key.as_str()) {
            Some(entry) => {

                log::debug!("getting key {} and value {:?}", key.clone(), entry);

                if !entry.expiration().is_expired() {
                    Some(entry.value().clone())
                } else {
                    drop(store);
                    let mut store = self.store.write().unwrap();
                    store.remove(key.as_str());
                    None
                }
            }
            None => None,
        }
    }

    pub async fn remove(&self, key: String) -> Result<()> {
        let mut store = self.store.write().unwrap();
        match store.get(key.as_str()) {
            Some(entry) => {
                log::debug!("removing key {} and value {:?}", key.clone(), entry);
                store.remove(key.as_str());
                Ok(())
            }
            _ => {
                Err(Error::msg(format!("Error in removing entry with key {:?}", key)))
            },
        }
    }

    pub async fn exists(&self, key: String) -> bool {
        let store = self.store.write().unwrap();
        store.contains_key(key.as_str())
    }

    pub async fn monitor_for_expiry(&self) {

        log::debug!("removing garbage in the background");

        let frequency = self.frequency;
        let mut interval = Interval::platform_new(frequency);
        loop {
            interval.as_mut().await;
            self.purge().await;
        }
    }

    pub async fn purge(&self) {
        let start = Instant::now();
        log::debug!("purging is starting in {:?}", start);

        let sample = self.sample;
        let threshold = self.threshold;
        let mut total = 0usize;
        let mut locked = Duration::from_nanos(0);
        let mut removed = 0;

        loop {

            let store = self.store.read().unwrap();

            if store.is_empty() {
                break;
            }

            total = store.len();
            let sample = cmp::min(sample, total);

            let mut gone = 0;

            let mut expired_keys = Vec::with_capacity(sample);
            let mut indices: BTreeSet<usize> = BTreeSet::new();

            {
                // fetch `sample` keys at random
                let mut rng = rand::thread_rng();
                while indices.len() < sample {
                    indices.insert(rng.gen_range(0..total));
                }
            }

            {
                // tracker for previous index
                let mut prev = 0;

                // boxed iterator to allow us to iterate a single time for all indices
                let mut iter: Box<dyn Iterator<Item = (&String, &Entry)>> =
                    Box::new(store.iter());

                // walk our index list
                for idx in indices {
                    // calculate how much we need to shift the iterator
                    let offset = idx
                        .checked_sub(prev)
                        .and_then(|idx| idx.checked_sub(1))
                        .unwrap_or(0);

                    // shift and mark the current index
                    iter = Box::new(iter.skip(offset));
                    prev = idx;

                    // fetch the next pair (at our index)
                    let (key, entry) = iter.next().unwrap();

                    // skip if not expired
                    if !entry.expiration().is_expired() {
                        continue;
                    }

                    // otherwise mark for removal
                    expired_keys.push(key.to_owned());

                    // and increment remove count
                    gone += 1;
                }
            }

            {
                // drop the read lock
                drop(store);

                // upgrade to a write guard so that we can make our changes
                let acquired = Instant::now();

                let mut store = self.store.write().unwrap();

                // remove all expired keys
                for key in &expired_keys {
                    store.remove(key);
                }

                // increment the lock timer tracking directly
                locked = locked.checked_add(acquired.elapsed()).unwrap();
            }

            log::debug!("Removed {} / {} ({:.2}%) of the sampled keys", gone, sample, (gone as f64 / sample as f64) * 100f64);

            removed += gone;

            if (gone as f64) < (sample as f64 * threshold) {
                break;
            }
        }
        log::debug!("Purge loop removed {} entries out of {} in {:.0?} ({:.0?} locked)", removed, total, start.elapsed(), locked);
    }

    pub async fn len(&self) -> usize {
        let store = self.store.read().unwrap();
        store.len()
    }

    pub async fn is_empty(&self) -> bool {
        let store = self.store.read().unwrap();
        return store.is_empty()
    }

    pub async fn existing(&self) -> usize {
        let store = self.store.read().unwrap();
        store.iter().filter(|(_, entry)| !entry.expiration().is_expired()).count()
    }

    pub async fn expired(&self) -> usize {
        let store = self.store.read().unwrap();
        store.iter().filter(|(_, entry)| entry.expiration().is_expired()).count()
    }

    pub async fn clear(&self) {
        let mut store = self.store.write().unwrap();
        store.clear();
    }

}

impl Default for Cache {
    fn default() -> Cache {
        Cache::new(25, 0.25, Duration::from_secs(1))
    }
}


#[cfg(test)]
mod tests {
    use super::*;
    use std::thread::sleep;
    use std::sync::Arc;
    use async_std::task;

    #[tokio::test]
    async fn test_set_get() {
        let cache = Cache::default();
        let key = "key".to_string();
        let value = "value".to_string();

        cache.set(key.clone(), value.clone()).await;
        let result = cache.get(key.clone()).await;
        assert_eq!(result, Some(value.clone()));
    }

    #[tokio::test]
    async fn test_expired_is_zero() {
        let cache = Cache::default();
        let expiry = Expiry::new(Instant::now()  + Duration::from_secs(2));
        cache.set_with_expiry("key".to_string(), "value".to_string(), expiry.clone()).await;
        let count = cache.expired().await;
        assert_eq!(count, 0);
    }

    #[tokio::test]
    async fn test_expired_is_one() {
        let cache = Cache::default();
        let expiry = Expiry::new(Instant::now());
        cache.set_with_expiry("key".to_string(), "value".to_string(), expiry.clone()).await;
        let count = cache.expired().await;
        assert_eq!(count, 1);
    }

    #[tokio::test]
    async fn test_len() {
        let cache = Cache::default();
        let expiry = Expiry::new(Instant::now());
        cache.set_with_expiry("key".to_string(), "value".to_string(), expiry.clone()).await;
        assert_eq!(cache.len().await, 1);
    }

    #[tokio::test]
    async fn test_clear() {
        let cache = Cache::default();
        let expiry = Expiry::new(Instant::now());
        cache.set_with_expiry("key".to_string(), "value".to_string(), expiry.clone()).await;
        cache.clear().await;
        assert_eq!(cache.len().await, 0);
    }

    #[tokio::test]
    async fn test_is_empty() {
        let cache = Cache::default();
        let expiry = Expiry::new(Instant::now());
        cache.set_with_expiry("key".to_string(), "value".to_string(), expiry.clone()).await;
        cache.clear().await;
        assert!(cache.is_empty().await);
    }

    #[tokio::test]
    async fn test_set_with_expiry_get() {
        let cache = Cache::default();
        let key = "key".to_string();
        let value = "value".to_string();

        let expiry = Expiry::new(Instant::now() + Duration::from_secs(2));
        cache.set_with_expiry(key.clone(), value.clone(), expiry.clone()).await;
        let result = cache.get(key.clone()).await;
        assert_eq!(result, Some(value.clone()));

        // Wait for expiration
        sleep(Duration::from_secs(3));

        let result_after_expiry = cache.get(key.clone()).await;
        assert_eq!(result_after_expiry, None);
    }

    #[tokio::test]
    async fn test_set_with_expiry_get_non_expired() {
        let cache = Cache::default();
        let key = "key".to_string();
        let value = "value".to_string();

        let expiry = Expiry::new(Instant::now() + Duration::from_secs(5));
        cache.set_with_expiry(key.clone(), value.clone(), expiry.clone()).await;
        let result = cache.get(key.clone()).await;
        assert_eq!(result, Some(value.clone()));
    }

    #[tokio::test]
    async fn test_set_with_expiry_update_expiry() {
        let cache = Cache::default();
        let key = "key".to_string();
        let value = "value".to_string();

        let expiry1 = Expiry::new(Instant::now() + Duration::from_secs(2));
        cache.set_with_expiry(key.clone(), value.clone(), expiry1.clone()).await;
        let result1 = cache.get(key.clone()).await;
        assert_eq!(result1, Some(value.clone()));

        // Update the expiry
        let expiry2 = Expiry::new(Instant::now() + Duration::from_secs(5));
        cache.set_with_expiry(key.clone(), value.clone(), expiry2.clone()).await;
        let result2 = cache.get(key.clone()).await;
        assert_eq!(result2, Some(value.clone()));

        // Wait for expiration of the original expiry
        sleep(Duration::from_secs(3));

        // After original expiry, the value should still be present due to updated expiry
        let result3 = cache.get(key.clone()).await;
        assert_eq!(result3, Some(value.clone()));
    }

    #[tokio::test]
    async fn test_get_non_existing_key() {
        let cache = Cache::default();
        let result = cache.get("non_existing_key".to_string()).await;
        assert_eq!(result, None);
    }

    #[tokio::test]
    async fn test_remove() {
        let cache = Cache::default();
        let key = "key".to_string();
        let value = "value".to_string();
        cache.set(key.clone(), value.clone()).await;
        let result = cache.get(key.clone()).await.unwrap();
        assert_eq!(result, value.to_string());
        _ = cache.remove(key.clone()).await;
        let result = cache.get(key.clone()).await;
        assert!(result.is_none());
    }

    #[tokio::test]
    async fn test_remove_key_doesnt_exist() {
        let cache = Cache::default();
        let key = "key".to_string();
        let result = cache.remove(key.clone()).await;
        assert!(result.is_err())
    }

    #[tokio::test]
    async fn test_existing_is_one() {
        let cache = Cache::default();
        let expiry = Expiry::new(Instant::now()  + Duration::from_secs(2));
        cache.set_with_expiry("key".to_string(), "value".to_string(), expiry.clone()).await;
        let count = cache.existing().await;
        assert_eq!(count, 1);
    }

    #[tokio::test]
    async fn test_purge_empty_cache() {
        let cache = Cache::new(10, 0.5, Duration::from_secs(1));
        cache.purge().await;
        assert_eq!(cache.len().await, 0);
    }

    #[tokio::test]
    async fn test_purge_expired_keys() {
        let cache = Cache::new(10, 0.5, Duration::from_millis(1));
        cache.set_with_expiry("key1".to_string(), "value1".to_string(), Duration::from_secs(1)).await;
        cache.set_with_expiry("key2".to_string(), "value2".to_string(), Duration::from_secs(2)).await;
        cache.set_with_expiry("key3".to_string(), "value3".to_string(), Duration::from_secs(3)).await;
        sleep(Duration::from_secs(2));
        cache.purge().await;
        assert_eq!(cache.len().await, 1);
        assert_eq!(cache.get("key1".to_string()).await, None);
        assert_eq!(cache.get("key2".to_string()).await, None);
        assert_eq!(cache.get("key3".to_string()).await, Some("value3".to_string()));
    }

    #[tokio::test]
    async fn test_expiry_formats() {
        let cache = Cache::new(10, 0.5, Duration::from_millis(1));
        cache.set_with_expiry("key1".to_string(), "value1".to_string(), (10u64, &"PX".to_string())).await;
        cache.set_with_expiry("key2".to_string(), "value2".to_string(), (1u64, &"EX".to_string())).await;
        cache.set_with_expiry("key3".to_string(), "value3".to_string(), (3u64, &"EX".to_string())).await;
        sleep(Duration::from_secs(2));
        cache.purge().await;
        assert_eq!(cache.len().await, 1);
        assert_eq!(cache.get("key1".to_string()).await, None);
        assert_eq!(cache.get("key2".to_string()).await, None);
        assert_eq!(cache.get("key3".to_string()).await, Some("value3".to_string()));
    }

    #[tokio::test]
    async fn test_purge_all_expired_entries() {
        let cache = Cache::new(2, 0.5, Duration::from_secs(1));
        let key1 = "key1".to_string();
        let key2 = "key2".to_string();

        cache.set_with_expiry(key1.clone(), "value1".to_string(), Duration::from_secs(0)).await;
        cache.set_with_expiry(key2.clone(), "value2".to_string(), Duration::from_secs(0)).await;

        // wait for the entries to expire
        sleep(Duration::from_millis(100));

        // purge all expired entries
        cache.purge().await;

        assert_eq!(cache.len().await, 0);
    }

    #[tokio::test]
    async fn test_purge_some_expired_entries() {
        let cache = Cache::new(3, 0.5, Duration::from_secs(1));
        let key1 = "key1".to_string();
        let key2 = "key2".to_string();
        let key3 = "key3".to_string();

        cache.set_with_expiry(key1.clone(), "value1".to_string(), Duration::from_secs(0)).await;
        cache.set_with_expiry(key2.clone(), "value2".to_string(), Duration::from_secs(0)).await;
        cache.set_with_expiry(key3.clone(), "value3".to_string(), Duration::from_secs(60)).await;

        // wait for the entries to expire
        sleep(Duration::from_millis(100));

        // purge a percentage of expired entries
        cache.purge().await;

        // only the entry with key3 should remain
        assert_eq!(cache.len().await, 1);
        assert_eq!(cache.get(key1.clone()).await, None);
        assert_eq!(cache.get(key2.clone()).await, None);
        assert_eq!(cache.get(key3.clone()).await, Some("value3".to_string()));
    }

    #[async_std::test]
    async fn test_monitor() {
        let cache = Arc::new(Cache::new(10, 0.5, Duration::from_millis(100)));
        let clone = cache.clone();
        // Insert some values with an expiry time of 3 seconds
        cache.set_with_expiry("key1".to_string(), "value1".to_string(), Duration::from_secs(3)).await;
        cache.set_with_expiry("key2".to_string(), "value2".to_string(), Duration::from_secs(2)).await;
        cache.set_with_expiry("key3".to_string(), "value1".to_string(), Duration::from_secs(1)).await;
        cache.set_with_expiry("key4".to_string(), "value2".to_string(), Duration::from_secs(1)).await;
        cache.set_with_expiry("key5".to_string(), "value1".to_string(), Duration::from_secs(2)).await;
        cache.set_with_expiry("key6".to_string(), "value2".to_string(), Duration::from_secs(2)).await;
        cache.set_with_expiry("key7".to_string(), "value1".to_string(), Duration::from_secs(3)).await;
        cache.set_with_expiry("key8".to_string(), "value2".to_string(), Duration::from_secs(2)).await;
        cache.set_with_expiry("key9".to_string(), "value1".to_string(), Duration::from_secs(3)).await;
        cache.set_with_expiry("key11".to_string(), "value1".to_string(), Duration::from_secs(3)).await;
        cache.set_with_expiry("key12".to_string(), "value2".to_string(), Duration::from_secs(2)).await;
        cache.set_with_expiry("key13".to_string(), "value1".to_string(), Duration::from_secs(1)).await;
        cache.set_with_expiry("key14".to_string(), "value2".to_string(), Duration::from_secs(1)).await;
        cache.set_with_expiry("key15".to_string(), "value1".to_string(), Duration::from_secs(2)).await;
        cache.set_with_expiry("key16".to_string(), "value2".to_string(), Duration::from_secs(2)).await;
        cache.set_with_expiry("key17".to_string(), "value1".to_string(), Duration::from_secs(3)).await;
        cache.set_with_expiry("key18".to_string(), "value2".to_string(), Duration::from_secs(2)).await;
        cache.set_with_expiry("key19".to_string(), "value1".to_string(), Duration::from_secs(4)).await;
        cache.set_with_expiry("key21".to_string(), "value1".to_string(), Duration::from_secs(3)).await;
        cache.set_with_expiry("key22".to_string(), "value2".to_string(), Duration::from_secs(2)).await;
        cache.set_with_expiry("key23".to_string(), "value1".to_string(), Duration::from_secs(1)).await;
        cache.set_with_expiry("key24".to_string(), "value2".to_string(), Duration::from_secs(1)).await;
        cache.set_with_expiry("key25".to_string(), "value1".to_string(), Duration::from_secs(2)).await;
        cache.set_with_expiry("key26".to_string(), "value2".to_string(), Duration::from_secs(2)).await;
        cache.set_with_expiry("key27".to_string(), "value1".to_string(), Duration::from_secs(4)).await;
        cache.set_with_expiry("key28".to_string(), "value2".to_string(), Duration::from_secs(2)).await;
        cache.set_with_expiry("key29".to_string(), "value1".to_string(), Duration::from_secs(3)).await;
        cache.set_with_expiry("key31".to_string(), "value1".to_string(), Duration::from_secs(3)).await;
        cache.set_with_expiry("key39".to_string(), "value1".to_string(), Duration::from_secs(6)).await;
        cache.set_with_expiry("key10".to_string(), "value2".to_string(), Duration::from_secs(7)).await;

        task::spawn(async move {
            clone.monitor_for_expiry().await;
        });

        // Sleep for 5 seconds to allow the monitoring task to run
        sleep(Duration::from_secs(5));

        // Check that the expired keys were removed
        assert_eq!(2, cache.len().await);
    }

}