use std::cmp::Ordering;
use std::{fmt, io};
use collate::Collate;
mod node;
mod range;
mod tree;
pub use node::{Block, Node};
pub use range::Range;
pub use tree::{BTree, BTreeLock, BTreeReadGuard, BTreeWriteGuard};
pub use collate;
pub type Key<V> = Vec<V>;
#[derive(Copy, Clone)]
pub struct Collator<C> {
value: C,
}
impl<C> Collator<C> {
pub fn new(value: C) -> Self {
Self { value }
}
pub fn inner(&self) -> &C {
&self.value
}
}
impl<C> Collate for Collator<C>
where
C: Collate,
{
type Value = Key<C::Value>;
fn cmp(&self, left: &Self::Value, right: &Self::Value) -> Ordering {
for i in 0..Ord::min(left.len(), right.len()) {
match self.value.cmp(&left[i], &right[i]) {
Ordering::Equal => {}
ord => return ord,
}
}
Ordering::Equal
}
}
impl<C> PartialEq for Collator<C>
where
C: Collate,
{
fn eq(&self, other: &Self) -> bool {
self.value == other.value
}
}
impl<C> Eq for Collator<C> where C: Collate {}
pub trait Schema: Eq + fmt::Debug {
type Error: std::error::Error + From<io::Error>;
type Value: Clone + Eq + Send + Sync + fmt::Debug + 'static;
fn block_size(&self) -> usize;
fn len(&self) -> usize;
fn order(&self) -> usize;
fn validate(&self, key: Key<Self::Value>) -> Result<Key<Self::Value>, Self::Error>;
}