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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/*
 * Copyright 2021 Luca Fulchir <luca@fenrirproject.org>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//! Basic LRU cache
//!
//! Extra features: callbacks on get/insert, lazy scan callback
//!
//! The lazy scan is a scan that only runs on one more element after each
//! get/insert  
//! This means that X elements will be fully scanned only after X get/insert

use crate::hashmap;
use crate::hashmap::user;
use crate::hashmap::user::EntryT;
use crate::results::{InsertResult, InsertResultShared};

type LRUEntry<K, V, Umeta> =
    user::Entry<K, V, ::std::marker::PhantomData<K>, Umeta>;
type HmapT<K, V, Umeta, HB> = hashmap::SimpleHmap<
    LRUEntry<K, V, Umeta>,
    K,
    V,
    ::std::marker::PhantomData<K>,
    Umeta,
    HB,
>;
/// LRU implementation that wraps LRUShared
///
/// note that we store the value as-is and we have pointers to those.
///
/// **If you need to grow the LRU dynamically, make sure to use `Box<V>`
/// as the value** (resize currently not supported)
// TODO: generalize: K in the first Hashmap template parameter is not
// necessarily the same K in the user::Entry<K>
// (e.g: could be a pointer to user::Entry<K>.key)
pub struct LRU<'a, K, V, Umeta, HB>
where
    K: user::Hash,
    V: user::Val,
    Umeta: user::Meta<V>,
    HB: ::std::hash::BuildHasher + Default,
{
    _hmap: HmapT<K, V, Umeta, HB>,
    _lru: LRUShared<
        'a,
        HmapT<K, V, Umeta, HB>,
        LRUEntry<K, V, Umeta>,
        K,
        V,
        ::std::marker::PhantomData<K>,
        Umeta,
        HB,
    >,
}
impl<
        'a,
        K: user::Hash,
        V: user::Val,
        Umeta: user::Meta<V>,
        HB: ::std::hash::BuildHasher + Default,
    > LRU<'a, K, V, Umeta, HB>
{
    /// Create a new empty LRU
    pub fn new(
        entries: usize,
        extra_hashmap_capacity: usize,
        hash_builder: HB,
    ) -> LRU<'a, K, V, Umeta, HB> {
        LRU {
            _hmap: HmapT::<K, V, Umeta, HB>::with_capacity_and_hasher(
                1 + entries + extra_hashmap_capacity,
                hash_builder,
            ),
            _lru: LRUShared::<
                '_,
                HmapT<K, V, Umeta, HB>,
                LRUEntry<K, V, Umeta>,
                K,
                V,
                ::std::marker::PhantomData<K>,
                Umeta,
                HB,
            >::new(entries, ::std::marker::PhantomData, None),
        }
    }
    /// insert a new entry in the LRU
    pub fn insert(&mut self, key: K, val: V) -> InsertResult<(K, V, Umeta)> {
        self.insert_with_meta(key, val, Umeta::new())
    }
    /// The same insert, but with metadata
    pub fn insert_with_meta(
        &mut self,
        key: K,
        val: V,
        user_data: Umeta,
    ) -> InsertResult<(K, V, Umeta)> {
        let e =
            user::Entry::<K, V, ::std::marker::PhantomData<K>, Umeta>::new_entry(
                None,
                None,
                key.clone(),
                val,
                ::std::marker::PhantomData,
                user_data,
            );
        // insert and get length and a ref to the value just inserted
        // we will use this ref to fix the linked lists in ll_tail/ll_head
        // of the various elements
        let (mut maybe_clash, new_entry_idx, _new_entry) = self._hmap.insert(e);
        let opt_ref_clash = maybe_clash.as_mut();
        match self._lru.insert_shared(
            &mut self._hmap,
            opt_ref_clash,
            new_entry_idx,
        ) {
            InsertResultShared::OldEntry { evicted } => {
                let c = match maybe_clash {
                    None => None,
                    Some(x) => Some(x.deconstruct()),
                };
                let e = match evicted {
                    None => None,
                    Some(x) => Some(x.deconstruct()),
                };
                InsertResult::OldEntry {
                    clash: c,
                    evicted: e,
                }
            }
            InsertResultShared::OldTailPtr { evicted } => {
                let c = match maybe_clash {
                    None => None,
                    Some(x) => Some(x.deconstruct()),
                };
                let removed = self._hmap.remove(unsafe { &*evicted.as_ptr() });
                InsertResult::OldTail {
                    clash: c,
                    evicted: removed.deconstruct(),
                }
            }
            InsertResultShared::Success => match maybe_clash {
                None => InsertResult::Success,
                Some(clash) => InsertResult::OldEntry {
                    clash: Some(clash.deconstruct()),
                    evicted: None,
                },
            },
        }
    }
    /// empty the whole LRU
    pub fn clear(&mut self) {
        self._hmap.clear();
        self._lru.clear_shared()
    }
    /// remove a single element from the lru
    pub fn remove(&mut self, key: &K) -> Option<(V, Umeta)> {
        let (idx, entry) = match self._hmap.get_full(key) {
            None => return None,
            Some((idx, entry)) => (idx, entry),
        };
        self._lru.remove_shared(entry);
        let (_, val, meta) = self._hmap.remove_idx(idx).deconstruct();
        Some((val, meta))
    }
    /// chech if a key exists in the LRU
    pub fn contains_key(&self, key: &K) -> bool {
        self._hmap.get_full(&key).is_some()
    }
    /// If present, make the entry the head of the LRU, and return references to
    /// the values
    pub fn make_head(&mut self, key: &K) -> Option<(&V, &Umeta)> {
        match self._hmap.get_full_mut(key) {
            None => None,
            Some((_, mut entry)) => {
                self._lru.make_head(&mut entry);
                Some((entry.get_val(), entry.get_user()))
            }
        }
    }
    /// get references to an entry
    pub fn get(&mut self, key: &K) -> Option<(&V, &Umeta)> {
        match self._hmap.get_full_mut(key) {
            None => None,
            Some((_, mut entry)) => {
                self._lru.on_get(&mut entry);
                Some((entry.get_val(), entry.get_user()))
            }
        }
    }
    /// get a mutable reference to the entry
    pub fn get_mut(&mut self, key: &K) -> Option<(&mut V, &mut Umeta)> {
        match self._hmap.get_full_mut(key) {
            None => None,
            //Some(mut entry) => Some((entry.get_val(), entry.get_user())),
            Some((_, mut entry)) => {
                self._lru.on_get(&mut entry);
                Some(entry.get_val_user_mut())
            }
        }
    }
}

/// Actual implementation of the LRU on a shared hashmap
pub struct LRUShared<'a, Hmap, E, K, V, CidT, Umeta, HB>
where
    Hmap: hashmap::HashMap<E, K, V, CidT, Umeta, HB>,
    E: user::EntryT<K, V, CidT, Umeta>,
    K: user::Hash,
    V: user::Val,
    CidT: user::Cid,
    Umeta: user::Meta<V>,
    HB: ::std::hash::BuildHasher + Default,
{
    _capacity: usize,
    _used: usize,

    _head: Option<::std::ptr::NonNull<E>>,
    _tail: Option<::std::ptr::NonNull<E>>,
    _cache_id: CidT,
    _hmap: ::std::marker::PhantomData<Hmap>,
    _key: ::std::marker::PhantomData<K>,
    _val: ::std::marker::PhantomData<V>,
    _meta: ::std::marker::PhantomData<Umeta>,
    _hashbuilder: ::std::marker::PhantomData<HB>,
    _scan: crate::scan::Scan<'a, E, K, V, CidT, Umeta>,
}

impl<
        'a,
        Hmap: hashmap::HashMap<E, K, V, CidT, Umeta, HB>,
        E: user::EntryT<K, V, CidT, Umeta>,
        K: user::Hash,
        V: user::Val,
        CidT: user::Cid,
        Umeta: user::Meta<V>,
        HB: ::std::hash::BuildHasher + Default,
    > LRUShared<'a, Hmap, E, K, V, CidT, Umeta, HB>
{
    /// Build a LRU that works on someone else's hasmap
    ///
    /// In this case each cache should have a different `Cid` (Cache ID) so that
    /// everyone known whose elements is being used, and call the proper
    /// cache methods
    pub fn new(
        entries: usize,
        cache_id: CidT,
        access_scan: Option<&'a dyn Fn(::std::ptr::NonNull<E>) -> ()>,
    ) -> Self {
        LRUShared {
            _capacity: entries,
            _used: 0,
            _head: None,
            _tail: None,
            _cache_id: cache_id,
            _hmap: ::std::marker::PhantomData,
            _key: ::std::marker::PhantomData,
            _val: ::std::marker::PhantomData,
            _meta: ::std::marker::PhantomData,
            _hashbuilder: std::marker::PhantomData,
            _scan: crate::scan::Scan::new(access_scan),
        }
    }
    /// change the scan callback
    pub fn set_scanf(
        &mut self,
        access_scan: Option<&'a dyn Fn(::std::ptr::NonNull<E>) -> ()>,
    ) {
        self._scan.set_scanf(access_scan)
    }

    /// `insert_shared` does not actually insert anything.
    ///
    /// It will only fix the LRU linked lists after something has been inserted
    /// by the parent.
    ///
    /// Note that `maybe_old_entry` is != `None` if and only if the
    /// ols entry is part of the same cache
    pub fn insert_shared(
        &mut self,
        hmap: &mut Hmap,
        maybe_old_entry: Option<&mut E>,
        new_entry_idx: usize,
    ) -> InsertResultShared<E> {
        let just_inserted = hmap.get_index_mut(new_entry_idx).unwrap();
        self._used += 1;
        self._scan.apply_raw(just_inserted.into());
        *just_inserted.get_cache_id_mut() = self._cache_id;

        match maybe_old_entry {
            None => {
                just_inserted.user_on_insert(None);
                // we did not clash with anything, but we might still be over
                // capacity
                if self._used >= self._capacity {
                    // reset head & tail to correct values, returen old tail
                    unsafe {
                        self._head
                            .unwrap()
                            .as_mut()
                            .set_head_ptr(Some(just_inserted.into()));
                    }
                    self._head = Some(just_inserted.into());
                    let mut to_remove = self._tail.unwrap();
                    self._scan.check_and_next(to_remove);
                    self._scan.apply_next();
                    unsafe {
                        let mut to_rm_head =
                            to_remove.as_mut().get_head_ptr().unwrap();
                        to_rm_head.as_mut().set_tail_ptr(None);
                        self._tail = Some(to_rm_head);
                        return InsertResultShared::OldTailPtr {
                            evicted: to_remove,
                        };
                    }
                }
                match self._head {
                    None => {
                        // first entry in the LRU, both head and tail
                        // since it's the first entry, we don't need to start
                        // scanning anything
                        self._head = Some(just_inserted.into());
                        self._tail = Some(just_inserted.into());
                    }
                    Some(mut old_head) => {
                        // just a new entry on a non-filled LRU
                        unsafe {
                            old_head
                                .as_mut()
                                .set_head_ptr(Some(just_inserted.into()))
                        };
                        self._head = Some(just_inserted.into());
                        self._scan.apply_next();
                    }
                }
                return InsertResultShared::Success;
            }
            Some(old_entry) => {
                // the callee has added an element to the hashmap, but it
                // clashed with something. We'll have to keep track of it and
                // we should fix it
                //
                // By definition, we know that we get the 'old_entry' only if it
                // is in the same cache_id as us
                // Also, we don't have to check the LRU size since here the
                // number of elements remains the same.
                // TL;DR: we had a clash, there can be no eviction

                just_inserted.user_on_insert(Some(old_entry));
                // The clash was on something in our own cache.
                // In this case the head or tail might need to be changed

                self._scan.check_and_next((old_entry).into());
                self._scan.apply_next();
                match old_entry.get_head_ptr() {
                    None => {
                        // we removed the old head with the hash clash
                        just_inserted.set_tail_ptr(old_entry.get_tail_ptr());
                        match old_entry.get_tail_ptr() {
                            None => {
                                // None    == old_entry.head
                                // None    == old_entry.tail
                                // basically the only element in the LRU
                                // both head and tail
                                self._tail = Some(just_inserted.into());
                            }
                            Some(mut old_entry_tail) => {
                                unsafe {
                                    // None    == old_entry.head
                                    // Some(_) == old_entry.tail
                                    // the head of the LRU
                                    old_entry_tail.as_mut().set_head_ptr(Some(
                                        just_inserted.into(),
                                    ));
                                }
                            }
                        }
                        self._head = Some(just_inserted.into());
                        return InsertResultShared::OldEntry { evicted: None };
                    }
                    Some(mut old_entry_head) => {
                        match old_entry.get_tail_ptr() {
                            None => {
                                // Some(_) == old_entry.head
                                // None    == old_entry.tail
                                // we removed the old tail, with a hash clash
                                // the new tail is the prev of the old tail.
                                unsafe {
                                    old_entry_head.as_mut().set_tail_ptr(None);
                                    self._head.unwrap().as_mut().set_head_ptr(
                                        Some(just_inserted.into()),
                                    );
                                }
                                self._head = Some(just_inserted.into());
                                self._tail = Some(old_entry_head);
                                return InsertResultShared::OldEntry {
                                    evicted: None,
                                };
                            }
                            Some(mut old_entry_tail) => {
                                // Some(_) == old_entry.head
                                // Some(_) == old_entry.tail
                                // we removed something in the middle
                                unsafe {
                                    old_entry_tail
                                        .as_mut()
                                        .set_head_ptr(old_entry.get_head_ptr());
                                    old_entry
                                        .get_head_ptr()
                                        .unwrap()
                                        .as_mut()
                                        .set_tail_ptr(old_entry.get_tail_ptr());
                                    self._head.unwrap().as_mut().set_head_ptr(
                                        Some(just_inserted.into()),
                                    );
                                }
                                self._head = Some(just_inserted.into());
                                return InsertResultShared::OldEntry {
                                    evicted: None,
                                };
                            }
                        }
                    }
                }
            }
        }
    }
    /// reset the LRU
    pub fn clear_shared(&mut self) {
        self._head = None;
        self._tail = None;
        self._scan.stop();
    }
    /// remove the pointers to this element in the LRU.
    ///
    /// Does not actually remove the element, that is done by the caller
    pub fn remove_shared(&mut self, entry: &E) {
        self._scan.check_and_next(entry.into());
        if None == entry.get_head_ptr() {
            // we removed the head
            match entry.get_tail_ptr() {
                None => {
                    // None == entry.head
                    // None == entry.tail
                    // we had only one element, we removed it
                    self._head = None;
                    self._tail = None;
                }
                Some(mut entry_tail) => {
                    // None == entry.head
                    // Some(_) == entry.tail
                    // we removed the head
                    unsafe {
                        entry_tail.as_mut().set_head_ptr(None);
                    }
                    self._head = Some(entry_tail);
                }
            }
        } else {
            match entry.get_tail_ptr() {
                None => {
                    // Some(_) == entry.head
                    // None == entry.tail
                    // we removed the tail
                    unsafe {
                        entry
                            .get_head_ptr()
                            .unwrap()
                            .as_mut()
                            .set_tail_ptr(None);
                    }
                    self._tail = entry.get_head_ptr();
                }
                Some(mut entry_tail) => {
                    // Some(_) == entry.head
                    // Some(_) == entry.tail
                    // we removed an intermediate entry
                    unsafe {
                        entry_tail.as_mut().set_head_ptr(entry.get_head_ptr());
                        entry
                            .get_head_ptr()
                            .unwrap()
                            .as_mut()
                            .set_tail_ptr(entry.get_tail_ptr());
                    }
                }
            }
        }
    }
    /// make the key the head of the LRU.
    pub fn make_head(&mut self, entry: &mut E) {
        self._scan.check_and_next(entry.into());
        match entry.get_head_ptr() {
            None => {
                // already the head, nothing to do
            }
            Some(mut entry_head) => {
                unsafe {
                    entry_head.as_mut().set_tail_ptr(entry.get_tail_ptr());
                }
                match entry.get_tail_ptr() {
                    None => {
                        // we moved the tail to the head.
                        unsafe {
                            self._head
                                .unwrap()
                                .as_mut()
                                .set_head_ptr(Some(entry.into()));
                            entry.set_tail_ptr(self._head);
                        }
                        self._head = Some(entry.into());
                        self._tail = Some(entry_head);
                    }
                    Some(mut entry_tail) => {
                        // we promoted to head something in the middle
                        // of the linked list
                        unsafe {
                            entry_tail.as_mut().set_head_ptr(Some(entry_head));
                            entry_head.as_mut().set_tail_ptr(Some(entry_tail));
                            self._head
                                .unwrap()
                                .as_mut()
                                .set_head_ptr(Some(entry.into()));
                        }
                        self._head = Some(entry.into());
                    }
                }
            }
        }
    }
    /// get the LRU cache id
    pub fn get_cache_id(&self) -> CidT {
        self._cache_id
    }
    /// Used when composing caches, run the callback on the entry
    ///
    /// This method should be passed down between parent/child cache
    /// and only the final cache which owns the element should execute it
    pub fn on_get(&mut self, entry: &mut E) {
        entry.user_on_get();
        self._scan.apply_next();
    }
    /// start the lazy scan  
    /// The scan will execute on the whole LRU but only once
    pub fn start_scan(&mut self) {
        match self._scan.is_running() {
            false => match self._head {
                Some(head) => self._scan.start_scan(head),
                None => {}
            },
            true => {}
        }
    }
    /// check if the scan is still running
    pub fn is_scan_running(&self) -> bool {
        self._scan.is_running()
    }
    /// get the LRU capacity
    pub fn capacity(&self) -> usize {
        self._capacity
    }
    /// get the LRU usage
    pub fn len(&self) -> usize {
        self._used
    }
}