zrx_stream/stream/operator/join.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//! Join 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::tuple::cons::IntoStreamTupleCons;
36use crate::stream::combinator::tuple::join::IntoJoin;
37use crate::stream::combinator::tuple::StreamTupleJoin;
38use crate::stream::value::tuple::{All, Any, First, Presence};
39use crate::stream::value::Tuple;
40use crate::stream::Stream;
41
42use super::Operator;
43
44// ----------------------------------------------------------------------------
45// Structs
46// ----------------------------------------------------------------------------
47
48/// Join operator.
49struct Join<T, P> {
50 /// Type marker.
51 marker: PhantomData<(T, P)>,
52}
53
54// ----------------------------------------------------------------------------
55// Implementations
56// ----------------------------------------------------------------------------
57
58impl<I, T> Stream<I, T>
59where
60 I: Id,
61 T: Value,
62{
63 pub fn join<S, O>(&self, streams: S) -> Stream<I, O::Item>
64 where
65 S: IntoStreamTupleCons<I, T, Output = O>,
66 O: IntoJoin<I, All>,
67 {
68 streams // fmt
69 .into_stream_tuple_cons(self.clone())
70 .into_join()
71 }
72
73 pub fn left_join<S, O>(&self, streams: S) -> Stream<I, O::Item>
74 where
75 S: IntoStreamTupleCons<I, T, Output = O>,
76 O: IntoJoin<I, First>,
77 {
78 streams // fmt
79 .into_stream_tuple_cons(self.clone())
80 .into_join()
81 }
82
83 pub fn full_join<S, O>(&self, streams: S) -> Stream<I, O::Item>
84 where
85 S: IntoStreamTupleCons<I, T, Output = O>,
86 O: IntoJoin<I, Any>,
87 {
88 streams // fmt
89 .into_stream_tuple_cons(self.clone())
90 .into_join()
91 }
92}
93
94// ----------------------------------------------------------------------------
95// Trait implementations
96// ----------------------------------------------------------------------------
97
98impl<I, T, P> Operator<I, T> for Join<T, P>
99where
100 I: Id,
101 T: Tuple<P>,
102{
103 type Item<'a> = Item<&'a I, T::Arguments<'a>>;
104
105 /// Handles the given item.
106 ///
107 /// Joins are one of the most complex operations in stream processing, yet
108 /// the implementation here is surprisingly straightforward. The core idea
109 /// is to leverage the [`Tuple`] trait to represent the combined state of
110 /// all streams being joined.
111 #[cfg_attr(
112 feature = "tracing",
113 tracing::instrument(level = "debug" skip_all, fields(id = %item.id))
114 )]
115 fn handle(&mut self, item: Self::Item<'_>) -> impl IntoOutputs<I> {
116 item.into_owned().map(Some)
117 }
118
119 /// Returns the descriptor.
120 #[inline]
121 fn descriptor(&self) -> Descriptor {
122 Descriptor::builder()
123 .property(Property::Pure)
124 .property(Property::Stable)
125 .property(Property::Flush)
126 .build()
127 }
128}
129
130// ----------------------------------------------------------------------------
131// Blanket implementations
132// ----------------------------------------------------------------------------
133
134impl<S, I, P> IntoJoin<I, P> for S
135where
136 S: StreamTupleJoin<I, P>,
137 I: Id,
138 P: Presence,
139{
140 fn into_join(self) -> Stream<I, Self::Item> {
141 self.workflow().add_operator(
142 self.ids(),
143 Join::<Self::Item, P> { marker: PhantomData },
144 )
145 }
146}