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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use std::marker::PhantomData;

use serde::Deserialize;

use crate::{
    dataflow::{stream::StreamId, Data},
    scheduler::channel_manager::{StreamEndpoints, StreamEndpointsT},
};

use super::Vertex;

#[derive(Clone, PartialEq, Eq, Debug)]
pub enum Channel {
    InterThread(ChannelMetadata),
    InterNode(ChannelMetadata),
    Unscheduled(ChannelMetadata),
}

/// Stores metadata about a data-flow channel.
///
/// A data-flow channel is an edge in the data-flow graph.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct ChannelMetadata {
    pub stream_id: StreamId,
    pub source: Vertex,
    pub sink: Vertex,
}

impl ChannelMetadata {
    pub fn new(stream_id: StreamId, source: Vertex, sink: Vertex) -> Self {
        Self {
            stream_id,
            source,
            sink,
        }
    }
}

#[derive(Clone)]
pub struct TypedStreamMetadata<D>
where
    for<'a> D: Data + Deserialize<'a>,
{
    id: StreamId,
    source: Vertex,
    channels: Vec<Channel>,
    phantom: PhantomData<D>,
}

impl<D> TypedStreamMetadata<D>
where
    for<'a> D: Data + Deserialize<'a>,
{
    pub fn new(id: StreamId, source: Vertex) -> Self {
        Self {
            id,
            source,
            channels: Vec::new(),
            phantom: PhantomData,
        }
    }
}

pub trait StreamMetadataT: Send {
    fn get_id(&self) -> StreamId;
    fn get_source(&self) -> Vertex;
    fn box_clone(&self) -> Box<dyn StreamMetadataT>;
    fn to_stream_endpoints_t(&self) -> Box<dyn StreamEndpointsT>;
    fn add_channel(&mut self, channel: Channel);
    fn get_channels(&self) -> Vec<Channel>;
    fn set_channels(&mut self, channels: Vec<Channel>);
}

impl<D> StreamMetadataT for TypedStreamMetadata<D>
where
    for<'a> D: Data + Deserialize<'a>,
{
    fn get_id(&self) -> StreamId {
        self.id
    }

    fn get_source(&self) -> Vertex {
        self.source.clone()
    }

    fn box_clone(&self) -> Box<dyn StreamMetadataT> {
        Box::new(self.clone())
    }

    fn to_stream_endpoints_t(&self) -> Box<dyn StreamEndpointsT> {
        Box::new(StreamEndpoints::<D>::new(self.id))
    }

    fn add_channel(&mut self, channel: Channel) {
        self.channels.push(channel);
    }

    fn get_channels(&self) -> Vec<Channel> {
        self.channels.clone()
    }

    fn set_channels(&mut self, channels: Vec<Channel>) {
        self.channels = channels;
    }
}

pub struct StreamMetadata {
    stream_metadata_t: Box<dyn StreamMetadataT>,
}

impl StreamMetadata {
    pub fn new<D>(id: StreamId, source: Vertex) -> Self
    where
        for<'a> D: Data + Deserialize<'a>,
    {
        Self {
            stream_metadata_t: Box::new(TypedStreamMetadata::<D>::new(id, source)),
        }
    }

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

    pub fn get_source(&self) -> Vertex {
        self.stream_metadata_t.get_source()
    }

    pub fn to_stream_endpoints_t(&self) -> Box<dyn StreamEndpointsT> {
        self.stream_metadata_t.to_stream_endpoints_t()
    }

    pub fn add_channel(&mut self, channel: Channel) {
        self.stream_metadata_t.add_channel(channel);
    }

    pub fn get_channels(&self) -> Vec<Channel> {
        self.stream_metadata_t.get_channels()
    }

    pub fn set_channels(&mut self, channels: Vec<Channel>) {
        self.stream_metadata_t.set_channels(channels)
    }
}

impl Clone for StreamMetadata {
    fn clone(&self) -> Self {
        Self {
            stream_metadata_t: self.stream_metadata_t.box_clone(),
        }
    }
}