Skip to main content

zrx_stream/stream/operator/
select.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//! Select operator.
27
28use std::marker::PhantomData;
29
30use zrx_scheduler::action::context::Binding;
31use zrx_scheduler::action::options::Interest;
32use zrx_scheduler::action::{Action, Context, Options};
33use zrx_scheduler::schedule::Subscriber;
34use zrx_scheduler::step::{IntoSteps, Scoped};
35use zrx_scheduler::{Id, Scope, Value};
36
37use crate::stream::barrier::{Barrier, Barriers};
38use crate::stream::Stream;
39
40use super::Operator;
41
42// ----------------------------------------------------------------------------
43// Structs
44// ----------------------------------------------------------------------------
45
46/// Select operator.
47#[derive(Debug)]
48pub struct Select<I, T> {
49    /// Barrier set.
50    barriers: Barriers<I>,
51    /// Capture types.
52    marker: PhantomData<T>,
53}
54
55// ----------------------------------------------------------------------------
56// Implementations
57// ----------------------------------------------------------------------------
58
59impl<I, T> Stream<I, T>
60where
61    I: Id + Value,
62    T: Value,
63{
64    /// Selects scopes from the stream using the provided barriers.
65    #[inline]
66    #[must_use]
67    pub fn select<B>(&self, iter: B) -> Stream<I, Vec<Scope<I>>>
68    where
69        B: IntoIterator<Item = (Scope<I>, Barrier<I>)>,
70    {
71        let options = Options::default().interest(Interest::Enter);
72        let barriers = Barriers::from_iter(iter);
73        self.subscribe(
74            Subscriber::new(Select { barriers, marker: PhantomData })
75                .with_options(options),
76        )
77    }
78}
79
80// ----------------------------------------------------------------------------
81// Trait implementations
82// ----------------------------------------------------------------------------
83
84impl<I, T> Action<I> for Select<I, T>
85where
86    I: Id + Value,
87    T: Value,
88{
89    type Inputs = (T,);
90    type Output<'a> = Vec<Scope<I>>;
91
92    /// Executes the operator.
93    fn execute(&mut self, ctx: Context<I, Self>) -> impl IntoSteps<I, Self> {
94        let Binding { events, scopes, mut output, .. } = ctx.bind();
95
96        // Drive all lifecycle events and notifications into the barrier set.
97        for event in events {
98            self.barriers.handle(&event);
99        }
100        for scope in scopes {
101            self.barriers.notify(&scope);
102        }
103
104        // Drain all fulfilled barriers in a single pass.
105        self.barriers.drain().map(move |advance| {
106            let new_scope = advance.scope().clone();
107            output.insert(
108                new_scope.clone(),
109                advance.into_iter().cloned().collect(),
110            );
111            Scoped::from(new_scope).done()
112        })
113    }
114}