-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbuilder.rs
More file actions
1497 lines (1341 loc) · 52.6 KB
/
builder.rs
File metadata and controls
1497 lines (1341 loc) · 52.6 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Database builder with type-safe record registration
//!
//! Provides `AimDb` and `AimDbBuilder` for constructing databases with
//! type-safe, self-registering records using the producer-consumer pattern.
use core::any::TypeId;
use core::fmt::Debug;
use core::marker::PhantomData;
extern crate alloc;
use alloc::vec::Vec;
use hashbrown::HashMap;
#[cfg(not(feature = "std"))]
use alloc::{boxed::Box, sync::Arc};
#[cfg(not(feature = "std"))]
use alloc::string::{String, ToString};
#[cfg(feature = "std")]
use std::{boxed::Box, sync::Arc};
use crate::extensions::Extensions;
use crate::graph::DependencyGraph;
/// Shorthand for a heap-pinned, `Send`, `'static` future — the unit of work
/// the `AimDbRunner` drives.
pub type BoxFuture = core::pin::Pin<Box<dyn core::future::Future<Output = ()> + Send + 'static>>;
/// Type-erased on_start function stored in `AimDbBuilder::start_fns`.
///
/// Defined once here so `on_start()` (which stores) and `build()` (which
/// downcasts) share the *exact same* type and a silent type mismatch cannot
/// cause a runtime panic. Single alias regardless of `std`/`no_std`.
type StartFnType<R> = Box<dyn FnOnce(Arc<R>) -> BoxFuture + Send>;
/// Type-erased per-record future collector stored in `AimDbBuilder::spawn_fns`.
///
/// At `build()` time each is invoked in topological order; the returned
/// `Vec<BoxFuture>` is appended to the runner's accumulator.
type SpawnFnType<R> =
Box<dyn FnOnce(&Arc<R>, &Arc<AimDb<R>>, RecordId) -> DbResult<Vec<BoxFuture>> + Send>;
use crate::record_id::{RecordId, RecordKey, StringKey};
use crate::typed_api::{RecordRegistrar, RecordT};
use crate::typed_record::{AnyRecord, AnyRecordExt, RecordFutureCollector, TypedRecord};
use crate::{DbError, DbResult};
/// Type alias for outbound route tuples returned by `collect_outbound_routes`
///
/// Each tuple contains:
/// - `String` - Default topic/destination from the URL path
/// - `Box<dyn ConsumerTrait>` - Consumer for subscribing to record values
/// - `SerializerKind` - User-provided serializer for the record type (raw or context-aware)
/// - `Vec<(String, String)>` - Configuration options from the URL query
/// - `Option<TopicProviderFn>` - Optional dynamic topic provider
pub type OutboundRoute = (
String,
Box<dyn crate::connector::ConsumerTrait>,
crate::connector::SerializerKind,
Vec<(String, String)>,
Option<crate::connector::TopicProviderFn>,
);
/// Marker type for untyped builder (before runtime is set)
pub struct NoRuntime;
/// Internal database state
///
/// Holds the registry of typed records with multiple index structures for
/// efficient access patterns:
///
/// - **`storages`**: Vec for O(1) hot-path access by RecordId
/// - **`by_key`**: HashMap for O(1) lookup by stable RecordKey
/// - **`by_type`**: HashMap for introspection (find all records of type T)
/// - **`types`**: Vec for runtime type validation during downcasts
/// - **`dependency_graph`**: DAG of record relationships (immutable after build)
pub struct AimDbInner {
/// Record storage (hot path - indexed by RecordId)
///
/// Order matches registration order. Immutable after build().
storages: Vec<Box<dyn AnyRecord>>,
/// Name → RecordId lookup (control plane)
///
/// Used by remote access, CLI, MCP for O(1) name resolution.
by_key: HashMap<StringKey, RecordId>,
/// TypeId → RecordIds lookup (introspection)
///
/// Enables "find all Temperature records" queries.
by_type: HashMap<TypeId, Vec<RecordId>>,
/// RecordId → TypeId lookup (type safety assertions)
///
/// Used to validate downcasts at runtime.
types: Vec<TypeId>,
/// RecordId → StringKey lookup (reverse mapping)
///
/// Used to get the key for a given record ID.
keys: Vec<StringKey>,
/// Dependency graph (immutable after build)
///
/// Contains all record nodes, edges, and topological order.
/// Used for introspection, spawn ordering, and graph queries.
dependency_graph: crate::graph::DependencyGraph,
/// Generic extension storage (frozen after build)
///
/// External crates (e.g. `aimdb-persistence`) store typed state here
/// during builder configuration and retrieve it at query time via
/// `AimDb::extensions()`.
pub(crate) extensions: Extensions,
}
impl AimDbInner {
/// Resolve RecordKey to RecordId (control plane - O(1) average)
#[inline]
pub fn resolve<K: RecordKey>(&self, key: &K) -> Option<RecordId> {
self.by_key.get(key.as_str()).copied()
}
/// Resolve string to RecordId (convenience for remote access)
///
/// O(1) average thanks to `Borrow<str>` implementation on `RecordKey`.
#[inline]
pub fn resolve_str(&self, name: &str) -> Option<RecordId> {
self.by_key.get(name).copied()
}
/// Get storage by RecordId (hot path - O(1))
#[inline]
pub fn storage(&self, id: RecordId) -> Option<&dyn AnyRecord> {
self.storages.get(id.index()).map(|b| b.as_ref())
}
/// Get the StringKey for a given RecordId
#[inline]
pub fn key_for(&self, id: RecordId) -> Option<&StringKey> {
self.keys.get(id.index())
}
/// Get all RecordIds for a type (introspection)
pub fn records_of_type<T: 'static>(&self) -> &[RecordId] {
self.by_type
.get(&TypeId::of::<T>())
.map(|v| v.as_slice())
.unwrap_or(&[])
}
/// Get the number of registered records
#[inline]
pub fn record_count(&self) -> usize {
self.storages.len()
}
/// Get the dependency graph (immutable reference)
///
/// The graph is constructed once during `build()` and never changes.
/// Contains all record nodes, edges, and topological ordering.
#[inline]
pub fn dependency_graph(&self) -> &crate::graph::DependencyGraph {
&self.dependency_graph
}
/// Helper to get a typed record by RecordKey
///
/// This encapsulates the common pattern of:
/// 1. Resolving key to RecordId
/// 2. Validating TypeId matches
/// 3. Downcasting to the typed record
pub fn get_typed_record_by_key<T, R>(
&self,
key: impl AsRef<str>,
) -> DbResult<&TypedRecord<T, R>>
where
T: Send + 'static + Debug + Clone,
R: aimdb_executor::RuntimeAdapter + 'static,
{
let key_str = key.as_ref();
// Resolve key to RecordId
let id = self.resolve_str(key_str).ok_or({
#[cfg(feature = "std")]
{
DbError::RecordKeyNotFound {
key: key_str.to_string(),
}
}
#[cfg(not(feature = "std"))]
{
DbError::RecordKeyNotFound { _key: () }
}
})?;
self.get_typed_record_by_id::<T, R>(id)
}
/// Helper to get a typed record by RecordId with type validation
pub fn get_typed_record_by_id<T, R>(&self, id: RecordId) -> DbResult<&TypedRecord<T, R>>
where
T: Send + 'static + Debug + Clone,
R: aimdb_executor::RuntimeAdapter + 'static,
{
use crate::typed_record::AnyRecordExt;
// Validate RecordId is in bounds
if id.index() >= self.storages.len() {
return Err(DbError::InvalidRecordId { id: id.raw() });
}
// Validate TypeId matches
let expected = TypeId::of::<T>();
let actual = self.types[id.index()];
if expected != actual {
#[cfg(feature = "std")]
return Err(DbError::TypeMismatch {
record_id: id.raw(),
expected_type: core::any::type_name::<T>().to_string(),
});
#[cfg(not(feature = "std"))]
return Err(DbError::TypeMismatch {
record_id: id.raw(),
_expected_type: (),
});
}
// Safe to downcast (type validated above)
let record = &self.storages[id.index()];
#[cfg(feature = "std")]
let typed_record = record.as_typed::<T, R>().ok_or(DbError::InvalidOperation {
operation: "get_typed_record_by_id".to_string(),
reason: "type mismatch during downcast".to_string(),
})?;
#[cfg(not(feature = "std"))]
let typed_record = record.as_typed::<T, R>().ok_or(DbError::InvalidOperation {
_operation: (),
_reason: (),
})?;
Ok(typed_record)
}
/// Collects metadata for all registered records (std only)
///
/// Returns a vector of `RecordMetadata` for remote access introspection.
/// Available only when the `std` feature is enabled.
#[cfg(feature = "std")]
pub fn list_records(&self) -> Vec<crate::remote::RecordMetadata> {
self.storages
.iter()
.enumerate()
.map(|(i, record)| {
let id = RecordId::new(i as u32);
let type_id = self.types[i];
let key = self.keys[i];
record.collect_metadata(type_id, key, id)
})
.collect()
}
/// Try to get record's latest value as JSON by record key (std only)
///
/// O(1) lookup using the key-based index.
///
/// # Arguments
/// * `record_key` - The record key (e.g., "sensors.temperature")
///
/// # Returns
/// `Some(JsonValue)` with the current record value, or `None`
#[cfg(feature = "std")]
pub fn try_latest_as_json(&self, record_key: &str) -> Option<serde_json::Value> {
let id = self.resolve_str(record_key)?;
self.storages.get(id.index())?.latest_json()
}
/// Sets a record value from JSON (remote access API)
///
/// Deserializes the JSON value and writes it to the record's buffer.
///
/// **SAFETY:** Enforces the "No Producer Override" rule:
/// - Only works for records with `producer_count == 0`
/// - Returns error if the record has active producers
///
/// # Arguments
/// * `record_key` - The record key (e.g., "config.app")
/// * `json_value` - JSON representation of the value
///
/// # Returns
/// - `Ok(())` - Successfully set the value
/// - `Err(DbError)` - If record not found, has producers, or deserialization fails
#[cfg(feature = "std")]
pub fn set_record_from_json(
&self,
record_key: &str,
json_value: serde_json::Value,
) -> DbResult<()> {
let id = self
.resolve_str(record_key)
.ok_or_else(|| DbError::RecordKeyNotFound {
key: record_key.to_string(),
})?;
self.storages[id.index()].set_from_json(json_value)
}
}
/// Database builder for producer-consumer pattern
///
/// Provides a fluent API for constructing databases with type-safe record registration.
/// Use `.runtime()` to set the runtime and transition to a typed builder.
pub struct AimDbBuilder<R = NoRuntime> {
/// Registered records with their keys (order matters for RecordId assignment)
records: Vec<(StringKey, TypeId, Box<dyn AnyRecord>)>,
/// Runtime adapter
runtime: Option<Arc<R>>,
/// Connector builders that will be invoked during build()
connector_builders: Vec<Box<dyn crate::connector::ConnectorBuilder<R>>>,
/// Spawn functions with their keys
spawn_fns: Vec<(StringKey, Box<dyn core::any::Any + Send>)>,
/// Startup tasks registered via on_start() — spawned after build() completes.
/// Stored type-erased (Box<Box<dyn FnOnce(Arc<R>) -> BoxFuture<…>>>) to allow
/// the field to exist on the unparameterised NoRuntime builder too.
start_fns: Vec<Box<dyn core::any::Any + Send>>,
/// Generic extension storage for external crates (e.g., persistence, metrics).
/// Moved into AimDbInner during build() so it can be read on the live AimDb handle.
extensions: Extensions,
/// Remote access configuration (std only)
#[cfg(feature = "std")]
remote_config: Option<crate::remote::AimxConfig>,
/// PhantomData to track the runtime type parameter
_phantom: PhantomData<R>,
}
impl AimDbBuilder<NoRuntime> {
/// Creates a new database builder without a runtime
///
/// Call `.runtime()` to set the runtime adapter.
pub fn new() -> Self {
Self {
records: Vec::new(),
runtime: None,
connector_builders: Vec::new(),
spawn_fns: Vec::new(),
start_fns: Vec::new(),
extensions: Extensions::new(),
#[cfg(feature = "std")]
remote_config: None,
_phantom: PhantomData,
}
}
/// Sets the runtime adapter
///
/// This transitions the builder from untyped to typed with concrete runtime `R`.
///
/// # Type Safety Note
///
/// The `connector_builders` field is intentionally reset to `Vec::new()` during this
/// transition because connectors are parameterized by the runtime type:
///
/// - Before: `Vec<Box<dyn ConnectorBuilder<NoRuntime>>>`
/// - After: `Vec<Box<dyn ConnectorBuilder<R>>>`
///
/// These types are incompatible and cannot be transferred. However, this is not a bug
/// because `.with_connector()` is only available AFTER calling `.runtime()` (it's defined
/// in the `impl<R> where R: RuntimeAdapter` block, not in `impl AimDbBuilder<NoRuntime>`).
///
/// This means the type system **enforces** the correct call order:
/// ```rust,ignore
/// AimDbBuilder::new()
/// .runtime(runtime) // ← Must be called first
/// .with_connector(connector) // ← Now available
/// ```
///
/// The `records` and `remote_config` are preserved across the transition since they
/// are not parameterized by the runtime type.
pub fn runtime<R>(self, rt: Arc<R>) -> AimDbBuilder<R>
where
R: aimdb_executor::RuntimeAdapter + 'static,
{
AimDbBuilder {
records: self.records,
runtime: Some(rt),
connector_builders: Vec::new(),
spawn_fns: Vec::new(),
start_fns: self.start_fns,
extensions: self.extensions,
#[cfg(feature = "std")]
remote_config: self.remote_config,
_phantom: PhantomData,
}
}
}
impl<R> AimDbBuilder<R>
where
R: aimdb_executor::RuntimeAdapter + 'static,
{
/// Returns a shared reference to the extension storage.
///
/// External crates use this to retrieve state stored via `extensions_mut()`.
pub fn extensions(&self) -> &Extensions {
&self.extensions
}
/// Returns a mutable reference to the extension storage.
///
/// Use this inside `AimDbBuilderPersistExt` (or similar) to store typed state
/// that `.persist()` and `AimDbQueryExt` will later retrieve.
pub fn extensions_mut(&mut self) -> &mut Extensions {
&mut self.extensions
}
/// Registers a task to be spawned after `build()` completes.
///
/// The closure receives an `Arc<R>` (the runtime adapter) and must return a
/// future that runs for as long as needed (e.g. an infinite cleanup loop).
/// Tasks are spawned in registration order, after all record tasks and
/// connectors have been started.
///
/// # Example
/// ```rust,ignore
/// builder.on_start(|runtime| async move {
/// loop {
/// do_cleanup().await;
/// runtime.sleep(Duration::from_secs(3600)).await;
/// }
/// });
/// ```
pub fn on_start<F, Fut>(&mut self, f: F) -> &mut Self
where
F: FnOnce(Arc<R>) -> Fut + Send + 'static,
Fut: core::future::Future<Output = ()> + Send + 'static,
{
// Type-erase so the field can be shared with the `NoRuntime` builder struct.
// Uses the module-level `StartFnType<R>` alias — must stay in sync with
// the downcast in `build()`.
let boxed: StartFnType<R> = Box::new(move |runtime| Box::pin(f(runtime)));
self.start_fns.push(Box::new(boxed));
self
}
/// Registers a connector builder that will be invoked during `build()`
///
/// The connector builder will be called after the database is constructed,
/// allowing it to collect routes and initialize the connector properly.
///
/// # Arguments
/// * `builder` - A connector builder that implements `ConnectorBuilder<R>`
///
/// # Example
///
/// ```rust,ignore
/// use aimdb_mqtt_connector::MqttConnector;
///
/// let db = AimDbBuilder::new()
/// .runtime(runtime)
/// .with_connector(MqttConnector::new("mqtt://broker.local:1883"))
/// .configure::<Temperature>(|reg| {
/// reg.link_from("mqtt://commands/temp")...
/// })
/// .build().await?;
/// ```
pub fn with_connector(
mut self,
builder: impl crate::connector::ConnectorBuilder<R> + 'static,
) -> Self {
self.connector_builders.push(Box::new(builder));
self
}
/// Enables remote access via AimX protocol (std only)
///
/// Configures the database to accept remote connections over a Unix domain socket,
/// allowing external clients to introspect records, subscribe to updates, and
/// (optionally) write data.
///
/// The remote access supervisor will be spawned automatically during `build()`.
///
/// # Arguments
/// * `config` - Remote access configuration (socket path, security policy, etc.)
///
/// # Example
///
/// ```rust,ignore
/// use aimdb_core::remote::{AimxConfig, SecurityPolicy};
///
/// let config = AimxConfig::new("/tmp/aimdb.sock")
/// .with_security(SecurityPolicy::read_only());
///
/// let db = AimDbBuilder::new()
/// .runtime(runtime)
/// .with_remote_access(config)
/// .build()?;
/// ```
#[cfg(feature = "std")]
pub fn with_remote_access(mut self, config: crate::remote::AimxConfig) -> Self {
self.remote_config = Some(config);
self
}
/// Configures a record type manually with a unique key
///
/// The key uniquely identifies this record instance. Multiple records of the same
/// type can exist with different keys (e.g., "sensor.temperature.room1" and
/// "sensor.temperature.room2").
///
/// # Arguments
/// * `key` - A unique identifier for this record. Can be a string literal, `StringKey`,
/// or any type implementing `RecordKey` (including user-defined enum keys).
/// * `f` - Configuration closure
///
/// # Example
/// ```rust,ignore
/// // Using string literal
/// builder.configure::<Temperature>("sensor.temp.room1", |reg| { ... });
///
/// // Using compile-time safe enum key
/// builder.configure::<Temperature>(SensorKey::TempRoom1, |reg| { ... });
/// ```
pub fn configure<T>(
&mut self,
key: impl RecordKey,
f: impl for<'a> FnOnce(&'a mut RecordRegistrar<'a, T, R>),
) -> &mut Self
where
T: Send + Sync + 'static + Debug + Clone,
{
// Convert any RecordKey to StringKey for internal storage
let record_key: StringKey = StringKey::from_dynamic(key.as_str());
let type_id = TypeId::of::<T>();
// Find existing record with this key, or create new one
let record_index = self.records.iter().position(|(k, _, _)| k == &record_key);
let (rec, is_new_record) = match record_index {
Some(idx) => {
// Use existing record
let (_, existing_type, record) = &mut self.records[idx];
assert!(
*existing_type == type_id,
"StringKey '{}' already registered with different type",
record_key.as_str()
);
(
record
.as_typed_mut::<T, R>()
.expect("type mismatch in record registry"),
false,
)
}
None => {
// Create new record
self.records
.push((record_key, type_id, Box::new(TypedRecord::<T, R>::new())));
let (_, _, record) = self.records.last_mut().unwrap();
(
record
.as_typed_mut::<T, R>()
.expect("type mismatch in record registry"),
true,
)
}
};
let mut reg = RecordRegistrar {
rec,
connector_builders: &self.connector_builders,
record_key: record_key.as_str().to_string(),
extensions: &self.extensions,
last_stage: None,
};
f(&mut reg);
// Only store spawn function for new records to avoid duplicates
if is_new_record {
let spawn_key = record_key;
let spawn_fn: SpawnFnType<R> =
Box::new(move |runtime: &Arc<R>, db: &Arc<AimDb<R>>, id: RecordId| {
let typed_record = db.inner().get_typed_record_by_id::<T, R>(id)?;
// Resolve the record's key for key-based Producer/Consumer construction.
let key = db
.inner()
.key_for(id)
.map(|k| k.as_str().to_string())
.unwrap_or_else(|| alloc::format!("__record_{}", id.index()));
RecordFutureCollector::<T>::collect_all_futures(
typed_record,
runtime,
db,
key.as_str(),
)
});
// Store the spawn function (type-erased in Box<dyn Any>)
self.spawn_fns.push((spawn_key, Box::new(spawn_fn)));
}
self
}
/// Registers a self-registering record type
///
/// The record type must implement `RecordT<R>`.
///
/// Uses the type name as the default key. For custom keys, use `configure()` directly.
pub fn register_record<T>(&mut self, cfg: &T::Config) -> &mut Self
where
T: RecordT<R>,
{
// Default key is the full type name for backward compatibility
let key = StringKey::new(core::any::type_name::<T>());
self.configure::<T>(key, |reg| T::register(reg, cfg))
}
/// Registers a self-registering record type with a custom key
///
/// The record type must implement `RecordT<R>`.
pub fn register_record_with_key<T>(&mut self, key: impl RecordKey, cfg: &T::Config) -> &mut Self
where
T: RecordT<R>,
{
self.configure::<T>(key, |reg| T::register(reg, cfg))
}
/// Builds the database and drives every collected future to completion.
///
/// Convenience wrapper for the common case: call `build()`, then immediately
/// `runner.run().await`. The database handle is dropped on exit.
///
/// For programmatic access to the handle (manual subscriptions, holding the
/// `AimDb<R>` for other uses), prefer `build()` directly.
///
/// # Returns
/// `DbResult<()>` — Ok once the database starts; the call then blocks until
/// every future the runner is driving has completed (typically forever).
///
/// # Example
///
/// ```rust,ignore
/// #[tokio::main]
/// async fn main() -> DbResult<()> {
/// AimDbBuilder::new()
/// .runtime(Arc::new(TokioAdapter::new()?))
/// .configure::<MyData>(|reg| {
/// reg.with_buffer(BufferCfg::SpmcRing { capacity: 100 })
/// .with_source(my_producer)
/// .with_tap(my_consumer);
/// })
/// .run().await // Runs forever
/// }
/// ```
pub async fn run(self) -> DbResult<()>
where
R: crate::RuntimeForProfiling,
{
#[cfg(feature = "tracing")]
tracing::info!("Building database and spawning background tasks...");
let (_db, runner) = self.build().await?;
#[cfg(feature = "tracing")]
tracing::info!("Database running, runner driving collected futures.");
runner.run().await;
Ok(())
}
/// Builds the database and returns a `(handle, runner)` pair.
///
/// `build()` collects every future the database needs to drive (producer
/// services, consumer taps, transforms with fan-in forwarders, connectors,
/// remote-access supervisor, `on_start` tasks) into the [`AimDbRunner`]
/// returned alongside the clone-able [`AimDb<R>`] handle. **No background
/// work runs until `runner.run().await` is awaited.**
///
/// Use this when you need programmatic access to the handle for manual
/// subscriptions or production. For typical services, use `.run().await`
/// which calls `build()` and `runner.run()` for you.
///
/// **Connector Setup:** Connectors are constructed during `build()` from
/// the `ConnectorBuilder`s previously registered via `.with_connector()`.
/// Their driving futures are appended to the runner.
///
/// # Returns
/// `DbResult<(AimDb<R>, AimDbRunner)>` — the handle (cloneable) and the
/// non-`Clone` runner that owns the collected futures.
///
/// # Example
///
/// ```rust,ignore
/// let (db, runner) = AimDbBuilder::new()
/// .runtime(runtime)
/// .configure::<Temp>("temp", |reg| { /* … */ })
/// .with_connector(mqtt_builder)
/// .build().await?;
///
/// let handle = db.clone(); // clone freely before runner.run()
/// runner.run().await; // drives everything to completion
/// ```
pub async fn build(self) -> DbResult<(AimDb<R>, AimDbRunner)>
where
R: crate::RuntimeForProfiling,
{
// Validate all records
for (key, _, record) in &self.records {
record.validate().map_err(|_msg| {
#[cfg(feature = "std")]
{
DbError::RuntimeError {
message: format!("Record '{}' validation failed: {}", key.as_str(), _msg),
}
}
#[cfg(not(feature = "std"))]
{
// Suppress unused warning for key in no_std
let _ = &key;
DbError::RuntimeError { _message: () }
}
})?;
}
// Ensure runtime is set
let runtime = self.runtime.ok_or({
#[cfg(feature = "std")]
{
DbError::RuntimeError {
message: "runtime not set (use .runtime())".into(),
}
}
#[cfg(not(feature = "std"))]
{
DbError::RuntimeError { _message: () }
}
})?;
// Build the new index structures
let record_count = self.records.len();
let mut storages: Vec<Box<dyn AnyRecord>> = Vec::with_capacity(record_count);
let mut by_key: HashMap<StringKey, RecordId> = HashMap::with_capacity(record_count);
let mut by_type: HashMap<TypeId, Vec<RecordId>> = HashMap::new();
let mut types: Vec<TypeId> = Vec::with_capacity(record_count);
let mut keys: Vec<StringKey> = Vec::with_capacity(record_count);
for (i, (key, type_id, record)) in self.records.into_iter().enumerate() {
let id = RecordId::new(i as u32);
// Check for duplicate keys (should not happen if configure() is used correctly)
if by_key.contains_key(&key) {
#[cfg(feature = "std")]
return Err(DbError::DuplicateRecordKey {
key: key.as_str().to_string(),
});
#[cfg(not(feature = "std"))]
return Err(DbError::DuplicateRecordKey { _key: () });
}
// Build index structures
storages.push(record);
by_key.insert(key, id);
by_type.entry(type_id).or_default().push(id);
types.push(type_id);
keys.push(key);
}
// Build dependency graph from record information
// Collect RecordGraphInfo for each record
let record_infos: Vec<crate::graph::RecordGraphInfo> = storages
.iter()
.enumerate()
.map(|(idx, record)| {
let key = keys[idx].as_str().to_string();
let origin = record.record_origin();
// Get buffer type and capacity from the record
let (buffer_type, buffer_capacity) = record.buffer_info();
crate::graph::RecordGraphInfo {
key,
origin,
buffer_type,
buffer_capacity,
tap_count: record.consumer_count(),
has_outbound_link: record.outbound_connector_count() > 0,
}
})
.collect();
// Build and validate the dependency graph
let dependency_graph = DependencyGraph::build_and_validate(&record_infos)?;
#[cfg(feature = "tracing")]
tracing::debug!(
"Dependency graph built successfully ({} nodes, {} edges, topo order: {:?})",
dependency_graph.nodes.len(),
dependency_graph.edges.len(),
dependency_graph.topo_order
);
let inner = Arc::new(AimDbInner {
storages,
by_key,
by_type,
types,
keys,
dependency_graph,
extensions: self.extensions,
});
#[cfg(feature = "profiling")]
let profiling_clock = crate::profiling::make_clock(runtime.clone());
let db = Arc::new(AimDb {
inner: inner.clone(),
runtime: runtime.clone(),
#[cfg(feature = "profiling")]
profiling_clock,
});
// Accumulator for every future the runner will drive.
let mut futures_acc: Vec<BoxFuture> = Vec::new();
#[cfg(feature = "tracing")]
tracing::info!("Collecting futures for {} records", self.spawn_fns.len());
// Build a lookup map from spawn_fns for topological ordering
let mut spawn_fn_map: HashMap<StringKey, Box<dyn core::any::Any + Send>> =
self.spawn_fns.into_iter().collect();
// Execute collectors in topological order — transforms collect after their inputs.
for key_str in inner.dependency_graph.topo_order() {
let key = match inner.by_key.keys().find(|k| k.as_str() == key_str) {
Some(k) => *k,
None => continue,
};
let spawn_fn_any = match spawn_fn_map.remove(&key) {
Some(f) => f,
None => continue,
};
let id = inner.resolve(&key).ok_or({
#[cfg(feature = "std")]
{
DbError::RecordKeyNotFound {
key: key.as_str().to_string(),
}
}
#[cfg(not(feature = "std"))]
{
DbError::RecordKeyNotFound { _key: () }
}
})?;
let spawn_fn = spawn_fn_any
.downcast::<SpawnFnType<R>>()
.expect("spawn function type mismatch");
futures_acc.extend((*spawn_fn)(&runtime, &db, id)?);
}
#[cfg(feature = "tracing")]
tracing::info!("Record future collection complete");
// Collect the remote-access supervisor future, if configured (std only).
#[cfg(feature = "std")]
if let Some(remote_cfg) = self.remote_config {
#[cfg(feature = "tracing")]
tracing::info!(
"Building remote access supervisor for socket: {}",
remote_cfg.socket_path.display()
);
// Apply security policy to mark writable records
let writable_keys = remote_cfg.security_policy.writable_records();
for key_str in writable_keys {
if let Some(id) = inner.resolve_str(&key_str) {
#[cfg(feature = "tracing")]
tracing::debug!("Marking record '{}' as writable", key_str);
inner.storages[id.index()].set_writable_erased(true);
}
}
let supervisor_future =
crate::remote::supervisor::build_supervisor_future(db.clone(), remote_cfg)?;
futures_acc.push(supervisor_future);
#[cfg(feature = "tracing")]
tracing::info!("Remote access supervisor future collected");
}
// Collect connector futures. After issue #88 connector builders return
// a `Vec<BoxFuture>` instead of an `Arc<dyn Connector>` (which previously
// was discarded anyway — see design doc 028 §"The dropped Arc<dyn Connector> object").
for builder in self.connector_builders {
#[cfg(feature = "tracing")]
let scheme = builder.scheme().to_string();
#[cfg(feature = "tracing")]
tracing::debug!("Building connector for scheme: {}", scheme);
let connector_futures = builder.build(&db).await?;
futures_acc.extend(connector_futures);
#[cfg(feature = "tracing")]
tracing::info!("Connector '{}' contributed {} future(s)", scheme, "n");
}
// Collect on_start futures (registered by external crates like aimdb-persistence).
if !self.start_fns.is_empty() {
#[cfg(feature = "tracing")]
tracing::debug!("Collecting {} on_start future(s)", self.start_fns.len());
for (idx, start_fn_any) in self.start_fns.into_iter().enumerate() {
let start_fn = start_fn_any
.downcast::<StartFnType<R>>()
.unwrap_or_else(|_| {
panic!("on_start fn[{idx}] type mismatch — this is a bug in aimdb-core")
});
futures_acc.push((*start_fn)(runtime.clone()));
}
}
let db_owned = (*db).clone();
Ok((db_owned, AimDbRunner::new(futures_acc)))
}
}
impl Default for AimDbBuilder<NoRuntime> {
fn default() -> Self {
Self::new()
}
}
/// Producer-consumer database
///
/// A database instance with type-safe record registration and cross-record
/// communication via the Emitter pattern. The type parameter `R` represents
/// the runtime adapter (e.g., TokioAdapter, EmbassyAdapter).
///
/// See `examples/` for usage.
///
/// # Examples
///
/// ```rust,ignore
/// use aimdb_tokio_adapter::TokioAdapter;
///
/// let runtime = Arc::new(TokioAdapter);
/// let db: AimDb<TokioAdapter> = AimDbBuilder::new()
/// .runtime(runtime)
/// .register_record::<Temperature>(&TemperatureConfig)
/// .build()?;
/// ```
pub struct AimDb<R: aimdb_executor::RuntimeAdapter + 'static> {
/// Internal state
inner: Arc<AimDbInner>,
/// Runtime adapter with concrete type
runtime: Arc<R>,
/// Shared wall clock for stage profiling, built from the runtime at `build()` time.
#[cfg(feature = "profiling")]
profiling_clock: crate::profiling::Clock,
}
impl<R: aimdb_executor::RuntimeAdapter + 'static> Clone for AimDb<R> {
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
runtime: self.runtime.clone(),
#[cfg(feature = "profiling")]
profiling_clock: self.profiling_clock.clone(),
}
}
}
// ============================================================================
// AimDbRunner — drives every future the builder collected
// ============================================================================
/// Non-`Clone` runner returned alongside [`AimDb<R>`] from
/// [`AimDbBuilder::build()`].
///
/// Owns the complete set of futures that drive the database (producer