pub mod cursor_list;
pub use self::cursor_list::CursorList;
use timely::progress::Timestamp;
use crate::lattice::Lattice;
use crate::difference::Semigroup;
use crate::trace::implementations::containers::BatchContainer;
pub trait Navigable {
type Cursor: Cursor<Storage = Self>;
fn cursor(&self) -> Self::Cursor;
}
pub type BatchCursor<Tr> = <<Tr as crate::trace::TraceReader>::Batch as Navigable>::Cursor;
pub type BatchKey<'a, Tr> = <BatchCursor<Tr> as Cursor>::Key<'a>;
pub type BatchVal<'a, Tr> = <BatchCursor<Tr> as Cursor>::Val<'a>;
pub type BatchValOwn<Tr> = <BatchCursor<Tr> as Cursor>::ValOwn;
pub type BatchDiffGat<'a, Tr> = <BatchCursor<Tr> as Cursor>::DiffGat<'a>;
pub type BatchDiff<Tr> = <BatchCursor<Tr> as Cursor>::Diff;
pub type BatchTimeGat<'a, Tr> = <BatchCursor<Tr> as Cursor>::TimeGat<'a>;
pub fn cursor_list<B: crate::trace::BatchReader + Navigable>(batches: Vec<B>) -> (CursorList<B::Cursor>, Vec<B>) {
let cursors = batches.iter().map(|batch| batch.cursor()).collect::<Vec<_>>();
let cursor = CursorList::new(cursors, &batches);
(cursor, batches)
}
pub trait Cursor {
type Storage;
type Key<'a>: Copy + Ord;
type ValOwn: Clone + Ord;
type Val<'a>: Copy + Ord;
type Time: Lattice + Timestamp;
type TimeGat<'a>: Copy + Ord;
type Diff: Semigroup + 'static;
type DiffGat<'a>: Copy + Ord;
type KeyContainer: for<'a> BatchContainer<ReadItem<'a> = Self::Key<'a>>;
type ValContainer: for<'a> BatchContainer<ReadItem<'a> = Self::Val<'a>, Owned = Self::ValOwn>;
type TimeContainer: for<'a> BatchContainer<ReadItem<'a> = Self::TimeGat<'a>, Owned = Self::Time>;
type DiffContainer: for<'a> BatchContainer<ReadItem<'a> = Self::DiffGat<'a>, Owned = Self::Diff>;
#[inline(always)] fn owned_val(val: Self::Val<'_>) -> Self::ValOwn { <Self::ValContainer as BatchContainer>::into_owned(val) }
#[inline(always)] fn owned_time(time: Self::TimeGat<'_>) -> Self::Time { <Self::TimeContainer as BatchContainer>::into_owned(time) }
#[inline(always)] fn owned_diff(diff: Self::DiffGat<'_>) -> Self::Diff { <Self::DiffContainer as BatchContainer>::into_owned(diff) }
#[inline(always)] fn clone_time_onto(time: Self::TimeGat<'_>, onto: &mut Self::Time) { <Self::TimeContainer as BatchContainer>::clone_onto(time, onto) }
fn key_valid(&self, storage: &Self::Storage) -> bool;
fn val_valid(&self, storage: &Self::Storage) -> bool;
fn key<'a>(&self, storage: &'a Self::Storage) -> Self::Key<'a>;
fn val<'a>(&self, storage: &'a Self::Storage) -> Self::Val<'a>;
fn get_key<'a>(&self, storage: &'a Self::Storage) -> Option<Self::Key<'a>>;
fn get_val<'a>(&self, storage: &'a Self::Storage) -> Option<Self::Val<'a>>;
fn map_times<L: FnMut(Self::TimeGat<'_>, Self::DiffGat<'_>)>(&mut self, storage: &Self::Storage, logic: L);
fn step_key(&mut self, storage: &Self::Storage);
fn seek_key(&mut self, storage: &Self::Storage, key: Self::Key<'_>);
fn step_val(&mut self, storage: &Self::Storage);
fn seek_val(&mut self, storage: &Self::Storage, val: Self::Val<'_>);
fn rewind_keys(&mut self, storage: &Self::Storage);
fn rewind_vals(&mut self, storage: &Self::Storage);
fn populate_key<'a>(&mut self, storage: &'a Self::Storage, key: Self::Key<'a>, meet: Option<&Self::Time>, target: &mut crate::operators::EditList<Self::Val<'a>, Self::Time, Self::Diff>) {
target.clear();
self.seek_key(storage, key);
if self.get_key(storage) == Some(key) {
self.rewind_vals(storage);
while let Some(val) = self.get_val(storage) {
self.map_times(storage, |time, diff| {
use crate::lattice::Lattice;
let mut time = Self::owned_time(time);
if let Some(meet) = meet { time.join_assign(meet); }
target.push(time, Self::owned_diff(diff))
});
target.seal(val);
self.step_val(storage);
}
}
}
fn to_vec<K, IK, V, IV>(&mut self, storage: &Self::Storage, into_key: IK, into_val: IV) -> Vec<((K, V), Vec<(Self::Time, Self::Diff)>)>
where
IK: for<'a> Fn(Self::Key<'a>) -> K,
IV: for<'a> Fn(Self::Val<'a>) -> V,
{
let mut out = Vec::new();
self.rewind_keys(storage);
while let Some(key) = self.get_key(storage) {
self.rewind_vals(storage);
while let Some(val) = self.get_val(storage) {
let mut kv_out = Vec::new();
self.map_times(storage, |ts, r| {
kv_out.push((Self::owned_time(ts), Self::owned_diff(r)));
});
out.push(((into_key(key), into_val(val)), kv_out));
self.step_val(storage);
}
self.step_key(storage);
}
out
}
}