use super::{KeyPosition, KeyspaceNode, NodeRef};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum KeyRange {
Bounded(KeyPosition, KeyPosition),
Unbounded(KeyPosition),
}
impl KeyRange {
pub fn new(start: KeyPosition, end: Option<KeyPosition>) -> Self {
match end {
Some(end) => KeyRange::Bounded(start, end),
None => KeyRange::Unbounded(start),
}
}
pub fn contains(&self, key_hash: KeyPosition) -> bool {
match self {
KeyRange::Bounded(start, end) => key_hash >= *start && key_hash < *end,
KeyRange::Unbounded(start) => key_hash >= *start,
}
}
}
#[derive(Debug, PartialEq, Eq)]
pub struct Interval<N: KeyspaceNode> {
key_range: KeyRange,
nodes: Vec<NodeRef<N>>,
}
impl<N: KeyspaceNode> Clone for Interval<N> {
fn clone(&self) -> Self {
Self {
key_range: self.key_range,
nodes: self.nodes.clone(),
}
}
}
impl<N: KeyspaceNode> Interval<N> {
pub(crate) fn new<I: IntoIterator<Item = NodeRef<N>>>(key_range: KeyRange, nodes: I) -> Self {
let nodes = nodes.into_iter().collect::<Vec<_>>();
Self { key_range, nodes }
}
pub fn key_range(&self) -> &KeyRange {
&self.key_range
}
pub fn nodes(&self) -> &Vec<NodeRef<N>> {
&self.nodes
}
}