Add PduId wrapper struct

Death to Vec<u8>
This commit is contained in:
Lambda 2024-08-26 16:47:50 +00:00
parent 341f4213d0
commit 26322d5a95
15 changed files with 110 additions and 71 deletions

View file

@ -44,6 +44,23 @@ use crate::{
Error, PduEvent, Result,
};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub(crate) struct PduId {
inner: Vec<u8>,
}
impl PduId {
pub(crate) fn new(inner: Vec<u8>) -> Self {
Self {
inner,
}
}
pub(crate) fn as_bytes(&self) -> &[u8] {
&self.inner
}
}
#[derive(Hash, PartialEq, Eq, Clone, Copy, Debug)]
pub(crate) enum PduCount {
Backfilled(u64),
@ -146,7 +163,7 @@ impl Service {
pub(crate) fn get_pdu_id(
&self,
event_id: &EventId,
) -> Result<Option<Vec<u8>>> {
) -> Result<Option<PduId>> {
self.db.get_pdu_id(event_id)
}
@ -165,7 +182,7 @@ impl Service {
/// This does __NOT__ check the outliers `Tree`.
pub(crate) fn get_pdu_from_id(
&self,
pdu_id: &[u8],
pdu_id: &PduId,
) -> Result<Option<PduEvent>> {
self.db.get_pdu_from_id(pdu_id)
}
@ -173,7 +190,7 @@ impl Service {
/// Returns the pdu as a `BTreeMap<String, CanonicalJsonValue>`.
pub(crate) fn get_pdu_json_from_id(
&self,
pdu_id: &[u8],
pdu_id: &PduId,
) -> Result<Option<CanonicalJsonObject>> {
self.db.get_pdu_json_from_id(pdu_id)
}
@ -182,7 +199,7 @@ impl Service {
#[tracing::instrument(skip(self))]
pub(crate) fn replace_pdu(
&self,
pdu_id: &[u8],
pdu_id: &PduId,
pdu_json: &CanonicalJsonObject,
pdu: &PduEvent,
) -> Result<()> {
@ -202,7 +219,7 @@ impl Service {
mut pdu_json: CanonicalJsonObject,
leaves: Vec<OwnedEventId>,
room_id: &KeyToken<OwnedRoomId, marker::State>,
) -> Result<Vec<u8>> {
) -> Result<PduId> {
assert_eq!(*pdu.room_id, **room_id, "Token for incorrect room passed");
let shortroomid = services()
@ -282,6 +299,7 @@ impl Service {
let count2 = services().globals.next_count()?;
let mut pdu_id = shortroomid.to_be_bytes().to_vec();
pdu_id.extend_from_slice(&count2.to_be_bytes());
let pdu_id = PduId::new(pdu_id);
// Insert pdu
self.db.append_pdu(&pdu_id, pdu, &pdu_json, count2)?;
@ -1106,7 +1124,7 @@ impl Service {
state_ids_compressed: Arc<HashSet<CompressedStateEvent>>,
soft_fail: bool,
room_id: &KeyToken<OwnedRoomId, marker::State>,
) -> Result<Option<Vec<u8>>> {
) -> Result<Option<PduId>> {
assert_eq!(*pdu.room_id, **room_id, "Token for incorrect room passed");
// We append to state before appending the pdu, so we don't have a
@ -1344,6 +1362,7 @@ impl Service {
let mut pdu_id = shortroomid.to_be_bytes().to_vec();
pdu_id.extend_from_slice(&0_u64.to_be_bytes());
pdu_id.extend_from_slice(&(u64::MAX - count).to_be_bytes());
let pdu_id = PduId::new(pdu_id);
// Insert pdu
self.db.prepend_backfill_pdu(&pdu_id, &event_id, &value)?;