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
//! A cache that holds a limited number of key-value pairs. When the capacity of
//! the cache is exceeded, the least-recently-used (where "used" means a look-up
//! or putting the pair into the cache) pair is automatically removed.
//!
//! Contrary to the [lru-cache](https://crates.io/crates/lru-cache) crate (which
//! this crate is heavily inspired by!), the capacity is not the number of items
//! in the cache, but can be given by an arbitrary criterion by implementing
//! [`Weighable`] for the value type V. A straight-forward example of this would
//! be to use the allocated size of the object, and provide a total capacity
//! which must not be exceeded by the cache.
//!
//! # Examples
//!```
//! use weight_cache::{Weighable, WeightCache};
//! use std::num::NonZeroUsize;
//!
//! #[derive(PartialEq, Debug)]
//! enum Food {
//!     Milk { milliliters: usize },
//!     Cucumber { pieces: usize },
//!     Meat { grams: usize },
//!     Potato { pieces: usize },
//!     Crab { grams: usize },
//! }
//!
//! impl Weighable for Food {
//!     fn measure(value: &Self) -> usize {
//!         match value {
//!             Food::Milk { milliliters } => milliliters * 104 / 100,
//!             Food::Cucumber { pieces } => pieces * 158,
//!             Food::Meat { grams } => *grams,
//!             Food::Potato { pieces } => pieces * 175,
//!             Food::Crab { grams } => *grams,
//!         }
//!     }
//! }
//!
//! let mut cache = WeightCache::new(NonZeroUsize::new(500).unwrap());
//!
//! // Can't put too much in!
//! assert!(cache.put(0, Food::Meat { grams: 600 }).is_err());
//! assert!(cache.is_empty());
//!
//! cache.put(1, Food::Milk { milliliters: 100 }).unwrap();
//! assert!(!cache.is_empty());
//! assert_eq!(*cache.get(&1).unwrap(), Food::Milk { milliliters: 100 });
//!
//! cache.put(2, Food::Crab { grams: 300 }).unwrap();
//! assert_eq!(*cache.get(&2).unwrap(), Food::Crab { grams: 300 });
//! assert_eq!(*cache.get(&1).unwrap(), Food::Milk { milliliters: 100 });
//!
//! cache.put(3, Food::Potato { pieces: 2 }).unwrap();
//! assert_eq!(*cache.get(&3).unwrap(), Food::Potato { pieces: 2});
//! assert!(cache.get(&2).is_none()); // 1 has been touched last
//! assert_eq!(*cache.get(&1).unwrap(), Food::Milk { milliliters: 100 });
//!```
//!
//! # Feature flags
//!
//! * `metrics`: Enables metric gathering on the cache. Register a
//! [`prometheus::Registry`] with a call to [`WeightCache::register`]; set a
//! custom metric namespace with [`WeightCache::new_with_namespace`]

use hash_map::RandomState;
use linked_hash_map::LinkedHashMap;
#[cfg(feature = "metrics")]
use prometheus::{
    core::{AtomicU64, GenericCounter, GenericGauge},
    Opts, Registry,
};
use std::{
    collections::hash_map,
    fmt,
    hash::{BuildHasher, Hash},
    num::NonZeroUsize,
};

/// A trait to implemented for the value type, providing a way to
/// [`Weighable::measure`] the thing.
pub trait Weighable {
    fn measure(value: &Self) -> usize;
}

#[derive(Debug)]
/// An error indicating that the to-be-inserted value is bigger than the max size
/// of the cache.
pub struct ValueTooBigError;
impl std::error::Error for ValueTooBigError {}
impl fmt::Display for ValueTooBigError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Value is bigger than the configured max size of the cache"
        )
    }
}

