Check if the refblock cache is full and we need to evict.
(&mut self, index: usize, block: T, write_callback: F)
| 140 | |
| 141 | // Check if the refblock cache is full and we need to evict. |
| 142 | pub fn insert<F>(&mut self, index: usize, block: T, write_callback: F) -> io::Result<()> |
| 143 | where |
| 144 | F: FnOnce(usize, T) -> io::Result<()>, |
| 145 | { |
| 146 | if self.map.len() == self.capacity { |
| 147 | // TODO(dgreid) - smarter eviction strategy. |
| 148 | let to_evict = *self.map.iter().next().unwrap().0; |
| 149 | if let Some(evicted) = self.map.remove(&to_evict) |
| 150 | && evicted.dirty() |
| 151 | { |
| 152 | write_callback(to_evict, evicted)?; |
| 153 | } |
| 154 | } |
| 155 | self.map.insert(index, block); |
| 156 | Ok(()) |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | #[cfg(test)] |