Truncates the index file starting from the entry with a key greater than or equal to the given key. If successful, `key` will no longer be in the index.
(&mut self, key: Key)
| 210 | /// |
| 211 | /// If successful, `key` will no longer be in the index. |
| 212 | pub(crate) fn truncate(&mut self, key: Key) -> Result<(), IndexError> { |
| 213 | let key = key.into(); |
| 214 | let (found_key, index) = self |
| 215 | .find_index(Key::from(key)) |
| 216 | .map(|(found, index)| (found.into(), index)) |
| 217 | .or_else(|e| { |
| 218 | match e { |
| 219 | // If key is smaller than first entry, truncate all entries |
| 220 | IndexError::KeyNotFound => Ok((key, 0)), |
| 221 | _ => Err(e), |
| 222 | } |
| 223 | })?; |
| 224 | |
| 225 | // If returned key is smaller than asked key, truncate from next entry |
| 226 | self.num_entries = if found_key == key { |
| 227 | index as usize |
| 228 | } else { |
| 229 | index as usize + 1 |
| 230 | }; |
| 231 | |
| 232 | let start = self.num_entries * ENTRY_SIZE; |
| 233 | trace!( |
| 234 | "truncate key={} found={} index={} num-entries={} start={}", |
| 235 | key, |
| 236 | found_key, |
| 237 | index, |
| 238 | self.num_entries, |
| 239 | start |
| 240 | ); |
| 241 | |
| 242 | if start < self.inner.len() { |
| 243 | self.inner[start..].fill(0); |
| 244 | } |
| 245 | |
| 246 | self.inner.flush()?; |
| 247 | |
| 248 | Ok(()) |
| 249 | } |
| 250 | |
| 251 | /// Obtain an iterator over the entries of the index. |
| 252 | pub fn entries(&self) -> Entries<'_, Key> { |