-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
778 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
pub mod time_tumbling_holistic; | ||
pub mod count_tumbling_holistic; | ||
pub mod count_sliding_holistic; | ||
pub mod time_tumbling_holistic_vec; | ||
// pub mod time_sliding_holistic; | ||
pub mod time_sliding_aligned_commutative_associative; | ||
pub mod time_sliding_aligned_holistic; |
File renamed without changes.
83 changes: 83 additions & 0 deletions
83
.../runtime/src/builtins/keyed_stream/window/time_sliding_aligned_commutative_associative.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use std::collections::BTreeMap; | ||
|
||
use crate::builtins::duration::Duration; | ||
use crate::builtins::keyed_stream::KeyedEvent; | ||
use crate::builtins::keyed_stream::KeyedStream; | ||
use crate::builtins::stream::window::WindowRange; | ||
use crate::builtins::time::Time; | ||
use crate::runner::context::Context; | ||
use crate::traits::Data; | ||
use crate::traits::Key; | ||
use crate::HashMap; | ||
|
||
impl<K: Key, T: Data> KeyedStream<K, T> { | ||
// Requires that duration % step == 0 | ||
#[allow(clippy::too_many_arguments)] | ||
pub fn time_sliding_aligned_commutative_associative_window<P, O>( | ||
mut self, | ||
ctx: &mut Context, | ||
duration: Duration, | ||
step: Duration, | ||
_init: P, | ||
lift: impl Fn(&T) -> P + Send + 'static, | ||
combine: impl Fn(&P, &P) -> P + Send + 'static, | ||
lower: impl Fn(&K, &P, WindowRange) -> O + Send + 'static, | ||
) -> KeyedStream<K, O> | ||
where | ||
O: Data, | ||
P: Data, | ||
{ | ||
assert!(duration % step == Duration::from_seconds(0)); | ||
ctx.keyed_operator(|tx| async move { | ||
let mut slices: BTreeMap<Time, HashMap<K, P>> = BTreeMap::new(); | ||
let mut output: HashMap<K, P> = HashMap::default(); | ||
loop { | ||
match self.recv().await { | ||
KeyedEvent::Data(time, key, data) => { | ||
let data = lift(&data); | ||
let wr = WindowRange::of(time, step, step); | ||
slices | ||
.entry(wr.t0) | ||
.or_default() | ||
.entry(key) | ||
.and_modify(|e| *e = combine(e, &data)) | ||
.or_insert(data); | ||
} | ||
KeyedEvent::Watermark(time) => { | ||
while let Some(entry) = slices.first_entry() { | ||
let t0 = *entry.key(); | ||
let t1 = t0 + duration; | ||
let wr = WindowRange::new(t0, t1); | ||
if wr.t1 <= time { | ||
for (_, kvs) in slices.range(..t1) { | ||
for (k, v) in kvs { | ||
output | ||
.entry(k.clone()) | ||
.and_modify(|e| *e = combine(e, v)) | ||
.or_insert_with(|| v.clone()); | ||
} | ||
} | ||
for (k, v) in output.drain() { | ||
let output = lower(&k, &v, wr); | ||
tx.send(KeyedEvent::Data(time, k, output.deep_clone())) | ||
.await; | ||
} | ||
slices.pop_first(); | ||
} else { | ||
break; | ||
} | ||
} | ||
tx.send(KeyedEvent::Watermark(time)).await; | ||
} | ||
KeyedEvent::Snapshot(i) => { | ||
tx.send(KeyedEvent::Snapshot(i)).await; | ||
} | ||
KeyedEvent::Sentinel => { | ||
tx.send(KeyedEvent::Sentinel).await; | ||
break; | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
crates/runtime/src/builtins/keyed_stream/window/time_sliding_aligned_holistic.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
use crate::builtins::duration::Duration; | ||
use crate::builtins::keyed_stream::KeyedEvent; | ||
use crate::builtins::keyed_stream::KeyedStream; | ||
use crate::builtins::stream::window::align; | ||
use crate::builtins::stream::window::WindowRange; | ||
use crate::builtins::time::Time; | ||
use crate::runner::context::Context; | ||
use crate::traits::Data; | ||
use crate::traits::Key; | ||
use crate::BTreeMap; | ||
use crate::HashMap; | ||
|
||
impl<K: Key, T: Data> KeyedStream<K, T> { | ||
pub fn time_sliding_aligned_holistic_window<O>( | ||
mut self, | ||
ctx: &mut Context, | ||
size: Duration, | ||
slide: Duration, | ||
compute: impl for<'a, 'b> Fn(&K, Window<'a, 'b, T>, WindowRange) -> O + Send + 'static, | ||
) -> KeyedStream<K, O> | ||
where | ||
O: Data, | ||
{ | ||
ctx.keyed_operator(|tx| async move { | ||
let mut slices: BTreeMap<Time, HashMap<K, Vec<T>>> = BTreeMap::new(); | ||
loop { | ||
match self.recv().await { | ||
KeyedEvent::Data(time, key, data) => { | ||
slices | ||
.entry(align(time, slide)) | ||
.or_default() | ||
.entry(key) | ||
.or_default() | ||
.push(data); | ||
} | ||
KeyedEvent::Watermark(time) => { | ||
while let Some((t0, _)) = slices.first_key_value() { | ||
let t1 = *t0 + size; | ||
let wr = WindowRange::new(*t0, t1); | ||
if wr.t1 < time { | ||
let mut output: HashMap<K, Vec<&[T]>> = HashMap::default(); | ||
for (_, kvs) in slices.range(..wr.t1) { | ||
for (k, vs) in kvs { | ||
output.entry(k.clone()).or_default().push(vs); | ||
} | ||
} | ||
for (k, vs) in output.drain() { | ||
let output = compute(&k, Window::new(vs.as_slice()), wr); | ||
tx.send(KeyedEvent::Data(time, k, output.deep_clone())) | ||
.await; | ||
} | ||
slices.pop_first(); | ||
} else { | ||
break; | ||
} | ||
} | ||
tx.send(KeyedEvent::Watermark(time)).await; | ||
} | ||
KeyedEvent::Snapshot(i) => { | ||
tx.send(KeyedEvent::Snapshot(i)).await; | ||
} | ||
KeyedEvent::Sentinel => { | ||
tx.send(KeyedEvent::Sentinel).await; | ||
break; | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
pub struct Window<'a, 'b, T> { | ||
slices: &'a [&'b [T]], | ||
} | ||
|
||
impl<'a, 'b, T> Window<'a, 'b, T> { | ||
fn new(slices: &'a [&'b [T]]) -> Self { | ||
Self { slices } | ||
} | ||
} | ||
|
||
pub struct WindowIter<'a, 'b, T> { | ||
slices: &'a [&'b [T]], | ||
idx: usize, | ||
} | ||
|
||
impl<'a, 'b, T> Iterator for WindowIter<'a, 'b, T> { | ||
type Item = &'b T; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
if self.idx < self.slices.len() { | ||
let slice = self.slices[self.idx]; | ||
self.idx += 1; | ||
slice.first() | ||
} else { | ||
None | ||
} | ||
} | ||
} | ||
|
||
impl<'a, 'b, T> IntoIterator for Window<'a, 'b, T> { | ||
type Item = &'b T; | ||
type IntoIter = WindowIter<'a, 'b, T>; | ||
|
||
fn into_iter(self) -> Self::IntoIter { | ||
WindowIter { | ||
slices: self.slices, | ||
idx: 0, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Oops, something went wrong.