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::{Event, Interest};
32use zrx_scheduler::action::{Action, Context, Options};
33use zrx_scheduler::schedule::Subscriber;
34use zrx_scheduler::step::{IntoSteps, Scope};
35use zrx_scheduler::{Id, Key, 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 pub fn select<B>(&self, iter: B) -> Stream<I, Vec<(Key<I>, T)>>
67 where
68 B: IntoIterator<Item = (Key<I>, Barrier<I>)>,
69 {
70 let options = Options::default().interest(Interest::Enter);
71 let barriers = Barriers::from_iter(iter);
72 self.subscribe(
73 Subscriber::new(Select { barriers, marker: PhantomData })
74 .with_options(options),
75 )
76 }
77}
78
79// ----------------------------------------------------------------------------
80// Trait implementations
81// ----------------------------------------------------------------------------
82
83impl<I, T> Action<I> for Select<I, T>
84where
85 I: Id + Value,
86 T: Value,
87{
88 type Inputs = (T,);
89 type Output<'a> = Vec<(Key<I>, T)>;
90
91 /// Executes the operator.
92 fn execute(&mut self, ctx: Context<I, Self>) -> impl IntoSteps<I, Self> {
93 let Binding {
94 events,
95 scopes,
96 inputs,
97 mut output,
98 ..
99 } = ctx.bind();
100
101 // Drive all lifecycle events and notifications into the barrier set.
102 for event in events {
103 self.barriers.handle(&event);
104 }
105 for scope in scopes {
106 if inputs.contains_key(scope.key()) {
107 self.barriers.notify(scope.key());
108 } else {
109 // A removal reaches the action as a scope whose input value
110 // has already been withdrawn. Remove it from all barriers so
111 // stale membership cannot block or poison later advances.
112 self.barriers.handle(&Event::Remove(scope.key().clone()));
113 }
114 }
115
116 // Drain all fulfilled barriers in a single pass.
117 self.barriers.drain().map(move |advance| {
118 let new_key = advance.scope().clone();
119 output.insert(
120 new_key.clone(),
121 advance
122 .into_iter()
123 .cloned()
124 .map(|key| {
125 let value = inputs.get(&key).expect("invariant");
126 (key, value.clone())
127 })
128 .collect(),
129 );
130 Scope::from(new_key).done()
131 })
132 }
133}