timely 0.29.0

A low-latency data-parallel dataflow system in Rust
Documentation
//! Exchange records between workers.

use crate::Container;
use crate::progress::Timestamp;
use crate::container::{DrainContainer, SizableContainer, PushInto};
use crate::dataflow::channels::pact::ExchangeCore;
use crate::dataflow::operators::generic::operator::Operator;
use crate::dataflow::Stream;

/// Exchange records between workers.
pub trait Exchange<C: DrainContainer> {
    /// Exchange records between workers.
    ///
    /// The closure supplied should map a reference to a record to a `u64`,
    /// whose value determines to which worker the record will be routed.
    ///
    /// # Examples
    /// ```
    /// use timely::dataflow::operators::{ToStream, Exchange, Inspect};
    ///
    /// timely::example(|scope| {
    ///     (0..10).to_stream(scope)
    ///            .container::<Vec<_>>()
    ///            .exchange(|x| *x)
    ///            .inspect(|x| println!("seen: {:?}", x));
    /// });
    /// ```
    fn exchange<F>(self, route: F) -> Self
    where
        for<'a> F: FnMut(&C::Item<'a>) -> u64 + 'static;
}

impl<T: Timestamp, C> Exchange<C> for Stream<'_, T, C>
where
    C: Container
        + SizableContainer
        + DrainContainer
        + Send
        + crate::dataflow::channels::ContainerBytes
        + for<'a> PushInto<C::Item<'a>>,
{
    fn exchange<F>(self, route: F) -> Self
    where
        for<'a> F: FnMut(&C::Item<'a>) -> u64 + 'static,
    {
        self.unary(ExchangeCore::new(route), "Exchange", |_, _| {
            move |input, output| {
                input.for_each_time(|time, data| {
                    output.session(&time).give_containers(data);
                });
            }
        })
    }
}