aimdb-core 1.0.1

Type-safe async data pipelines — one Rust codebase from MCU to cloud
Documentation
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
//! Record identification types for stable, O(1) lookups
//!
//! This module provides key types for record identification:
//!
//! - [`RecordKey`]: A **trait** for type-safe record identifiers
//! - [`StringKey`]: Default string-based key implementation
//! - [`RecordId`]: An internal index for O(1) hot-path lookups
//!
//! # Design Rationale
//!
//! AimDB separates *logical identity* (RecordKey) from *physical identity* (RecordId):
//!
//! - **RecordKey** is a trait that can be implemented by user-defined enums for
//!   compile-time checked keys, or use the default `StringKey` for string-based keys.
//!
//! - **RecordId** is the hot-path identifier used internally for O(1) Vec indexing
//!   during produce/consume operations.
//!
//! # Examples
//!
//! ## StringKey (default, edge/cloud)
//!
//! ```rust
//! use aimdb_core::record_id::StringKey;
//!
//! // Static keys (zero allocation, preferred)
//! let key: StringKey = "sensors.temperature".into();
//! assert!(key.is_static());
//!
//! // Dynamic keys (for runtime-generated names)
//! let tenant_id = "acme";
//! let key = StringKey::intern(format!("tenant.{}.sensors", tenant_id));
//! assert!(!key.is_static());
//! ```
//!
//! ## Enum Keys (compile-time safe, embedded)
//!
//! ```rust,ignore
//! use aimdb_derive::RecordKey;
//!
//! #[derive(RecordKey, Clone, Copy, PartialEq, Eq)]
//! pub enum AppKey {
//!     #[key = "temp.indoor"]
//!     TempIndoor,
//!     #[key = "temp.outdoor"]
//!     TempOutdoor,
//! }
//!
//! // Compile-time typo detection!
//! let producer = db.producer::<Temperature>(AppKey::TempIndoor);
//! ```

#[cfg(not(feature = "std"))]
extern crate alloc;

#[cfg(not(feature = "std"))]
use alloc::{boxed::Box, string::ToString};

#[cfg(feature = "std")]
use std::boxed::Box;

#[cfg(all(debug_assertions, feature = "std"))]
use core::sync::atomic::{AtomicUsize, Ordering};

/// Counter for interned keys (debug builds only, std only)
#[cfg(all(debug_assertions, feature = "std"))]
static INTERNED_KEY_COUNT: AtomicUsize = AtomicUsize::new(0);

/// Maximum expected interned keys before warning.
/// If exceeded, a debug assertion fires to catch potential misuse.
#[cfg(all(debug_assertions, feature = "std"))]
const MAX_EXPECTED_INTERNED_KEYS: usize = 1000;

// Re-export derive macro when feature is enabled
#[cfg(feature = "derive")]
pub use aimdb_derive::RecordKey;

// ============================================================================
// RecordKey Trait
// ============================================================================

