zrx_stream/stream/operator/coalesce.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//! Coalesce 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/// Coalesce operator.
45struct Coalesce<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,
58{
59 pub fn coalesce<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 Coalesce::<T> { marker: PhantomData },
67 )
68 }
69}
70
71// ----------------------------------------------------------------------------
72
73impl<I, T> StreamSet<I, T>
74where
75 I: Id,
76 T: Value + Clone,
77{
78 pub fn into_coalesce(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 Coalesce::<T> { marker: PhantomData },
85 )
86 })
87 }
88}
89
90// ----------------------------------------------------------------------------
91// Trait implementations
92// ----------------------------------------------------------------------------
93
94impl<I, T> Operator<I, T> for Coalesce<T>
95where
96 I: Id,
97 T: Value + Clone,
98{
99 type Item<'a> = Item<&'a I, Vec<Option<&'a T>>>;
100
101 /// Handles the given item.
102 ///
103 /// Coalescing is implemented by checking the streams one after another for
104 /// the presence of an item, and returning the first occurrence.
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 item.map(|data| data.into_iter().find_map(|option| option))
111 .into_owned()
112 }
113
114 /// Returns the descriptor.
115 #[inline]
116 fn descriptor(&self) -> Descriptor {
117 Descriptor::builder()
118 .property(Property::Pure)
119 .property(Property::Stable)
120 .property(Property::Flush)
121 .build()
122 }
123}