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