/// Trait for record key types
///
/// Enables compile-time checked enum keys for embedded while preserving
/// String flexibility for edge/cloud deployments.
///
/// The `Borrow<str>` bound is required for O(1) HashMap lookups by string
/// in the remote access layer (e.g., `hashmap.get("record.name")`).
///
/// # Implementing RecordKey
///
/// The easiest way is to use the derive macro:
///
/// ```rust,ignore
/// #[derive(RecordKey, Clone, Copy, PartialEq, Eq)]
/// pub enum AppKey {
///     #[key = "temp.indoor"]
///     TempIndoor,
///     #[key = "temp.outdoor"]
///     TempOutdoor,
/// }
/// ```
///
/// With connector metadata (MQTT topics, KNX addresses):
///
/// ```rust,ignore
/// #[derive(RecordKey, Clone, Copy, PartialEq, Eq)]
/// pub enum SensorKey {
///     #[key = "temp.indoor"]
///     #[link_address = "mqtt://sensors/temp/indoor"]
///     TempIndoor,
///
///     #[key = "temp.outdoor"]
///     #[link_address = "knx://9/1/0"]
///     TempOutdoor,
/// }
/// ```
///
/// For manual implementation:
///
/// ```rust
/// use aimdb_core::RecordKey;
/// use core::borrow::Borrow;
///
/// #[derive(Clone, Copy, PartialEq, Eq, Hash)]
/// pub enum MyKey {
///     Temperature,
///     Humidity,
/// }
///
/// impl RecordKey for MyKey {
///     fn as_str(&self) -> &'static str {
///         match self {
///             Self::Temperature => "sensor.temp",
///             Self::Humidity => "sensor.humid",
///         }
///     }
/// }
///
/// impl Borrow<str> for MyKey {
///     fn borrow(&self) -> &str {
///         self.as_str()
///     }
/// }
/// ```
///
/// # Hash and Borrow Contract
///
/// The `RecordKey` trait requires `Hash` because keys are stored in HashMaps.
/// Per Rust's `Borrow` trait contract, `hash(key) == hash(key.borrow())`—meaning
/// your `Hash` implementation must hash the same value as `as_str()` returns.
///
/// **Using the derive macro:** `#[derive(RecordKey)]` automatically generates
/// both `Hash` and `Borrow<str>` implementations that satisfy this contract.
/// Do **not** also derive `Hash` manually—it would conflict.
///
/// **Manual implementation:** Implement `Hash` by hashing `self.as_str()`:
///
/// ```rust,ignore
/// impl Hash for MyKey {
///     fn hash<H: Hasher>(&self, state: &mut H) {
///         self.as_str().hash(state);
///     }
/// }
/// ```
pub trait RecordKey:
    Clone + Eq + core::hash::Hash + core::borrow::Borrow<str> + Send + Sync + 'static
{
    /// String representation for connectors, logging, serialization, remote access
    fn as_str(&self) -> &'static str;

    /// Connector address for this key
    ///
    /// Returns the URL/address to use with connectors (MQTT topics, KNX addresses, etc.).
    /// Use with `.link_to()` for outbound or `.link_from()` for inbound connections.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// #[derive(RecordKey)]
    /// pub enum SensorKey {
    ///     #[key = "temp.indoor"]
    ///     #[link_address = "mqtt://sensors/temp/indoor"]
    ///     TempIndoor,
    /// }
    ///
    /// // Use with link_to for outbound
    /// reg.link_to(SensorKey::TempIndoor.link_address().unwrap())
    ///
    /// // Or with link_from for inbound
    /// reg.link_from(SensorKey::TempIndoor.link_address().unwrap())
    /// ```
    #[inline]
    fn link_address(&self) -> Option<&str> {
        None
    }
}

// Blanket implementation for &'static str
impl RecordKey for &'static str {
    #[inline]
    fn as_str(&self) -> &'static str {
        self
    }
}

// ============================================================================
// StringKey - Default Implementation
// ============================================================================

/// Default string-based record key
///
/// Supports both static (zero-cost) and interned (leaked) names.
/// Use string literals for the common case; they auto-convert via `From`.
///
/// # Memory Model
///
/// Dynamic keys are "interned" by leaking memory into `'static` lifetime.
/// This is optimal for AimDB's use case where keys are:
/// - Registered once at startup
/// - Never deallocated during runtime
/// - Frequently cloned (now just pointer copy)
///
/// The tradeoff: memory for dynamic keys is never freed. This is acceptable
/// because typical deployments have <100 keys totaling <4KB.
///
/// # Naming Convention
///
/// Recommended format: `<namespace>.<category>.<instance>`
///
/// Examples:
/// - `sensors.temperature.outdoor`
/// - `sensors.temperature.indoor`
/// - `mesh.weather.sf-bay`
/// - `config.app.settings`
/// - `tenant.acme.sensors.temp`
///
/// # Examples
///
/// ```rust
/// use aimdb_core::record_id::StringKey;
///
/// // Static (preferred) - zero allocation
/// let key: StringKey = "sensors.temperature".into();
///
/// // Dynamic - for runtime-generated names
/// let key = StringKey::intern(format!("tenant.{}.sensors", "acme"));
/// ```
#[derive(Clone, Copy)]
pub struct StringKey(StringKeyInner);

