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
//! # Evicted Map

use crate::api;
#[cfg(feature = "serialize")]
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, LinkedList};

/// A hash map with a capped number of attributes that retains the most
/// recently set entries.
#[cfg_attr(feature = "serialize", derive(Deserialize, Serialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct EvictedHashMap {
    map: HashMap<api::Key, api::Value>,
    evict_list: LinkedList<api::Key>,
    capacity: u32,
    dropped_count: u32,
}

impl EvictedHashMap {
    /// Create a new `EvictedHashMap` with a given capacity.
    pub fn new(capacity: u32) -> Self {
        EvictedHashMap {
            map: HashMap::new(),
            evict_list: Default::default(),
            capacity,
            dropped_count: 0,
        }
    }

    /// Inserts a key-value pair into the map.
    pub fn insert(&mut self, item: api::KeyValue) {
        // Check for existing item
        if let Some(value) = self.map.get_mut(&item.key) {
            *value = item.value;
            self.move_key_to_front(item.key);
            return;
        }

        // Add new item
        self.evict_list.push_front(item.key.clone());
        self.map.insert(item.key, item.value);

        // Verify size not exceeded
        if self.evict_list.len() as u32 > self.capacity {
            self.remove_oldest();
            self.dropped_count += 1;
        }
    }

    /// Returns the number of elements in the map.
    pub fn len(&self) -> usize {
        self.map.len()
    }

    /// Returns `true` if the map is empty.
    pub fn is_empty(&self) -> bool {
        self.map.is_empty()
    }

    /// Returns the dropped attribute count
    pub fn dropped_count(&self) -> u32 {
        self.dropped_count
    }

    /// Returns a front-to-back iterator.
    pub fn iter(&self) -> std::collections::hash_map::Iter<api::Key, api::Value> {
        self.map.iter()
    }

    /// Returns a reference to the value corresponding to the key if it exists
    pub fn get(&self, key: &api::Key) -> Option<&api::Value> {
        self.map.get(key)
    }

    fn move_key_to_front(&mut self, key: api::Key) {
        if self.evict_list.is_empty() {
            // If empty, push front
            self.evict_list.push_front(key);
        } else if self.evict_list.front() == Some(&key) {
            // Already the front, ignore
        } else {
            // Else split linked lists around key and combine
            let key_idx = self
                .evict_list
                .iter()
                .position(|k| k == &key)
                .expect("key must exist in evicted hash map, this is a bug");
            let mut tail = self.evict_list.split_off(key_idx);
            let item = tail.pop_front().unwrap();
            self.evict_list.push_front(item);
            self.evict_list.append(&mut tail);
        }
    }

    fn remove_oldest(&mut self) {
        if let Some(oldest_item) = self.evict_list.pop_back() {
            self.map.remove(&oldest_item);
        }
    }
}

impl IntoIterator for EvictedHashMap {
    type Item = (api::Key, api::Value);
    type IntoIter = std::collections::hash_map::IntoIter<api::Key, api::Value>;

    /// Creates a consuming iterator, that is, one that moves each key-value
    /// pair out of the map in arbitrary order. The map cannot be used after
    /// calling this.
    fn into_iter(self) -> Self::IntoIter {
        self.map.into_iter()
    }
}

impl<'a> IntoIterator for &'a EvictedHashMap {
    type Item = (&'a api::Key, &'a api::Value);
    type IntoIter = std::collections::hash_map::Iter<'a, api::Key, api::Value>;

    fn into_iter(self) -> Self::IntoIter {
        self.map.iter()
    }
}

impl<'a> IntoIterator for &'a mut EvictedHashMap {
    type Item = (&'a api::Key, &'a mut api::Value);
    type IntoIter = std::collections::hash_map::IterMut<'a, api::Key, api::Value>;

    fn into_iter(self) -> Self::IntoIter {
        self.map.iter_mut()
    }
}

#[cfg(test)]
mod tests {
    use super::EvictedHashMap;
    use crate::api::Key;
    use std::collections::HashSet;

    #[test]
    fn insert_over_capacity_test() {
        let capacity = 10;
        let mut map = EvictedHashMap::new(capacity);

        for i in 0..=capacity {
            map.insert(Key::new(i.to_string()).bool(true))
        }

        assert_eq!(map.dropped_count, 1);
        assert_eq!(map.len(), capacity as usize);
        assert_eq!(
            map.map.keys().cloned().collect::<HashSet<_>>(),
            (1..=capacity)
                .map(|i| Key::new(i.to_string()))
                .collect::<HashSet<_>>()
        );
    }
}