uqa_storage/key_value/
notification_records.rs1use crate::{
10 mvcc::{
11 NotificationRecordLayout, SharedRecordValue, VersionError, VersionResult,
12 NOTIFICATION_PUBLICATION_KEY,
13 },
14 notifications::{NotificationPublication, NotificationPublicationView},
15 read_control::StorageReadControl,
16};
17use uqa_core::memory::BudgetedVec;
18
19pub struct KeyValueNotificationRecords;
20
21impl NotificationRecordLayout for KeyValueNotificationRecords {
22 fn key(&self, control: &StorageReadControl) -> VersionResult<BudgetedVec<u8>> {
23 control.check()?;
24 let mut key = BudgetedVec::new(control.memory());
25 key.push(super::TAG_METADATA)?;
26 key.extend_from_slice(
27 &u32::try_from(NOTIFICATION_PUBLICATION_KEY.len())
28 .map_err(|_| VersionError::InvalidEncoding("notification key is too long"))?
29 .to_be_bytes(),
30 )?;
31 key.extend_from_slice(NOTIFICATION_PUBLICATION_KEY.as_bytes())?;
32 Ok(key)
33 }
34
35 fn encode(
36 &self,
37 publication: &NotificationPublication,
38 control: &StorageReadControl,
39 ) -> VersionResult<SharedRecordValue> {
40 control.check()?;
41 Ok(publication.shared_bytes())
42 }
43
44 fn decode<'a>(
45 &self,
46 key: &[u8],
47 record: &'a [u8],
48 control: &StorageReadControl,
49 ) -> VersionResult<NotificationPublicationView<'a>> {
50 if key != self.key(control)?.as_ref() {
51 return Err(VersionError::InvalidEncoding(
52 "invalid notification metadata key",
53 ));
54 }
55 NotificationPublicationView::decode(record, control)
56 }
57}