#[derive(Clone, Copy)]
enum StringKeyInner {
    /// Static string literal (zero allocation)
    Static(&'static str),
    /// Interned runtime string (leaked into 'static lifetime)
    ///
    /// Memory is intentionally leaked for O(1) cloning and comparison.
    /// This is safe because keys are registered once at startup.
    Interned(&'static str),
}

impl StringKey {
    /// Create from a static string literal
    ///
    /// This is a const fn, usable in const contexts.
    #[inline]
    #[must_use]
    pub const fn new(s: &'static str) -> Self {
        Self(StringKeyInner::Static(s))
    }

    /// Create from a runtime-generated string
    ///
    /// Use this for dynamic names (multi-tenant, config-driven, etc.).
    ///
    /// # Memory
    ///
    /// The string is leaked into `'static` lifetime. This is intentional:
    /// - Keys are registered once at startup
    /// - Enables O(1) Copy/Clone
    /// - Typical overhead: <4KB for 100 keys
    ///
    /// # Panics (debug builds only)
    ///
    /// In debug builds with `std` feature, panics if more than 1000 keys are
    /// interned. This catches accidental misuse (e.g., creating keys in a loop).
    /// Production builds have no limit.
    #[inline]
    #[must_use]
    pub fn intern(s: impl AsRef<str>) -> Self {
        #[cfg(all(debug_assertions, feature = "std"))]
        {
            let count = INTERNED_KEY_COUNT.fetch_add(1, Ordering::Relaxed);
            debug_assert!(
                count < MAX_EXPECTED_INTERNED_KEYS,
                "StringKey::intern() called {} times. This exceeds the expected limit of {}. \
                 Interned keys leak memory and should only be created at startup. \
                 Use static string literals or enum keys for better performance.",
                count + 1,
                MAX_EXPECTED_INTERNED_KEYS
            );
        }
        let leaked: &'static str = Box::leak(s.as_ref().to_string().into_boxed_str());
        Self(StringKeyInner::Interned(leaked))
    }

    /// Create from a runtime string (alias for `intern`)
    ///
    /// The string is leaked into `'static` lifetime for O(1) cloning.
    #[inline]
    #[must_use]
    pub fn from_dynamic(s: &str) -> Self {
        Self::intern(s)
    }

    /// Returns true if this is a static (compile-time) key
    #[inline]
    pub fn is_static(&self) -> bool {
        matches!(self.0, StringKeyInner::Static(_))
    }

    /// Returns true if this is an interned (runtime) key
    #[inline]
    pub fn is_interned(&self) -> bool {
        matches!(self.0, StringKeyInner::Interned(_))
    }
}

// Implement RecordKey trait for StringKey
impl RecordKey for StringKey {
    #[inline]
    fn as_str(&self) -> &'static str {
        match self.0 {
            StringKeyInner::Static(s) => s,
            StringKeyInner::Interned(s) => s,
        }
    }
}

// Custom Debug to show Static vs Interned variant
impl core::fmt::Debug for StringKey {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self.0 {
            StringKeyInner::Static(s) => f.debug_tuple("StringKey::Static").field(&s).finish(),
            StringKeyInner::Interned(s) => f.debug_tuple("StringKey::Interned").field(&s).finish(),
        }
    }
}

impl core::hash::Hash for StringKey {
    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
        // Hash the string content, not the enum variant
        self.as_str().hash(state);
    }
}

impl PartialEq for StringKey {
    fn eq(&self, other: &Self) -> bool {
        self.as_str() == other.as_str()
    }
}

impl Eq for StringKey {}

/// Enable direct comparison with &str
impl PartialEq<str> for StringKey {
    fn eq(&self, other: &str) -> bool {
        self.as_str() == other
    }
}

/// Enable direct comparison with &str reference
impl PartialEq<&str> for StringKey {
    fn eq(&self, other: &&str) -> bool {
        self.as_str() == *other
    }
}

impl PartialOrd for StringKey {
    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for StringKey {
    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
        self.as_str().cmp(other.as_str())
    }
}

/// Ergonomic conversion from string literals
impl From<&'static str> for StringKey {
    #[inline]
    fn from(s: &'static str) -> Self {
        Self::new(s)
    }
}