struct ValueWithWeight<V> {
    value: V,
    weight: usize,
}
/// A cache that holds a limited number of key-value pairs. When the capacity of
/// the cache is exceeded, the least-recently-used (where "used" means a look-up
/// or putting the pair into the cache) pairs are automatically removed until the
/// size limit is met again.
pub struct WeightCache<K, V, S = hash_map::RandomState> {
    max: usize,
    current: usize,
    inner: LinkedHashMap<K, ValueWithWeight<V>, S>,
    #[cfg(feature = "metrics")]
    metrics: Metrics,
}
impl<K, V, S> fmt::Debug for WeightCache<K, V, S> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("WeightCache")
            .field("max", &self.max)
            .field("current", &self.current)
            .finish()
    }
}

impl<K: Hash + Eq, V: Weighable> Default for WeightCache<K, V> {
    fn default() -> Self {
        WeightCache::<K, V, RandomState>::new(
            NonZeroUsize::new(usize::max_value()).expect("MAX > 0"),
        )
    }
}

#[cfg(feature = "metrics")]
struct Metrics {
    hits: GenericCounter<AtomicU64>,
    misses: GenericCounter<AtomicU64>,
    inserts: GenericCounter<AtomicU64>,
    inserts_fail: GenericCounter<AtomicU64>,
    size: GenericGauge<AtomicU64>,
}
#[cfg(feature = "metrics")]
impl Metrics {
    fn new(namespace: Option<&str>) -> Self {
        if let Some(namespace) = namespace {
            let cache_size = GenericGauge::with_opts(
                Opts::new("cache_size", "Current size of the cache").namespace(namespace),
            )
            .unwrap();
            cache_size.set(0);
            Self {
                hits: GenericCounter::with_opts(
                    Opts::new("cache_hit", "Number of cache hits").namespace(namespace),
                )
                .unwrap(),
                misses: GenericCounter::with_opts(
                    Opts::new("cache_miss", "Number of cache misses").namespace(namespace),
                )
                .unwrap(),
                inserts: GenericCounter::with_opts(
                    Opts::new("cache_insert", "Number of successful cache insertions")
                        .namespace(namespace),
                )
                .unwrap(),
                inserts_fail: GenericCounter::with_opts(
                    Opts::new("cache_insert_fail", "Number of failed cache insertions")
                        .namespace(namespace),
                )
                .unwrap(),
                size: cache_size,
            }
        } else {
            let cache_size = GenericGauge::new("cache_size", "Current size of the cache").unwrap();
            cache_size.set(0);
            Self {
                hits: GenericCounter::new("cache_hit", "Number of cache hits").unwrap(),
                misses: GenericCounter::new("cache_miss", "Number of cache misses").unwrap(),
                inserts: GenericCounter::new(
                    "cache_insert",
                    "Number of successful cache insertions",
                )
                .unwrap(),
                inserts_fail: GenericCounter::new(
                    "cache_insert_fail",
                    "Number of failed cache insertions",
                )
                .unwrap(),
                size: cache_size,
            }
        }
    }
}

impl<K: Hash + Eq, V: Weighable> WeightCache<K, V> {
    pub fn new(capacity: NonZeroUsize) -> Self {
        Self {
            max: capacity.get(),
            current: 0,
            inner: LinkedHashMap::new(),
            #[cfg(feature = "metrics")]
            metrics: Metrics::new(None),
        }
    }
    #[cfg(feature = "metrics")]
    pub fn new_with_namespace(capacity: NonZeroUsize, metrics_namespace: Option<&str>) -> Self {
        Self {
            max: capacity.get(),
            current: 0,
            inner: LinkedHashMap::new(),
            metrics: Metrics::new(metrics_namespace),
        }
    }
}
impl<K: Hash + Eq, V: Weighable, S: BuildHasher> WeightCache<K, V, S> {
    /// Create a [`WeightCache`] with a custom hasher.
    pub fn with_hasher(capacity: NonZeroUsize, hasher: S) -> Self {
        Self {
            max: capacity.get(),
            current: 0,
            inner: LinkedHashMap::with_hasher(hasher),
            #[cfg(feature = "metrics")]
            metrics: Metrics::new(None),
        }
    }
    #[cfg(feature = "metrics")]
    /// Registers metrics with a [`prometheus::Registry`]
    pub fn register(&self, registry: &Registry) -> Result<(), prometheus::Error> {
        registry.register(Box::new(self.metrics.hits.clone()))?;
        registry.register(Box::new(self.metrics.misses.clone()))?;
        registry.register(Box::new(self.metrics.inserts.clone()))?;
        registry.register(Box::new(self.metrics.inserts_fail.clone()))?;
        registry.register(Box::new(self.metrics.size.clone()))?;
        Ok(())
    }

