Skip to main content

zrx_stream/stream/operator/
join.rs

1// Copyright (c) 2025-2026 Zensical and contributors
2
3// SPDX-License-Identifier: MIT
4// All contributions are certified under the DCO
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;
29
30use zrx_scheduler::action::context::Binding;
31use zrx_scheduler::action::{Action, Context};
32use zrx_scheduler::step::IntoSteps;
33use zrx_scheduler::{Id, Value};
34use zrx_storage::accessor::Join as _;
35use zrx_storage::borrow::IntoOwned;
36
37// ----------------------------------------------------------------------------
38// Structs
39// ----------------------------------------------------------------------------
40
41/// Join operator.
42#[derive(Debug)]
43pub struct Join<T> {
44    /// Capture types.
45    marker: PhantomData<T>,
46}
47
48// ----------------------------------------------------------------------------
49// Implementations
50// ----------------------------------------------------------------------------
51
52impl<T> Join<T> {
53    /// Creates a join operator.
54    #[must_use]
55    pub fn new() -> Self {
56        Self { marker: PhantomData }
57    }
58}
59
60// ----------------------------------------------------------------------------
61// Macros
62// ----------------------------------------------------------------------------
63
64/// Implements action trait.
65macro_rules! impl_action {
66    ($($T:ident),+ $(,)?) => {
67        impl<I, $($T),+> Action<I> for Join<($($T),+)>
68        where
69            I: Id,
70            $($T: Value,)+
71        {
72            type Inputs = ($($T,)+);
73            type Output<'a> = ($($T,)+);
74
75            /// Executes the operator.
76            fn execute(
77                &mut self, ctx: Context<I, Self>
78            ) -> impl IntoSteps<I, Self> {
79                let Binding { scopes, inputs, mut output, .. } = ctx.bind();
80                scopes.into_iter().map(move |mut scope| {
81                    match inputs.join(scope.key()) {
82                        Some(value) => {
83                            output.insert(scope.key().clone(), value.into_owned());
84                        }
85                        None => {
86                            output.remove(scope.key());
87                        }
88                    }
89                    scope.done()
90                })
91            }
92        }
93    }
94}
95
96// ----------------------------------------------------------------------------
97
98impl_action!(T1, T2);
99impl_action!(T1, T2, T3);
100impl_action!(T1, T2, T3, T4);
101impl_action!(T1, T2, T3, T4, T5);
102impl_action!(T1, T2, T3, T4, T5, T6);
103impl_action!(T1, T2, T3, T4, T5, T6, T7);
104impl_action!(T1, T2, T3, T4, T5, T6, T7, T8);