Skip to main content

zrx_stream/stream/operator/
group.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//! Group operator.
27
28use ahash::HashMap;
29
30use zrx_scheduler::action::output::IntoOutputs;
31use zrx_scheduler::action::{Descriptor, Report};
32use zrx_scheduler::effect::Item;
33use zrx_scheduler::{Id, Value};
34
35use crate::stream::function::SelectFn;
36use crate::stream::value::Delta;
37use crate::stream::Stream;
38
39use super::Operator;
40
41// ----------------------------------------------------------------------------
42// Structs
43// ----------------------------------------------------------------------------
44
45/// Group operator.
46struct Group<F, I> {
47    /// Operator function.
48    function: F,
49    /// Store of group identifiers.
50    store: HashMap<I, I>,
51}
52
53// ----------------------------------------------------------------------------
54// Implementations
55// ----------------------------------------------------------------------------
56
57impl<I, T> Stream<I, T>
58where
59    I: Id,
60    T: Value + Clone,
61{
62    pub fn group<F, U>(&self, f: F) -> Stream<I, Delta<I, U>>
63    where
64        F: SelectFn<I, T, I>,
65        U: Value,
66    {
67        self.workflow.add_operator(
68            [self.id],
69            Group {
70                function: f,
71                store: HashMap::default(),
72            },
73        )
74    }
75}
76
77// ----------------------------------------------------------------------------
78// Trait implementations
79// ----------------------------------------------------------------------------
80
81impl<I, T, F> Operator<I, T> for Group<F, I>
82where
83    I: Id,
84    T: Value + Clone,
85    F: SelectFn<I, T, I>,
86{
87    type Item<'a> = Item<&'a I, Option<&'a T>>;
88
89    /// Handles the given item.
90    ///
91    /// Grouping is achieved by applying the operator function to each incoming
92    /// item to determine its group identifier. An internal mapping of items to
93    /// the computed group identifiers is maintained, as items might need to be
94    /// migrated between groups when their associated data changes.
95    #[cfg_attr(
96        feature = "tracing",
97        tracing::instrument(level = "debug", skip_all, fields(id = %item.id))
98    )]
99    fn handle(&mut self, item: Self::Item<'_>) -> impl IntoOutputs<I> {
100        if let Some(data) = item.data {
101            // When new data arrives, we apply the operator function to compute
102            // the group identifier. If the item was previously associated with
103            // another group, we emit a deletion for the prior and an insertion
104            // for the new group. Otherwise, we only emit an insertion.
105            self.function.execute(item.id, data).map(|report| {
106                report.map(|to| {
107                    let group = self.store.insert(item.id.clone(), to.clone());
108                    let prior = group.filter(|id| *id != to).map(|id| {
109                        let inner = Item::new(item.id.clone(), None);
110                        Item::new(id, Some(Delta::from([inner])))
111                    });
112
113                    // The item returned receives the group identifier, wrapping
114                    // the inner item, which is the item passed to the operator
115                    let inner = item.into_owned().map(Some);
116                    let delta = Delta::from([inner]);
117                    prior
118                        .into_iter()
119                        .chain(Some(Item::new(to, Some(delta))))
120                        .collect()
121                })
122            })
123        } else {
124            // If the incoming item has no data, interpret this as a deletion,
125            // removing the item from its previously allocated group, if any
126            let prior = self.store.remove(item.id).map(|id| {
127                let inner = Item::new(item.id.clone(), None);
128                Item::new(id, Some(Delta::from([inner])))
129            });
130
131            // Return delta of items
132            Ok(Report::new(prior.into_iter().collect::<Vec<_>>()))
133        }
134    }
135
136    /// Returns the descriptor.
137    #[inline]
138    fn descriptor(&self) -> Descriptor {
139        Descriptor::default()
140    }
141}