differential_dataflow/trace/cursor/
mod.rs1pub mod cursor_list;
9
10pub use self::cursor_list::CursorList;
11
12use timely::progress::Timestamp;
13use crate::lattice::Lattice;
14use crate::difference::Semigroup;
15use crate::trace::implementations::containers::BatchContainer;
16
17pub trait Navigable {
26
27 type Cursor: Cursor<Storage = Self>;
32
33 fn cursor(&self) -> Self::Cursor;
35}
36
37pub type BatchCursor<Tr> = <<Tr as crate::trace::TraceReader>::Batch as Navigable>::Cursor;
39
40pub type BatchKey<'a, Tr> = <BatchCursor<Tr> as Cursor>::Key<'a>;
42pub type BatchVal<'a, Tr> = <BatchCursor<Tr> as Cursor>::Val<'a>;
44pub type BatchValOwn<Tr> = <BatchCursor<Tr> as Cursor>::ValOwn;
46pub type BatchDiffGat<'a, Tr> = <BatchCursor<Tr> as Cursor>::DiffGat<'a>;
48pub type BatchDiff<Tr> = <BatchCursor<Tr> as Cursor>::Diff;
50pub type BatchTimeGat<'a, Tr> = <BatchCursor<Tr> as Cursor>::TimeGat<'a>;
52
53pub fn cursor_list<B: crate::trace::BatchReader + Navigable>(batches: Vec<B>) -> (CursorList<B::Cursor>, Vec<B>) {
59 let cursors = batches.iter().map(|batch| batch.cursor()).collect::<Vec<_>>();
60 let cursor = CursorList::new(cursors, &batches);
61 (cursor, batches)
62}
63
64pub trait Cursor {
66
67 type Storage;
69
70 type Key<'a>: Copy + Ord;
72 type ValOwn: Clone + Ord;
74 type Val<'a>: Copy + Ord;
76 type Time: Lattice + Timestamp;
78 type TimeGat<'a>: Copy + Ord;
80 type Diff: Semigroup + 'static;
82 type DiffGat<'a>: Copy + Ord;
84
85 type KeyContainer: for<'a> BatchContainer<ReadItem<'a> = Self::Key<'a>>;
87 type ValContainer: for<'a> BatchContainer<ReadItem<'a> = Self::Val<'a>, Owned = Self::ValOwn>;
89 type TimeContainer: for<'a> BatchContainer<ReadItem<'a> = Self::TimeGat<'a>, Owned = Self::Time>;
91 type DiffContainer: for<'a> BatchContainer<ReadItem<'a> = Self::DiffGat<'a>, Owned = Self::Diff>;
93
94 #[inline(always)] fn owned_val(val: Self::Val<'_>) -> Self::ValOwn { <Self::ValContainer as BatchContainer>::into_owned(val) }
96 #[inline(always)] fn owned_time(time: Self::TimeGat<'_>) -> Self::Time { <Self::TimeContainer as BatchContainer>::into_owned(time) }
98 #[inline(always)] fn owned_diff(diff: Self::DiffGat<'_>) -> Self::Diff { <Self::DiffContainer as BatchContainer>::into_owned(diff) }
100 #[inline(always)] fn clone_time_onto(time: Self::TimeGat<'_>, onto: &mut Self::Time) { <Self::TimeContainer as BatchContainer>::clone_onto(time, onto) }
102
103 fn key_valid(&self, storage: &Self::Storage) -> bool;
107 fn val_valid(&self, storage: &Self::Storage) -> bool;
111
112 fn key<'a>(&self, storage: &'a Self::Storage) -> Self::Key<'a>;
114 fn val<'a>(&self, storage: &'a Self::Storage) -> Self::Val<'a>;
116
117 fn get_key<'a>(&self, storage: &'a Self::Storage) -> Option<Self::Key<'a>>;
119 fn get_val<'a>(&self, storage: &'a Self::Storage) -> Option<Self::Val<'a>>;
121
122 fn map_times<L: FnMut(Self::TimeGat<'_>, Self::DiffGat<'_>)>(&mut self, storage: &Self::Storage, logic: L);
125
126 fn step_key(&mut self, storage: &Self::Storage);
128 fn seek_key(&mut self, storage: &Self::Storage, key: Self::Key<'_>);
130
131 fn step_val(&mut self, storage: &Self::Storage);
133 fn seek_val(&mut self, storage: &Self::Storage, val: Self::Val<'_>);
135
136 fn rewind_keys(&mut self, storage: &Self::Storage);
138 fn rewind_vals(&mut self, storage: &Self::Storage);
140
141 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>) {
146 target.clear();
147 self.seek_key(storage, key);
148 if self.get_key(storage) == Some(key) {
149 self.rewind_vals(storage);
150 while let Some(val) = self.get_val(storage) {
151 self.map_times(storage, |time, diff| {
152 use crate::lattice::Lattice;
153 let mut time = Self::owned_time(time);
154 if let Some(meet) = meet { time.join_assign(meet); }
155 target.push(time, Self::owned_diff(diff))
156 });
157 target.seal(val);
158 self.step_val(storage);
159 }
160 }
161 }
162
163 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)>)>
165 where
166 IK: for<'a> Fn(Self::Key<'a>) -> K,
167 IV: for<'a> Fn(Self::Val<'a>) -> V,
168 {
169 let mut out = Vec::new();
170 self.rewind_keys(storage);
171 while let Some(key) = self.get_key(storage) {
172 self.rewind_vals(storage);
173 while let Some(val) = self.get_val(storage) {
174 let mut kv_out = Vec::new();
175 self.map_times(storage, |ts, r| {
176 kv_out.push((Self::owned_time(ts), Self::owned_diff(r)));
177 });
178 out.push(((into_key(key), into_val(val)), kv_out));
179 self.step_val(storage);
180 }
181 self.step_key(storage);
182 }
183 out
184 }
185}