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