    /// Returns a reference to the value corresponding to the given key, if it
    /// exists.
    pub fn get(&mut self, k: &K) -> Option<&V> {
        if let Some(v) = self.inner.get_refresh(k) {
            #[cfg(feature = "metrics")]
            self.metrics.hits.inc();
            Some(&v.value as &V)
        } else {
            #[cfg(feature = "metrics")]
            self.metrics.misses.inc();
            None
        }
    }

    /// Returns the number of key-value pairs in the cache.
    pub fn len(&self) -> usize {
        self.inner.len()
    }

    /// Returns `true` if the cache contains no key-value pairs.
    pub fn is_empty(&self) -> bool {
        self.inner.is_empty()
    }

    /// Inserts a key-value pair into the cache. Returns an error if the value is
    /// bigger than the cache's configured max size.
    pub fn put(&mut self, key: K, value: V) -> Result<(), ValueTooBigError> {
        let weight = V::measure(&value);
        if weight > self.max {
            #[cfg(feature = "metrics")]
            self.metrics.inserts_fail.inc();
            Err(ValueTooBigError)
        } else {
            self.current += weight;
            // did we remove an element?
            if let Some(x) = self.inner.insert(key, ValueWithWeight { value, weight }) {
                self.current -= x.weight;
            }

            // remove elements until we're below the size boundary again
            self.shrink_to_fit();
            #[cfg(feature = "metrics")]
            self.metrics.inserts.inc();
            Ok(())
        }
    }

    fn shrink_to_fit(&mut self) {
        while self.current > self.max && !self.inner.is_empty() {
            let (_, v) = self.inner.pop_front().expect("Not empty");
            self.current -= v.weight;
        }
        #[cfg(feature = "metrics")]
        self.metrics.size.set(self.current as u64);
    }
}

impl<K: Hash + Eq + 'static, V: Weighable + 'static, S: BuildHasher> WeightCache<K, V, S> {
    /// Returns an iterator over the cache's key-value pairs in least- to
    /// most-recently-used order consuming the cache.
    pub fn consume(self) -> Box<dyn Iterator<Item = (K, V)> + 'static> {
        #[cfg(feature = "metrics")]
        self.metrics.size.set(0);
        Box::new(self.inner.into_iter().map(|(k, v)| (k, v.value)))
    }
}

#[cfg(test)]
mod test {
    use std::convert::TryInto;

    use super::*;
    use quickcheck::{Arbitrary, Gen};
    use quickcheck_macros::quickcheck;

    #[derive(Clone, Debug, PartialEq)]
    struct HeavyWeight(usize);
    impl Weighable for HeavyWeight {
        fn measure(v: &Self) -> usize {
            v.0
        }
    }
    impl Arbitrary for HeavyWeight {
        fn arbitrary(g: &mut Gen) -> Self {
            Self(usize::arbitrary(g))
        }
        fn shrink(&self) -> Box<dyn Iterator<Item = Self>> {
            Box::new(usize::shrink(&self.0).map(HeavyWeight))
        }
    }
    #[derive(Clone, Debug, PartialEq)]
    struct UnitWeight;
    impl Weighable for UnitWeight {
        fn measure(_: &Self) -> usize {
            1
        }
    }
    impl Arbitrary for UnitWeight {
        fn arbitrary(_: &mut Gen) -> Self {
            Self
        }
    }

