use crate::rowid::RowId;
use std::collections::BTreeMap;
pub struct HotIndex {
inner: BTreeMap<Vec<u8>, RowId>,
}
impl Default for HotIndex {
fn default() -> Self {
Self::new()
}
}
impl HotIndex {
pub fn new() -> Self {
Self {
inner: BTreeMap::new(),
}
}
pub fn insert(&mut self, key: Vec<u8>, row_id: RowId) {
self.inner.insert(key, row_id);
}
pub fn get(&self, key: &[u8]) -> Option<RowId> {
self.inner.get(key).copied()
}
pub fn remove(&mut self, key: &[u8]) -> Option<RowId> {
self.inner.remove(key)
}
pub fn len(&self) -> usize {
self.inner.len()
}
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
pub fn entries(&self) -> Vec<(Vec<u8>, RowId)> {
self.inner.iter().map(|(k, v)| (k.clone(), *v)).collect()
}
pub fn from_entries(entries: Vec<(Vec<u8>, RowId)>) -> Self {
Self {
inner: entries.into_iter().collect(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn insert_and_get() {
let mut h = HotIndex::new();
h.insert(b"alice".to_vec(), RowId(1));
h.insert(b"bob".to_vec(), RowId(2));
assert_eq!(h.get(b"alice"), Some(RowId(1)));
assert_eq!(h.get(b"bob"), Some(RowId(2)));
assert_eq!(h.get(b"carol"), None);
h.insert(b"alice".to_vec(), RowId(9));
assert_eq!(h.get(b"alice"), Some(RowId(9)));
}
}