typename some loose u64 ShortId's

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-10-31 08:41:43 +00:00
parent 0bc6fdd589
commit f746be82c1
3 changed files with 37 additions and 33 deletions

View file

@ -24,6 +24,7 @@ struct Services {
globals: Dep<globals::Service>,
}
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::<u64>();
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<u64> {
pub async fn multi_get_or_create_shorteventid(&self, event_ids: &[&EventId]) -> Vec<ShortEventId> {
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<u64> {
pub async fn get_shortstatekey(&self, event_type: &StateEventType, state_key: &str) -> Result<ShortStateKey> {
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::<u64>();
pub async fn get_or_create_shortstatekey(&self, event_type: &StateEventType, state_key: &str) -> ShortStateKey {
const BUFSIZE: usize = size_of::<ShortStateKey>();
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<Arc<EventId>> {
const BUFSIZE: usize = size_of::<u64>();
pub async fn get_eventid_from_short(&self, shorteventid: ShortEventId) -> Result<Arc<EventId>> {
const BUFSIZE: usize = size_of::<ShortEventId>();
self.db
.shorteventid_eventid
@ -157,8 +158,8 @@ pub async fn get_eventid_from_short(&self, shorteventid: u64) -> Result<Arc<Even
}
#[implement(Service)]
pub async fn multi_get_eventid_from_short(&self, shorteventid: &[u64]) -> Vec<Result<Arc<EventId>>> {
const BUFSIZE: usize = size_of::<u64>();
pub async fn multi_get_eventid_from_short(&self, shorteventid: &[ShortEventId]) -> Vec<Result<Arc<EventId>>> {
const BUFSIZE: usize = size_of::<ShortEventId>();
let keys: Vec<[u8; BUFSIZE]> = shorteventid
.iter()
@ -174,8 +175,8 @@ pub async fn multi_get_eventid_from_short(&self, shorteventid: &[u64]) -> Vec<Re
}
#[implement(Service)]
pub async fn get_statekey_from_short(&self, shortstatekey: u64) -> Result<(StateEventType, String)> {
const BUFSIZE: usize = size_of::<u64>();
pub async fn get_statekey_from_short(&self, shortstatekey: ShortStateKey) -> Result<(StateEventType, String)> {
const BUFSIZE: usize = size_of::<ShortStateKey>();
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::<u64>();
pub async fn get_or_create_shortstatehash(&self, state_hash: &[u8]) -> (ShortStateHash, bool) {
const BUFSIZE: usize = size_of::<ShortStateHash>();
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<u64> {
pub async fn get_shortroomid(&self, room_id: &RoomId) -> Result<ShortRoomId> {
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::<u64>();
const BUFSIZE: usize = size_of::<ShortRoomId>();
let short = self.services.globals.next_count().unwrap();
debug_assert!(size_of_val(&short) == BUFSIZE, "buffer requirement changed");

View file

@ -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<Map>,
@ -36,7 +36,7 @@ impl Data {
}
#[allow(unused_qualifications)] // async traits
pub(super) async fn state_full_ids(&self, shortstatehash: u64) -> Result<HashMap<u64, Arc<EventId>>> {
pub(super) async fn state_full_ids(&self, shortstatehash: ShortStateHash) -> Result<HashMap<u64, Arc<EventId>>> {
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<HashMap<(StateEventType, String), Arc<PduEvent>>> {
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<Arc<EventId>> {
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<Arc<PduEvent>> {
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<u64> {
pub(super) async fn pdu_shortstatehash(&self, event_id: &EventId) -> Result<ShortStateHash> {
self.eventid_shorteventid
.get(event_id)
.and_then(|shorteventid| self.shorteventid_shortstatehash.get(&shorteventid))

View file

@ -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<StateInfoLruCache>,
@ -49,9 +53,8 @@ pub struct HashSetCompressStateEvent {
pub removed: Arc<HashSet<CompressedStateEvent>>,
}
pub type ShortStateHash = ShortId;
pub(crate) type CompressedStateEvent = [u8; 2 * size_of::<u64>()];
type StateInfoLruCache = LruCache<u64, ShortStateInfoVec>;
type StateInfoLruCache = LruCache<ShortStateHash, ShortStateInfoVec>;
type ShortStateInfoVec = Vec<ShortStateInfo>;
type ParentStatesVec = Vec<ShortStateInfo>;
@ -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<ShortStateInfoVec> {
pub async fn load_shortstatehash_info(&self, shortstatehash: ShortStateHash) -> Result<ShortStateInfoVec> {
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<EventId>)> {
) -> Result<(ShortStateKey, Arc<EventId>)> {
use utils::u64_from_u8;
let shortstatekey = u64_from_u8(&compressed_event[0..size_of::<u64>()]);
@ -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<HashSet<CompressedStateEvent>>,
&self, shortstatehash: ShortStateHash, statediffnew: Arc<HashSet<CompressedStateEvent>>,
statediffremoved: Arc<HashSet<CompressedStateEvent>>, diff_to_sibling: usize,
mut parent_states: ParentStatesVec,
) -> Result {
@ -377,9 +380,9 @@ impl Service {
})
}
async fn get_statediff(&self, shortstatehash: u64) -> Result<StateDiff> {
const BUFSIZE: usize = size_of::<u64>();
const STRIDE: usize = size_of::<u64>();
async fn get_statediff(&self, shortstatehash: ShortStateHash) -> Result<StateDiff> {
const BUFSIZE: usize = size_of::<ShortStateHash>();
const STRIDE: usize = size_of::<ShortStateHash>();
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[..]);