/// Ergonomic conversion from owned String (no_std with alloc)
#[cfg(all(feature = "alloc", not(feature = "std")))]
impl From<alloc::string::String> for StringKey {
    #[inline]
    fn from(s: alloc::string::String) -> Self {
        Self::intern(s)
    }
}

/// Ergonomic conversion from owned String (std)
#[cfg(feature = "std")]
impl From<String> for StringKey {
    #[inline]
    fn from(s: String) -> Self {
        Self::intern(s)
    }
}

impl core::fmt::Display for StringKey {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.write_str(self.as_str())
    }
}

impl AsRef<str> for StringKey {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

/// Enable O(1) HashMap lookup by &str
///
/// This allows `hashmap.get("string_literal")` without allocating a StringKey.
impl core::borrow::Borrow<str> for StringKey {
    fn borrow(&self) -> &str {
        self.as_str()
    }
}

// ===== Serde Support (std only) =====

#[cfg(feature = "std")]
impl serde::Serialize for StringKey {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_str(self.as_str())
    }
}

#[cfg(feature = "std")]
impl<'de> serde::Deserialize<'de> for StringKey {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let s = String::deserialize(deserializer)?;
        Ok(Self::intern(s))
    }
}

// ============================================================================
// RecordId - Internal Index
// ============================================================================

/// Internal record identifier (index into storage Vec)
///
/// This is the hot-path identifier used for O(1) lookups during
/// produce/consume operations. Not exposed to external APIs.
///
/// # Why u32?
///
/// - 4 billion records is more than enough for any deployment
/// - Smaller than `usize` on 64-bit systems (cache efficiency)
/// - Copy-friendly (no clone overhead)
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct RecordId(pub(crate) u32);

impl RecordId {
    /// Create a new RecordId from an index
    ///
    /// # Arguments
    /// * `index` - The index into the record storage array
    #[inline]
    pub const fn new(index: u32) -> Self {
        Self(index)
    }

    /// Get the underlying index
    #[inline]
    pub const fn index(self) -> usize {
        self.0 as usize
    }

    /// Get the raw u32 value (for serialization)
    #[inline]
    pub const fn raw(self) -> u32 {
        self.0
    }
}

impl core::fmt::Display for RecordId {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "RecordId({})", self.0)
    }
}

#[cfg(feature = "std")]
impl serde::Serialize for RecordId {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_u32(self.0)
    }
}

#[cfg(feature = "std")]
impl<'de> serde::Deserialize<'de> for RecordId {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let index = u32::deserialize(deserializer)?;
        Ok(Self(index))
    }
}

// ===== Unit Tests (std only - uses String, HashMap, format!) =====

#[cfg(all(test, feature = "std"))]
mod tests {
    use super::*;

    #[test]
    fn test_string_key_static() {
        let key: StringKey = "sensors.temperature".into();
        assert!(key.is_static());
        assert_eq!(key.as_str(), "sensors.temperature");
    }

    #[test]
    fn test_string_key_interned() {
        // Concatenation creates a truly dynamic String
        let key = StringKey::intern(["sensors", ".", "temperature"].concat());
        assert!(!key.is_static());
        assert!(key.is_interned());
        assert_eq!(key.as_str(), "sensors.temperature");
    }

    #[test]
    fn test_string_key_equality() {
        let static_key: StringKey = "sensors.temp".into();
        // Concatenation creates a truly dynamic String
        let interned_key = StringKey::intern(["sensors", ".", "temp"].concat());

        // Static and interned keys with same content should be equal
        assert_eq!(static_key, interned_key);
    }

    #[test]
    fn test_string_key_hash_consistency() {
        use core::hash::{Hash, Hasher};
        use std::collections::hash_map::DefaultHasher;

        fn hash_key(key: &StringKey) -> u64 {
            let mut hasher = DefaultHasher::new();
            key.hash(&mut hasher);
            hasher.finish()
        }

        let static_key: StringKey = "sensors.temp".into();
        // Concatenation creates a truly dynamic String
        let interned_key = StringKey::intern(["sensors", ".", "temp"].concat());

        // Hash should be the same for equal keys
        assert_eq!(hash_key(&static_key), hash_key(&interned_key));
    }