    #[test]
    fn should_not_evict_under_max_size() {
        let xs: Vec<_> = (0..10000).map(HeavyWeight).collect();
        let mut cache =
            WeightCache::<usize, HeavyWeight>::new(usize::max_value().try_into().unwrap());
        for (k, v) in xs.iter().enumerate() {
            cache.put(k, v.clone()).expect("empty")
        }
        let cached = cache.consume().map(|x| x.1).collect::<Vec<_>>();

        assert_eq!(xs, cached);
    }

    #[cfg(feature = "metrics")]
    fn metrics_test(namespace: Option<&str>) {
        let mut cache =
            WeightCache::<usize, UnitWeight>::new_with_namespace(3.try_into().unwrap(), namespace);
        let registry = Registry::new();
        cache.register(&registry).unwrap();
        for i in 0usize..5 {
            cache.put(i, UnitWeight).unwrap();
        }
        for i in 0usize..5 {
            cache.get(&i);
        }
        for metric in registry.gather() {
            println!("{} {:?}", metric.get_name(), metric.get_metric()[0]);
            match metric.get_name() {
                x if x
                    == format!(
                        "{}cache_size",
                        namespace.map(|y| format!("{}_", y)).unwrap_or_default()
                    ) =>
                {
                    assert_eq!(3, metric.get_metric()[0].get_gauge().get_value() as usize)
                }

                x if x
                    == format!(
                        "{}cache_insert",
                        namespace.map(|y| format!("{}_", y)).unwrap_or_default()
                    ) =>
                {
                    assert_eq!(5, metric.get_metric()[0].get_counter().get_value() as usize)
                }

                x if x
                    == format!(
                        "{}cache_insert_fail",
                        namespace.map(|y| format!("{}_", y)).unwrap_or_default()
                    ) =>
                {
                    assert_eq!(0, metric.get_metric()[0].get_counter().get_value() as usize)
                }

                x if x
                    == format!(
                        "{}cache_hit",
                        namespace.map(|y| format!("{}_", y)).unwrap_or_default()
                    ) =>
                {
                    assert_eq!(3, metric.get_metric()[0].get_counter().get_value() as usize)
                }

                x if x
                    == format!(
                        "{}cache_miss",
                        namespace.map(|y| format!("{}_", y)).unwrap_or_default()
                    ) =>
                {
                    assert_eq!(2, metric.get_metric()[0].get_counter().get_value() as usize)
                }
                x => panic!("unknown metrics {}", x),
            }
        }
    }

    #[cfg(feature = "metrics")]
    #[test]
    fn should_gather_metrics() {
        metrics_test(None);
        metrics_test(Some("test"));
    }

    #[quickcheck]
    fn should_reject_too_heavy_values(total_size: NonZeroUsize, input: HeavyWeight) -> bool {
        let mut cache = WeightCache::<usize, HeavyWeight>::new(total_size);
        let res = cache.put(42, input.clone());
        match res {
            Ok(_) if input.0 < total_size.get() => true,
            Err(_) if input.0 >= total_size.get() => true,
            _ => false,
        }
    }

    #[quickcheck]
    fn should_evict_once_the_size_target_is_hit(
        input: Vec<UnitWeight>,
        max_size: NonZeroUsize,
    ) -> bool {
        let mut cache_size = 0usize;
        let mut cache = WeightCache::<usize, UnitWeight>::new(max_size);
        for (k, v) in input.into_iter().enumerate() {
            let weight = UnitWeight::measure(&v);
            cache_size += weight;
            let len_before = cache.len();
            cache.put(k, v).unwrap();
            let len_after = cache.len();
            if cache_size > max_size.get() {
                assert_eq!(len_before, len_after);
                cache_size -= weight;
            } else {
                assert_eq!(len_before + 1, len_after);
            }
        }

        true
    }
}