use std::borrow::Borrow;
use std::cmp::Ordering;
use std::fmt;
use collate::{Collate, CollateRef};
mod group;
mod node;
mod range;
mod tree;
pub use node::{Block, Node};
pub use range::Range;
pub use tree::{BTree, BTreeLock, BTreeReadGuard, BTreeWriteGuard, Keys};
pub use collate;
const NODE_STACK_SIZE: usize = 32;
pub const KEY_STACK_SIZE: usize = 32;
pub type Key<V> = smallvec::SmallVec<[V; KEY_STACK_SIZE]>;
#[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> Collator<C> {
fn cmp_slices<L, R>(&self, left: &[L], right: &[R]) -> Ordering
where
L: Borrow<C::Value>,
R: Borrow<C::Value>,
{
for i in 0..Ord::min(left.len(), right.len()) {
match self.value.cmp(left[i].borrow(), right[i].borrow()) {
Ordering::Equal => {}
ord => return ord,
}
}
Ordering::Equal
}
}
impl<C: Collate> Collate for Collator<C> {
type Value = Vec<C::Value>;
fn cmp(&self, left: &Self::Value, right: &Self::Value) -> Ordering {
self.cmp_slices(left, right)
}
}
impl<C: Collate> CollateRef<[C::Value]> for Collator<C> {
fn cmp_ref(&self, left: &[C::Value], right: &[C::Value]) -> Ordering {
self.cmp_slices(left, right)
}
}
impl<C: Collate> CollateRef<Key<C::Value>> for Collator<C> {
fn cmp_ref(&self, left: &Key<C::Value>, right: &Key<C::Value>) -> Ordering {
self.cmp_slices(left, right)
}
}
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 + Send + Sync + 'static;
type Value: Default + Clone + Eq + Send + Sync + fmt::Debug + 'static;
fn block_size(&self) -> usize;
fn len(&self) -> usize;
fn order(&self) -> usize;
fn validate_key(&self, key: Vec<Self::Value>) -> Result<Vec<Self::Value>, Self::Error>;
}