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

@ -27,7 +27,14 @@ use tracing::{debug, error, info, info_span, warn, Instrument};
use crate::{ use crate::{
config::DatabaseBackend, config::DatabaseBackend,
observability::FilterReloadHandles, observability::FilterReloadHandles,
service::{media::MediaFileKey, rooms::timeline::PduCount}, service::{
media::MediaFileKey,
rooms::{
short::{ShortEventId, ShortStateHash, ShortStateKey},
state_compressor::CompressedStateEvent,
timeline::PduCount,
},
},
services, utils, Config, Error, PduEvent, Result, Services, SERVICES, services, utils, Config, Error, PduEvent, Result, Services, SERVICES,
}; };
@ -236,13 +243,14 @@ pub(crate) struct KeyValueDatabase {
// Uncategorized trees // Uncategorized trees
pub(super) pdu_cache: Mutex<LruCache<OwnedEventId, Arc<PduEvent>>>, pub(super) pdu_cache: Mutex<LruCache<OwnedEventId, Arc<PduEvent>>>,
pub(super) shorteventid_cache: Mutex<LruCache<u64, Arc<EventId>>>, pub(super) shorteventid_cache: Mutex<LruCache<ShortEventId, Arc<EventId>>>,
pub(super) auth_chain_cache: Mutex<LruCache<Vec<u64>, Arc<HashSet<u64>>>>, pub(super) auth_chain_cache:
pub(super) eventidshort_cache: Mutex<LruCache<OwnedEventId, u64>>, Mutex<LruCache<Vec<ShortEventId>, Arc<HashSet<ShortEventId>>>>,
pub(super) eventidshort_cache: Mutex<LruCache<OwnedEventId, ShortEventId>>,
pub(super) statekeyshort_cache: pub(super) statekeyshort_cache:
Mutex<LruCache<(StateEventType, String), u64>>, Mutex<LruCache<(StateEventType, String), ShortStateKey>>,
pub(super) shortstatekey_cache: pub(super) shortstatekey_cache:
Mutex<LruCache<u64, (StateEventType, String)>>, Mutex<LruCache<ShortStateKey, (StateEventType, String)>>,
pub(super) our_real_users_cache: pub(super) our_real_users_cache:
RwLock<HashMap<OwnedRoomId, Arc<HashSet<OwnedUserId>>>>, RwLock<HashMap<OwnedRoomId, Arc<HashSet<OwnedUserId>>>>,
pub(super) appservice_in_room_cache: pub(super) appservice_in_room_cache:
@ -695,15 +703,15 @@ impl KeyValueDatabase {
if services().globals.database_version()? < 7 { if services().globals.database_version()? < 7 {
// Upgrade state store // Upgrade state store
let mut last_roomstates: HashMap<OwnedRoomId, u64> = let mut last_roomstates: HashMap<OwnedRoomId, ShortStateHash> =
HashMap::new(); HashMap::new();
let mut current_sstatehash: Option<u64> = None; let mut current_sstatehash: Option<ShortStateHash> = None;
let mut current_room = None; let mut current_room = None;
let mut current_state = HashSet::new(); let mut current_state = HashSet::new();
let mut counter = 0; let mut counter = 0;
let mut handle_state = let mut handle_state =
|current_sstatehash: u64, |current_sstatehash: ShortStateHash,
current_room: &RoomId, current_room: &RoomId,
current_state: HashSet<_>, current_state: HashSet<_>,
last_roomstates: &mut HashMap<_, _>| { last_roomstates: &mut HashMap<_, _>| {
@ -762,10 +770,14 @@ impl KeyValueDatabase {
for (k, seventid) in for (k, seventid) in
db.db.open_tree("stateid_shorteventid")?.iter() db.db.open_tree("stateid_shorteventid")?.iter()
{ {
let sstatehash = let sstatehash = ShortStateHash::new(
utils::u64_from_bytes(&k[0..size_of::<u64>()]) utils::u64_from_bytes(&k[0..size_of::<u64>()])
.expect("number of bytes is correct"); .expect("number of bytes is correct"),
let sstatekey = k[size_of::<u64>()..].to_vec(); );
let sstatekey = ShortStateKey::new(
utils::u64_from_bytes(&k[size_of::<u64>()..])
.expect("number of bytes is correct"),
);
if Some(sstatehash) != current_sstatehash { if Some(sstatehash) != current_sstatehash {
if let Some(current_sstatehash) = current_sstatehash { if let Some(current_sstatehash) = current_sstatehash {
handle_state( handle_state(
@ -803,10 +815,14 @@ impl KeyValueDatabase {
} }
} }
let mut val = sstatekey; let seventid = ShortEventId::new(
val.extend_from_slice(&seventid); utils::u64_from_bytes(&seventid)
current_state .expect("number of bytes is correct"),
.insert(val.try_into().expect("size is correct")); );
current_state.insert(CompressedStateEvent {
state: sstatekey,
event: seventid,
});
} }
if let Some(current_sstatehash) = current_sstatehash { if let Some(current_sstatehash) = current_sstatehash {

View file

@ -74,6 +74,7 @@ impl service::globals::Data for KeyValueDatabase {
.ok() .ok()
.flatten() .flatten()
.expect("room exists") .expect("room exists")
.get()
.to_be_bytes() .to_be_bytes()
.to_vec(); .to_vec();

View file

@ -3,15 +3,16 @@ use std::{collections::HashSet, mem::size_of, sync::Arc};
use crate::{ use crate::{
database::KeyValueDatabase, database::KeyValueDatabase,
observability::{FoundIn, Lookup, METRICS}, observability::{FoundIn, Lookup, METRICS},
service, utils, Result, service::{self, rooms::short::ShortEventId},
utils, Result,
}; };
impl service::rooms::auth_chain::Data for KeyValueDatabase { impl service::rooms::auth_chain::Data for KeyValueDatabase {
#[tracing::instrument(skip(self, key))] #[tracing::instrument(skip(self, key))]
fn get_cached_eventid_authchain( fn get_cached_eventid_authchain(
&self, &self,
key: &[u64], key: &[ShortEventId],
) -> Result<Option<Arc<HashSet<u64>>>> { ) -> Result<Option<Arc<HashSet<ShortEventId>>>> {
let lookup = Lookup::AuthChain; let lookup = Lookup::AuthChain;
// Check RAM cache // Check RAM cache
@ -26,13 +27,15 @@ impl service::rooms::auth_chain::Data for KeyValueDatabase {
// Check DB cache // Check DB cache
let chain = self let chain = self
.shorteventid_authchain .shorteventid_authchain
.get(&key[0].to_be_bytes())? .get(&key[0].get().to_be_bytes())?
.map(|chain| { .map(|chain| {
chain chain
.chunks_exact(size_of::<u64>()) .chunks_exact(size_of::<u64>())
.map(|chunk| { .map(|chunk| {
utils::u64_from_bytes(chunk) ShortEventId::new(
.expect("byte length is correct") utils::u64_from_bytes(chunk)
.expect("byte length is correct"),
)
}) })
.collect() .collect()
}); });
@ -57,16 +60,16 @@ impl service::rooms::auth_chain::Data for KeyValueDatabase {
fn cache_auth_chain( fn cache_auth_chain(
&self, &self,
key: Vec<u64>, key: Vec<ShortEventId>,
auth_chain: Arc<HashSet<u64>>, auth_chain: Arc<HashSet<ShortEventId>>,
) -> Result<()> { ) -> Result<()> {
// Only persist single events in db // Only persist single events in db
if key.len() == 1 { if key.len() == 1 {
self.shorteventid_authchain.insert( self.shorteventid_authchain.insert(
&key[0].to_be_bytes(), &key[0].get().to_be_bytes(),
&auth_chain &auth_chain
.iter() .iter()
.flat_map(|s| s.to_be_bytes().to_vec()) .flat_map(|s| s.get().to_be_bytes().to_vec())
.collect::<Vec<u8>>(), .collect::<Vec<u8>>(),
)?; )?;
} }

View file

@ -8,7 +8,7 @@ impl service::rooms::metadata::Data for KeyValueDatabase {
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
fn exists(&self, room_id: &RoomId) -> Result<bool> { fn exists(&self, room_id: &RoomId) -> Result<bool> {
let prefix = match services().rooms.short.get_shortroomid(room_id)? { let prefix = match services().rooms.short.get_shortroomid(room_id)? {
Some(b) => b.to_be_bytes().to_vec(), Some(b) => b.get().to_be_bytes().to_vec(),
None => return Ok(false), None => return Ok(false),
}; };

View file

@ -6,7 +6,10 @@ use crate::{
database::KeyValueDatabase, database::KeyValueDatabase,
service::{ service::{
self, self,
rooms::timeline::{PduCount, PduId}, rooms::{
short::ShortRoomId,
timeline::{PduCount, PduId},
},
}, },
services, utils, Error, PduEvent, Result, services, utils, Error, PduEvent, Result,
}; };
@ -22,7 +25,7 @@ impl service::rooms::pdu_metadata::Data for KeyValueDatabase {
fn relations_until<'a>( fn relations_until<'a>(
&'a self, &'a self,
user_id: &'a UserId, user_id: &'a UserId,
shortroomid: u64, shortroomid: ShortRoomId,
target: u64, target: u64,
until: PduCount, until: PduCount,
) -> Result<Box<dyn Iterator<Item = Result<(PduCount, PduEvent)>> + 'a>> ) -> Result<Box<dyn Iterator<Item = Result<(PduCount, PduEvent)>> + 'a>>
@ -51,7 +54,7 @@ impl service::rooms::pdu_metadata::Data for KeyValueDatabase {
Error::bad_database("Invalid count in tofrom_relation.") Error::bad_database("Invalid count in tofrom_relation.")
})?; })?;
let mut pduid = shortroomid.to_be_bytes().to_vec(); let mut pduid = shortroomid.get().to_be_bytes().to_vec();
pduid.extend_from_slice(&from.to_be_bytes()); pduid.extend_from_slice(&from.to_be_bytes());
let pduid = PduId::new(pduid); let pduid = PduId::new(pduid);

View file

@ -2,7 +2,10 @@ use ruma::RoomId;
use crate::{ use crate::{
database::KeyValueDatabase, database::KeyValueDatabase,
service::{self, rooms::timeline::PduId}, service::{
self,
rooms::{short::ShortRoomId, timeline::PduId},
},
services, utils, Result, services, utils, Result,
}; };
@ -21,12 +24,12 @@ impl service::rooms::search::Data for KeyValueDatabase {
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
fn index_pdu( fn index_pdu(
&self, &self,
shortroomid: u64, shortroomid: ShortRoomId,
pdu_id: &PduId, pdu_id: &PduId,
message_body: &str, message_body: &str,
) -> Result<()> { ) -> Result<()> {
let mut batch = tokenize(message_body).map(|word| { let mut batch = tokenize(message_body).map(|word| {
let mut key = shortroomid.to_be_bytes().to_vec(); let mut key = shortroomid.get().to_be_bytes().to_vec();
key.extend_from_slice(word.as_bytes()); key.extend_from_slice(word.as_bytes());
key.push(0xFF); key.push(0xFF);
// TODO: currently we save the room id a second time here // TODO: currently we save the room id a second time here
@ -40,12 +43,12 @@ impl service::rooms::search::Data for KeyValueDatabase {
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
fn deindex_pdu( fn deindex_pdu(
&self, &self,
shortroomid: u64, shortroomid: ShortRoomId,
pdu_id: &PduId, pdu_id: &PduId,
message_body: &str, message_body: &str,
) -> Result<()> { ) -> Result<()> {
let batch = tokenize(message_body).map(|word| { let batch = tokenize(message_body).map(|word| {
let mut key = shortroomid.to_be_bytes().to_vec(); let mut key = shortroomid.get().to_be_bytes().to_vec();
key.extend_from_slice(word.as_bytes()); key.extend_from_slice(word.as_bytes());
key.push(0xFF); key.push(0xFF);
// TODO: currently we save the room id a second time here // TODO: currently we save the room id a second time here
@ -73,6 +76,7 @@ impl service::rooms::search::Data for KeyValueDatabase {
.short .short
.get_shortroomid(room_id)? .get_shortroomid(room_id)?
.expect("room exists") .expect("room exists")
.get()
.to_be_bytes() .to_be_bytes()
.to_vec(); .to_vec();

View file

@ -5,12 +5,21 @@ use ruma::{events::StateEventType, EventId, RoomId};
use crate::{ use crate::{
database::KeyValueDatabase, database::KeyValueDatabase,
observability::{FoundIn, Lookup, METRICS}, observability::{FoundIn, Lookup, METRICS},
service, services, utils, Error, Result, service::{
self,
rooms::short::{
ShortEventId, ShortRoomId, ShortStateHash, ShortStateKey,
},
},
services, utils, Error, Result,
}; };
impl service::rooms::short::Data for KeyValueDatabase { impl service::rooms::short::Data for KeyValueDatabase {
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
fn get_or_create_shorteventid(&self, event_id: &EventId) -> Result<u64> { fn get_or_create_shorteventid(
&self,
event_id: &EventId,
) -> Result<ShortEventId> {
let lookup = Lookup::CreateEventIdToShort; let lookup = Lookup::CreateEventIdToShort;
if let Some(short) = if let Some(short) =
@ -39,6 +48,8 @@ impl service::rooms::short::Data for KeyValueDatabase {
shorteventid shorteventid
}; };
let short = ShortEventId::new(short);
self.eventidshort_cache self.eventidshort_cache
.lock() .lock()
.unwrap() .unwrap()
@ -52,7 +63,7 @@ impl service::rooms::short::Data for KeyValueDatabase {
&self, &self,
event_type: &StateEventType, event_type: &StateEventType,
state_key: &str, state_key: &str,
) -> Result<Option<u64>> { ) -> Result<Option<ShortStateKey>> {
let lookup = Lookup::StateKeyToShort; let lookup = Lookup::StateKeyToShort;
if let Some(short) = self if let Some(short) = self
@ -73,9 +84,11 @@ impl service::rooms::short::Data for KeyValueDatabase {
.statekey_shortstatekey .statekey_shortstatekey
.get(&db_key)? .get(&db_key)?
.map(|shortstatekey| { .map(|shortstatekey| {
utils::u64_from_bytes(&shortstatekey).map_err(|_| { utils::u64_from_bytes(&shortstatekey)
Error::bad_database("Invalid shortstatekey in db.") .map_err(|_| {
}) Error::bad_database("Invalid shortstatekey in db.")
})
.map(ShortStateKey::new)
}) })
.transpose()?; .transpose()?;
@ -98,7 +111,7 @@ impl service::rooms::short::Data for KeyValueDatabase {
&self, &self,
event_type: &StateEventType, event_type: &StateEventType,
state_key: &str, state_key: &str,
) -> Result<u64> { ) -> Result<ShortStateKey> {
let lookup = Lookup::CreateStateKeyToShort; let lookup = Lookup::CreateStateKeyToShort;
if let Some(short) = self if let Some(short) = self
@ -134,6 +147,8 @@ impl service::rooms::short::Data for KeyValueDatabase {
shortstatekey shortstatekey
}; };
let short = ShortStateKey::new(short);
self.statekeyshort_cache self.statekeyshort_cache
.lock() .lock()
.unwrap() .unwrap()
@ -145,7 +160,7 @@ impl service::rooms::short::Data for KeyValueDatabase {
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
fn get_eventid_from_short( fn get_eventid_from_short(
&self, &self,
shorteventid: u64, shorteventid: ShortEventId,
) -> Result<Arc<EventId>> { ) -> Result<Arc<EventId>> {
let lookup = Lookup::ShortToEventId; let lookup = Lookup::ShortToEventId;
@ -158,7 +173,7 @@ impl service::rooms::short::Data for KeyValueDatabase {
let bytes = self let bytes = self
.shorteventid_eventid .shorteventid_eventid
.get(&shorteventid.to_be_bytes())? .get(&shorteventid.get().to_be_bytes())?
.ok_or_else(|| { .ok_or_else(|| {
Error::bad_database("Shorteventid does not exist") Error::bad_database("Shorteventid does not exist")
})?; })?;
@ -187,7 +202,7 @@ impl service::rooms::short::Data for KeyValueDatabase {
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
fn get_statekey_from_short( fn get_statekey_from_short(
&self, &self,
shortstatekey: u64, shortstatekey: ShortStateKey,
) -> Result<(StateEventType, String)> { ) -> Result<(StateEventType, String)> {
let lookup = Lookup::ShortToStateKey; let lookup = Lookup::ShortToStateKey;
@ -200,7 +215,7 @@ impl service::rooms::short::Data for KeyValueDatabase {
let bytes = self let bytes = self
.shortstatekey_statekey .shortstatekey_statekey
.get(&shortstatekey.to_be_bytes())? .get(&shortstatekey.get().to_be_bytes())?
.ok_or_else(|| { .ok_or_else(|| {
Error::bad_database("Shortstatekey does not exist") Error::bad_database("Shortstatekey does not exist")
})?; })?;
@ -244,51 +259,56 @@ impl service::rooms::short::Data for KeyValueDatabase {
fn get_or_create_shortstatehash( fn get_or_create_shortstatehash(
&self, &self,
state_hash: &[u8], state_hash: &[u8],
) -> Result<(u64, bool)> { ) -> Result<(ShortStateHash, bool)> {
Ok( let (short, existed) = if let Some(shortstatehash) =
if let Some(shortstatehash) = self.statehash_shortstatehash.get(state_hash)?
self.statehash_shortstatehash.get(state_hash)? {
{ (
( utils::u64_from_bytes(&shortstatehash).map_err(|_| {
utils::u64_from_bytes(&shortstatehash).map_err(|_| { Error::bad_database("Invalid shortstatehash in db.")
Error::bad_database("Invalid shortstatehash in db.") })?,
})?, true,
true, )
) } else {
} else { let shortstatehash = services().globals.next_count()?;
let shortstatehash = services().globals.next_count()?; self.statehash_shortstatehash
self.statehash_shortstatehash .insert(state_hash, &shortstatehash.to_be_bytes())?;
.insert(state_hash, &shortstatehash.to_be_bytes())?; (shortstatehash, false)
(shortstatehash, false) };
},
) Ok((ShortStateHash::new(short), existed))
} }
fn get_shortroomid(&self, room_id: &RoomId) -> Result<Option<u64>> { fn get_shortroomid(&self, room_id: &RoomId) -> Result<Option<ShortRoomId>> {
self.roomid_shortroomid self.roomid_shortroomid
.get(room_id.as_bytes())? .get(room_id.as_bytes())?
.map(|bytes| { .map(|bytes| {
utils::u64_from_bytes(&bytes).map_err(|_| { utils::u64_from_bytes(&bytes)
Error::bad_database("Invalid shortroomid in db.") .map_err(|_| {
}) Error::bad_database("Invalid shortroomid in db.")
})
.map(ShortRoomId::new)
}) })
.transpose() .transpose()
} }
fn get_or_create_shortroomid(&self, room_id: &RoomId) -> Result<u64> { fn get_or_create_shortroomid(
Ok( &self,
if let Some(short) = room_id: &RoomId,
self.roomid_shortroomid.get(room_id.as_bytes())? ) -> Result<ShortRoomId> {
{ let short = if let Some(short) =
utils::u64_from_bytes(&short).map_err(|_| { self.roomid_shortroomid.get(room_id.as_bytes())?
Error::bad_database("Invalid shortroomid in db.") {
})? utils::u64_from_bytes(&short).map_err(|_| {
} else { Error::bad_database("Invalid shortroomid in db.")
let short = services().globals.next_count()?; })?
self.roomid_shortroomid } else {
.insert(room_id.as_bytes(), &short.to_be_bytes())?; let short = services().globals.next_count()?;
short self.roomid_shortroomid
}, .insert(room_id.as_bytes(), &short.to_be_bytes())?;
) short
};
Ok(ShortRoomId::new(short))
} }
} }

View file

@ -4,21 +4,30 @@ use ruma::{EventId, OwnedEventId, OwnedRoomId, RoomId};
use crate::{ use crate::{
database::KeyValueDatabase, database::KeyValueDatabase,
service::{self, globals::marker}, service::{
self,
globals::marker,
rooms::short::{ShortEventId, ShortStateHash},
},
utils::{self, on_demand_hashmap::KeyToken}, utils::{self, on_demand_hashmap::KeyToken},
Error, Result, Error, Result,
}; };
impl service::rooms::state::Data for KeyValueDatabase { impl service::rooms::state::Data for KeyValueDatabase {
fn get_room_shortstatehash(&self, room_id: &RoomId) -> Result<Option<u64>> { fn get_room_shortstatehash(
&self,
room_id: &RoomId,
) -> Result<Option<ShortStateHash>> {
self.roomid_shortstatehash.get(room_id.as_bytes())?.map_or( self.roomid_shortstatehash.get(room_id.as_bytes())?.map_or(
Ok(None), Ok(None),
|bytes| { |bytes| {
Ok(Some(utils::u64_from_bytes(&bytes).map_err(|_| { Ok(Some(ShortStateHash::new(
Error::bad_database( utils::u64_from_bytes(&bytes).map_err(|_| {
"Invalid shortstatehash in roomid_shortstatehash", Error::bad_database(
) "Invalid shortstatehash in roomid_shortstatehash",
})?)) )
})?,
)))
}, },
) )
} }
@ -26,21 +35,23 @@ impl service::rooms::state::Data for KeyValueDatabase {
fn set_room_state( fn set_room_state(
&self, &self,
room_id: &KeyToken<OwnedRoomId, marker::State>, room_id: &KeyToken<OwnedRoomId, marker::State>,
new_shortstatehash: u64, new_shortstatehash: ShortStateHash,
) -> Result<()> { ) -> Result<()> {
self.roomid_shortstatehash self.roomid_shortstatehash.insert(
.insert(room_id.as_bytes(), &new_shortstatehash.to_be_bytes())?; room_id.as_bytes(),
&new_shortstatehash.get().to_be_bytes(),
)?;
Ok(()) Ok(())
} }
fn set_event_state( fn set_event_state(
&self, &self,
shorteventid: u64, shorteventid: ShortEventId,
shortstatehash: u64, shortstatehash: ShortStateHash,
) -> Result<()> { ) -> Result<()> {
self.shorteventid_shortstatehash.insert( self.shorteventid_shortstatehash.insert(
&shorteventid.to_be_bytes(), &shorteventid.get().to_be_bytes(),
&shortstatehash.to_be_bytes(), &shortstatehash.get().to_be_bytes(),
)?; )?;
Ok(()) Ok(())
} }

View file

@ -4,16 +4,20 @@ use async_trait::async_trait;
use ruma::{events::StateEventType, EventId, RoomId}; use ruma::{events::StateEventType, EventId, RoomId};
use crate::{ use crate::{
database::KeyValueDatabase, service, services, utils, Error, PduEvent, database::KeyValueDatabase,
Result, service::{
self,
rooms::short::{ShortStateHash, ShortStateKey},
},
services, utils, Error, PduEvent, Result,
}; };
#[async_trait] #[async_trait]
impl service::rooms::state_accessor::Data for KeyValueDatabase { impl service::rooms::state_accessor::Data for KeyValueDatabase {
async fn state_full_ids( async fn state_full_ids(
&self, &self,
shortstatehash: u64, shortstatehash: ShortStateHash,
) -> Result<HashMap<u64, Arc<EventId>>> { ) -> Result<HashMap<ShortStateKey, Arc<EventId>>> {
let full_state = services() let full_state = services()
.rooms .rooms
.state_compressor .state_compressor
@ -40,7 +44,7 @@ impl service::rooms::state_accessor::Data for KeyValueDatabase {
async fn state_full( async fn state_full(
&self, &self,
shortstatehash: u64, shortstatehash: ShortStateHash,
) -> Result<HashMap<(StateEventType, String), Arc<PduEvent>>> { ) -> Result<HashMap<(StateEventType, String), Arc<PduEvent>>> {
let full_state = services() let full_state = services()
.rooms .rooms
@ -87,7 +91,7 @@ impl service::rooms::state_accessor::Data for KeyValueDatabase {
/// `state_key`). /// `state_key`).
fn state_get_id( fn state_get_id(
&self, &self,
shortstatehash: u64, shortstatehash: ShortStateHash,
event_type: &StateEventType, event_type: &StateEventType,
state_key: &str, state_key: &str,
) -> Result<Option<Arc<EventId>>> { ) -> Result<Option<Arc<EventId>>> {
@ -105,7 +109,7 @@ impl service::rooms::state_accessor::Data for KeyValueDatabase {
.full_state; .full_state;
Ok(full_state Ok(full_state
.iter() .iter()
.find(|bytes| bytes.starts_with(&shortstatekey.to_be_bytes())) .find(|compressed| compressed.state == shortstatekey)
.and_then(|compressed| { .and_then(|compressed| {
services() services()
.rooms .rooms
@ -120,7 +124,7 @@ impl service::rooms::state_accessor::Data for KeyValueDatabase {
/// `state_key`). /// `state_key`).
fn state_get( fn state_get(
&self, &self,
shortstatehash: u64, shortstatehash: ShortStateHash,
event_type: &StateEventType, event_type: &StateEventType,
state_key: &str, state_key: &str,
) -> Result<Option<Arc<PduEvent>>> { ) -> Result<Option<Arc<PduEvent>>> {
@ -131,19 +135,24 @@ impl service::rooms::state_accessor::Data for KeyValueDatabase {
} }
/// Returns the state hash for this pdu. /// Returns the state hash for this pdu.
fn pdu_shortstatehash(&self, event_id: &EventId) -> Result<Option<u64>> { fn pdu_shortstatehash(
&self,
event_id: &EventId,
) -> Result<Option<ShortStateHash>> {
self.eventid_shorteventid.get(event_id.as_bytes())?.map_or( self.eventid_shorteventid.get(event_id.as_bytes())?.map_or(
Ok(None), Ok(None),
|shorteventid| { |shorteventid| {
self.shorteventid_shortstatehash self.shorteventid_shortstatehash
.get(&shorteventid)? .get(&shorteventid)?
.map(|bytes| { .map(|bytes| {
utils::u64_from_bytes(&bytes).map_err(|_| { utils::u64_from_bytes(&bytes)
Error::bad_database( .map_err(|_| {
"Invalid shortstatehash bytes in \ Error::bad_database(
shorteventid_shortstatehash", "Invalid shortstatehash bytes in \
) shorteventid_shortstatehash",
}) )
})
.map(ShortStateHash::new)
}) })
.transpose() .transpose()
}, },

View file

@ -2,19 +2,28 @@ use std::{collections::HashSet, mem::size_of, sync::Arc};
use crate::{ use crate::{
database::KeyValueDatabase, database::KeyValueDatabase,
service::{self, rooms::state_compressor::data::StateDiff}, service::{
self,
rooms::{
short::ShortStateHash,
state_compressor::{data::StateDiff, CompressedStateEvent},
},
},
utils, Error, Result, utils, Error, Result,
}; };
impl service::rooms::state_compressor::Data for KeyValueDatabase { impl service::rooms::state_compressor::Data for KeyValueDatabase {
fn get_statediff(&self, shortstatehash: u64) -> Result<StateDiff> { fn get_statediff(
&self,
shortstatehash: ShortStateHash,
) -> Result<StateDiff> {
let value = self let value = self
.shortstatehash_statediff .shortstatehash_statediff
.get(&shortstatehash.to_be_bytes())? .get(&shortstatehash.get().to_be_bytes())?
.ok_or_else(|| Error::bad_database("State hash does not exist"))?; .ok_or_else(|| Error::bad_database("State hash does not exist"))?;
let parent = utils::u64_from_bytes(&value[0..size_of::<u64>()]) let parent = utils::u64_from_bytes(&value[0..size_of::<u64>()])
.expect("bytes have right length"); .expect("bytes have right length");
let parent = (parent != 0).then_some(parent); let parent = (parent != 0).then_some(ShortStateHash::new(parent));
let mut add_mode = true; let mut add_mode = true;
let mut added = HashSet::new(); let mut added = HashSet::new();
@ -28,10 +37,13 @@ impl service::rooms::state_compressor::Data for KeyValueDatabase {
continue; continue;
} }
if add_mode { if add_mode {
added.insert(v.try_into().expect("we checked the size above")); added.insert(CompressedStateEvent::from_bytes(
v.try_into().expect("we checked the size above"),
));
} else { } else {
removed removed.insert(CompressedStateEvent::from_bytes(
.insert(v.try_into().expect("we checked the size above")); v.try_into().expect("we checked the size above"),
));
} }
i += 2 * size_of::<u64>(); i += 2 * size_of::<u64>();
} }
@ -45,22 +57,23 @@ impl service::rooms::state_compressor::Data for KeyValueDatabase {
fn save_statediff( fn save_statediff(
&self, &self,
shortstatehash: u64, shortstatehash: ShortStateHash,
diff: StateDiff, diff: StateDiff,
) -> Result<()> { ) -> Result<()> {
let mut value = diff.parent.unwrap_or(0).to_be_bytes().to_vec(); let mut value =
diff.parent.map_or(0, |h| h.get()).to_be_bytes().to_vec();
for new in diff.added.iter() { for new in diff.added.iter() {
value.extend_from_slice(&new[..]); value.extend_from_slice(&new.as_bytes());
} }
if !diff.removed.is_empty() { if !diff.removed.is_empty() {
value.extend_from_slice(&0_u64.to_be_bytes()); value.extend_from_slice(&0_u64.to_be_bytes());
for removed in diff.removed.iter() { for removed in diff.removed.iter() {
value.extend_from_slice(&removed[..]); value.extend_from_slice(&removed.as_bytes());
} }
} }
self.shortstatehash_statediff self.shortstatehash_statediff
.insert(&shortstatehash.to_be_bytes(), &value) .insert(&shortstatehash.get().to_be_bytes(), &value)
} }
} }

View file

@ -24,6 +24,7 @@ impl service::rooms::threads::Data for KeyValueDatabase {
.short .short
.get_shortroomid(room_id)? .get_shortroomid(room_id)?
.expect("room exists") .expect("room exists")
.get()
.to_be_bytes() .to_be_bytes()
.to_vec(); .to_vec();

View file

@ -383,6 +383,7 @@ fn count_to_id(
.ok_or_else(|| { .ok_or_else(|| {
Error::bad_database("Looked for bad shortroomid in timeline") Error::bad_database("Looked for bad shortroomid in timeline")
})? })?
.get()
.to_be_bytes() .to_be_bytes()
.to_vec(); .to_vec();
let mut pdu_id = prefix.clone(); let mut pdu_id = prefix.clone();

View file

@ -1,7 +1,9 @@
use ruma::{OwnedRoomId, OwnedUserId, RoomId, UserId}; use ruma::{OwnedRoomId, OwnedUserId, RoomId, UserId};
use crate::{ use crate::{
database::KeyValueDatabase, service, services, utils, Error, Result, database::KeyValueDatabase,
service::{self, rooms::short::ShortStateHash},
services, utils, Error, Result,
}; };
impl service::rooms::user::Data for KeyValueDatabase { impl service::rooms::user::Data for KeyValueDatabase {
@ -95,7 +97,7 @@ impl service::rooms::user::Data for KeyValueDatabase {
&self, &self,
room_id: &RoomId, room_id: &RoomId,
token: u64, token: u64,
shortstatehash: u64, shortstatehash: ShortStateHash,
) -> Result<()> { ) -> Result<()> {
let shortroomid = services() let shortroomid = services()
.rooms .rooms
@ -103,36 +105,38 @@ impl service::rooms::user::Data for KeyValueDatabase {
.get_shortroomid(room_id)? .get_shortroomid(room_id)?
.expect("room exists"); .expect("room exists");
let mut key = shortroomid.to_be_bytes().to_vec(); let mut key = shortroomid.get().to_be_bytes().to_vec();
key.extend_from_slice(&token.to_be_bytes()); key.extend_from_slice(&token.to_be_bytes());
self.roomsynctoken_shortstatehash self.roomsynctoken_shortstatehash
.insert(&key, &shortstatehash.to_be_bytes()) .insert(&key, &shortstatehash.get().to_be_bytes())
} }
fn get_token_shortstatehash( fn get_token_shortstatehash(
&self, &self,
room_id: &RoomId, room_id: &RoomId,
token: u64, token: u64,
) -> Result<Option<u64>> { ) -> Result<Option<ShortStateHash>> {
let shortroomid = services() let shortroomid = services()
.rooms .rooms
.short .short
.get_shortroomid(room_id)? .get_shortroomid(room_id)?
.expect("room exists"); .expect("room exists");
let mut key = shortroomid.to_be_bytes().to_vec(); let mut key = shortroomid.get().to_be_bytes().to_vec();
key.extend_from_slice(&token.to_be_bytes()); key.extend_from_slice(&token.to_be_bytes());
self.roomsynctoken_shortstatehash self.roomsynctoken_shortstatehash
.get(&key)? .get(&key)?
.map(|bytes| { .map(|bytes| {
utils::u64_from_bytes(&bytes).map_err(|_| { utils::u64_from_bytes(&bytes)
Error::bad_database( .map_err(|_| {
"Invalid shortstatehash in \ Error::bad_database(
roomsynctoken_shortstatehash", "Invalid shortstatehash in \
) roomsynctoken_shortstatehash",
}) )
})
.map(ShortStateHash::new)
}) })
.transpose() .transpose()
} }

View file

@ -8,6 +8,7 @@ pub(crate) use data::Data;
use ruma::{api::client::error::ErrorKind, EventId, RoomId}; use ruma::{api::client::error::ErrorKind, EventId, RoomId};
use tracing::{debug, error, warn}; use tracing::{debug, error, warn};
use super::short::ShortEventId;
use crate::{services, utils::debug_slice_truncated, Error, Result}; use crate::{services, utils::debug_slice_truncated, Error, Result};
pub(crate) struct Service { pub(crate) struct Service {
@ -17,16 +18,16 @@ pub(crate) struct Service {
impl Service { impl Service {
pub(crate) fn get_cached_eventid_authchain( pub(crate) fn get_cached_eventid_authchain(
&self, &self,
key: &[u64], key: &[ShortEventId],
) -> Result<Option<Arc<HashSet<u64>>>> { ) -> Result<Option<Arc<HashSet<ShortEventId>>>> {
self.db.get_cached_eventid_authchain(key) self.db.get_cached_eventid_authchain(key)
} }
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
pub(crate) fn cache_auth_chain( pub(crate) fn cache_auth_chain(
&self, &self,
key: Vec<u64>, key: Vec<ShortEventId>,
auth_chain: Arc<HashSet<u64>>, auth_chain: Arc<HashSet<ShortEventId>>,
) -> Result<()> { ) -> Result<()> {
self.db.cache_auth_chain(key, auth_chain) self.db.cache_auth_chain(key, auth_chain)
} }
@ -51,7 +52,7 @@ impl Service {
// I'm afraid to change this in case there is accidental reliance on // I'm afraid to change this in case there is accidental reliance on
// the truncation // the truncation
#[allow(clippy::as_conversions, clippy::cast_possible_truncation)] #[allow(clippy::as_conversions, clippy::cast_possible_truncation)]
let bucket_id = (short % NUM_BUCKETS as u64) as usize; let bucket_id = (short.get() % NUM_BUCKETS as u64) as usize;
buckets[bucket_id].insert((short, id.clone())); buckets[bucket_id].insert((short, id.clone()));
i += 1; i += 1;
if i % 100 == 0 { if i % 100 == 0 {
@ -68,7 +69,7 @@ impl Service {
continue; continue;
} }
let chunk_key: Vec<u64> = let chunk_key: Vec<_> =
chunk.iter().map(|(short, _)| short).copied().collect(); chunk.iter().map(|(short, _)| short).copied().collect();
if let Some(cached) = if let Some(cached) =
self.get_cached_eventid_authchain(&chunk_key)? self.get_cached_eventid_authchain(&chunk_key)?
@ -139,7 +140,7 @@ impl Service {
&self, &self,
room_id: &RoomId, room_id: &RoomId,
event_id: &EventId, event_id: &EventId,
) -> Result<HashSet<u64>> { ) -> Result<HashSet<ShortEventId>> {
let mut todo = vec![Arc::from(event_id)]; let mut todo = vec![Arc::from(event_id)];
let mut found = HashSet::new(); let mut found = HashSet::new();

View file

@ -1,15 +1,15 @@
use std::{collections::HashSet, sync::Arc}; use std::{collections::HashSet, sync::Arc};
use crate::Result; use crate::{service::rooms::short::ShortEventId, Result};
pub(crate) trait Data: Send + Sync { pub(crate) trait Data: Send + Sync {
fn get_cached_eventid_authchain( fn get_cached_eventid_authchain(
&self, &self,
shorteventid: &[u64], shorteventid: &[ShortEventId],
) -> Result<Option<Arc<HashSet<u64>>>>; ) -> Result<Option<Arc<HashSet<ShortEventId>>>>;
fn cache_auth_chain( fn cache_auth_chain(
&self, &self,
shorteventid: Vec<u64>, shorteventid: Vec<ShortEventId>,
auth_chain: Arc<HashSet<u64>>, auth_chain: Arc<HashSet<ShortEventId>>,
) -> Result<()>; ) -> Result<()>;
} }

View file

@ -37,7 +37,10 @@ use serde_json::value::RawValue as RawJsonValue;
use tokio::sync::{RwLock, RwLockWriteGuard, Semaphore}; use tokio::sync::{RwLock, RwLockWriteGuard, Semaphore};
use tracing::{debug, error, info, trace, warn}; use tracing::{debug, error, info, trace, warn};
use super::{state_compressor::CompressedStateEvent, timeline::PduId}; use super::{
short::ShortStateKey, state_compressor::CompressedStateEvent,
timeline::PduId,
};
use crate::{ use crate::{
service::{globals::SigningKeys, pdu}, service::{globals::SigningKeys, pdu},
services, services,
@ -1120,7 +1123,7 @@ impl Service {
&self, &self,
room_id: &RoomId, room_id: &RoomId,
room_version_id: &RoomVersionId, room_version_id: &RoomVersionId,
incoming_state: HashMap<u64, Arc<EventId>>, incoming_state: HashMap<ShortStateKey, Arc<EventId>>,
) -> Result<Arc<HashSet<CompressedStateEvent>>> { ) -> Result<Arc<HashSet<CompressedStateEvent>>> {
debug!("Loading current room state ids"); debug!("Loading current room state ids");
let current_sstatehash = services() let current_sstatehash = services()

View file

@ -2,7 +2,10 @@ use std::sync::Arc;
use ruma::{EventId, RoomId, UserId}; use ruma::{EventId, RoomId, UserId};
use crate::{service::rooms::timeline::PduCount, PduEvent, Result}; use crate::{
service::rooms::{short::ShortRoomId, timeline::PduCount},
PduEvent, Result,
};
pub(crate) trait Data: Send + Sync { pub(crate) trait Data: Send + Sync {
fn add_relation(&self, from: u64, to: u64) -> Result<()>; fn add_relation(&self, from: u64, to: u64) -> Result<()>;
@ -10,7 +13,7 @@ pub(crate) trait Data: Send + Sync {
fn relations_until<'a>( fn relations_until<'a>(
&'a self, &'a self,
user_id: &'a UserId, user_id: &'a UserId,
room_id: u64, room_id: ShortRoomId,
target: u64, target: u64,
until: PduCount, until: PduCount,
) -> Result<Box<dyn Iterator<Item = Result<(PduCount, PduEvent)>> + 'a>>; ) -> Result<Box<dyn Iterator<Item = Result<(PduCount, PduEvent)>> + 'a>>;

View file

@ -1,18 +1,21 @@
use ruma::RoomId; use ruma::RoomId;
use crate::{service::rooms::timeline::PduId, Result}; use crate::{
service::rooms::{short::ShortRoomId, timeline::PduId},
Result,
};
pub(crate) trait Data: Send + Sync { pub(crate) trait Data: Send + Sync {
fn index_pdu( fn index_pdu(
&self, &self,
shortroomid: u64, shortroomid: ShortRoomId,
pdu_id: &PduId, pdu_id: &PduId,
message_body: &str, message_body: &str,
) -> Result<()>; ) -> Result<()>;
fn deindex_pdu( fn deindex_pdu(
&self, &self,
shortroomid: u64, shortroomid: ShortRoomId,
pdu_id: &PduId, pdu_id: &PduId,
message_body: &str, message_body: &str,
) -> Result<()>; ) -> Result<()>;

View file

@ -1,4 +1,27 @@
mod data; mod data;
macro_rules! short_id_type {
($name:ident) => {
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub(crate) struct $name(u64);
impl $name {
pub(crate) fn new(id: u64) -> Self {
Self(id)
}
pub(crate) fn get(&self) -> u64 {
self.0
}
}
};
}
short_id_type!(ShortRoomId);
short_id_type!(ShortEventId);
short_id_type!(ShortStateHash);
short_id_type!(ShortStateKey);
pub(crate) use data::Data; pub(crate) use data::Data;
pub(crate) type Service = &'static dyn Data; pub(crate) type Service = &'static dyn Data;

View file

@ -2,38 +2,47 @@ use std::sync::Arc;
use ruma::{events::StateEventType, EventId, RoomId}; use ruma::{events::StateEventType, EventId, RoomId};
use super::{ShortEventId, ShortRoomId, ShortStateHash, ShortStateKey};
use crate::Result; use crate::Result;
pub(crate) trait Data: Send + Sync { pub(crate) trait Data: Send + Sync {
fn get_or_create_shorteventid(&self, event_id: &EventId) -> Result<u64>; fn get_or_create_shorteventid(
&self,
event_id: &EventId,
) -> Result<ShortEventId>;
fn get_shortstatekey( fn get_shortstatekey(
&self, &self,
event_type: &StateEventType, event_type: &StateEventType,
state_key: &str, state_key: &str,
) -> Result<Option<u64>>; ) -> Result<Option<ShortStateKey>>;
fn get_or_create_shortstatekey( fn get_or_create_shortstatekey(
&self, &self,
event_type: &StateEventType, event_type: &StateEventType,
state_key: &str, state_key: &str,
) -> Result<u64>; ) -> Result<ShortStateKey>;
fn get_eventid_from_short(&self, shorteventid: u64) fn get_eventid_from_short(
-> Result<Arc<EventId>>; &self,
shorteventid: ShortEventId,
) -> Result<Arc<EventId>>;
fn get_statekey_from_short( fn get_statekey_from_short(
&self, &self,
shortstatekey: u64, shortstatekey: ShortStateKey,
) -> Result<(StateEventType, String)>; ) -> Result<(StateEventType, String)>;
/// Returns `(shortstatehash, already_existed)` /// Returns `(shortstatehash, already_existed)`
fn get_or_create_shortstatehash( fn get_or_create_shortstatehash(
&self, &self,
state_hash: &[u8], state_hash: &[u8],
) -> Result<(u64, bool)>; ) -> Result<(ShortStateHash, bool)>;
fn get_shortroomid(&self, room_id: &RoomId) -> Result<Option<u64>>; fn get_shortroomid(&self, room_id: &RoomId) -> Result<Option<ShortRoomId>>;
fn get_or_create_shortroomid(&self, room_id: &RoomId) -> Result<u64>; fn get_or_create_shortroomid(
&self,
room_id: &RoomId,
) -> Result<ShortRoomId>;
} }

View file

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

View file

@ -3,25 +3,33 @@ use std::{collections::HashSet, sync::Arc};
use ruma::{EventId, OwnedEventId, OwnedRoomId, RoomId}; use ruma::{EventId, OwnedEventId, OwnedRoomId, RoomId};
use crate::{ use crate::{
service::globals::marker, utils::on_demand_hashmap::KeyToken, Result, service::{
globals::marker,
rooms::short::{ShortEventId, ShortStateHash},
},
utils::on_demand_hashmap::KeyToken,
Result,
}; };
pub(crate) trait Data: Send + Sync { pub(crate) trait Data: Send + Sync {
/// Returns the last state hash key added to the db for the given room. /// Returns the last state hash key added to the db for the given room.
fn get_room_shortstatehash(&self, room_id: &RoomId) -> Result<Option<u64>>; fn get_room_shortstatehash(
&self,
room_id: &RoomId,
) -> Result<Option<ShortStateHash>>;
/// Set the state hash to a new version, but does not update `state_cache`. /// Set the state hash to a new version, but does not update `state_cache`.
fn set_room_state( fn set_room_state(
&self, &self,
room_id: &KeyToken<OwnedRoomId, marker::State>, room_id: &KeyToken<OwnedRoomId, marker::State>,
new_shortstatehash: u64, new_shortstatehash: ShortStateHash,
) -> Result<()>; ) -> Result<()>;
/// Associates a state with an event. /// Associates a state with an event.
fn set_event_state( fn set_event_state(
&self, &self,
shorteventid: u64, shorteventid: ShortEventId,
shortstatehash: u64, shortstatehash: ShortStateHash,
) -> Result<()>; ) -> Result<()>;
/// Returns all events we would send as the `prev_events` of the next event. /// Returns all events we would send as the `prev_events` of the next event.

View file

@ -26,6 +26,7 @@ use ruma::{
use serde_json::value::to_raw_value; use serde_json::value::to_raw_value;
use tracing::{error, warn}; use tracing::{error, warn};
use super::short::{ShortStateHash, ShortStateKey};
use crate::{ use crate::{
observability::{FoundIn, Lookup, METRICS}, observability::{FoundIn, Lookup, METRICS},
service::{globals::marker, pdu::PduBuilder}, service::{globals::marker, pdu::PduBuilder},
@ -37,8 +38,9 @@ use crate::{
pub(crate) struct Service { pub(crate) struct Service {
pub(crate) db: &'static dyn Data, pub(crate) db: &'static dyn Data,
pub(crate) server_visibility_cache: pub(crate) server_visibility_cache:
Mutex<LruCache<(OwnedServerName, u64), bool>>, Mutex<LruCache<(OwnedServerName, ShortStateHash), bool>>,
pub(crate) user_visibility_cache: Mutex<LruCache<(OwnedUserId, u64), bool>>, pub(crate) user_visibility_cache:
Mutex<LruCache<(OwnedUserId, ShortStateHash), bool>>,
} }
impl Service { impl Service {
@ -47,15 +49,15 @@ impl Service {
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
pub(crate) async fn state_full_ids( pub(crate) async fn state_full_ids(
&self, &self,
shortstatehash: u64, shortstatehash: ShortStateHash,
) -> Result<HashMap<u64, Arc<EventId>>> { ) -> Result<HashMap<ShortStateKey, Arc<EventId>>> {
self.db.state_full_ids(shortstatehash).await self.db.state_full_ids(shortstatehash).await
} }
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
pub(crate) async fn state_full( pub(crate) async fn state_full(
&self, &self,
shortstatehash: u64, shortstatehash: ShortStateHash,
) -> Result<HashMap<(StateEventType, String), Arc<PduEvent>>> { ) -> Result<HashMap<(StateEventType, String), Arc<PduEvent>>> {
self.db.state_full(shortstatehash).await self.db.state_full(shortstatehash).await
} }
@ -65,7 +67,7 @@ impl Service {
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
pub(crate) fn state_get_id( pub(crate) fn state_get_id(
&self, &self,
shortstatehash: u64, shortstatehash: ShortStateHash,
event_type: &StateEventType, event_type: &StateEventType,
state_key: &str, state_key: &str,
) -> Result<Option<Arc<EventId>>> { ) -> Result<Option<Arc<EventId>>> {
@ -77,7 +79,7 @@ impl Service {
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
pub(crate) fn state_get( pub(crate) fn state_get(
&self, &self,
shortstatehash: u64, shortstatehash: ShortStateHash,
event_type: &StateEventType, event_type: &StateEventType,
state_key: &str, state_key: &str,
) -> Result<Option<Arc<PduEvent>>> { ) -> Result<Option<Arc<PduEvent>>> {
@ -88,7 +90,7 @@ impl Service {
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
fn user_membership( fn user_membership(
&self, &self,
shortstatehash: u64, shortstatehash: ShortStateHash,
user_id: &UserId, user_id: &UserId,
) -> Result<MembershipState> { ) -> Result<MembershipState> {
self.state_get( self.state_get(
@ -109,7 +111,11 @@ impl Service {
/// The user was a joined member at this state (potentially in the past) /// The user was a joined member at this state (potentially in the past)
#[tracing::instrument(skip(self), ret(level = "trace"))] #[tracing::instrument(skip(self), ret(level = "trace"))]
fn user_was_joined(&self, shortstatehash: u64, user_id: &UserId) -> bool { fn user_was_joined(
&self,
shortstatehash: ShortStateHash,
user_id: &UserId,
) -> bool {
self.user_membership(shortstatehash, user_id) self.user_membership(shortstatehash, user_id)
.is_ok_and(|s| s == MembershipState::Join) .is_ok_and(|s| s == MembershipState::Join)
} }
@ -117,7 +123,11 @@ impl Service {
/// The user was an invited or joined room member at this state (potentially /// The user was an invited or joined room member at this state (potentially
/// in the past) /// in the past)
#[tracing::instrument(skip(self), ret(level = "trace"))] #[tracing::instrument(skip(self), ret(level = "trace"))]
fn user_was_invited(&self, shortstatehash: u64, user_id: &UserId) -> bool { fn user_was_invited(
&self,
shortstatehash: ShortStateHash,
user_id: &UserId,
) -> bool {
self.user_membership(shortstatehash, user_id).is_ok_and(|s| { self.user_membership(shortstatehash, user_id).is_ok_and(|s| {
s == MembershipState::Join || s == MembershipState::Invite s == MembershipState::Join || s == MembershipState::Invite
}) })
@ -315,7 +325,7 @@ impl Service {
pub(crate) fn pdu_shortstatehash( pub(crate) fn pdu_shortstatehash(
&self, &self,
event_id: &EventId, event_id: &EventId,
) -> Result<Option<u64>> { ) -> Result<Option<ShortStateHash>> {
self.db.pdu_shortstatehash(event_id) self.db.pdu_shortstatehash(event_id)
} }

View file

@ -3,7 +3,10 @@ use std::{collections::HashMap, sync::Arc};
use async_trait::async_trait; use async_trait::async_trait;
use ruma::{events::StateEventType, EventId, RoomId}; use ruma::{events::StateEventType, EventId, RoomId};
use crate::{PduEvent, Result}; use crate::{
service::rooms::short::{ShortStateHash, ShortStateKey},
PduEvent, Result,
};
#[async_trait] #[async_trait]
pub(crate) trait Data: Send + Sync { pub(crate) trait Data: Send + Sync {
@ -11,19 +14,19 @@ pub(crate) trait Data: Send + Sync {
/// with state_hash, this gives the full state for the given state_hash. /// with state_hash, this gives the full state for the given state_hash.
async fn state_full_ids( async fn state_full_ids(
&self, &self,
shortstatehash: u64, shortstatehash: ShortStateHash,
) -> Result<HashMap<u64, Arc<EventId>>>; ) -> Result<HashMap<ShortStateKey, Arc<EventId>>>;
async fn state_full( async fn state_full(
&self, &self,
shortstatehash: u64, shortstatehash: ShortStateHash,
) -> Result<HashMap<(StateEventType, String), Arc<PduEvent>>>; ) -> Result<HashMap<(StateEventType, String), Arc<PduEvent>>>;
/// Returns a single PDU from `room_id` with key (`event_type`, /// Returns a single PDU from `room_id` with key (`event_type`,
/// `state_key`). /// `state_key`).
fn state_get_id( fn state_get_id(
&self, &self,
shortstatehash: u64, shortstatehash: ShortStateHash,
event_type: &StateEventType, event_type: &StateEventType,
state_key: &str, state_key: &str,
) -> Result<Option<Arc<EventId>>>; ) -> Result<Option<Arc<EventId>>>;
@ -32,13 +35,16 @@ pub(crate) trait Data: Send + Sync {
/// `state_key`). /// `state_key`).
fn state_get( fn state_get(
&self, &self,
shortstatehash: u64, shortstatehash: ShortStateHash,
event_type: &StateEventType, event_type: &StateEventType,
state_key: &str, state_key: &str,
) -> Result<Option<Arc<PduEvent>>>; ) -> Result<Option<Arc<PduEvent>>>;
/// Returns the state hash for this pdu. /// Returns the state hash for this pdu.
fn pdu_shortstatehash(&self, event_id: &EventId) -> Result<Option<u64>>; fn pdu_shortstatehash(
&self,
event_id: &EventId,
) -> Result<Option<ShortStateHash>>;
/// Returns the full room state. /// Returns the full room state.
async fn room_state_full( async fn room_state_full(

View file

@ -1,4 +1,5 @@
use std::{ use std::{
array,
collections::HashSet, collections::HashSet,
mem::size_of, mem::size_of,
sync::{Arc, Mutex}, sync::{Arc, Mutex},
@ -17,9 +18,11 @@ pub(crate) mod data;
pub(crate) use data::Data; pub(crate) use data::Data;
use data::StateDiff; use data::StateDiff;
use super::short::{ShortEventId, ShortStateHash, ShortStateKey};
#[derive(Clone)] #[derive(Clone)]
pub(crate) struct CompressedStateLayer { pub(crate) struct CompressedStateLayer {
pub(crate) shortstatehash: u64, pub(crate) shortstatehash: ShortStateHash,
pub(crate) full_state: Arc<HashSet<CompressedStateEvent>>, pub(crate) full_state: Arc<HashSet<CompressedStateEvent>>,
pub(crate) added: Arc<HashSet<CompressedStateEvent>>, pub(crate) added: Arc<HashSet<CompressedStateEvent>>,
pub(crate) removed: Arc<HashSet<CompressedStateEvent>>, pub(crate) removed: Arc<HashSet<CompressedStateEvent>>,
@ -29,10 +32,45 @@ pub(crate) struct Service {
pub(crate) db: &'static dyn Data, pub(crate) db: &'static dyn Data,
#[allow(clippy::type_complexity)] #[allow(clippy::type_complexity)]
pub(crate) stateinfo_cache: Mutex<LruCache<u64, Vec<CompressedStateLayer>>>, pub(crate) stateinfo_cache:
Mutex<LruCache<ShortStateHash, Vec<CompressedStateLayer>>>,
} }
pub(crate) type CompressedStateEvent = [u8; 2 * size_of::<u64>()]; #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) struct CompressedStateEvent {
pub(crate) state: ShortStateKey,
pub(crate) event: ShortEventId,
}
impl CompressedStateEvent {
pub(crate) fn as_bytes(
&self,
) -> [u8; size_of::<ShortStateKey>() + size_of::<ShortEventId>()] {
let mut bytes = self
.state
.get()
.to_be_bytes()
.into_iter()
.chain(self.event.get().to_be_bytes());
array::from_fn(|_| bytes.next().unwrap())
}
pub(crate) fn from_bytes(
bytes: [u8; size_of::<ShortStateKey>() + size_of::<ShortEventId>()],
) -> Self {
let state = ShortStateKey::new(u64::from_be_bytes(
bytes[0..8].try_into().unwrap(),
));
let event = ShortEventId::new(u64::from_be_bytes(
bytes[8..16].try_into().unwrap(),
));
Self {
state,
event,
}
}
}
impl Service { impl Service {
/// Returns a stack with info on shortstatehash, full state, added diff and /// Returns a stack with info on shortstatehash, full state, added diff and
@ -41,7 +79,7 @@ impl Service {
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
pub(crate) fn load_shortstatehash_info( pub(crate) fn load_shortstatehash_info(
&self, &self,
shortstatehash: u64, shortstatehash: ShortStateHash,
) -> Result<Vec<CompressedStateLayer>> { ) -> Result<Vec<CompressedStateLayer>> {
let lookup = Lookup::StateInfo; let lookup = Lookup::StateInfo;
@ -96,18 +134,16 @@ impl Service {
#[allow(clippy::unused_self)] #[allow(clippy::unused_self)]
pub(crate) fn compress_state_event( pub(crate) fn compress_state_event(
&self, &self,
shortstatekey: u64, shortstatekey: ShortStateKey,
event_id: &EventId, event_id: &EventId,
) -> Result<CompressedStateEvent> { ) -> Result<CompressedStateEvent> {
let mut v = shortstatekey.to_be_bytes().to_vec(); Ok(CompressedStateEvent {
v.extend_from_slice( state: shortstatekey,
&services() event: services()
.rooms .rooms
.short .short
.get_or_create_shorteventid(event_id)? .get_or_create_shorteventid(event_id)?,
.to_be_bytes(), })
);
Ok(v.try_into().expect("we checked the size above"))
} }
/// Returns shortstatekey, event id /// Returns shortstatekey, event id
@ -116,14 +152,13 @@ impl Service {
pub(crate) fn parse_compressed_state_event( pub(crate) fn parse_compressed_state_event(
&self, &self,
compressed_event: &CompressedStateEvent, compressed_event: &CompressedStateEvent,
) -> Result<(u64, Arc<EventId>)> { ) -> Result<(ShortStateKey, Arc<EventId>)> {
Ok(( Ok((
utils::u64_from_bytes(&compressed_event[0..size_of::<u64>()]) compressed_event.state,
.expect("bytes have right length"), services()
services().rooms.short.get_eventid_from_short( .rooms
utils::u64_from_bytes(&compressed_event[size_of::<u64>()..]) .short
.expect("bytes have right length"), .get_eventid_from_short(compressed_event.event)?,
)?,
)) ))
} }
@ -155,7 +190,7 @@ impl Service {
))] ))]
pub(crate) fn save_state_from_diff( pub(crate) fn save_state_from_diff(
&self, &self,
shortstatehash: u64, shortstatehash: ShortStateHash,
statediffnew: Arc<HashSet<CompressedStateEvent>>, statediffnew: Arc<HashSet<CompressedStateEvent>>,
statediffremoved: Arc<HashSet<CompressedStateEvent>>, statediffremoved: Arc<HashSet<CompressedStateEvent>>,
diff_to_sibling: usize, diff_to_sibling: usize,
@ -275,7 +310,7 @@ impl Service {
room_id: &RoomId, room_id: &RoomId,
new_state_ids_compressed: Arc<HashSet<CompressedStateEvent>>, new_state_ids_compressed: Arc<HashSet<CompressedStateEvent>>,
) -> Result<( ) -> Result<(
u64, ShortStateHash,
Arc<HashSet<CompressedStateEvent>>, Arc<HashSet<CompressedStateEvent>>,
Arc<HashSet<CompressedStateEvent>>, Arc<HashSet<CompressedStateEvent>>,
)> { )> {
@ -283,7 +318,7 @@ impl Service {
services().rooms.state.get_room_shortstatehash(room_id)?; services().rooms.state.get_room_shortstatehash(room_id)?;
let state_hash = utils::calculate_hash( let state_hash = utils::calculate_hash(
new_state_ids_compressed.iter().map(|bytes| &bytes[..]), new_state_ids_compressed.iter().map(CompressedStateEvent::as_bytes),
); );
let (new_shortstatehash, already_existed) = let (new_shortstatehash, already_existed) =

View file

@ -1,19 +1,22 @@
use std::{collections::HashSet, sync::Arc}; use std::{collections::HashSet, sync::Arc};
use super::CompressedStateEvent; use super::CompressedStateEvent;
use crate::Result; use crate::{service::rooms::short::ShortStateHash, Result};
pub(crate) struct StateDiff { pub(crate) struct StateDiff {
pub(crate) parent: Option<u64>, pub(crate) parent: Option<ShortStateHash>,
pub(crate) added: Arc<HashSet<CompressedStateEvent>>, pub(crate) added: Arc<HashSet<CompressedStateEvent>>,
pub(crate) removed: Arc<HashSet<CompressedStateEvent>>, pub(crate) removed: Arc<HashSet<CompressedStateEvent>>,
} }
pub(crate) trait Data: Send + Sync { pub(crate) trait Data: Send + Sync {
fn get_statediff(&self, shortstatehash: u64) -> Result<StateDiff>; fn get_statediff(
&self,
shortstatehash: ShortStateHash,
) -> Result<StateDiff>;
fn save_statediff( fn save_statediff(
&self, &self,
shortstatehash: u64, shortstatehash: ShortStateHash,
diff: StateDiff, diff: StateDiff,
) -> Result<()>; ) -> Result<()>;
} }

View file

@ -31,7 +31,7 @@ use serde_json::value::{to_raw_value, RawValue as RawJsonValue};
use tokio::sync::RwLock; use tokio::sync::RwLock;
use tracing::{error, info, warn}; use tracing::{error, info, warn};
use super::state_compressor::CompressedStateEvent; use super::{short::ShortRoomId, state_compressor::CompressedStateEvent};
use crate::{ use crate::{
api::server_server, api::server_server,
service::{ service::{
@ -297,7 +297,7 @@ impl Service {
.reset_notification_counts(&pdu.sender, &pdu.room_id)?; .reset_notification_counts(&pdu.sender, &pdu.room_id)?;
let count2 = services().globals.next_count()?; let count2 = services().globals.next_count()?;
let mut pdu_id = shortroomid.to_be_bytes().to_vec(); let mut pdu_id = shortroomid.get().to_be_bytes().to_vec();
pdu_id.extend_from_slice(&count2.to_be_bytes()); pdu_id.extend_from_slice(&count2.to_be_bytes());
let pdu_id = PduId::new(pdu_id); let pdu_id = PduId::new(pdu_id);
@ -1194,7 +1194,7 @@ impl Service {
&self, &self,
event_id: &EventId, event_id: &EventId,
reason: &PduEvent, reason: &PduEvent,
shortroomid: u64, shortroomid: ShortRoomId,
) -> Result<()> { ) -> Result<()> {
// TODO: Don't reserialize, keep original json // TODO: Don't reserialize, keep original json
if let Some(pdu_id) = self.get_pdu_id(event_id)? { if let Some(pdu_id) = self.get_pdu_id(event_id)? {
@ -1359,7 +1359,7 @@ impl Service {
.await; .await;
let count = services().globals.next_count()?; let count = services().globals.next_count()?;
let mut pdu_id = shortroomid.to_be_bytes().to_vec(); let mut pdu_id = shortroomid.get().to_be_bytes().to_vec();
pdu_id.extend_from_slice(&0_u64.to_be_bytes()); pdu_id.extend_from_slice(&0_u64.to_be_bytes());
pdu_id.extend_from_slice(&(u64::MAX - count).to_be_bytes()); pdu_id.extend_from_slice(&(u64::MAX - count).to_be_bytes());
let pdu_id = PduId::new(pdu_id); let pdu_id = PduId::new(pdu_id);

View file

@ -1,6 +1,6 @@
use ruma::{OwnedRoomId, OwnedUserId, RoomId, UserId}; use ruma::{OwnedRoomId, OwnedUserId, RoomId, UserId};
use crate::Result; use crate::{service::rooms::short::ShortStateHash, Result};
pub(crate) trait Data: Send + Sync { pub(crate) trait Data: Send + Sync {
fn reset_notification_counts( fn reset_notification_counts(
@ -32,14 +32,14 @@ pub(crate) trait Data: Send + Sync {
&self, &self,
room_id: &RoomId, room_id: &RoomId,
token: u64, token: u64,
shortstatehash: u64, shortstatehash: ShortStateHash,
) -> Result<()>; ) -> Result<()>;
fn get_token_shortstatehash( fn get_token_shortstatehash(
&self, &self,
room_id: &RoomId, room_id: &RoomId,
token: u64, token: u64,
) -> Result<Option<u64>>; ) -> Result<Option<ShortStateHash>>;
fn get_shared_rooms<'a>( fn get_shared_rooms<'a>(
&'a self, &'a self,