diff --git a/src/service/rooms/short/mod.rs b/src/service/rooms/short/mod.rs index 62011605..a903ef22 100644 --- a/src/service/rooms/short/mod.rs +++ b/src/service/rooms/short/mod.rs @@ -24,6 +24,7 @@ struct Services { globals: Dep, } +pub type ShortStateHash = ShortId; pub type ShortStateKey = ShortId; pub type ShortEventId = ShortId; pub type ShortRoomId = ShortId; @@ -50,7 +51,7 @@ impl crate::Service for Service { } #[implement(Service)] -pub async fn get_or_create_shorteventid(&self, event_id: &EventId) -> u64 { +pub async fn get_or_create_shorteventid(&self, event_id: &EventId) -> ShortEventId { const BUFSIZE: usize = size_of::(); if let Ok(shorteventid) = self @@ -78,7 +79,7 @@ pub async fn get_or_create_shorteventid(&self, event_id: &EventId) -> u64 { } #[implement(Service)] -pub async fn multi_get_or_create_shorteventid(&self, event_ids: &[&EventId]) -> Vec { +pub async fn multi_get_or_create_shorteventid(&self, event_ids: &[&EventId]) -> Vec { self.db .eventid_shorteventid .get_batch_blocking(event_ids.iter()) @@ -106,7 +107,7 @@ pub async fn multi_get_or_create_shorteventid(&self, event_ids: &[&EventId]) -> } #[implement(Service)] -pub async fn get_shortstatekey(&self, event_type: &StateEventType, state_key: &str) -> Result { +pub async fn get_shortstatekey(&self, event_type: &StateEventType, state_key: &str) -> Result { let key = (event_type, state_key); self.db .statekey_shortstatekey @@ -116,8 +117,8 @@ pub async fn get_shortstatekey(&self, event_type: &StateEventType, state_key: &s } #[implement(Service)] -pub async fn get_or_create_shortstatekey(&self, event_type: &StateEventType, state_key: &str) -> u64 { - const BUFSIZE: usize = size_of::(); +pub async fn get_or_create_shortstatekey(&self, event_type: &StateEventType, state_key: &str) -> ShortStateKey { + const BUFSIZE: usize = size_of::(); let key = (event_type, state_key); if let Ok(shortstatekey) = self @@ -145,8 +146,8 @@ pub async fn get_or_create_shortstatekey(&self, event_type: &StateEventType, sta } #[implement(Service)] -pub async fn get_eventid_from_short(&self, shorteventid: u64) -> Result> { - const BUFSIZE: usize = size_of::(); +pub async fn get_eventid_from_short(&self, shorteventid: ShortEventId) -> Result> { + const BUFSIZE: usize = size_of::(); self.db .shorteventid_eventid @@ -157,8 +158,8 @@ pub async fn get_eventid_from_short(&self, shorteventid: u64) -> Result Vec>> { - const BUFSIZE: usize = size_of::(); +pub async fn multi_get_eventid_from_short(&self, shorteventid: &[ShortEventId]) -> Vec>> { + const BUFSIZE: usize = size_of::(); let keys: Vec<[u8; BUFSIZE]> = shorteventid .iter() @@ -174,8 +175,8 @@ pub async fn multi_get_eventid_from_short(&self, shorteventid: &[u64]) -> Vec Result<(StateEventType, String)> { - const BUFSIZE: usize = size_of::(); +pub async fn get_statekey_from_short(&self, shortstatekey: ShortStateKey) -> Result<(StateEventType, String)> { + const BUFSIZE: usize = size_of::(); self.db .shortstatekey_statekey @@ -191,8 +192,8 @@ pub async fn get_statekey_from_short(&self, shortstatekey: u64) -> Result<(State /// Returns (shortstatehash, already_existed) #[implement(Service)] -pub async fn get_or_create_shortstatehash(&self, state_hash: &[u8]) -> (u64, bool) { - const BUFSIZE: usize = size_of::(); +pub async fn get_or_create_shortstatehash(&self, state_hash: &[u8]) -> (ShortStateHash, bool) { + const BUFSIZE: usize = size_of::(); if let Ok(shortstatehash) = self .db @@ -215,19 +216,19 @@ pub async fn get_or_create_shortstatehash(&self, state_hash: &[u8]) -> (u64, boo } #[implement(Service)] -pub async fn get_shortroomid(&self, room_id: &RoomId) -> Result { +pub async fn get_shortroomid(&self, room_id: &RoomId) -> Result { self.db.roomid_shortroomid.get(room_id).await.deserialized() } #[implement(Service)] -pub async fn get_or_create_shortroomid(&self, room_id: &RoomId) -> u64 { +pub async fn get_or_create_shortroomid(&self, room_id: &RoomId) -> ShortRoomId { self.db .roomid_shortroomid .get(room_id) .await .deserialized() .unwrap_or_else(|_| { - const BUFSIZE: usize = size_of::(); + const BUFSIZE: usize = size_of::(); let short = self.services.globals.next_count().unwrap(); debug_assert!(size_of_val(&short) == BUFSIZE, "buffer requirement changed"); diff --git a/src/service/rooms/state_accessor/data.rs b/src/service/rooms/state_accessor/data.rs index f77a6d80..9c96785f 100644 --- a/src/service/rooms/state_accessor/data.rs +++ b/src/service/rooms/state_accessor/data.rs @@ -5,7 +5,7 @@ use database::{Deserialized, Map}; use futures::TryFutureExt; use ruma::{events::StateEventType, EventId, RoomId}; -use crate::{rooms, Dep}; +use crate::{rooms, rooms::short::ShortStateHash, Dep}; pub(super) struct Data { eventid_shorteventid: Arc, @@ -36,7 +36,7 @@ impl Data { } #[allow(unused_qualifications)] // async traits - pub(super) async fn state_full_ids(&self, shortstatehash: u64) -> Result>> { + pub(super) async fn state_full_ids(&self, shortstatehash: ShortStateHash) -> Result>> { let full_state = self .services .state_compressor @@ -69,7 +69,7 @@ impl Data { #[allow(unused_qualifications)] // async traits pub(super) async fn state_full( - &self, shortstatehash: u64, + &self, shortstatehash: ShortStateHash, ) -> Result>> { let full_state = self .services @@ -107,7 +107,7 @@ impl Data { /// Returns a single PDU from `room_id` with key (`event_type`,`state_key`). #[allow(clippy::unused_self)] pub(super) async fn state_get_id( - &self, shortstatehash: u64, event_type: &StateEventType, state_key: &str, + &self, shortstatehash: ShortStateHash, event_type: &StateEventType, state_key: &str, ) -> Result> { let shortstatekey = self .services @@ -147,7 +147,7 @@ impl Data { /// Returns a single PDU from `room_id` with key (`event_type`,`state_key`). pub(super) async fn state_get( - &self, shortstatehash: u64, event_type: &StateEventType, state_key: &str, + &self, shortstatehash: ShortStateHash, event_type: &StateEventType, state_key: &str, ) -> Result> { self.state_get_id(shortstatehash, event_type, state_key) .and_then(|event_id| async move { self.services.timeline.get_pdu(&event_id).await }) @@ -155,7 +155,7 @@ impl Data { } /// Returns the state hash for this pdu. - pub(super) async fn pdu_shortstatehash(&self, event_id: &EventId) -> Result { + pub(super) async fn pdu_shortstatehash(&self, event_id: &EventId) -> Result { self.eventid_shorteventid .get(event_id) .and_then(|shorteventid| self.shorteventid_shortstatehash.get(&shorteventid)) diff --git a/src/service/rooms/state_compressor/mod.rs b/src/service/rooms/state_compressor/mod.rs index 1f351f40..e213490b 100644 --- a/src/service/rooms/state_compressor/mod.rs +++ b/src/service/rooms/state_compressor/mod.rs @@ -10,7 +10,11 @@ use database::Map; use lru_cache::LruCache; use ruma::{EventId, RoomId}; -use crate::{rooms, rooms::short::ShortId, Dep}; +use crate::{ + rooms, + rooms::short::{ShortStateHash, ShortStateKey}, + Dep, +}; pub struct Service { pub stateinfo_cache: Mutex, @@ -49,9 +53,8 @@ pub struct HashSetCompressStateEvent { pub removed: Arc>, } -pub type ShortStateHash = ShortId; pub(crate) type CompressedStateEvent = [u8; 2 * size_of::()]; -type StateInfoLruCache = LruCache; +type StateInfoLruCache = LruCache; type ShortStateInfoVec = Vec; type ParentStatesVec = Vec; @@ -86,7 +89,7 @@ impl crate::Service for Service { impl Service { /// Returns a stack with info on shortstatehash, full state, added diff and /// removed diff for the selected shortstatehash and each parent layer. - pub async fn load_shortstatehash_info(&self, shortstatehash: u64) -> Result { + pub async fn load_shortstatehash_info(&self, shortstatehash: ShortStateHash) -> Result { if let Some(r) = self .stateinfo_cache .lock() @@ -141,7 +144,7 @@ impl Service { } } - pub async fn compress_state_event(&self, shortstatekey: u64, event_id: &EventId) -> CompressedStateEvent { + pub async fn compress_state_event(&self, shortstatekey: ShortStateKey, event_id: &EventId) -> CompressedStateEvent { let mut v = shortstatekey.to_be_bytes().to_vec(); v.extend_from_slice( &self @@ -159,7 +162,7 @@ impl Service { #[inline] pub async fn parse_compressed_state_event( &self, compressed_event: &CompressedStateEvent, - ) -> Result<(u64, Arc)> { + ) -> Result<(ShortStateKey, Arc)> { use utils::u64_from_u8; let shortstatekey = u64_from_u8(&compressed_event[0..size_of::()]); @@ -192,7 +195,7 @@ impl Service { /// added diff and removed diff for each parent layer #[tracing::instrument(skip_all, level = "debug")] pub fn save_state_from_diff( - &self, shortstatehash: u64, statediffnew: Arc>, + &self, shortstatehash: ShortStateHash, statediffnew: Arc>, statediffremoved: Arc>, diff_to_sibling: usize, mut parent_states: ParentStatesVec, ) -> Result { @@ -377,9 +380,9 @@ impl Service { }) } - async fn get_statediff(&self, shortstatehash: u64) -> Result { - const BUFSIZE: usize = size_of::(); - const STRIDE: usize = size_of::(); + async fn get_statediff(&self, shortstatehash: ShortStateHash) -> Result { + const BUFSIZE: usize = size_of::(); + const STRIDE: usize = size_of::(); let value = self .db @@ -418,7 +421,7 @@ impl Service { }) } - fn save_statediff(&self, shortstatehash: u64, diff: &StateDiff) { + fn save_statediff(&self, shortstatehash: ShortStateHash, diff: &StateDiff) { let mut value = diff.parent.unwrap_or(0).to_be_bytes().to_vec(); for new in diff.added.iter() { value.extend_from_slice(&new[..]);