lru_time_cache/
iter.rs

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
// Copyright 2019 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under the MIT license <LICENSE-MIT
// http://opensource.org/licenses/MIT> or the Modified BSD license <LICENSE-BSD
// https://opensource.org/licenses/BSD-3-Clause>, at your option. This file may not be copied,
// modified, or distributed except according to those terms. Please review the Licences for the
// specific language governing permissions and limitations relating to use of the SAFE Network
// Software.

//! Misc LRU cache iterators.

#[cfg(feature = "sn_fake_clock")]
use sn_fake_clock::FakeClock as Instant;
use std::collections::{BTreeMap, VecDeque};
use std::time::Duration;
#[cfg(not(feature = "sn_fake_clock"))]
use std::time::Instant;

/// An iterator over an `LruCache`'s entries that updates the timestamps as values are traversed.
/// Values are produced in the most recently used order.
pub struct Iter<'a, Key, Value> {
    /// Reference to the iterated cache.
    map: &'a mut BTreeMap<Key, (Value, Instant)>,
    /// Ordered cache entry keys where the least recently used items are first.
    list: &'a mut VecDeque<Key>,
    lru_cache_ttl: Option<Duration>,
    /// Index in `list` of the previously used item.
    item_index: usize,
}

impl<'a, Key, Value> Iter<'a, Key, Value>
where
    Key: Ord,
{
    #[doc(hidden)]
    pub fn new(
        map: &'a mut BTreeMap<Key, (Value, Instant)>,
        list: &'a mut VecDeque<Key>,
        lru_cache_ttl: Option<Duration>,
    ) -> Self {
        let item_index = list.len();
        Self {
            map,
            list,
            lru_cache_ttl,
            item_index,
        }
    }

    /// Returns next unexpired item in the cache or `None` if no such items.
    /// Expired items are removed from the cache.
    fn next_unexpired(&mut self, now: Instant) -> Option<Key> {
        loop {
            self.item_index = self.item_index.checked_sub(1)?;
            let key = self.list.remove(self.item_index)?;
            let value = self.map.get(&key)?;

            if let Some(ttl) = self.lru_cache_ttl {
                if value.1 + ttl > now {
                    return Some(key);
                } else {
                    let _ = self.map.remove(&key);
                }
            } else {
                return Some(key);
            }
        }
    }
}

impl<'a, Key, Value> Iterator for Iter<'a, Key, Value>
where
    Key: Ord + Clone,
{
    type Item = (&'a Key, &'a Value);

    /// Returns the next element in the cache and moves it to the top of the cache.
    /// The most recently used items are yield first.
    #[allow(unsafe_code)]
    fn next(&mut self) -> Option<(&'a Key, &'a Value)> {
        let now = Instant::now();
        let key = self.next_unexpired(now)?;
        self.list.push_back(key);
        let key = self.list.back()?;
        let mut value = self.map.get_mut(&key)?;
        value.1 = now;

        unsafe {
            let key = std::mem::transmute::<&Key, &'a Key>(key);
            let value = std::mem::transmute::<&Value, &'a Value>(&value.0);
            Some((key, value))
        }
    }
}

/// Entry produced by `NotifyIter` that might be still valid or expired.
pub enum TimedEntry<'a, Key: 'a, Value: 'a> {
    /// Entry has not yet expired.
    Valid(&'a Key, &'a Value),
    /// Entry got expired and was evicted from the cache.
    Expired(Key, Value),
}

/// Much like `Iter` except will produce expired entries too where `Iter` silently drops them.
pub struct NotifyIter<'a, Key, Value> {
    /// Reference to the iterated cache.
    map: &'a mut BTreeMap<Key, (Value, Instant)>,
    /// Ordered cache entry keys where the least recently used items are first.
    list: &'a mut VecDeque<Key>,
    lru_cache_ttl: Option<Duration>,
    /// Index in `list` of the previously used item.
    item_index: usize,
}

impl<'a, Key, Value> NotifyIter<'a, Key, Value>
where
    Key: Ord + Clone,
{
    #[doc(hidden)]
    pub fn new(
        map: &'a mut BTreeMap<Key, (Value, Instant)>,
        list: &'a mut VecDeque<Key>,
        lru_cache_ttl: Option<Duration>,
    ) -> Self {
        let item_index = list.len();
        Self {
            map,
            list,
            lru_cache_ttl,
            item_index,
        }
    }
}

impl<'a, Key, Value> Iterator for NotifyIter<'a, Key, Value>
where
    Key: Ord + Clone,
{
    type Item = TimedEntry<'a, Key, Value>;

    /// Returns the next element in the cache and moves it to the top of the cache.
    /// The most recently used items are yield first.
    #[allow(unsafe_code)]
    fn next(&mut self) -> Option<Self::Item> {
        self.item_index = self.item_index.checked_sub(1)?;
        let key = self.list.remove(self.item_index)?;
        let mut value = self.map.get_mut(&key)?;
        let now = Instant::now();

        if let Some(ttl) = self.lru_cache_ttl {
            if value.1 + ttl <= now {
                let value = self.map.remove(&key)?;
                return Some(TimedEntry::Expired(key, value.0));
            }
        }

        self.list.push_back(key);
        let key = self.list.back()?;
        value.1 = now;
        unsafe {
            let key = std::mem::transmute::<&Key, &'a Key>(key);
            let value = std::mem::transmute::<&Value, &'a Value>(&value.0);
            Some(TimedEntry::Valid(key, value))
        }
    }
}

/// An iterator over an `LruCache`'s entries that does not modify the timestamp.
pub struct PeekIter<'a, Key, Value> {
    /// Reference to the iterated cache.
    map: &'a BTreeMap<Key, (Value, Instant)>,
    /// Ordered cache entry keys where the least recently used items are first.
    list: &'a VecDeque<Key>,
    lru_cache_ttl: Option<Duration>,
    /// Index in `list` of the previously used item.
    item_index: usize,
}

impl<'a, Key, Value> PeekIter<'a, Key, Value>
where
    Key: Ord,
{
    #[doc(hidden)]
    pub fn new(
        map: &'a BTreeMap<Key, (Value, Instant)>,
        list: &'a VecDeque<Key>,
        lru_cache_ttl: Option<Duration>,
    ) -> Self {
        let item_index = list.len();
        Self {
            map,
            list,
            lru_cache_ttl,
            item_index,
        }
    }

    /// Returns next unexpired item in the cache or `None` if no such items.
    fn next_unexpired(&mut self, now: Instant) -> Option<()> {
        loop {
            self.item_index = self.item_index.checked_sub(1)?;
            let value = self.map.get(&self.list[self.item_index])?;

            if let Some(ttl) = self.lru_cache_ttl {
                if value.1 + ttl > now {
                    return Some(());
                }
            } else {
                return Some(());
            }
        }
    }
}

impl<'a, Key, Value> Iterator for PeekIter<'a, Key, Value>
where
    Key: Ord + Clone,
{
    type Item = (&'a Key, &'a Value);

    /// Returns the next element in the cache that has not expired yet.
    /// The most recently used items are yield first.
    #[allow(unsafe_code)]
    fn next(&mut self) -> Option<(&'a Key, &'a Value)> {
        let now = Instant::now();
        self.next_unexpired(now)?;
        let key = &self.list[self.item_index];
        let value = self.map.get(&key)?;

        unsafe {
            let key = std::mem::transmute::<&Key, &'a Key>(key);
            let value = std::mem::transmute::<&Value, &'a Value>(&value.0);
            Some((key, value))
        }
    }
}