Skip to main content

qualia_core_db/sensing/acquisition/
events.rs

1use super::*;
2/// Explicit replay record: signed tick i64, channel u32, event count u64 (20 bytes).
3/// This is a local view contract, not an allocated EMF/QAC wire record.
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5pub enum EventOrdering {
6    AcquisitionOrder,
7    NondecreasingTick,
8}
9#[derive(Clone, Copy, Debug, PartialEq, Eq)]
10pub struct Event {
11    pub tick: i64,
12    pub channel: u32,
13    pub count: u64,
14}
15#[derive(Clone, Copy, Debug)]
16pub struct EventView<'a> {
17    region: Region<'a>,
18    records: usize,
19    channels: u32,
20    order: ByteOrder,
21    ordering: EventOrdering,
22    clock: Clock,
23    context: Context,
24}
25impl<'a> EventView<'a> {
26    pub fn admit(
27        region: Region<'a>,
28        channels: u32,
29        order: ByteOrder,
30        ordering: EventOrdering,
31        clock: Clock,
32        context: Context,
33        max_records: usize,
34    ) -> Result<Self, Error> {
35        if channels == 0 || region.bytes().len() % 20 != 0 {
36            return Err(Error::Shape);
37        }
38        let records = region.bytes().len() / 20;
39        if records > max_records {
40            return Err(Error::Budget);
41        }
42        context.validate()?;
43        let view = Self {
44            region,
45            records,
46            channels,
47            order,
48            ordering,
49            clock,
50            context,
51        };
52        let mut previous = None;
53        for i in 0..records {
54            let e = view.event(i)?;
55            if e.channel >= channels {
56                return Err(Error::Bounds);
57            }
58            clock.tick_time(e.tick)?;
59            if ordering == EventOrdering::NondecreasingTick && previous.is_some_and(|x| e.tick < x)
60            {
61                return Err(Error::Ordering);
62            }
63            previous = Some(e.tick);
64        }
65        Ok(view)
66    }
67    pub fn event(self, index: usize) -> Result<Event, Error> {
68        if index >= self.records {
69            return Err(Error::Bounds);
70        }
71        let start = index.checked_mul(20).ok_or(Error::Overflow)?;
72        let b = &self.region.bytes()[start..start + 20];
73        Ok(if self.order == ByteOrder::Little {
74            Event {
75                tick: i64::from_le_bytes(b[..8].try_into().unwrap()),
76                channel: u32::from_le_bytes(b[8..12].try_into().unwrap()),
77                count: u64::from_le_bytes(b[12..20].try_into().unwrap()),
78            }
79        } else {
80            Event {
81                tick: i64::from_be_bytes(b[..8].try_into().unwrap()),
82                channel: u32::from_be_bytes(b[8..12].try_into().unwrap()),
83                count: u64::from_be_bytes(b[12..20].try_into().unwrap()),
84            }
85        })
86    }
87    pub fn time_at(self, index: usize) -> Result<ExactTime, Error> {
88        self.clock.tick_time(self.event(index)?.tick)
89    }
90    pub fn region(self) -> Region<'a> {
91        self.region
92    }
93    pub fn records(self) -> usize {
94        self.records
95    }
96    pub fn channels(self) -> u32 {
97        self.channels
98    }
99    pub fn byte_order(self) -> ByteOrder {
100        self.order
101    }
102    pub fn ordering(self) -> EventOrdering {
103        self.ordering
104    }
105    pub fn context(self) -> Context {
106        self.context
107    }
108    pub fn clock(self) -> Clock {
109        self.clock
110    }
111}