Skip to main content

hydro_lang/live_collections/
keyed_singleton.rs

1//! Definitions for the [`KeyedSingleton`] live collection.
2
3use std::cell::RefCell;
4use std::collections::HashMap;
5use std::hash::Hash;
6use std::marker::PhantomData;
7use std::ops::Deref;
8use std::rc::Rc;
9
10use stageleft::{IntoQuotedMut, QuotedWithContext, q};
11
12use super::boundedness::{Bounded, Boundedness, IsBounded, Unbounded};
13use super::keyed_stream::KeyedStream;
14use super::optional::Optional;
15use super::singleton::Singleton;
16use super::stream::{ExactlyOnce, NoOrder, Stream, TotalOrder};
17use crate::compile::builder::CycleId;
18use crate::compile::ir::{
19    CollectionKind, HydroIrOpMetadata, HydroNode, HydroRoot, KeyedSingletonBoundKind, TeeNode,
20};
21#[cfg(stageleft_runtime)]
22use crate::forward_handle::{CycleCollection, ReceiverComplete};
23use crate::forward_handle::{ForwardRef, TickCycle};
24use crate::live_collections::stream::{Ordering, Retries};
25#[cfg(stageleft_runtime)]
26use crate::location::dynamic::{DynLocation, LocationId};
27use crate::location::tick::DeferTick;
28use crate::location::{Atomic, Location, NoTick, Tick, check_matching_location};
29use crate::manual_expr::ManualExpr;
30use crate::nondet::{NonDet, nondet};
31use crate::properties::ManualProof;
32
33/// A marker trait indicating which components of a [`KeyedSingleton`] may change.
34///
35/// In addition to [`Bounded`] (all entries are fixed) and [`Unbounded`] (entries may be added /
36/// changed, but not removed), this also includes an additional variant [`BoundedValue`], which
37/// indicates that entries may be added over time, but once an entry is added it will never be
38/// removed and its value will never change.
39pub trait KeyedSingletonBound {
40    /// The [`Boundedness`] of the [`Stream`] underlying the keyed singleton.
41    type UnderlyingBound: Boundedness;
42    /// The [`Boundedness`] of each entry's value; [`Bounded`] means it is immutable.
43    type ValueBound: Boundedness;
44
45    /// The type of the keyed singleton if the value for each key is immutable.
46    type WithBoundedValue: KeyedSingletonBound<UnderlyingBound = Self::UnderlyingBound, ValueBound = Bounded>;
47
48    /// The type of the keyed singleton if the value for each key may change asynchronously.
49    type WithUnboundedValue: KeyedSingletonBound<UnderlyingBound = Self::UnderlyingBound, ValueBound = Unbounded>;
50
51    /// Returns the [`KeyedSingletonBoundKind`] corresponding to this type.
52    fn bound_kind() -> KeyedSingletonBoundKind;
53}
54
55impl KeyedSingletonBound for Unbounded {
56    type UnderlyingBound = Unbounded;
57    type ValueBound = Unbounded;
58    type WithBoundedValue = BoundedValue;
59    type WithUnboundedValue = Unbounded;
60
61    fn bound_kind() -> KeyedSingletonBoundKind {
62        KeyedSingletonBoundKind::Unbounded
63    }
64}
65
66impl KeyedSingletonBound for Bounded {
67    type UnderlyingBound = Bounded;
68    type ValueBound = Bounded;
69    type WithBoundedValue = Bounded;
70    type WithUnboundedValue = UnreachableBound;
71
72    fn bound_kind() -> KeyedSingletonBoundKind {
73        KeyedSingletonBoundKind::Bounded
74    }
75}
76
77/// A variation of boundedness specific to [`KeyedSingleton`], which indicates that once a key appears,
78/// its value is bounded and will never change. If the `KeyBound` is [`Bounded`], then the entire set of entries
79/// is bounded, but if it is [`Unbounded`], then new entries may appear asynchronously.
80pub struct BoundedValue;
81
82impl KeyedSingletonBound for BoundedValue {
83    type UnderlyingBound = Unbounded;
84    type ValueBound = Bounded;
85    type WithBoundedValue = BoundedValue;
86    type WithUnboundedValue = Unbounded;
87
88    fn bound_kind() -> KeyedSingletonBoundKind {
89        KeyedSingletonBoundKind::BoundedValue
90    }
91}
92
93#[doc(hidden)]
94pub struct UnreachableBound;
95
96impl KeyedSingletonBound for UnreachableBound {
97    type UnderlyingBound = Bounded;
98    type ValueBound = Unbounded;
99
100    type WithBoundedValue = Bounded;
101    type WithUnboundedValue = UnreachableBound;
102
103    fn bound_kind() -> KeyedSingletonBoundKind {
104        unreachable!("UnreachableBound cannot be instantiated")
105    }
106}
107
108/// Mapping from keys of type `K` to values of type `V`.
109///
110/// Keyed Singletons capture an asynchronously updated mapping from keys of the `K` to values of
111/// type `V`, where the order of keys is non-deterministic. In addition to the standard boundedness
112/// variants ([`Bounded`] for finite and immutable, [`Unbounded`] for asynchronously changing),
113/// keyed singletons can use [`BoundedValue`] to declare that new keys may be added over time, but
114/// keys cannot be removed and the value for each key is immutable.
115///
116/// Type Parameters:
117/// - `K`: the type of the key for each entry
118/// - `V`: the type of the value for each entry
119/// - `Loc`: the [`Location`] where the keyed singleton is materialized
120/// - `Bound`: tracks whether the entries are:
121///     - [`Bounded`] (local and finite)
122///     - [`Unbounded`] (asynchronous with entries added / removed / changed over time)
123///     - [`BoundedValue`] (asynchronous with immutable values for each key and no removals)
124pub struct KeyedSingleton<K, V, Loc, Bound: KeyedSingletonBound> {
125    pub(crate) location: Loc,
126    pub(crate) ir_node: RefCell<HydroNode>,
127
128    _phantom: PhantomData<(K, V, Loc, Bound)>,
129}
130
131impl<'a, K: Clone, V: Clone, Loc: Location<'a>, Bound: KeyedSingletonBound> Clone
132    for KeyedSingleton<K, V, Loc, Bound>
133{
134    fn clone(&self) -> Self {
135        if !matches!(self.ir_node.borrow().deref(), HydroNode::Tee { .. }) {
136            let orig_ir_node = self.ir_node.replace(HydroNode::Placeholder);
137            *self.ir_node.borrow_mut() = HydroNode::Tee {
138                inner: TeeNode(Rc::new(RefCell::new(orig_ir_node))),
139                metadata: self.location.new_node_metadata(Self::collection_kind()),
140            };
141        }
142
143        if let HydroNode::Tee { inner, metadata } = self.ir_node.borrow().deref() {
144            KeyedSingleton {
145                location: self.location.clone(),
146                ir_node: HydroNode::Tee {
147                    inner: TeeNode(inner.0.clone()),
148                    metadata: metadata.clone(),
149                }
150                .into(),
151                _phantom: PhantomData,
152            }
153        } else {
154            unreachable!()
155        }
156    }
157}
158
159impl<'a, K, V, L, B: KeyedSingletonBound> CycleCollection<'a, ForwardRef>
160    for KeyedSingleton<K, V, L, B>
161where
162    L: Location<'a> + NoTick,
163{
164    type Location = L;
165
166    fn create_source(cycle_id: CycleId, location: L) -> Self {
167        KeyedSingleton {
168            location: location.clone(),
169            ir_node: RefCell::new(HydroNode::CycleSource {
170                cycle_id,
171                metadata: location.new_node_metadata(Self::collection_kind()),
172            }),
173            _phantom: PhantomData,
174        }
175    }
176}
177
178impl<'a, K, V, L> CycleCollection<'a, TickCycle> for KeyedSingleton<K, V, Tick<L>, Bounded>
179where
180    L: Location<'a>,
181{
182    type Location = Tick<L>;
183
184    fn create_source(cycle_id: CycleId, location: Tick<L>) -> Self {
185        KeyedSingleton::new(
186            location.clone(),
187            HydroNode::CycleSource {
188                cycle_id,
189                metadata: location.new_node_metadata(Self::collection_kind()),
190            },
191        )
192    }
193}
194
195impl<'a, K, V, L> DeferTick for KeyedSingleton<K, V, Tick<L>, Bounded>
196where
197    L: Location<'a>,
198{
199    fn defer_tick(self) -> Self {
200        KeyedSingleton::defer_tick(self)
201    }
202}
203
204impl<'a, K, V, L, B: KeyedSingletonBound> ReceiverComplete<'a, ForwardRef>
205    for KeyedSingleton<K, V, L, B>
206where
207    L: Location<'a> + NoTick,
208{
209    fn complete(self, cycle_id: CycleId, expected_location: LocationId) {
210        assert_eq!(
211            Location::id(&self.location),
212            expected_location,
213            "locations do not match"
214        );
215        self.location
216            .flow_state()
217            .borrow_mut()
218            .push_root(HydroRoot::CycleSink {
219                cycle_id,
220                input: Box::new(self.ir_node.into_inner()),
221                op_metadata: HydroIrOpMetadata::new(),
222            });
223    }
224}
225
226impl<'a, K, V, L> ReceiverComplete<'a, TickCycle> for KeyedSingleton<K, V, Tick<L>, Bounded>
227where
228    L: Location<'a>,
229{
230    fn complete(self, cycle_id: CycleId, expected_location: LocationId) {
231        assert_eq!(
232            Location::id(&self.location),
233            expected_location,
234            "locations do not match"
235        );
236        self.location
237            .flow_state()
238            .borrow_mut()
239            .push_root(HydroRoot::CycleSink {
240                cycle_id,
241                input: Box::new(self.ir_node.into_inner()),
242                op_metadata: HydroIrOpMetadata::new(),
243            });
244    }
245}
246
247impl<'a, K, V, L: Location<'a>, B: KeyedSingletonBound> KeyedSingleton<K, V, L, B> {
248    pub(crate) fn new(location: L, ir_node: HydroNode) -> Self {
249        debug_assert_eq!(ir_node.metadata().location_id, Location::id(&location));
250        debug_assert_eq!(ir_node.metadata().collection_kind, Self::collection_kind());
251
252        KeyedSingleton {
253            location,
254            ir_node: RefCell::new(ir_node),
255            _phantom: PhantomData,
256        }
257    }
258
259    /// Returns the [`Location`] where this keyed singleton is being materialized.
260    pub fn location(&self) -> &L {
261        &self.location
262    }
263}
264
265#[cfg(stageleft_runtime)]
266fn key_count_inside_tick<'a, K, V, L: Location<'a>>(
267    me: KeyedSingleton<K, V, L, Bounded>,
268) -> Singleton<usize, L, Bounded> {
269    me.entries().count()
270}
271
272#[cfg(stageleft_runtime)]
273fn into_singleton_inside_tick<'a, K, V, L: Location<'a>>(
274    me: KeyedSingleton<K, V, L, Bounded>,
275) -> Singleton<HashMap<K, V>, L, Bounded>
276where
277    K: Eq + Hash,
278{
279    me.entries()
280        .assume_ordering(nondet!(
281            /// Because this is a keyed singleton, there is only one value per key.
282        ))
283        .fold(
284            q!(|| HashMap::new()),
285            q!(|map, (k, v)| {
286                map.insert(k, v);
287            }),
288        )
289}
290
291impl<'a, K, V, L: Location<'a>, B: KeyedSingletonBound> KeyedSingleton<K, V, L, B> {
292    pub(crate) fn collection_kind() -> CollectionKind {
293        CollectionKind::KeyedSingleton {
294            bound: B::bound_kind(),
295            key_type: stageleft::quote_type::<K>().into(),
296            value_type: stageleft::quote_type::<V>().into(),
297        }
298    }
299
300    /// Transforms each value by invoking `f` on each element, with keys staying the same
301    /// after transformation. If you need access to the key, see [`KeyedSingleton::map_with_key`].
302    ///
303    /// If you do not want to modify the stream and instead only want to view
304    /// each item use [`KeyedSingleton::inspect`] instead.
305    ///
306    /// # Example
307    /// ```rust
308    /// # #[cfg(feature = "deploy")] {
309    /// # use hydro_lang::prelude::*;
310    /// # use futures::StreamExt;
311    /// # tokio_test::block_on(hydro_lang::test_util::stream_transform_test(|process| {
312    /// let keyed_singleton = // { 1: 2, 2: 4 }
313    /// # process
314    /// #     .source_iter(q!(vec![(1, 2), (2, 4)]))
315    /// #     .into_keyed()
316    /// #     .first();
317    /// keyed_singleton.map(q!(|v| v + 1))
318    /// #   .entries()
319    /// # }, |mut stream| async move {
320    /// // { 1: 3, 2: 5 }
321    /// # let mut results = Vec::new();
322    /// # for _ in 0..2 {
323    /// #     results.push(stream.next().await.unwrap());
324    /// # }
325    /// # results.sort();
326    /// # assert_eq!(results, vec![(1, 3), (2, 5)]);
327    /// # }));
328    /// # }
329    /// ```
330    pub fn map<U, F>(self, f: impl IntoQuotedMut<'a, F, L> + Copy) -> KeyedSingleton<K, U, L, B>
331    where
332        F: Fn(V) -> U + 'a,
333    {
334        let f: ManualExpr<F, _> = ManualExpr::new(move |ctx: &L| f.splice_fn1_ctx(ctx));
335        let map_f = q!({
336            let orig = f;
337            move |(k, v)| (k, orig(v))
338        })
339        .splice_fn1_ctx::<(K, V), (K, U)>(&self.location)
340        .into();
341
342        KeyedSingleton::new(
343            self.location.clone(),
344            HydroNode::Map {
345                f: map_f,
346                input: Box::new(self.ir_node.into_inner()),
347                metadata: self
348                    .location
349                    .new_node_metadata(KeyedSingleton::<K, U, L, B>::collection_kind()),
350            },
351        )
352    }
353
354    /// Transforms each value by invoking `f` on each key-value pair, with keys staying the same
355    /// after transformation. Unlike [`KeyedSingleton::map`], this gives access to both the key and value.
356    ///
357    /// The closure `f` receives a tuple `(K, V)` containing both the key and value, and returns
358    /// the new value `U`. The key remains unchanged in the output.
359    ///
360    /// # Example
361    /// ```rust
362    /// # #[cfg(feature = "deploy")] {
363    /// # use hydro_lang::prelude::*;
364    /// # use futures::StreamExt;
365    /// # tokio_test::block_on(hydro_lang::test_util::stream_transform_test(|process| {
366    /// let keyed_singleton = // { 1: 2, 2: 4 }
367    /// # process
368    /// #     .source_iter(q!(vec![(1, 2), (2, 4)]))
369    /// #     .into_keyed()
370    /// #     .first();
371    /// keyed_singleton.map_with_key(q!(|(k, v)| k + v))
372    /// #   .entries()
373    /// # }, |mut stream| async move {
374    /// // { 1: 3, 2: 6 }
375    /// # let mut results = Vec::new();
376    /// # for _ in 0..2 {
377    /// #     results.push(stream.next().await.unwrap());
378    /// # }
379    /// # results.sort();
380    /// # assert_eq!(results, vec![(1, 3), (2, 6)]);
381    /// # }));
382    /// # }
383    /// ```
384    pub fn map_with_key<U, F>(
385        self,
386        f: impl IntoQuotedMut<'a, F, L> + Copy,
387    ) -> KeyedSingleton<K, U, L, B>
388    where
389        F: Fn((K, V)) -> U + 'a,
390        K: Clone,
391    {
392        let f: ManualExpr<F, _> = ManualExpr::new(move |ctx: &L| f.splice_fn1_ctx(ctx));
393        let map_f = q!({
394            let orig = f;
395            move |(k, v)| {
396                let out = orig((Clone::clone(&k), v));
397                (k, out)
398            }
399        })
400        .splice_fn1_ctx::<(K, V), (K, U)>(&self.location)
401        .into();
402
403        KeyedSingleton::new(
404            self.location.clone(),
405            HydroNode::Map {
406                f: map_f,
407                input: Box::new(self.ir_node.into_inner()),
408                metadata: self
409                    .location
410                    .new_node_metadata(KeyedSingleton::<K, U, L, B>::collection_kind()),
411            },
412        )
413    }
414
415    /// Gets the number of keys in the keyed singleton.
416    ///
417    /// The output singleton will be unbounded if the input is [`Unbounded`] or [`BoundedValue`],
418    /// since keys may be added / removed over time. When the set of keys changes, the count will
419    /// be asynchronously updated.
420    ///
421    /// # Example
422    /// ```rust
423    /// # #[cfg(feature = "deploy")] {
424    /// # use hydro_lang::prelude::*;
425    /// # use futures::StreamExt;
426    /// # tokio_test::block_on(hydro_lang::test_util::stream_transform_test(|process| {
427    /// # let tick = process.tick();
428    /// let keyed_singleton = // { 1: "a", 2: "b", 3: "c" }
429    /// # process
430    /// #     .source_iter(q!(vec![(1, "a"), (2, "b"), (3, "c")]))
431    /// #     .into_keyed()
432    /// #     .batch(&tick, nondet!(/** test */))
433    /// #     .first();
434    /// keyed_singleton.key_count()
435    /// # .all_ticks()
436    /// # }, |mut stream| async move {
437    /// // 3
438    /// # assert_eq!(stream.next().await.unwrap(), 3);
439    /// # }));
440    /// # }
441    /// ```
442    pub fn key_count(self) -> Singleton<usize, L, B::UnderlyingBound> {
443        if B::ValueBound::BOUNDED {
444            let me: KeyedSingleton<K, V, L, B::WithBoundedValue> = KeyedSingleton {
445                location: self.location,
446                ir_node: self.ir_node,
447                _phantom: PhantomData,
448            };
449
450            me.entries().count()
451        } else if L::is_top_level()
452            && let Some(tick) = self.location.try_tick()
453        {
454            let me: KeyedSingleton<K, V, L, B::WithUnboundedValue> = KeyedSingleton {
455                location: self.location,
456                ir_node: self.ir_node,
457                _phantom: PhantomData,
458            };
459
460            let out =
461                key_count_inside_tick(me.snapshot(&tick, nondet!(/** eventually stabilizes */)))
462                    .latest();
463            Singleton::new(out.location, out.ir_node.into_inner())
464        } else {
465            panic!("Unbounded KeyedSingleton inside a tick");
466        }
467    }
468
469    /// Converts this keyed singleton into a [`Singleton`] containing a `HashMap` from keys to values.
470    ///
471    /// As the values for each key are updated asynchronously, the `HashMap` will be updated
472    /// asynchronously as well.
473    ///
474    /// # Example
475    /// ```rust
476    /// # #[cfg(feature = "deploy")] {
477    /// # use hydro_lang::prelude::*;
478    /// # use futures::StreamExt;
479    /// # tokio_test::block_on(hydro_lang::test_util::stream_transform_test(|process| {
480    /// let keyed_singleton = // { 1: "a", 2: "b", 3: "c" }
481    /// # process
482    /// #     .source_iter(q!(vec![(1, "a".to_owned()), (2, "b".to_owned()), (3, "c".to_owned())]))
483    /// #     .into_keyed()
484    /// #     .batch(&process.tick(), nondet!(/** test */))
485    /// #     .first();
486    /// keyed_singleton.into_singleton()
487    /// # .all_ticks()
488    /// # }, |mut stream| async move {
489    /// // { 1: "a", 2: "b", 3: "c" }
490    /// # assert_eq!(stream.next().await.unwrap(), vec![(1, "a".to_owned()), (2, "b".to_owned()), (3, "c".to_owned())].into_iter().collect());
491    /// # }));
492    /// # }
493    /// ```
494    pub fn into_singleton(self) -> Singleton<HashMap<K, V>, L, B::UnderlyingBound>
495    where
496        K: Eq + Hash,
497    {
498        if B::ValueBound::BOUNDED {
499            let me: KeyedSingleton<K, V, L, B::WithBoundedValue> = KeyedSingleton {
500                location: self.location,
501                ir_node: self.ir_node,
502                _phantom: PhantomData,
503            };
504
505            me.entries()
506                .assume_ordering(nondet!(
507                    /// Because this is a keyed singleton, there is only one value per key.
508                ))
509                .fold(
510                    q!(|| HashMap::new()),
511                    q!(|map, (k, v)| {
512                        // TODO(shadaj): make this commutative but really-debug-assert that there is no key overlap
513                        map.insert(k, v);
514                    }),
515                )
516        } else if L::is_top_level()
517            && let Some(tick) = self.location.try_tick()
518        {
519            let me: KeyedSingleton<K, V, L, B::WithUnboundedValue> = KeyedSingleton {
520                location: self.location,
521                ir_node: self.ir_node,
522                _phantom: PhantomData,
523            };
524
525            let out = into_singleton_inside_tick(
526                me.snapshot(&tick, nondet!(/** eventually stabilizes */)),
527            )
528            .latest();
529            Singleton::new(out.location, out.ir_node.into_inner())
530        } else {
531            panic!("Unbounded KeyedSingleton inside a tick");
532        }
533    }
534
535    /// An operator which allows you to "name" a `HydroNode`.
536    /// This is only used for testing, to correlate certain `HydroNode`s with IDs.
537    pub fn ir_node_named(self, name: &str) -> KeyedSingleton<K, V, L, B> {
538        {
539            let mut node = self.ir_node.borrow_mut();
540            let metadata = node.metadata_mut();
541            metadata.tag = Some(name.to_owned());
542        }
543        self
544    }
545
546    /// Strengthens the boundedness guarantee to `Bounded`, given that `B: IsBounded`, which
547    /// implies that `B == Bounded`.
548    pub fn make_bounded(self) -> KeyedSingleton<K, V, L, Bounded>
549    where
550        B: IsBounded,
551    {
552        KeyedSingleton::new(self.location, self.ir_node.into_inner())
553    }
554
555    /// Gets the value associated with a specific key from the keyed singleton.
556    ///
557    /// # Example
558    /// ```rust
559    /// # #[cfg(feature = "deploy")] {
560    /// # use hydro_lang::prelude::*;
561    /// # use futures::StreamExt;
562    /// # tokio_test::block_on(hydro_lang::test_util::stream_transform_test(|process| {
563    /// let tick = process.tick();
564    /// let keyed_data = process
565    ///     .source_iter(q!(vec![(1, 2), (2, 3)]))
566    ///     .into_keyed()
567    ///     .batch(&tick, nondet!(/** test */))
568    ///     .first();
569    /// let key = tick.singleton(q!(1));
570    /// keyed_data.get(key).all_ticks()
571    /// # }, |mut stream| async move {
572    /// // 2
573    /// # assert_eq!(stream.next().await.unwrap(), 2);
574    /// # }));
575    /// # }
576    /// ```
577    pub fn get(self, key: Singleton<K, L, Bounded>) -> Optional<V, L, Bounded>
578    where
579        B: IsBounded,
580        K: Hash + Eq,
581    {
582        self.make_bounded()
583            .into_keyed_stream()
584            .get(key)
585            .assume_ordering::<TotalOrder>(nondet!(/** only a single key, so totally ordered */))
586            .first()
587    }
588
589    /// Emit a keyed stream containing keys shared between the keyed singleton and the
590    /// keyed stream, where each value in the output keyed stream is a tuple of
591    /// (the keyed singleton's value, the keyed stream's value).
592    ///
593    /// # Example
594    /// ```rust
595    /// # #[cfg(feature = "deploy")] {
596    /// # use hydro_lang::prelude::*;
597    /// # use futures::StreamExt;
598    /// # tokio_test::block_on(hydro_lang::test_util::stream_transform_test(|process| {
599    /// let tick = process.tick();
600    /// let keyed_data = process
601    ///     .source_iter(q!(vec![(1, 10), (2, 20)]))
602    ///     .into_keyed()
603    ///     .batch(&tick, nondet!(/** test */))
604    ///     .first();
605    /// let other_data = process
606    ///     .source_iter(q!(vec![(1, 100), (2, 200), (1, 101)]))
607    ///     .into_keyed()
608    ///     .batch(&tick, nondet!(/** test */));
609    /// keyed_data.join_keyed_stream(other_data).entries().all_ticks()
610    /// # }, |mut stream| async move {
611    /// // { 1: [(10, 100), (10, 101)], 2: [(20, 200)] } in any order
612    /// # let mut results = vec![];
613    /// # for _ in 0..3 {
614    /// #     results.push(stream.next().await.unwrap());
615    /// # }
616    /// # results.sort();
617    /// # assert_eq!(results, vec![(1, (10, 100)), (1, (10, 101)), (2, (20, 200))]);
618    /// # }));
619    /// # }
620    /// ```
621    pub fn join_keyed_stream<O2: Ordering, R2: Retries, V2>(
622        self,
623        keyed_stream: KeyedStream<K, V2, L, Bounded, O2, R2>,
624    ) -> KeyedStream<K, (V, V2), L, Bounded, NoOrder, R2>
625    where
626        B: IsBounded,
627        K: Eq + Hash,
628    {
629        self.make_bounded()
630            .entries()
631            .weaken_retries::<R2>()
632            .join(keyed_stream.entries())
633            .into_keyed()
634    }
635
636    /// Emit a keyed singleton containing all keys shared between two keyed singletons,
637    /// where each value in the output keyed singleton is a tuple of
638    /// (self.value, other.value).
639    ///
640    /// # Example
641    /// ```rust
642    /// # #[cfg(feature = "deploy")] {
643    /// # use hydro_lang::prelude::*;
644    /// # use futures::StreamExt;
645    /// # tokio_test::block_on(hydro_lang::test_util::stream_transform_test(|process| {
646    /// # let tick = process.tick();
647    /// let requests = // { 1: 10, 2: 20, 3: 30 }
648    /// # process
649    /// #     .source_iter(q!(vec![(1, 10), (2, 20), (3, 30)]))
650    /// #     .into_keyed()
651    /// #     .batch(&tick, nondet!(/** test */))
652    /// #     .first();
653    /// let other = // { 1: 100, 2: 200, 4: 400 }
654    /// # process
655    /// #     .source_iter(q!(vec![(1, 100), (2, 200), (4, 400)]))
656    /// #     .into_keyed()
657    /// #     .batch(&tick, nondet!(/** test */))
658    /// #     .first();
659    /// requests.join_keyed_singleton(other)
660    /// # .entries().all_ticks()
661    /// # }, |mut stream| async move {
662    /// // { 1: (10, 100), 2: (20, 200) }
663    /// # let mut results = vec![];
664    /// # for _ in 0..2 {
665    /// #     results.push(stream.next().await.unwrap());
666    /// # }
667    /// # results.sort();
668    /// # assert_eq!(results, vec![(1, (10, 100)), (2, (20, 200))]);
669    /// # }));
670    /// # }
671    /// ```
672    pub fn join_keyed_singleton<V2: Clone>(
673        self,
674        other: KeyedSingleton<K, V2, L, Bounded>,
675    ) -> KeyedSingleton<K, (V, V2), L, Bounded>
676    where
677        B: IsBounded,
678        K: Eq + Hash,
679    {
680        let result_stream = self
681            .make_bounded()
682            .entries()
683            .join(other.entries())
684            .into_keyed();
685
686        // The cast is guaranteed to succeed, since each key (in both `self` and `other`) has at most one value.
687        KeyedSingleton::new(
688            result_stream.location.clone(),
689            HydroNode::Cast {
690                inner: Box::new(result_stream.ir_node.into_inner()),
691                metadata: result_stream.location.new_node_metadata(KeyedSingleton::<
692                    K,
693                    (V, V2),
694                    L,
695                    Bounded,
696                >::collection_kind(
697                )),
698            },
699        )
700    }
701
702    /// For each value in `self`, find the matching key in `lookup`.
703    /// The output is a keyed singleton with the key from `self`, and a value
704    /// that is a tuple of (`self`'s value, Option<`lookup`'s value>).
705    /// If the key is not present in `lookup`, the option will be [`None`].
706    ///
707    /// # Example
708    /// ```rust
709    /// # #[cfg(feature = "deploy")] {
710    /// # use hydro_lang::prelude::*;
711    /// # use futures::StreamExt;
712    /// # tokio_test::block_on(hydro_lang::test_util::stream_transform_test(|process| {
713    /// # let tick = process.tick();
714    /// let requests = // { 1: 10, 2: 20 }
715    /// # process
716    /// #     .source_iter(q!(vec![(1, 10), (2, 20)]))
717    /// #     .into_keyed()
718    /// #     .batch(&tick, nondet!(/** test */))
719    /// #     .first();
720    /// let other_data = // { 10: 100, 11: 110 }
721    /// # process
722    /// #     .source_iter(q!(vec![(10, 100), (11, 110)]))
723    /// #     .into_keyed()
724    /// #     .batch(&tick, nondet!(/** test */))
725    /// #     .first();
726    /// requests.lookup_keyed_singleton(other_data)
727    /// # .entries().all_ticks()
728    /// # }, |mut stream| async move {
729    /// // { 1: (10, Some(100)), 2: (20, None) }
730    /// # let mut results = vec![];
731    /// # for _ in 0..2 {
732    /// #     results.push(stream.next().await.unwrap());
733    /// # }
734    /// # results.sort();
735    /// # assert_eq!(results, vec![(1, (10, Some(100))), (2, (20, None))]);
736    /// # }));
737    /// # }
738    /// ```
739    pub fn lookup_keyed_singleton<V2>(
740        self,
741        lookup: KeyedSingleton<V, V2, L, Bounded>,
742    ) -> KeyedSingleton<K, (V, Option<V2>), L, Bounded>
743    where
744        B: IsBounded,
745        K: Eq + Hash + Clone,
746        V: Eq + Hash + Clone,
747        V2: Clone,
748    {
749        let result_stream = self
750            .make_bounded()
751            .into_keyed_stream()
752            .lookup_keyed_stream(lookup.into_keyed_stream());
753
754        // The cast is guaranteed to succeed since both lookup and self contain at most 1 value per key
755        KeyedSingleton::new(
756            result_stream.location.clone(),
757            HydroNode::Cast {
758                inner: Box::new(result_stream.ir_node.into_inner()),
759                metadata: result_stream.location.new_node_metadata(KeyedSingleton::<
760                    K,
761                    (V, Option<V2>),
762                    L,
763                    Bounded,
764                >::collection_kind(
765                )),
766            },
767        )
768    }
769
770    /// For each value in `self`, find the matching key in `lookup`.
771    /// The output is a keyed stream with the key from `self`, and a value
772    /// that is a tuple of (`self`'s value, Option<`lookup`'s value>).
773    /// If the key is not present in `lookup`, the option will be [`None`].
774    ///
775    /// # Example
776    /// ```rust
777    /// # #[cfg(feature = "deploy")] {
778    /// # use hydro_lang::prelude::*;
779    /// # use futures::StreamExt;
780    /// # tokio_test::block_on(hydro_lang::test_util::stream_transform_test(|process| {
781    /// # let tick = process.tick();
782    /// let requests = // { 1: 10, 2: 20 }
783    /// # process
784    /// #     .source_iter(q!(vec![(1, 10), (2, 20)]))
785    /// #     .into_keyed()
786    /// #     .batch(&tick, nondet!(/** test */))
787    /// #     .first();
788    /// let other_data = // { 10: 100, 10: 110 }
789    /// # process
790    /// #     .source_iter(q!(vec![(10, 100), (10, 110)]))
791    /// #     .into_keyed()
792    /// #     .batch(&tick, nondet!(/** test */));
793    /// requests.lookup_keyed_stream(other_data)
794    /// # .entries().all_ticks()
795    /// # }, |mut stream| async move {
796    /// // { 1: [(10, Some(100)), (10, Some(110))], 2: (20, None) }
797    /// # let mut results = vec![];
798    /// # for _ in 0..3 {
799    /// #     results.push(stream.next().await.unwrap());
800    /// # }
801    /// # results.sort();
802    /// # assert_eq!(results, vec![(1, (10, Some(100))), (1, (10, Some(110))), (2, (20, None))]);
803    /// # }));
804    /// # }
805    /// ```
806    pub fn lookup_keyed_stream<V2, O: Ordering, R: Retries>(
807        self,
808        lookup: KeyedStream<V, V2, L, Bounded, O, R>,
809    ) -> KeyedStream<K, (V, Option<V2>), L, Bounded, NoOrder, R>
810    where
811        B: IsBounded,
812        K: Eq + Hash + Clone,
813        V: Eq + Hash + Clone,
814        V2: Clone,
815    {
816        self.make_bounded()
817            .entries()
818            .weaken_retries::<R>() // TODO: Once weaken_retries() is implemented for KeyedSingleton, remove entries() and into_keyed()
819            .into_keyed()
820            .lookup_keyed_stream(lookup)
821    }
822}
823
824impl<'a, K, V, L: Location<'a>, B: KeyedSingletonBound<ValueBound = Bounded>>
825    KeyedSingleton<K, V, L, B>
826{
827    /// Flattens the keyed singleton into an unordered stream of key-value pairs.
828    ///
829    /// The value for each key must be bounded, otherwise the resulting stream elements would be
830    /// non-deterministic. As new entries are added to the keyed singleton, they will be streamed
831    /// into the output.
832    ///
833    /// # Example
834    /// ```rust
835    /// # #[cfg(feature = "deploy")] {
836    /// # use hydro_lang::prelude::*;
837    /// # use futures::StreamExt;
838    /// # tokio_test::block_on(hydro_lang::test_util::stream_transform_test(|process| {
839    /// let keyed_singleton = // { 1: 2, 2: 4 }
840    /// # process
841    /// #     .source_iter(q!(vec![(1, 2), (2, 4)]))
842    /// #     .into_keyed()
843    /// #     .first();
844    /// keyed_singleton.entries()
845    /// # }, |mut stream| async move {
846    /// // (1, 2), (2, 4) in any order
847    /// # let mut results = Vec::new();
848    /// # for _ in 0..2 {
849    /// #     results.push(stream.next().await.unwrap());
850    /// # }
851    /// # results.sort();
852    /// # assert_eq!(results, vec![(1, 2), (2, 4)]);
853    /// # }));
854    /// # }
855    /// ```
856    pub fn entries(self) -> Stream<(K, V), L, B::UnderlyingBound, NoOrder, ExactlyOnce> {
857        self.into_keyed_stream().entries()
858    }
859
860    /// Flattens the keyed singleton into an unordered stream of just the values.
861    ///
862    /// The value for each key must be bounded, otherwise the resulting stream elements would be
863    /// non-deterministic. As new entries are added to the keyed singleton, they will be streamed
864    /// into the output.
865    ///
866    /// # Example
867    /// ```rust
868    /// # #[cfg(feature = "deploy")] {
869    /// # use hydro_lang::prelude::*;
870    /// # use futures::StreamExt;
871    /// # tokio_test::block_on(hydro_lang::test_util::stream_transform_test(|process| {
872    /// let keyed_singleton = // { 1: 2, 2: 4 }
873    /// # process
874    /// #     .source_iter(q!(vec![(1, 2), (2, 4)]))
875    /// #     .into_keyed()
876    /// #     .first();
877    /// keyed_singleton.values()
878    /// # }, |mut stream| async move {
879    /// // 2, 4 in any order
880    /// # let mut results = Vec::new();
881    /// # for _ in 0..2 {
882    /// #     results.push(stream.next().await.unwrap());
883    /// # }
884    /// # results.sort();
885    /// # assert_eq!(results, vec![2, 4]);
886    /// # }));
887    /// # }
888    /// ```
889    pub fn values(self) -> Stream<V, L, B::UnderlyingBound, NoOrder, ExactlyOnce> {
890        let map_f = q!(|(_, v)| v)
891            .splice_fn1_ctx::<(K, V), V>(&self.location)
892            .into();
893
894        Stream::new(
895            self.location.clone(),
896            HydroNode::Map {
897                f: map_f,
898                input: Box::new(self.ir_node.into_inner()),
899                metadata: self.location.new_node_metadata(Stream::<
900                    V,
901                    L,
902                    B::UnderlyingBound,
903                    NoOrder,
904                    ExactlyOnce,
905                >::collection_kind()),
906            },
907        )
908    }
909
910    /// Flattens the keyed singleton into an unordered stream of just the keys.
911    ///
912    /// The value for each key must be bounded, otherwise the removal of keys would result in
913    /// non-determinism. As new entries are added to the keyed singleton, they will be streamed
914    /// into the output.
915    ///
916    /// # Example
917    /// ```rust
918    /// # #[cfg(feature = "deploy")] {
919    /// # use hydro_lang::prelude::*;
920    /// # use futures::StreamExt;
921    /// # tokio_test::block_on(hydro_lang::test_util::stream_transform_test(|process| {
922    /// let keyed_singleton = // { 1: 2, 2: 4 }
923    /// # process
924    /// #     .source_iter(q!(vec![(1, 2), (2, 4)]))
925    /// #     .into_keyed()
926    /// #     .first();
927    /// keyed_singleton.keys()
928    /// # }, |mut stream| async move {
929    /// // 1, 2 in any order
930    /// # let mut results = Vec::new();
931    /// # for _ in 0..2 {
932    /// #     results.push(stream.next().await.unwrap());
933    /// # }
934    /// # results.sort();
935    /// # assert_eq!(results, vec![1, 2]);
936    /// # }));
937    /// # }
938    /// ```
939    pub fn keys(self) -> Stream<K, L, B::UnderlyingBound, NoOrder, ExactlyOnce> {
940        self.entries().map(q!(|(k, _)| k))
941    }
942
943    /// Given a bounded stream of keys `K`, returns a new keyed singleton containing only the
944    /// entries whose keys are not in the provided stream.
945    ///
946    /// # Example
947    /// ```rust
948    /// # #[cfg(feature = "deploy")] {
949    /// # use hydro_lang::prelude::*;
950    /// # use futures::StreamExt;
951    /// # tokio_test::block_on(hydro_lang::test_util::stream_transform_test(|process| {
952    /// let tick = process.tick();
953    /// let keyed_singleton = // { 1: 2, 2: 4 }
954    /// # process
955    /// #     .source_iter(q!(vec![(1, 2), (2, 4)]))
956    /// #     .into_keyed()
957    /// #     .first()
958    /// #     .batch(&tick, nondet!(/** test */));
959    /// let keys_to_remove = process
960    ///     .source_iter(q!(vec![1]))
961    ///     .batch(&tick, nondet!(/** test */));
962    /// keyed_singleton.filter_key_not_in(keys_to_remove)
963    /// #   .entries().all_ticks()
964    /// # }, |mut stream| async move {
965    /// // { 2: 4 }
966    /// # for w in vec![(2, 4)] {
967    /// #     assert_eq!(stream.next().await.unwrap(), w);
968    /// # }
969    /// # }));
970    /// # }
971    /// ```
972    pub fn filter_key_not_in<O2: Ordering, R2: Retries>(
973        self,
974        other: Stream<K, L, Bounded, O2, R2>,
975    ) -> Self
976    where
977        K: Hash + Eq,
978    {
979        check_matching_location(&self.location, &other.location);
980
981        KeyedSingleton::new(
982            self.location.clone(),
983            HydroNode::AntiJoin {
984                pos: Box::new(self.ir_node.into_inner()),
985                neg: Box::new(other.ir_node.into_inner()),
986                metadata: self.location.new_node_metadata(Self::collection_kind()),
987            },
988        )
989    }
990
991    /// An operator which allows you to "inspect" each value of a keyed singleton without
992    /// modifying it. The closure `f` is called on a reference to each value. This is
993    /// mainly useful for debugging, and should not be used to generate side-effects.
994    ///
995    /// # Example
996    /// ```rust
997    /// # #[cfg(feature = "deploy")] {
998    /// # use hydro_lang::prelude::*;
999    /// # use futures::StreamExt;
1000    /// # tokio_test::block_on(hydro_lang::test_util::stream_transform_test(|process| {
1001    /// let keyed_singleton = // { 1: 2, 2: 4 }
1002    /// # process
1003    /// #     .source_iter(q!(vec![(1, 2), (2, 4)]))
1004    /// #     .into_keyed()
1005    /// #     .first();
1006    /// keyed_singleton
1007    ///     .inspect(q!(|v| println!("{}", v)))
1008    /// #   .entries()
1009    /// # }, |mut stream| async move {
1010    /// // { 1: 2, 2: 4 }
1011    /// # for w in vec![(1, 2), (2, 4)] {
1012    /// #     assert_eq!(stream.next().await.unwrap(), w);
1013    /// # }
1014    /// # }));
1015    /// # }
1016    /// ```
1017    pub fn inspect<F>(self, f: impl IntoQuotedMut<'a, F, L> + Copy) -> Self
1018    where
1019        F: Fn(&V) + 'a,
1020    {
1021        let f: ManualExpr<F, _> = ManualExpr::new(move |ctx: &L| f.splice_fn1_borrow_ctx(ctx));
1022        let inspect_f = q!({
1023            let orig = f;
1024            move |t: &(_, _)| orig(&t.1)
1025        })
1026        .splice_fn1_borrow_ctx::<(K, V), ()>(&self.location)
1027        .into();
1028
1029        KeyedSingleton::new(
1030            self.location.clone(),
1031            HydroNode::Inspect {
1032                f: inspect_f,
1033                input: Box::new(self.ir_node.into_inner()),
1034                metadata: self.location.new_node_metadata(Self::collection_kind()),
1035            },
1036        )
1037    }
1038
1039    /// An operator which allows you to "inspect" each entry of a keyed singleton without
1040    /// modifying it. The closure `f` is called on a reference to each key-value pair. This is
1041    /// mainly useful for debugging, and should not be used to generate side-effects.
1042    ///
1043    /// # Example
1044    /// ```rust
1045    /// # #[cfg(feature = "deploy")] {
1046    /// # use hydro_lang::prelude::*;
1047    /// # use futures::StreamExt;
1048    /// # tokio_test::block_on(hydro_lang::test_util::stream_transform_test(|process| {
1049    /// let keyed_singleton = // { 1: 2, 2: 4 }
1050    /// # process
1051    /// #     .source_iter(q!(vec![(1, 2), (2, 4)]))
1052    /// #     .into_keyed()
1053    /// #     .first();
1054    /// keyed_singleton
1055    ///     .inspect_with_key(q!(|(k, v)| println!("{}: {}", k, v)))
1056    /// #   .entries()
1057    /// # }, |mut stream| async move {
1058    /// // { 1: 2, 2: 4 }
1059    /// # for w in vec![(1, 2), (2, 4)] {
1060    /// #     assert_eq!(stream.next().await.unwrap(), w);
1061    /// # }
1062    /// # }));
1063    /// # }
1064    /// ```
1065    pub fn inspect_with_key<F>(self, f: impl IntoQuotedMut<'a, F, L>) -> Self
1066    where
1067        F: Fn(&(K, V)) + 'a,
1068    {
1069        let inspect_f = f.splice_fn1_borrow_ctx::<(K, V), ()>(&self.location).into();
1070
1071        KeyedSingleton::new(
1072            self.location.clone(),
1073            HydroNode::Inspect {
1074                f: inspect_f,
1075                input: Box::new(self.ir_node.into_inner()),
1076                metadata: self.location.new_node_metadata(Self::collection_kind()),
1077            },
1078        )
1079    }
1080
1081    /// Gets the key-value tuple with the largest key among all entries in this [`KeyedSingleton`].
1082    ///
1083    /// Because this method requires values to be bounded, the output [`Optional`] will only be
1084    /// asynchronously updated if a new key is added that is higher than the previous max key.
1085    ///
1086    /// # Example
1087    /// ```rust
1088    /// # #[cfg(feature = "deploy")] {
1089    /// # use hydro_lang::prelude::*;
1090    /// # use futures::StreamExt;
1091    /// # tokio_test::block_on(hydro_lang::test_util::stream_transform_test(|process| {
1092    /// let tick = process.tick();
1093    /// let keyed_singleton = // { 1: 123, 2: 456, 0: 789 }
1094    /// # Stream::<_, _>::from(process.source_iter(q!(vec![(1, 123), (2, 456), (0, 789)])))
1095    /// #     .into_keyed()
1096    /// #     .first();
1097    /// keyed_singleton.get_max_key()
1098    /// # .sample_eager(nondet!(/** test */))
1099    /// # }, |mut stream| async move {
1100    /// // (2, 456)
1101    /// # assert_eq!(stream.next().await.unwrap(), (2, 456));
1102    /// # }));
1103    /// # }
1104    /// ```
1105    pub fn get_max_key(self) -> Optional<(K, V), L, B::UnderlyingBound>
1106    where
1107        K: Ord,
1108    {
1109        self.entries()
1110            .assume_ordering_trusted(nondet!(
1111                /// There is only one element associated with each key, and the keys are totallly
1112                /// ordered so we will produce a deterministic value. The closure technically
1113                /// isn't commutative in the case where both passed entries have the same key
1114                /// but different values.
1115                ///
1116                /// In the future, we may want to have an `assume!(...)` statement in the UDF that
1117                /// the two inputs do not have the same key.
1118            ))
1119            .reduce(q!(
1120                move |curr, new| {
1121                    if new.0 > curr.0 {
1122                        *curr = new;
1123                    }
1124                },
1125                idempotent = ManualProof(/* repeated elements are ignored */)
1126            ))
1127    }
1128
1129    /// Converts this keyed singleton into a [`KeyedStream`] with each group having a single
1130    /// element, the value.
1131    ///
1132    /// This is the equivalent of [`Singleton::into_stream`] but keyed.
1133    ///
1134    /// # Example
1135    /// ```rust
1136    /// # #[cfg(feature = "deploy")] {
1137    /// # use hydro_lang::prelude::*;
1138    /// # use futures::StreamExt;
1139    /// # tokio_test::block_on(hydro_lang::test_util::stream_transform_test(|process| {
1140    /// let keyed_singleton = // { 1: 2, 2: 4 }
1141    /// # Stream::<_, _>::from(process.source_iter(q!(vec![(1, 2), (2, 4)])))
1142    /// #     .into_keyed()
1143    /// #     .first();
1144    /// keyed_singleton
1145    ///     .clone()
1146    ///     .into_keyed_stream()
1147    ///     .interleave(
1148    ///         keyed_singleton.into_keyed_stream()
1149    ///     )
1150    /// #   .entries()
1151    /// # }, |mut stream| async move {
1152    /// /// // { 1: [2, 2], 2: [4, 4] }
1153    /// # for w in vec![(1, 2), (2, 4), (1, 2), (2, 4)] {
1154    /// #     assert_eq!(stream.next().await.unwrap(), w);
1155    /// # }
1156    /// # }));
1157    /// # }
1158    /// ```
1159    pub fn into_keyed_stream(
1160        self,
1161    ) -> KeyedStream<K, V, L, B::UnderlyingBound, TotalOrder, ExactlyOnce> {
1162        KeyedStream::new(
1163            self.location.clone(),
1164            HydroNode::Cast {
1165                inner: Box::new(self.ir_node.into_inner()),
1166                metadata: self.location.new_node_metadata(KeyedStream::<
1167                    K,
1168                    V,
1169                    L,
1170                    B::UnderlyingBound,
1171                    TotalOrder,
1172                    ExactlyOnce,
1173                >::collection_kind()),
1174            },
1175        )
1176    }
1177}
1178
1179impl<'a, K, V, L, B: KeyedSingletonBound> KeyedSingleton<K, V, L, B>
1180where
1181    L: Location<'a>,
1182{
1183    /// Shifts this keyed singleton into an atomic context, which guarantees that any downstream logic
1184    /// will all be executed synchronously before any outputs are yielded (in [`KeyedSingleton::end_atomic`]).
1185    ///
1186    /// This is useful to enforce local consistency constraints, such as ensuring that a write is
1187    /// processed before an acknowledgement is emitted. Entering an atomic section requires a [`Tick`]
1188    /// argument that declares where the keyed singleton will be atomically processed. Batching a
1189    /// keyed singleton into the _same_ [`Tick`] will preserve the synchronous execution, while
1190    /// batching into a different [`Tick`] will introduce asynchrony.
1191    pub fn atomic(self, tick: &Tick<L>) -> KeyedSingleton<K, V, Atomic<L>, B> {
1192        let out_location = Atomic { tick: tick.clone() };
1193        KeyedSingleton::new(
1194            out_location.clone(),
1195            HydroNode::BeginAtomic {
1196                inner: Box::new(self.ir_node.into_inner()),
1197                metadata: out_location
1198                    .new_node_metadata(KeyedSingleton::<K, V, Atomic<L>, B>::collection_kind()),
1199            },
1200        )
1201    }
1202}
1203
1204impl<'a, K, V, L, B: KeyedSingletonBound> KeyedSingleton<K, V, Atomic<L>, B>
1205where
1206    L: Location<'a> + NoTick,
1207{
1208    /// Yields the elements of this keyed singleton back into a top-level, asynchronous execution context.
1209    /// See [`KeyedSingleton::atomic`] for more details.
1210    pub fn end_atomic(self) -> KeyedSingleton<K, V, L, B> {
1211        KeyedSingleton::new(
1212            self.location.tick.l.clone(),
1213            HydroNode::EndAtomic {
1214                inner: Box::new(self.ir_node.into_inner()),
1215                metadata: self
1216                    .location
1217                    .tick
1218                    .l
1219                    .new_node_metadata(KeyedSingleton::<K, V, L, B>::collection_kind()),
1220            },
1221        )
1222    }
1223}
1224
1225impl<'a, K, V, L: Location<'a>> KeyedSingleton<K, V, Tick<L>, Bounded> {
1226    /// Shifts the state in `self` to the **next tick**, so that the returned keyed singleton at
1227    /// tick `T` always has the entries of `self` at tick `T - 1`.
1228    ///
1229    /// At tick `0`, the output has no entries, since there is no previous tick.
1230    ///
1231    /// This operator enables stateful iterative processing with ticks, by sending data from one
1232    /// tick to the next. For example, you can use it to compare state across consecutive batches.
1233    ///
1234    /// # Example
1235    /// ```rust
1236    /// # #[cfg(feature = "deploy")] {
1237    /// # use hydro_lang::prelude::*;
1238    /// # use futures::StreamExt;
1239    /// # tokio_test::block_on(hydro_lang::test_util::stream_transform_test(|process| {
1240    /// let tick = process.tick();
1241    /// # // ticks are lazy by default, forces the second tick to run
1242    /// # tick.spin_batch(q!(1)).all_ticks().for_each(q!(|_| {}));
1243    /// # let batch_first_tick = process
1244    /// #   .source_iter(q!(vec![(1, 2), (2, 3)]))
1245    /// #   .batch(&tick, nondet!(/** test */))
1246    /// #   .into_keyed();
1247    /// # let batch_second_tick = process
1248    /// #   .source_iter(q!(vec![(2, 4), (3, 5)]))
1249    /// #   .batch(&tick, nondet!(/** test */))
1250    /// #   .into_keyed()
1251    /// #   .defer_tick(); // appears on the second tick
1252    /// let input_batch = // first tick: { 1: 2, 2: 3 }, second tick: { 2: 4, 3: 5 }
1253    /// # batch_first_tick.chain(batch_second_tick).first();
1254    /// input_batch.clone().filter_key_not_in(
1255    ///     input_batch.defer_tick().keys() // keys present in the previous tick
1256    /// )
1257    /// # .entries().all_ticks()
1258    /// # }, |mut stream| async move {
1259    /// // { 1: 2, 2: 3 } (first tick), { 3: 5 } (second tick)
1260    /// # for w in vec![(1, 2), (2, 3), (3, 5)] {
1261    /// #     assert_eq!(stream.next().await.unwrap(), w);
1262    /// # }
1263    /// # }));
1264    /// # }
1265    /// ```
1266    pub fn defer_tick(self) -> KeyedSingleton<K, V, Tick<L>, Bounded> {
1267        KeyedSingleton::new(
1268            self.location.clone(),
1269            HydroNode::DeferTick {
1270                input: Box::new(self.ir_node.into_inner()),
1271                metadata: self
1272                    .location
1273                    .new_node_metadata(KeyedSingleton::<K, V, Tick<L>, Bounded>::collection_kind()),
1274            },
1275        )
1276    }
1277}
1278
1279impl<'a, K, V, L, B: KeyedSingletonBound<ValueBound = Unbounded>> KeyedSingleton<K, V, L, B>
1280where
1281    L: Location<'a>,
1282{
1283    /// Returns a keyed singleton with a snapshot of each key-value entry at a non-deterministic
1284    /// point in time.
1285    ///
1286    /// # Non-Determinism
1287    /// Because this picks a snapshot of each entry, which is continuously changing, each output has a
1288    /// non-deterministic set of entries since each snapshot can be at an arbitrary point in time.
1289    pub fn snapshot(
1290        self,
1291        tick: &Tick<L>,
1292        _nondet: NonDet,
1293    ) -> KeyedSingleton<K, V, Tick<L>, Bounded> {
1294        assert_eq!(Location::id(tick.outer()), Location::id(&self.location));
1295        KeyedSingleton::new(
1296            tick.clone(),
1297            HydroNode::Batch {
1298                inner: Box::new(self.ir_node.into_inner()),
1299                metadata: tick
1300                    .new_node_metadata(KeyedSingleton::<K, V, Tick<L>, Bounded>::collection_kind()),
1301            },
1302        )
1303    }
1304}
1305
1306impl<'a, K, V, L, B: KeyedSingletonBound<ValueBound = Unbounded>> KeyedSingleton<K, V, Atomic<L>, B>
1307where
1308    L: Location<'a> + NoTick,
1309{
1310    /// Returns a keyed singleton with a snapshot of each key-value entry, consistent with the
1311    /// state of the keyed singleton being atomically processed.
1312    ///
1313    /// # Non-Determinism
1314    /// Because this picks a snapshot of each entry, which is continuously changing, each output has a
1315    /// non-deterministic set of entries since each snapshot can be at an arbitrary point in time.
1316    pub fn snapshot_atomic(self, _nondet: NonDet) -> KeyedSingleton<K, V, Tick<L>, Bounded> {
1317        KeyedSingleton::new(
1318            self.location.clone().tick,
1319            HydroNode::Batch {
1320                inner: Box::new(self.ir_node.into_inner()),
1321                metadata: self.location.tick.new_node_metadata(KeyedSingleton::<
1322                    K,
1323                    V,
1324                    Tick<L>,
1325                    Bounded,
1326                >::collection_kind(
1327                )),
1328            },
1329        )
1330    }
1331}
1332
1333impl<'a, K, V, L, B: KeyedSingletonBound<ValueBound = Bounded>> KeyedSingleton<K, V, L, B>
1334where
1335    L: Location<'a>,
1336{
1337    /// Creates a keyed singleton containing only the key-value pairs where the value satisfies a predicate `f`.
1338    ///
1339    /// The closure `f` receives a reference `&V` to each value and returns a boolean. If the predicate
1340    /// returns `true`, the key-value pair is included in the output. If it returns `false`, the pair
1341    /// is filtered out.
1342    ///
1343    /// The closure `f` receives a reference `&V` rather than an owned value `V` because filtering does
1344    /// not modify or take ownership of the values. If you need to modify the values while filtering
1345    /// use [`KeyedSingleton::filter_map`] instead.
1346    ///
1347    /// # Example
1348    /// ```rust
1349    /// # #[cfg(feature = "deploy")] {
1350    /// # use hydro_lang::prelude::*;
1351    /// # use futures::StreamExt;
1352    /// # tokio_test::block_on(hydro_lang::test_util::stream_transform_test(|process| {
1353    /// let keyed_singleton = // { 1: 2, 2: 4, 3: 1 }
1354    /// # process
1355    /// #     .source_iter(q!(vec![(1, 2), (2, 4), (3, 1)]))
1356    /// #     .into_keyed()
1357    /// #     .first();
1358    /// keyed_singleton.filter(q!(|&v| v > 1))
1359    /// #   .entries()
1360    /// # }, |mut stream| async move {
1361    /// // { 1: 2, 2: 4 }
1362    /// # let mut results = Vec::new();
1363    /// # for _ in 0..2 {
1364    /// #     results.push(stream.next().await.unwrap());
1365    /// # }
1366    /// # results.sort();
1367    /// # assert_eq!(results, vec![(1, 2), (2, 4)]);
1368    /// # }));
1369    /// # }
1370    /// ```
1371    pub fn filter<F>(self, f: impl IntoQuotedMut<'a, F, L> + Copy) -> KeyedSingleton<K, V, L, B>
1372    where
1373        F: Fn(&V) -> bool + 'a,
1374    {
1375        let f: ManualExpr<F, _> = ManualExpr::new(move |ctx: &L| f.splice_fn1_borrow_ctx(ctx));
1376        let filter_f = q!({
1377            let orig = f;
1378            move |t: &(_, _)| orig(&t.1)
1379        })
1380        .splice_fn1_borrow_ctx::<(K, V), bool>(&self.location)
1381        .into();
1382
1383        KeyedSingleton::new(
1384            self.location.clone(),
1385            HydroNode::Filter {
1386                f: filter_f,
1387                input: Box::new(self.ir_node.into_inner()),
1388                metadata: self
1389                    .location
1390                    .new_node_metadata(KeyedSingleton::<K, V, L, B>::collection_kind()),
1391            },
1392        )
1393    }
1394
1395    /// An operator that both filters and maps values. It yields only the key-value pairs where
1396    /// the supplied closure `f` returns `Some(value)`.
1397    ///
1398    /// The closure `f` receives each value `V` and returns `Option<U>`. If the closure returns
1399    /// `Some(new_value)`, the key-value pair `(key, new_value)` is included in the output.
1400    /// If it returns `None`, the key-value pair is filtered out.
1401    ///
1402    /// # Example
1403    /// ```rust
1404    /// # #[cfg(feature = "deploy")] {
1405    /// # use hydro_lang::prelude::*;
1406    /// # use futures::StreamExt;
1407    /// # tokio_test::block_on(hydro_lang::test_util::stream_transform_test(|process| {
1408    /// let keyed_singleton = // { 1: "42", 2: "hello", 3: "100" }
1409    /// # process
1410    /// #     .source_iter(q!(vec![(1, "42"), (2, "hello"), (3, "100")]))
1411    /// #     .into_keyed()
1412    /// #     .first();
1413    /// keyed_singleton.filter_map(q!(|s| s.parse::<i32>().ok()))
1414    /// #   .entries()
1415    /// # }, |mut stream| async move {
1416    /// // { 1: 42, 3: 100 }
1417    /// # let mut results = Vec::new();
1418    /// # for _ in 0..2 {
1419    /// #     results.push(stream.next().await.unwrap());
1420    /// # }
1421    /// # results.sort();
1422    /// # assert_eq!(results, vec![(1, 42), (3, 100)]);
1423    /// # }));
1424    /// # }
1425    /// ```
1426    pub fn filter_map<F, U>(
1427        self,
1428        f: impl IntoQuotedMut<'a, F, L> + Copy,
1429    ) -> KeyedSingleton<K, U, L, B>
1430    where
1431        F: Fn(V) -> Option<U> + 'a,
1432    {
1433        let f: ManualExpr<F, _> = ManualExpr::new(move |ctx: &L| f.splice_fn1_ctx(ctx));
1434        let filter_map_f = q!({
1435            let orig = f;
1436            move |(k, v)| orig(v).map(|o| (k, o))
1437        })
1438        .splice_fn1_ctx::<(K, V), Option<(K, U)>>(&self.location)
1439        .into();
1440
1441        KeyedSingleton::new(
1442            self.location.clone(),
1443            HydroNode::FilterMap {
1444                f: filter_map_f,
1445                input: Box::new(self.ir_node.into_inner()),
1446                metadata: self
1447                    .location
1448                    .new_node_metadata(KeyedSingleton::<K, U, L, B>::collection_kind()),
1449            },
1450        )
1451    }
1452
1453    /// Returns a keyed singleton with entries consisting of _new_ key-value pairs that have
1454    /// arrived since the previous batch was released.
1455    ///
1456    /// Currently, there is no `all_ticks` dual on [`KeyedSingleton`], instead you may want to use
1457    /// [`KeyedSingleton::into_keyed_stream`] then yield with [`KeyedStream::all_ticks`].
1458    ///
1459    /// # Non-Determinism
1460    /// Because this picks a batch of asynchronously added entries, each output keyed singleton
1461    /// has a non-deterministic set of key-value pairs.
1462    pub fn batch(self, tick: &Tick<L>, nondet: NonDet) -> KeyedSingleton<K, V, Tick<L>, Bounded>
1463    where
1464        L: NoTick,
1465    {
1466        self.atomic(tick).batch_atomic(nondet)
1467    }
1468}
1469
1470impl<'a, K, V, L, B: KeyedSingletonBound<ValueBound = Bounded>> KeyedSingleton<K, V, Atomic<L>, B>
1471where
1472    L: Location<'a> + NoTick,
1473{
1474    /// Returns a keyed singleton with entries consisting of _new_ key-value pairs that are being
1475    /// atomically processed.
1476    ///
1477    /// Currently, there is no dual to asynchronously yield back outside the tick, instead you
1478    /// should use [`KeyedSingleton::into_keyed_stream`] and yield a [`KeyedStream`].
1479    ///
1480    /// # Non-Determinism
1481    /// Because this picks a batch of asynchronously added entries, each output keyed singleton
1482    /// has a non-deterministic set of key-value pairs.
1483    pub fn batch_atomic(self, nondet: NonDet) -> KeyedSingleton<K, V, Tick<L>, Bounded> {
1484        let _ = nondet;
1485        KeyedSingleton::new(
1486            self.location.clone().tick,
1487            HydroNode::Batch {
1488                inner: Box::new(self.ir_node.into_inner()),
1489                metadata: self.location.tick.new_node_metadata(KeyedSingleton::<
1490                    K,
1491                    V,
1492                    Tick<L>,
1493                    Bounded,
1494                >::collection_kind(
1495                )),
1496            },
1497        )
1498    }
1499}
1500
1501#[cfg(test)]
1502mod tests {
1503    #[cfg(feature = "deploy")]
1504    use futures::{SinkExt, StreamExt};
1505    #[cfg(feature = "deploy")]
1506    use hydro_deploy::Deployment;
1507    #[cfg(any(feature = "deploy", feature = "sim"))]
1508    use stageleft::q;
1509
1510    #[cfg(any(feature = "deploy", feature = "sim"))]
1511    use crate::compile::builder::FlowBuilder;
1512    #[cfg(any(feature = "deploy", feature = "sim"))]
1513    use crate::location::Location;
1514    #[cfg(any(feature = "deploy", feature = "sim"))]
1515    use crate::nondet::nondet;
1516
1517    #[cfg(feature = "deploy")]
1518    #[tokio::test]
1519    async fn key_count_bounded_value() {
1520        let mut deployment = Deployment::new();
1521
1522        let mut flow = FlowBuilder::new();
1523        let node = flow.process::<()>();
1524        let external = flow.external::<()>();
1525
1526        let (input_port, input) = node.source_external_bincode(&external);
1527        let out = input
1528            .into_keyed()
1529            .first()
1530            .key_count()
1531            .sample_eager(nondet!(/** test */))
1532            .send_bincode_external(&external);
1533
1534        let nodes = flow
1535            .with_process(&node, deployment.Localhost())
1536            .with_external(&external, deployment.Localhost())
1537            .deploy(&mut deployment);
1538
1539        deployment.deploy().await.unwrap();
1540
1541        let mut external_in = nodes.connect(input_port).await;
1542        let mut external_out = nodes.connect(out).await;
1543
1544        deployment.start().await.unwrap();
1545
1546        assert_eq!(external_out.next().await.unwrap(), 0);
1547
1548        external_in.send((1, 1)).await.unwrap();
1549        assert_eq!(external_out.next().await.unwrap(), 1);
1550
1551        external_in.send((2, 2)).await.unwrap();
1552        assert_eq!(external_out.next().await.unwrap(), 2);
1553    }
1554
1555    #[cfg(feature = "deploy")]
1556    #[tokio::test]
1557    async fn key_count_unbounded_value() {
1558        let mut deployment = Deployment::new();
1559
1560        let mut flow = FlowBuilder::new();
1561        let node = flow.process::<()>();
1562        let external = flow.external::<()>();
1563
1564        let (input_port, input) = node.source_external_bincode(&external);
1565        let out = input
1566            .into_keyed()
1567            .fold(q!(|| 0), q!(|acc, _| *acc += 1))
1568            .key_count()
1569            .sample_eager(nondet!(/** test */))
1570            .send_bincode_external(&external);
1571
1572        let nodes = flow
1573            .with_process(&node, deployment.Localhost())
1574            .with_external(&external, deployment.Localhost())
1575            .deploy(&mut deployment);
1576
1577        deployment.deploy().await.unwrap();
1578
1579        let mut external_in = nodes.connect(input_port).await;
1580        let mut external_out = nodes.connect(out).await;
1581
1582        deployment.start().await.unwrap();
1583
1584        assert_eq!(external_out.next().await.unwrap(), 0);
1585
1586        external_in.send((1, 1)).await.unwrap();
1587        assert_eq!(external_out.next().await.unwrap(), 1);
1588
1589        external_in.send((1, 2)).await.unwrap();
1590        assert_eq!(external_out.next().await.unwrap(), 1);
1591
1592        external_in.send((2, 2)).await.unwrap();
1593        assert_eq!(external_out.next().await.unwrap(), 2);
1594
1595        external_in.send((1, 1)).await.unwrap();
1596        assert_eq!(external_out.next().await.unwrap(), 2);
1597
1598        external_in.send((3, 1)).await.unwrap();
1599        assert_eq!(external_out.next().await.unwrap(), 3);
1600    }
1601
1602    #[cfg(feature = "deploy")]
1603    #[tokio::test]
1604    async fn into_singleton_bounded_value() {
1605        let mut deployment = Deployment::new();
1606
1607        let mut flow = FlowBuilder::new();
1608        let node = flow.process::<()>();
1609        let external = flow.external::<()>();
1610
1611        let (input_port, input) = node.source_external_bincode(&external);
1612        let out = input
1613            .into_keyed()
1614            .first()
1615            .into_singleton()
1616            .sample_eager(nondet!(/** test */))
1617            .send_bincode_external(&external);
1618
1619        let nodes = flow
1620            .with_process(&node, deployment.Localhost())
1621            .with_external(&external, deployment.Localhost())
1622            .deploy(&mut deployment);
1623
1624        deployment.deploy().await.unwrap();
1625
1626        let mut external_in = nodes.connect(input_port).await;
1627        let mut external_out = nodes.connect(out).await;
1628
1629        deployment.start().await.unwrap();
1630
1631        assert_eq!(
1632            external_out.next().await.unwrap(),
1633            std::collections::HashMap::new()
1634        );
1635
1636        external_in.send((1, 1)).await.unwrap();
1637        assert_eq!(
1638            external_out.next().await.unwrap(),
1639            vec![(1, 1)].into_iter().collect()
1640        );
1641
1642        external_in.send((2, 2)).await.unwrap();
1643        assert_eq!(
1644            external_out.next().await.unwrap(),
1645            vec![(1, 1), (2, 2)].into_iter().collect()
1646        );
1647    }
1648
1649    #[cfg(feature = "deploy")]
1650    #[tokio::test]
1651    async fn into_singleton_unbounded_value() {
1652        let mut deployment = Deployment::new();
1653
1654        let mut flow = FlowBuilder::new();
1655        let node = flow.process::<()>();
1656        let external = flow.external::<()>();
1657
1658        let (input_port, input) = node.source_external_bincode(&external);
1659        let out = input
1660            .into_keyed()
1661            .fold(q!(|| 0), q!(|acc, _| *acc += 1))
1662            .into_singleton()
1663            .sample_eager(nondet!(/** test */))
1664            .send_bincode_external(&external);
1665
1666        let nodes = flow
1667            .with_process(&node, deployment.Localhost())
1668            .with_external(&external, deployment.Localhost())
1669            .deploy(&mut deployment);
1670
1671        deployment.deploy().await.unwrap();
1672
1673        let mut external_in = nodes.connect(input_port).await;
1674        let mut external_out = nodes.connect(out).await;
1675
1676        deployment.start().await.unwrap();
1677
1678        assert_eq!(
1679            external_out.next().await.unwrap(),
1680            std::collections::HashMap::new()
1681        );
1682
1683        external_in.send((1, 1)).await.unwrap();
1684        assert_eq!(
1685            external_out.next().await.unwrap(),
1686            vec![(1, 1)].into_iter().collect()
1687        );
1688
1689        external_in.send((1, 2)).await.unwrap();
1690        assert_eq!(
1691            external_out.next().await.unwrap(),
1692            vec![(1, 2)].into_iter().collect()
1693        );
1694
1695        external_in.send((2, 2)).await.unwrap();
1696        assert_eq!(
1697            external_out.next().await.unwrap(),
1698            vec![(1, 2), (2, 1)].into_iter().collect()
1699        );
1700
1701        external_in.send((1, 1)).await.unwrap();
1702        assert_eq!(
1703            external_out.next().await.unwrap(),
1704            vec![(1, 3), (2, 1)].into_iter().collect()
1705        );
1706
1707        external_in.send((3, 1)).await.unwrap();
1708        assert_eq!(
1709            external_out.next().await.unwrap(),
1710            vec![(1, 3), (2, 1), (3, 1)].into_iter().collect()
1711        );
1712    }
1713
1714    #[cfg(feature = "sim")]
1715    #[test]
1716    fn sim_unbounded_singleton_snapshot() {
1717        let mut flow = FlowBuilder::new();
1718        let node = flow.process::<()>();
1719
1720        let (input_port, input) = node.sim_input();
1721        let output = input
1722            .into_keyed()
1723            .fold(q!(|| 0), q!(|acc, _| *acc += 1))
1724            .snapshot(&node.tick(), nondet!(/** test */))
1725            .entries()
1726            .all_ticks()
1727            .sim_output();
1728
1729        let count = flow.sim().exhaustive(async || {
1730            input_port.send((1, 123));
1731            input_port.send((1, 456));
1732            input_port.send((2, 123));
1733
1734            let all = output.collect_sorted::<Vec<_>>().await;
1735            assert_eq!(all.last().unwrap(), &(2, 1));
1736        });
1737
1738        assert_eq!(count, 8);
1739    }
1740
1741    #[cfg(feature = "deploy")]
1742    #[tokio::test]
1743    async fn join_keyed_stream() {
1744        let mut deployment = Deployment::new();
1745
1746        let mut flow = FlowBuilder::new();
1747        let node = flow.process::<()>();
1748        let external = flow.external::<()>();
1749
1750        let tick = node.tick();
1751        let keyed_data = node
1752            .source_iter(q!(vec![(1, 10), (2, 20)]))
1753            .into_keyed()
1754            .batch(&tick, nondet!(/** test */))
1755            .first();
1756        let requests = node
1757            .source_iter(q!(vec![(1, 100), (2, 200), (3, 300)]))
1758            .into_keyed()
1759            .batch(&tick, nondet!(/** test */));
1760
1761        let out = keyed_data
1762            .join_keyed_stream(requests)
1763            .entries()
1764            .all_ticks()
1765            .send_bincode_external(&external);
1766
1767        let nodes = flow
1768            .with_process(&node, deployment.Localhost())
1769            .with_external(&external, deployment.Localhost())
1770            .deploy(&mut deployment);
1771
1772        deployment.deploy().await.unwrap();
1773
1774        let mut external_out = nodes.connect(out).await;
1775
1776        deployment.start().await.unwrap();
1777
1778        let mut results = vec![];
1779        for _ in 0..2 {
1780            results.push(external_out.next().await.unwrap());
1781        }
1782        results.sort();
1783
1784        assert_eq!(results, vec![(1, (10, 100)), (2, (20, 200))]);
1785    }
1786}