aimdb_core/transform/mod.rs
1//! Reactive transform primitives for derived records.
2//!
3//! # Transform Archetypes
4//!
5//! - **Map** (1:1, stateless): Transform each input value to zero-or-one output value
6//! - **Accumulate** (N:1, stateful): Aggregate a stream of values with persistent state
7//! - **Join** (M×N:1, stateful, multi-input): Combine values from multiple input records
8//!
9//! All three are handled by a unified API surface:
10//! - Single-input: `.transform()` with `TransformBuilder`
11//! - Multi-input: `.transform_join()` with `JoinBuilder`
12
13mod events;
14
15use core::any::TypeId;
16use core::fmt::Debug;
17
18use alloc::{boxed::Box, string::String, sync::Arc, vec::Vec};
19
20use crate::typed_record::BoxFuture;
21
22pub mod join;
23pub mod single;
24
25// Public re-exports
26pub use single::{StatefulTransformBuilder, TransformBuilder, TransformPipeline};
27
28#[cfg(feature = "observability")]
29use crate::{profiling::Clock, StageMetrics};
30pub use join::{JoinBuilder, JoinEventRx, JoinPipeline, JoinTrigger};
31
32// ============================================================================
33// TransformDescriptor — stored per output record in TypedRecord
34// ============================================================================
35
36/// Futures contributed by a transform at build-time collection.
37///
38/// `task_future` is the transform's main event loop; `fanin_futures` is any
39/// per-input forwarder needed for multi-input joins (empty for single-input).
40pub(crate) struct CollectedTransform {
41 pub task_future: BoxFuture<'static, ()>,
42 pub fanin_futures: Vec<BoxFuture<'static, ()>>,
43}
44
45#[cfg(feature = "observability")]
46pub(crate) type TransformProfiling = Option<(Arc<StageMetrics>, Clock)>;
47
48#[cfg(not(feature = "observability"))]
49pub(crate) type TransformProfiling = ();
50
51pub(crate) struct TransformDescriptor<T>
52where
53 T: Send + 'static + Debug + Clone,
54{
55 pub input_keys: Vec<String>,
56
57 /// Type each input is read as, parallel to `input_keys`. `build()` checks
58 /// that every input record holds this type.
59 pub input_types: Vec<(TypeId, &'static str)>,
60
61 /// Build the transform's futures.
62 ///
63 /// Receives:
64 /// - `Producer<T>` — the pre-resolved write handle for the output record.
65 /// - `Arc<AimDb>` — used by input forwarders to resolve their input
66 /// consumers at startup time.
67 /// - `&str` — the output record's key, threaded through so the transform
68 /// task can include it in tracing messages even though `Producer<T>` no
69 /// longer carries a `.key()` accessor. Important when
70 /// multiple records share type `T` under different keys.
71 #[allow(clippy::type_complexity)]
72 pub build_fn: Box<
73 dyn FnOnce(
74 crate::Producer<T>,
75 Arc<crate::AimDb>,
76 &str,
77 TransformProfiling,
78 ) -> CollectedTransform
79 + Send,
80 >,
81}