Skip to main content

aimdb_core/
inbound_dispatch.rs

1//! Inbound entry point for connectors.
2//!
3//! A connector builds one [`InboundDispatch`] per scheme and calls
4//! [`dispatch`](InboundDispatch::dispatch) with borrows of each message it
5//! receives. Core matches the topic, deserializes and produces in place.
6
7use alloc::sync::Arc;
8use alloc::vec::Vec;
9
10use crate::router::Router;
11use crate::transport::ConnectorConfig;
12use crate::{AimDb, DbResult, RuntimeContext, TopicGrammar};
13
14/// Every inbound link of one scheme, compiled against the connector's grammar.
15///
16/// Cheap to clone: clones share the compiled routes, so a connector can
17/// dispatch from several tasks into the same records.
18#[derive(Clone)]
19pub struct InboundDispatch {
20    router: Arc<Router>,
21    routes: Arc<[InboundRouteInfo]>,
22    ctx: RuntimeContext,
23}
24
25/// One inbound route, for parsing per-route configuration once at build.
26#[derive(Debug, Clone)]
27#[non_exhaustive]
28pub struct InboundRouteInfo {
29    /// The resolved topic or pattern, after any topic resolver.
30    pub topic: Arc<str>,
31    /// `TypeId` of the record the link belongs to.
32    pub type_id: core::any::TypeId,
33    /// Index of the record the link belongs to, in registration order (the
34    /// record's `RecordId`). Set by core, never from the link's query.
35    pub record_index: usize,
36    /// The link's configuration.
37    pub config: ConnectorConfig,
38}
39
40impl InboundDispatch {
41    /// Compiles every inbound link of `scheme` against `grammar`.
42    ///
43    /// Rejects every link the grammar or its key cannot compile, naming the
44    /// record and the resolved topic.
45    pub fn new(db: &AimDb, scheme: &str, grammar: &'static dyn TopicGrammar) -> DbResult<Self> {
46        let (router, routes) = db.inbound_router(scheme, grammar)?;
47        Ok(Self {
48            router: Arc::new(router),
49            routes: routes.into(),
50            ctx: db.runtime_ctx(),
51        })
52    }
53
54    /// A dispatcher over an already compiled `router`, for the session tests.
55    #[cfg(all(test, feature = "connector-session"))]
56    pub(crate) fn from_parts(router: Router, ctx: RuntimeContext) -> Self {
57        Self {
58            router: Arc::new(router),
59            routes: Arc::from([]),
60            ctx,
61        }
62    }
63
64    /// Matches `topic`, deserializes `payload` and produces into every
65    /// matching record.
66    ///
67    /// Synchronous and never blocks. Ingest failures, unmatched topics, full
68    /// buffers and full key tables are logged, not returned.
69    pub fn dispatch(&self, topic: &str, payload: &[u8]) {
70        // `Router::route` only ever returns `Ok`.
71        let _ = self.router.route(topic, payload, &self.ctx);
72    }
73
74    /// Filters to subscribe at the transport. Allocates; call it at connect
75    /// time, not per message.
76    pub fn subscriptions(&self) -> Vec<Arc<str>> {
77        self.router.subscriptions()
78    }
79
80    /// Routes, one per inbound link, for parsing per-route configuration once
81    /// at build.
82    pub fn routes(&self) -> &[InboundRouteInfo] {
83        &self.routes
84    }
85
86    /// Number of inbound routes.
87    pub fn route_count(&self) -> usize {
88        self.router.route_count()
89    }
90}