    #[test]
    fn test_string_key_borrow() {
        use std::collections::HashMap;

        let mut map: HashMap<StringKey, i32> = HashMap::new();
        map.insert("sensors.temp".into(), 42);

        // Can lookup by &str without allocation
        assert_eq!(map.get("sensors.temp"), Some(&42));
    }

    #[test]
    fn test_record_id_basic() {
        let id = RecordId::new(42);
        assert_eq!(id.index(), 42);
        assert_eq!(id.raw(), 42);
    }

    #[test]
    fn test_record_id_copy() {
        let id1 = RecordId::new(10);
        let id2 = id1; // Copy, not move
        assert_eq!(id1, id2);
    }

    #[test]
    fn test_string_key_display() {
        let key: StringKey = "sensors.temperature".into();
        assert_eq!(format!("{}", key), "sensors.temperature");
    }

    #[test]
    fn test_string_key_debug() {
        let static_key: StringKey = "sensors.temp".into();
        // Concatenation creates a truly dynamic String
        let interned_key = StringKey::intern(["sensors", ".", "temp"].concat());

        // Debug output should distinguish static vs interned
        let static_debug = format!("{:?}", static_key);
        let interned_debug = format!("{:?}", interned_key);

        assert!(static_debug.contains("Static"));
        assert!(interned_debug.contains("Interned"));
    }

    #[test]
    fn test_string_key_partial_eq_str() {
        let key: StringKey = "sensors.temperature".into();

        // Direct comparison with &str
        assert!(key == "sensors.temperature");
        assert!(key != "other.key");

        // Also works with str reference
        let s: &str = "sensors.temperature";
        assert!(key == s);
    }

    #[test]
    fn test_string_key_from_string() {
        let owned = "sensors.temperature".to_string();
        let key: StringKey = owned.into();

        assert!(!key.is_static());
        assert_eq!(key.as_str(), "sensors.temperature");
    }

    #[test]
    fn test_record_id_display() {
        let id = RecordId::new(42);
        assert_eq!(format!("{}", id), "RecordId(42)");
    }

    #[test]
    fn test_static_str_record_key() {
        // &'static str implements RecordKey
        let key: &'static str = "sensors.temp";
        assert_eq!(<&str as RecordKey>::as_str(&key), "sensors.temp");
    }

    #[test]
    fn test_string_key_record_key_trait() {
        use crate::RecordKey;

        let key: StringKey = "sensors.temp".into();
        assert_eq!(RecordKey::as_str(&key), "sensors.temp");
    }

    /// Test that hash(key) == hash(key.borrow()) per Rust's Borrow trait contract.
    ///
    /// This is critical for HashMap operations: if Borrow<str> is implemented,
    /// the hash of the key must match the hash of its borrowed string form.
    #[test]
    fn test_string_key_hash_borrow_contract() {
        use core::borrow::Borrow;
        use core::hash::{Hash, Hasher};
        use std::collections::hash_map::DefaultHasher;

        fn hash_value<T: Hash>(t: &T) -> u64 {
            let mut h = DefaultHasher::new();
            t.hash(&mut h);
            h.finish()
        }

        // Test static key
        let static_key: StringKey = "sensors.temp".into();
        let borrowed: &str = static_key.borrow();
        assert_eq!(
            hash_value(&static_key),
            hash_value(&borrowed),
            "Static StringKey hash must match its borrowed string hash"
        );

        // Test interned key
        let interned_key = StringKey::intern(["sensors", ".", "temp"].concat());
        let borrowed: &str = interned_key.borrow();
        assert_eq!(
            hash_value(&interned_key),
            hash_value(&borrowed),
            "Interned StringKey hash must match its borrowed string hash"
        );

        // Test that HashMap lookup by &str works (practical consequence)
        use std::collections::HashMap;
        let mut map: HashMap<StringKey, i32> = HashMap::new();
        map.insert("key.one".into(), 1);
        map.insert(StringKey::intern("key.two"), 2);

        assert_eq!(map.get("key.one"), Some(&1));
        assert_eq!(map.get("key.two"), Some(&2));
        assert_eq!(map.get("key.nonexistent"), None);
    }
}