zrx_stream/stream/operator/transpose.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//! Transpose 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, Task};
33use zrx_scheduler::{Id, Value};
34
35use crate::stream::value::Delta;
36use crate::stream::Stream;
37
38use super::{Operator, OperatorExt};
39
40// ----------------------------------------------------------------------------
41// Structs
42// ----------------------------------------------------------------------------
43
44/// Transpose operator.
45struct Transpose<T> {
46 /// Type marker.
47 marker: PhantomData<T>,
48}
49
50// ----------------------------------------------------------------------------
51// Implementations
52// ----------------------------------------------------------------------------
53
54impl<I, T> Stream<I, Delta<I, T>>
55where
56 I: Id,
57 T: Value + Clone,
58{
59 pub fn transpose(&self) -> Stream<I, Delta<I, T>> {
60 self.with_operator(Transpose { marker: PhantomData })
61 }
62}
63
64// ----------------------------------------------------------------------------
65// Trait implementations
66// ----------------------------------------------------------------------------
67
68impl<I, T> Operator<I, Delta<I, T>> for Transpose<T>
69where
70 I: Id,
71 T: Value + Clone,
72{
73 type Item<'a> = Item<&'a I, &'a Delta<I, T>>;
74
75 /// Handles the given item.
76 ///
77 /// Transposition is implemented as a swap of the identifiers of a delta of
78 /// items, by hoisting the inner items to the outer level, and wrapping the
79 /// associated data of the inner items with the outer item. This allows to
80 /// effectively invert relationships, adhering to differential semantics.
81 #[cfg_attr(
82 feature = "tracing",
83 tracing::instrument(skip_all, fields(id = %item.id))
84 )]
85 fn handle(&mut self, item: Self::Item<'_>) -> impl IntoOutputs<I> {
86 let item = item.into_owned();
87 Task::new(move || {
88 let iter = item.data.into_iter().map(|part| {
89 let inner = Item::new(item.id.clone(), part.data);
90 Item {
91 id: part.id,
92 data: Some(Delta::from([inner])),
93 }
94 });
95
96 // Return delta of items
97 iter.collect::<Vec<_>>()
98 })
99 }
100
101 /// Returns the descriptor.
102 #[inline]
103 fn descriptor(&self) -> Descriptor {
104 Descriptor::builder() // fmt
105 .property(Property::Pure)
106 .build()
107 }
108}