zrx_stream/stream/operator/
difference.rs

1// Copyright (c) Zensical LLC <https://zensical.org>
2
3// SPDX-License-Identifier: MIT
4// Third-party contributions licensed under CLA
5
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to
8// deal in the Software without restriction, including without limitation the
9// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10// sell copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12
13// The above copyright notice and this permission notice shall be included in
14// all copies or substantial portions of the Software.
15
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22// IN THE SOFTWARE.
23
24// ----------------------------------------------------------------------------
25
26//! Difference operator.
27
28use std::marker::PhantomData;
29use zrx_scheduler::action::descriptor::Property;
30use zrx_scheduler::action::output::IntoOutputs;
31use zrx_scheduler::action::Descriptor;
32use zrx_scheduler::effect::Item;
33use zrx_scheduler::{Id, Value};
34
35use crate::stream::combinator::{IntoStreamSet, StreamSet};
36use crate::stream::Stream;
37
38use super::Operator;
39
40// ----------------------------------------------------------------------------
41// Structs
42// ----------------------------------------------------------------------------
43
44/// Difference operator.
45struct Difference<T> {
46    /// Type marker.
47    marker: PhantomData<T>,
48}
49
50// ----------------------------------------------------------------------------
51// Implementations
52// ----------------------------------------------------------------------------
53
54impl<I, T> Stream<I, T>
55where
56    I: Id,
57    T: Value + Clone + Eq,
58{
59    pub fn difference<S>(&self, streams: S) -> Stream<I, T>
60    where
61        S: IntoStreamSet<I, T>,
62    {
63        let set = self.into_stream_set().union(streams);
64        self.workflow.add_operator(
65            set.into_iter().map(|stream| stream.id),
66            Difference::<T> { marker: PhantomData },
67        )
68    }
69}
70
71// ----------------------------------------------------------------------------
72
73impl<I, T> StreamSet<I, T>
74where
75    I: Id,
76    T: Value + Clone + Eq,
77{
78    pub fn into_difference(self) -> Option<Stream<I, T>> {
79        self.get(0)
80            .map(|head| head.workflow.clone())
81            .map(|workflow| {
82                workflow.add_operator(
83                    self.into_iter().map(|stream| stream.id),
84                    Difference::<T> { marker: PhantomData },
85                )
86            })
87    }
88}
89
90// ----------------------------------------------------------------------------
91// Trait implementations
92// ----------------------------------------------------------------------------
93
94impl<I, T> Operator<I, T> for Difference<T>
95where
96    I: Id,
97    T: Value + Clone + Eq,
98{
99    type Item<'a> = Item<&'a I, Vec<Option<&'a T>>>;
100
101    /// Handles the given item.
102    ///
103    /// Differences of streams are computed by checking that each subsequent
104    /// stream does not have an exact copy of the item of the first stream.
105    #[cfg_attr(
106        feature = "tracing",
107        tracing::instrument(level = "debug", skip_all, fields(id = %item.id))
108    )]
109    fn handle(&mut self, item: Self::Item<'_>) -> impl IntoOutputs<I> {
110        let item = item.map(|data| {
111            let mut iter = data.into_iter();
112            iter.next()?.and_then(|head| {
113                iter.all(|option| option != Some(head)).then_some(head)
114            })
115        });
116
117        // Return item
118        item.into_owned()
119    }
120
121    /// Returns the descriptor.
122    #[inline]
123    fn descriptor(&self) -> Descriptor {
124        Descriptor::builder()
125            .property(Property::Pure)
126            .property(Property::Stable)
127            .property(Property::Flush)
128            .build()
129    }
130}