use std::collections::VecDeque;
use std::fmt;
use std::ops::ControlFlow;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
use crate::capability::HNil;
use crate::concurrency::{Concurrency, fork_all};
use crate::coordination::{Queue, QueueError};
use crate::failure::Never;
use crate::kernel::{Effect, fail, succeed, suspend, yield_fiber};
use crate::streaming::chunk::Chunk;
pub const DEFAULT_CHUNK_SIZE: usize = 64;
pub(crate) type Step<A, E, R> = Option<(Vec<A>, Stream<A, E, R>)>;
type PullFn<A, E, R> = Box<dyn FnOnce(usize) -> Effect<Step<A, E, R>, E, R> + Send>;
pub(crate) type FailSlot<E> = Arc<Mutex<Option<E>>>;
pub(crate) fn lock<T>(m: &Mutex<T>) -> MutexGuard<'_, T> {
m.lock().unwrap_or_else(PoisonError::into_inner)
}
pub struct Stream<A, E = Never, R = HNil> {
repr: Repr<A, E, R>,
}
enum Repr<A, E, R> {
Done,
Pull(PullFn<A, E, R>),
}
impl<A, E, R> fmt::Debug for Stream<A, E, R> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self.repr {
Repr::Done => "Stream::Done",
Repr::Pull(_) => "Stream::Pull",
})
}
}
pub(crate) enum StageStep<B> {
Emit(Vec<B>),
Last(Vec<B>),
}
impl<A, E, R> Stream<A, E, R>
where
A: Send + 'static,
E: Send + 'static,
R: Send + Sync + 'static,
{
pub fn empty() -> Self {
Self { repr: Repr::Done }
}
pub(crate) fn from_step(
f: impl FnOnce(usize) -> Effect<Step<A, E, R>, E, R> + Send + 'static,
) -> Self {
Self {
repr: Repr::Pull(Box::new(f)),
}
}
pub(crate) fn pull_raw(self, max: usize) -> Effect<Step<A, E, R>, E, R> {
match self.repr {
Repr::Done => succeed(None),
Repr::Pull(f) => f(max.max(1)),
}
}
pub fn next_chunk_with_size(self, max: usize) -> Effect<Option<(Chunk<A>, Self)>, E, R> {
let max = max.max(1);
suspend(move || self.next_chunk_now(max))
}
pub fn next_chunk(self) -> Effect<Option<(Chunk<A>, Self)>, E, R> {
self.next_chunk_with_size(DEFAULT_CHUNK_SIZE)
}
fn next_chunk_now(self, max: usize) -> Effect<Option<(Chunk<A>, Self)>, E, R> {
self.pull_raw(max).flat_map(move |step| match step {
None => succeed(None),
Some((items, rest)) if items.is_empty() => {
yield_fiber().flat_map(move |()| rest.next_chunk_now(max))
}
Some((items, rest)) => succeed(Some((Chunk::from_vec(items), rest))),
})
}
pub fn succeed(a: A) -> Self {
Self::from_vec(vec![a])
}
pub fn fail(e: E) -> Self {
Self::from_step(move |_| fail(e))
}
pub fn from_chunk(chunk: Chunk<A>) -> Self {
Self::from_vec(chunk.into_vec())
}
pub fn from_vec(items: Vec<A>) -> Self {
Self::buffered(items.into())
}
pub(crate) fn buffered(mut buf: VecDeque<A>) -> Self {
if buf.is_empty() {
return Self::empty();
}
Self::from_step(move |n| {
let out: Vec<A> = buf.drain(..n.min(buf.len())).collect();
succeed(Some((out, Self::buffered(buf))))
})
}
pub fn from_iterable<I>(iter: I) -> Self
where
I: IntoIterator<Item = A>,
I::IntoIter: Send + 'static,
{
Self::from_iter_state(iter.into_iter())
}
fn from_iter_state<It: Iterator<Item = A> + Send + 'static>(mut it: It) -> Self {
Self::from_step(move |n| {
let batch: Vec<A> = it.by_ref().take(n).collect();
succeed((!batch.is_empty()).then(|| (batch, Self::from_iter_state(it))))
})
}
pub fn unfold<S, F>(init: S, mut f: F) -> Self
where
S: Send + 'static,
F: FnMut(S) -> Option<(A, S)> + Send + 'static,
{
let mut state = Some(init);
Self::from_iter_state(std::iter::from_fn(move || {
let (a, s) = f(state.take()?)?;
state = Some(s);
Some(a)
}))
}
pub fn unfold_effect<S, F>(init: S, f: F) -> Self
where
S: Send + 'static,
F: FnMut(S) -> Effect<Option<(A, S)>, E, R> + Send + 'static,
{
Self::from_step(move |n| Self::unfold_gather(init, f, n, Vec::new()))
}
fn unfold_gather<S, F>(s: S, mut f: F, n: usize, mut out: Vec<A>) -> Effect<Step<A, E, R>, E, R>
where
S: Send + 'static,
F: FnMut(S) -> Effect<Option<(A, S)>, E, R> + Send + 'static,
{
f(s).flat_map(move |next| match next {
None => succeed((!out.is_empty()).then(|| (out, Self::empty()))),
Some((a, s)) => {
out.push(a);
if out.len() >= n {
succeed(Some((out, Self::unfold_effect(s, f))))
} else {
Self::unfold_gather(s, f, n, out)
}
}
})
}
pub fn from_pull<F>(f: F) -> Self
where
F: FnMut(usize) -> Effect<Option<Vec<A>>, E, R> + Send + 'static,
{
Self::pull_source(f, VecDeque::new())
}
fn pull_source<F>(mut f: F, mut buf: VecDeque<A>) -> Self
where
F: FnMut(usize) -> Effect<Option<Vec<A>>, E, R> + Send + 'static,
{
Self::from_step(move |n| {
if !buf.is_empty() {
let out: Vec<A> = buf.drain(..n.min(buf.len())).collect();
return succeed(Some((out, Self::pull_source(f, buf))));
}
f(n).map(move |batch| {
batch.map(|mut items| {
let rest: VecDeque<A> = if items.len() > n {
items.split_off(n).into()
} else {
VecDeque::new()
};
(items, Self::pull_source(f, rest))
})
})
})
}
pub fn from_effect(effect: Effect<Vec<A>, E, R>) -> Self {
Self::from_step(move |n| effect.flat_map(move |items| Self::from_vec(items).pull_raw(n)))
}
pub fn from_queue(queue: Queue<A>) -> Self {
queue_source(queue, Arc::new(Mutex::new(None)), VecDeque::new(), |v| v)
}
#[must_use]
pub fn chain(self, second: Self) -> Self {
if matches!(self.repr, Repr::Done) {
return second;
}
Self::from_step(move |n| {
self.pull_raw(n).flat_map(move |step| match step {
None => second.pull_raw(n),
Some((items, rest)) => succeed(Some((items, rest.chain(second)))),
})
})
}
#[must_use]
pub fn concat(self, second: Self) -> Self {
self.chain(second)
}
pub(crate) fn pull_stage<B, S, W, F>(self, state: S, want: W, step: F) -> Stream<B, E, R>
where
B: Send + 'static,
S: Send + 'static,
W: Fn(&S, usize) -> usize + Send + 'static,
F: FnMut(&mut S, Option<Vec<A>>) -> StageStep<B> + Send + 'static,
{
Stream::from_step(move |n| {
let w = want(&state, n).max(1);
self.pull_raw(w).map(move |res| {
let (mut state, mut step) = (state, step);
let (input, rest) = match res {
Some((items, rest)) => (Some(items), Some(rest)),
None => (None, None),
};
match (step(&mut state, input), rest) {
(StageStep::Emit(out), Some(rest)) => Some((out, rest.pull_stage(state, want, step))),
(StageStep::Emit(out) | StageStep::Last(out), _) => {
(!out.is_empty()).then(|| (out, Stream::empty()))
}
}
})
})
}
pub(crate) fn stage<B, F>(self, mut step: F) -> Stream<B, E, R>
where
B: Send + 'static,
F: FnMut(Option<Vec<A>>) -> StageStep<B> + Send + 'static,
{
self.pull_stage((), |(), n| n, move |(), input| step(input))
}
pub fn map<B, F>(self, f: F) -> Stream<B, E, R>
where
B: Send + 'static,
F: Fn(A) -> B + Send + Sync + 'static,
{
self.stage(move |input| StageStep::Emit(input.map_or_else(Vec::new, |v| bulk_map(v, &f))))
}
pub fn map_serial<B, F>(self, mut f: F) -> Stream<B, E, R>
where
B: Send + 'static,
F: FnMut(A) -> B + Send + 'static,
{
self.stage(move |input| {
StageStep::Emit(input.map_or_else(Vec::new, |v| v.into_iter().map(&mut f).collect()))
})
}
pub fn filter<P>(self, p: P) -> Self
where
P: Fn(&A) -> bool + Send + Sync + 'static,
{
self.stage(move |input| StageStep::Emit(input.map_or_else(Vec::new, |v| bulk_filter(v, &p))))
}
pub fn filter_serial<P>(self, mut p: P) -> Self
where
P: FnMut(&A) -> bool + Send + 'static,
{
self.stage(move |input| {
StageStep::Emit(input.map_or_else(Vec::new, |v| v.into_iter().filter(|a| p(a)).collect()))
})
}
pub fn take_while<P>(self, mut p: P) -> Self
where
P: FnMut(&A) -> bool + Send + 'static,
{
self.stage(move |input| {
let Some(items) = input else {
return StageStep::Emit(Vec::new());
};
let mut out = Vec::with_capacity(items.len());
for item in items {
if !p(&item) {
return StageStep::Last(out);
}
out.push(item);
}
StageStep::Emit(out)
})
}
pub fn drop_while<P>(self, mut p: P) -> Self
where
P: FnMut(&A) -> bool + Send + 'static,
{
let mut skipping = true;
self.stage(move |input| {
let Some(items) = input else {
return StageStep::Emit(Vec::new());
};
if !skipping {
return StageStep::Emit(items);
}
let out: Vec<A> = items.into_iter().skip_while(|a| p(a)).collect();
if !out.is_empty() {
skipping = false;
}
StageStep::Emit(out)
})
}
pub fn take(self, n: usize) -> Self {
if n == 0 {
return Self::empty();
}
self.pull_stage(
n,
|remaining: &usize, want| want.min(*remaining),
|remaining, input| {
let Some(mut items) = input else {
return StageStep::Emit(Vec::new());
};
items.truncate(*remaining);
*remaining -= items.len();
if *remaining == 0 {
StageStep::Last(items)
} else {
StageStep::Emit(items)
}
},
)
}
pub fn grouped(self, size: usize) -> Stream<Vec<A>, E, R> {
if size == 0 {
return Stream::empty();
}
let mut cur: Vec<A> = Vec::with_capacity(size);
self.stage(move |input| {
let Some(items) = input else {
let last = std::mem::take(&mut cur);
return StageStep::Emit(if last.is_empty() {
Vec::new()
} else {
vec![last]
});
};
let mut out: Vec<Vec<A>> = Vec::new();
for item in items {
cur.push(item);
if cur.len() == size {
out.push(std::mem::replace(&mut cur, Vec::with_capacity(size)));
}
}
StageStep::Emit(out)
})
}
pub fn scan<S, B, F>(self, state: S, mut f: F) -> Stream<B, E, R>
where
S: Send + 'static,
B: Send + 'static,
F: FnMut(&mut S, A) -> B + Send + 'static,
{
self.pull_stage(
state,
|_, n| n,
move |s, input| {
StageStep::Emit(input.map_or_else(Vec::new, |v| v.into_iter().map(|a| f(s, a)).collect()))
},
)
}
pub fn state_scan<S, B, F>(self, init: S, mut step: F) -> Stream<B, E, R>
where
S: Send + 'static,
B: Send + 'static,
F: FnMut(S, A) -> (S, Option<B>) + Send + 'static,
{
let mut state = Some(init);
self.stage(move |input| {
let mut out = Vec::new();
for item in input.unwrap_or_default() {
let Some(s) = state.take() else { break };
let (next, emitted) = step(s, item);
state = Some(next);
out.extend(emitted);
}
StageStep::Emit(out)
})
}
pub fn flat_map<B, F>(self, f: F) -> Stream<B, E, R>
where
B: Send + 'static,
F: FnMut(A) -> Stream<B, E, R> + Send + 'static,
{
flat_map_go(Some(self), VecDeque::new(), None, f)
}
pub fn map_effect<B, F>(self, f: F) -> Stream<B, E, R>
where
B: Send + 'static,
F: FnMut(A) -> Effect<B, E, R> + Send + 'static,
{
Stream::from_step(move |n| {
self.pull_raw(n).flat_map(move |step| match step {
None => succeed(None),
Some((items, rest)) => {
let cap = items.len();
seq_map(items.into_iter(), f, Vec::with_capacity(cap))
.map(move |(out, f)| Some((out, rest.map_effect(f))))
}
})
})
}
pub fn map_effect_par<B, F>(self, n: usize, f: F) -> Stream<B, E, R>
where
B: Send + 'static,
F: Fn(A) -> Effect<B, E, R> + Send + Sync + 'static,
{
self.map_effect_par_arc(n.max(1), Arc::new(f))
}
fn map_effect_par_arc<B, F>(self, limit: usize, f: Arc<F>) -> Stream<B, E, R>
where
B: Send + 'static,
F: Fn(A) -> Effect<B, E, R> + Send + Sync + 'static,
{
Stream::from_step(move |n| {
self.pull_raw(n).flat_map(move |step| match step {
None => succeed(None),
Some((items, rest)) => {
let mode = if limit == 1 || items.len() <= 1 {
Concurrency::Sequential
} else {
Concurrency::Bounded(limit)
};
let effects: Vec<Effect<B, E, R>> = items.into_iter().map(|a| f(a)).collect();
fork_all(effects, mode).map(move |out| Some((out, rest.map_effect_par_arc(limit, f))))
}
})
})
}
pub fn map_err<E2, F>(self, f: F) -> Stream<A, E2, R>
where
E2: Send + 'static,
F: Fn(E) -> E2 + Send + Sync + 'static,
{
self.map_err_arc(Arc::new(f))
}
fn map_err_arc<E2, F>(self, f: Arc<F>) -> Stream<A, E2, R>
where
E2: Send + 'static,
F: Fn(E) -> E2 + Send + Sync + 'static,
{
match self.repr {
Repr::Done => Stream::empty(),
Repr::Pull(pull) => Stream::from_step(move |n| {
let g = Arc::clone(&f);
pull(n)
.map_err(move |e| g(e))
.map(move |step| step.map(|(items, rest)| (items, rest.map_err_arc(f))))
}),
}
}
#[must_use]
pub fn with_throughput_counter(self, counter: Arc<AtomicU64>) -> Self {
match self.repr {
Repr::Done => self,
Repr::Pull(pull) => Self::from_step(move |n| {
pull(n).map(move |step| {
step.map(|(items, rest)| {
counter.fetch_add(items.len() as u64, Ordering::Relaxed);
(items, rest.with_throughput_counter(counter))
})
})
}),
}
}
pub(crate) fn fold_chunks<B, F>(self, init: B, f: F) -> Effect<B, E, R>
where
B: Send + 'static,
F: FnMut(B, Vec<A>) -> ControlFlow<B, B> + Send + 'static,
{
suspend(move || self.fold_chunks_now(init, f))
}
fn fold_chunks_now<B, F>(self, acc: B, mut f: F) -> Effect<B, E, R>
where
B: Send + 'static,
F: FnMut(B, Vec<A>) -> ControlFlow<B, B> + Send + 'static,
{
self
.pull_raw(DEFAULT_CHUNK_SIZE)
.flat_map(move |step| match step {
None => succeed(acc),
Some((items, rest)) => {
let empty = items.is_empty();
match f(acc, items) {
ControlFlow::Break(b) => succeed(b),
ControlFlow::Continue(b) if empty => {
yield_fiber().flat_map(move |()| rest.fold_chunks_now(b, f))
}
ControlFlow::Continue(b) => rest.fold_chunks_now(b, f),
}
}
})
}
pub fn run_collect(self) -> Effect<Vec<A>, E, R> {
self.fold_chunks(Vec::new(), |mut acc, items| {
acc.extend(items);
ControlFlow::Continue(acc)
})
}
pub fn run_fold<B, F>(self, init: B, mut f: F) -> Effect<B, E, R>
where
B: Send + 'static,
F: FnMut(B, A) -> B + Send + 'static,
{
self.fold_chunks(init, move |acc, items| {
ControlFlow::Continue(items.into_iter().fold(acc, &mut f))
})
}
pub fn run_fold_effect<B, F>(self, init: B, f: F) -> Effect<B, E, R>
where
B: Send + 'static,
F: FnMut(B, A) -> Effect<B, E, R> + Send + 'static,
{
suspend(move || self.fold_effect_now(init, f))
}
fn fold_effect_now<B, F>(self, acc: B, f: F) -> Effect<B, E, R>
where
B: Send + 'static,
F: FnMut(B, A) -> Effect<B, E, R> + Send + 'static,
{
self
.pull_raw(DEFAULT_CHUNK_SIZE)
.flat_map(move |step| match step {
None => succeed(acc),
Some((items, rest)) => {
let empty = items.is_empty();
seq_fold(items.into_iter(), acc, f).flat_map(move |(acc, f)| {
if empty {
yield_fiber().flat_map(move |()| rest.fold_effect_now(acc, f))
} else {
rest.fold_effect_now(acc, f)
}
})
}
})
}
pub fn run_for_each_effect<F>(self, mut f: F) -> Effect<(), E, R>
where
F: FnMut(A) -> Effect<(), E, R> + Send + 'static,
{
self.run_fold_effect((), move |(), a| f(a))
}
pub fn run_for_each<F>(self, f: F) -> Effect<(), E, R>
where
F: FnMut(A) -> Effect<(), E, R> + Send + 'static,
{
self.run_for_each_effect(f)
}
pub fn run_reduce<F>(self, mut f: F) -> Effect<Option<A>, E, R>
where
F: FnMut(A, A) -> A + Send + 'static,
{
self.run_fold(None, move |acc, a| {
Some(match acc {
Some(cur) => f(cur, a),
None => a,
})
})
}
pub fn run_drain(self) -> Effect<(), E, R> {
self.fold_chunks((), |(), _| ControlFlow::Continue(()))
}
pub fn run<B: Send + 'static>(self, sink: super::Sink<B, A, E, R>) -> Effect<B, E, R> {
sink.run(self)
}
}
impl<E, R> Stream<i64, E, R>
where
E: Send + 'static,
R: Send + Sync + 'static,
{
pub fn range(start: i64, end_exclusive: i64) -> Self {
Self::from_iterable(start..end_exclusive)
}
}
impl<A, R> Stream<A, Never, R>
where
A: Send + 'static,
R: Send + Sync + 'static,
{
pub fn widen<E: Send + 'static>(self) -> Stream<A, E, R> {
self.map_err(|n| match n {})
}
}
pub(crate) fn queue_source<T, A, E, R>(
queue: Queue<T>,
failure: FailSlot<E>,
mut buf: VecDeque<A>,
unpack: fn(Vec<T>) -> Vec<A>,
) -> Stream<A, E, R>
where
T: Send + 'static,
A: Send + 'static,
E: Send + 'static,
R: Send + Sync + 'static,
{
Stream::from_step(move |n| {
if !buf.is_empty() {
let out: Vec<A> = buf.drain(..n.min(buf.len())).collect();
return succeed(Some((out, queue_source(queue, failure, buf, unpack))));
}
queue
.take_up_to::<R>(n)
.map(Some)
.catch(|_: QueueError| succeed(None))
.flat_map(move |taken| match taken {
Some(raw) => {
let mut items = unpack(raw);
let rest: VecDeque<A> = if items.len() > n {
items.split_off(n).into()
} else {
VecDeque::new()
};
succeed(Some((items, queue_source(queue, failure, rest, unpack))))
}
None => match lock(&failure).take() {
Some(e) => fail(e),
None => succeed(None),
},
})
})
}
fn seq_map<A, B, E, R, F>(
mut items: std::vec::IntoIter<A>,
mut f: F,
mut out: Vec<B>,
) -> Effect<(Vec<B>, F), E, R>
where
A: Send + 'static,
B: Send + 'static,
E: Send + 'static,
R: Send + Sync + 'static,
F: FnMut(A) -> Effect<B, E, R> + Send + 'static,
{
match items.next() {
None => succeed((out, f)),
Some(a) => f(a).flat_map(move |b| {
out.push(b);
seq_map(items, f, out)
}),
}
}
fn seq_fold<A, B, E, R, F>(
mut items: std::vec::IntoIter<A>,
acc: B,
mut f: F,
) -> Effect<(B, F), E, R>
where
A: Send + 'static,
B: Send + 'static,
E: Send + 'static,
R: Send + Sync + 'static,
F: FnMut(B, A) -> Effect<B, E, R> + Send + 'static,
{
match items.next() {
None => succeed((acc, f)),
Some(a) => f(acc, a).flat_map(move |acc| seq_fold(items, acc, f)),
}
}
fn flat_map_go<A, B, E, R, F>(
outer: Option<Stream<A, E, R>>,
mut pending: VecDeque<A>,
inner: Option<Stream<B, E, R>>,
mut f: F,
) -> Stream<B, E, R>
where
A: Send + 'static,
B: Send + 'static,
E: Send + 'static,
R: Send + Sync + 'static,
F: FnMut(A) -> Stream<B, E, R> + Send + 'static,
{
Stream::from_step(move |n| {
if let Some(inner) = inner {
return inner.pull_raw(n).flat_map(move |step| match step {
Some((items, rest)) => succeed(Some((items, flat_map_go(outer, pending, Some(rest), f)))),
None => flat_map_go(outer, pending, None, f).pull_raw(n),
});
}
if let Some(a) = pending.pop_front() {
let inner = f(a);
return flat_map_go(outer, pending, Some(inner), f).pull_raw(n);
}
match outer {
None => succeed(None),
Some(outer) => outer.pull_raw(n).flat_map(move |step| match step {
None => succeed(None),
Some((items, rest)) => flat_map_go(Some(rest), items.into(), None, f).pull_raw(n),
}),
}
})
}
fn bulk_map<A: Send, B: Send, F: Fn(A) -> B + Sync>(items: Vec<A>, f: &F) -> Vec<B> {
#[cfg(feature = "compute")]
if crate::compute::should_parallelize(items.len()) {
use rayon::prelude::*;
return crate::compute::install_parallel(|| items.into_par_iter().map(f).collect());
}
items.into_iter().map(f).collect()
}
fn bulk_filter<A: Send, P: Fn(&A) -> bool + Sync>(items: Vec<A>, p: &P) -> Vec<A> {
#[cfg(feature = "compute")]
if crate::compute::should_parallelize(items.len()) {
use rayon::prelude::*;
return crate::compute::install_parallel(|| items.into_par_iter().filter(|a| p(a)).collect());
}
items.into_iter().filter(|a| p(a)).collect()
}