Files
abomonation
abomonation_derive
ansi_term
async_trait
atty
bincode
bitflags
byteorder
bytes
cfg_if
chrono
clap
dirs
dirs_sys
erdos
fixedbitset
fnv
futures
futures_channel
futures_core
futures_executor
futures_io
futures_macro
futures_sink
futures_task
futures_util
async_await
future
io
lock
sink
stream
task
indexmap
iovec
lazy_static
libc
log
memchr
mio
net2
num_cpus
num_integer
num_traits
petgraph
pin_project_lite
pin_utils
proc_macro2
proc_macro_hack
proc_macro_nested
quote
rand
rand_chacha
rand_core
rand_hc
rand_isaac
rand_jitter
rand_os
rand_pcg
rand_xorshift
serde
serde_derive
sha1
slab
slog
slog_term
strsim
syn
synstructure
term
textwrap
thread_local
time
tokio
future
io
loom
macros
net
park
runtime
stream
sync
task
time
util
tokio_macros
tokio_serde
tokio_serde_bincode
tokio_util
unicode_width
unicode_xid
uuid
vec_map
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use std::{cell::RefCell, rc::Rc};

use serde::Deserialize;

use crate::dataflow::{Data, Message, State, Timestamp};

use super::{
    errors::{ReadError, TryReadError},
    IngestStream, InternalReadStream, LoopStream, StatefulReadStream, StreamId, WriteStream,
};

/// Allows reading from a stream and registering callbacks.
///
/// Passed as an argument to `Operator::new` and `Operator::connect`.
/// Also used in the driver to construct a dataflow.
#[derive(Clone, Default)]
pub struct ReadStream<D: Data> {
    /// Stores information and internal information about the stream
    internal_stream: Rc<RefCell<InternalReadStream<D>>>,
}

impl<D: Data> ReadStream<D> {
    /// Create a stream into which we can write data.
    pub fn new() -> Self {
        Self {
            internal_stream: Rc::new(RefCell::new(InternalReadStream::new())),
        }
    }

    /// Add a callback to be invoked when the stream receives a message.
    pub fn add_callback<F: 'static + Fn(&Timestamp, &D)>(&self, callback: F) {
        self.internal_stream.borrow_mut().add_callback(callback);
    }

    /// Add a callback to be invoked after the stream received, and the operator
    /// processed all the messages with a timestamp.
    pub fn add_watermark_callback<F: 'static + Fn(&Timestamp)>(&self, callback: F) {
        self.internal_stream
            .borrow_mut()
            .add_watermark_callback(callback);
    }

    /// Returns a new instance of the stream with an associated state.
    ///
    /// This state is accessible by callbacks added to the
    /// [`StatefulReadStream`].
    pub fn add_state<S: State>(&self, state: S) -> StatefulReadStream<D, S> {
        StatefulReadStream::from(self.internal_stream.borrow_mut().add_state(state))
    }

    pub fn get_id(&self) -> StreamId {
        self.internal_stream.borrow().get_id()
    }

    pub fn get_name(&self) -> String {
        self.internal_stream.borrow().get_name().to_string()
    }

    pub fn is_closed(&self) -> bool {
        self.internal_stream.borrow().is_closed()
    }

    /// Tries to read a message from a channel.
    ///
    /// Returns an immutable reference, or `None` if no messages are
    /// available at the moment (i.e., non-blocking read).
    pub fn try_read(&self) -> Result<Message<D>, TryReadError> {
        self.internal_stream.borrow_mut().try_read()
    }

    /// Blocking read. Returns `None` if the stream doesn't have a receive endpoint.
    pub fn read(&self) -> Result<Message<D>, ReadError> {
        self.internal_stream.borrow_mut().read()
    }
}

impl<D: Data> From<&ReadStream<D>> for ReadStream<D> {
    fn from(read_stream: &ReadStream<D>) -> Self {
        read_stream.clone()
    }
}

impl<D: Data> From<&WriteStream<D>> for ReadStream<D> {
    fn from(write_stream: &WriteStream<D>) -> Self {
        Self::from(InternalReadStream::new_with_id_name(
            write_stream.get_id(),
            write_stream.get_name(),
        ))
    }
}

impl<D> From<&LoopStream<D>> for ReadStream<D>
where
    for<'a> D: Data + Deserialize<'a>,
{
    fn from(loop_stream: &LoopStream<D>) -> Self {
        Self::from(InternalReadStream::new_with_id_name(
            loop_stream.get_id(),
            loop_stream.get_name(),
        ))
    }
}

impl<D> From<&IngestStream<D>> for ReadStream<D>
where
    for<'a> D: Data + Deserialize<'a>,
{
    fn from(ingest_stream: &IngestStream<D>) -> Self {
        Self::from(InternalReadStream::new_with_id_name(
            ingest_stream.get_id(),
            ingest_stream.get_name(),
        ))
    }
}

impl<D: Data> From<InternalReadStream<D>> for ReadStream<D> {
    fn from(internal_stream: InternalReadStream<D>) -> Self {
        Self {
            internal_stream: Rc::new(RefCell::new(internal_stream)),
        }
    }
}

impl<D: Data> From<Rc<RefCell<InternalReadStream<D>>>> for ReadStream<D> {
    fn from(internal_stream: Rc<RefCell<InternalReadStream<D>>>) -> Self {
        Self { internal_stream }
    }
}

impl<D: Data> From<&ReadStream<D>> for Rc<RefCell<InternalReadStream<D>>> {
    fn from(read_stream: &ReadStream<D>) -> Self {
        Rc::clone(&read_stream.internal_stream)
    }
}