use crate::stream::joiner::plan::{EqualityPlan, IndexedPlan};
use crate::stream::relational::index::HashIndex;
use crate::stream::relational::RowHandle;
use std::borrow::Cow;
pub trait KeyOf: IndexedPlan {
type Key: Eq + std::hash::Hash + Clone;
fn left_candidates_by_key<'i>(
&self,
indexes: &'i Self::Indexes,
key: &Self::Key,
) -> Cow<'i, [RowHandle]>;
}
pub trait RightKeyOf: IndexedPlan {
type Key: Eq + std::hash::Hash + Clone;
fn right_row_key<'i>(
&self,
indexes: &'i Self::Indexes,
handle: RowHandle,
) -> Option<&'i Self::Key>;
}
impl<P> KeyOf for P
where
P: EqualityPlan + IndexedPlan,
P::Key: 'static,
P::Indexes: LeftHashIndex<P::Key>,
{
type Key = P::Key;
#[inline]
fn left_candidates_by_key<'i>(
&self,
indexes: &'i Self::Indexes,
key: &P::Key,
) -> Cow<'i, [RowHandle]> {
Cow::Borrowed(indexes.left_hash().lookup(key))
}
}
impl<P> RightKeyOf for P
where
P: EqualityPlan + IndexedPlan,
P::Key: 'static,
P::Indexes: RightHashIndex<P::Key>,
{
type Key = P::Key;
#[inline]
fn right_row_key<'i>(
&self,
indexes: &'i Self::Indexes,
handle: RowHandle,
) -> Option<&'i P::Key> {
indexes.right_hash().key_of(handle)
}
}
pub trait LeftHashIndex<K> {
fn left_hash(&self) -> &HashIndex<K>;
}
pub trait RightHashIndex<K> {
fn right_hash(&self) -> &HashIndex<K>;
}
impl<K: Eq + std::hash::Hash + Clone + 'static> LeftHashIndex<K> for (HashIndex<K>, HashIndex<K>) {
#[inline]
fn left_hash(&self) -> &HashIndex<K> {
&self.0
}
}
impl<K: Eq + std::hash::Hash + Clone + 'static> RightHashIndex<K> for (HashIndex<K>, HashIndex<K>) {
#[inline]
fn right_hash(&self) -> &HashIndex<K> {
&self.1
}
}