Add wrapper types for short IDs

This commit is contained in:
Lambda 2024-08-27 14:27:12 +00:00
parent f1642c92d1
commit b0f33207fe
28 changed files with 427 additions and 232 deletions

View file

@ -18,7 +18,7 @@ use ruma::{
use serde::Deserialize;
use tracing::warn;
use super::state_compressor::CompressedStateEvent;
use super::{short::ShortStateHash, state_compressor::CompressedStateEvent};
use crate::{
service::globals::marker,
services,
@ -38,7 +38,7 @@ impl Service {
pub(crate) async fn force_state(
&self,
room_id: &KeyToken<OwnedRoomId, marker::State>,
shortstatehash: u64,
shortstatehash: ShortStateHash,
statediffnew: Arc<HashSet<CompressedStateEvent>>,
_statediffremoved: Arc<HashSet<CompressedStateEvent>>,
) -> Result<()> {
@ -126,15 +126,16 @@ impl Service {
event_id: &EventId,
room_id: &RoomId,
state_ids_compressed: Arc<HashSet<CompressedStateEvent>>,
) -> Result<u64> {
) -> Result<ShortStateHash> {
let shorteventid =
services().rooms.short.get_or_create_shorteventid(event_id)?;
let previous_shortstatehash =
self.db.get_room_shortstatehash(room_id)?;
let state_hash =
calculate_hash(state_ids_compressed.iter().map(|s| &s[..]));
let state_hash = calculate_hash(
state_ids_compressed.iter().map(CompressedStateEvent::as_bytes),
);
let (shortstatehash, already_existed) =
services().rooms.short.get_or_create_shortstatehash(&state_hash)?;
@ -187,7 +188,10 @@ impl Service {
/// This adds all current state events (not including the incoming event)
/// to `stateid_pduid` and adds the incoming event to `eventid_statehash`.
#[tracing::instrument(skip(self, new_pdu))]
pub(crate) fn append_to_state(&self, new_pdu: &PduEvent) -> Result<u64> {
pub(crate) fn append_to_state(
&self,
new_pdu: &PduEvent,
) -> Result<ShortStateHash> {
let shorteventid = services()
.rooms
.short
@ -225,9 +229,9 @@ impl Service {
let replaces = states_parents
.last()
.map(|info| {
info.full_state.iter().find(|bytes| {
bytes.starts_with(&shortstatekey.to_be_bytes())
})
info.full_state
.iter()
.find(|compressed| compressed.state == shortstatekey)
})
.unwrap_or_default();
@ -236,7 +240,8 @@ impl Service {
}
// TODO: statehash with deterministic inputs
let shortstatehash = services().globals.next_count()?;
let shortstatehash =
ShortStateHash::new(services().globals.next_count()?);
let mut statediffnew = HashSet::new();
statediffnew.insert(new);
@ -320,7 +325,7 @@ impl Service {
pub(crate) fn set_room_state(
&self,
room_id: &KeyToken<OwnedRoomId, marker::State>,
shortstatehash: u64,
shortstatehash: ShortStateHash,
) -> Result<()> {
self.db.set_room_state(room_id, shortstatehash)
}
@ -362,7 +367,7 @@ impl Service {
pub(crate) fn get_room_shortstatehash(
&self,
room_id: &RoomId,
) -> Result<Option<u64>> {
) -> Result<Option<ShortStateHash>> {
self.db.get_room_shortstatehash(room_id)
}