2022-10-10 14:09:11 +02:00
|
|
|
use std::{collections::HashSet, sync::Arc};
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-06-28 22:51:39 +00:00
|
|
|
use conduit::{utils, Error, Result};
|
|
|
|
|
use database::{Database, Map};
|
2022-10-09 17:25:06 +02:00
|
|
|
use ruma::{EventId, OwnedEventId, RoomId};
|
2024-07-09 20:04:43 +00:00
|
|
|
|
2024-07-09 21:10:14 +00:00
|
|
|
use super::RoomMutexGuard;
|
2022-09-06 23:15:09 +02:00
|
|
|
|
2024-06-28 22:51:39 +00:00
|
|
|
pub(super) struct Data {
|
|
|
|
|
shorteventid_shortstatehash: Arc<Map>,
|
|
|
|
|
roomid_pduleaves: Arc<Map>,
|
|
|
|
|
roomid_shortstatehash: Arc<Map>,
|
2022-06-25 16:12:23 +02:00
|
|
|
}
|
2024-05-26 21:29:19 +00:00
|
|
|
|
2024-05-27 03:17:20 +00:00
|
|
|
impl Data {
|
2024-06-28 22:51:39 +00:00
|
|
|
pub(super) fn new(db: &Arc<Database>) -> Self {
|
2024-05-27 03:17:20 +00:00
|
|
|
Self {
|
2024-06-28 22:51:39 +00:00
|
|
|
shorteventid_shortstatehash: db["shorteventid_shortstatehash"].clone(),
|
|
|
|
|
roomid_pduleaves: db["roomid_pduleaves"].clone(),
|
|
|
|
|
roomid_shortstatehash: db["roomid_shortstatehash"].clone(),
|
2024-05-27 03:17:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(super) fn get_room_shortstatehash(&self, room_id: &RoomId) -> Result<Option<u64>> {
|
2024-05-26 21:29:19 +00:00
|
|
|
self.roomid_shortstatehash
|
|
|
|
|
.get(room_id.as_bytes())?
|
|
|
|
|
.map_or(Ok(None), |bytes| {
|
|
|
|
|
Ok(Some(utils::u64_from_bytes(&bytes).map_err(|_| {
|
|
|
|
|
Error::bad_database("Invalid shortstatehash in roomid_shortstatehash")
|
|
|
|
|
})?))
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-03 20:06:43 +00:00
|
|
|
#[inline]
|
2024-05-27 03:17:20 +00:00
|
|
|
pub(super) fn set_room_state(
|
2024-05-26 21:29:19 +00:00
|
|
|
&self,
|
|
|
|
|
room_id: &RoomId,
|
|
|
|
|
new_shortstatehash: u64,
|
2024-07-09 20:04:43 +00:00
|
|
|
_mutex_lock: &RoomMutexGuard, // Take mutex guard to make sure users get the room state mutex
|
2024-05-26 21:29:19 +00:00
|
|
|
) -> Result<()> {
|
|
|
|
|
self.roomid_shortstatehash
|
|
|
|
|
.insert(room_id.as_bytes(), &new_shortstatehash.to_be_bytes())?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-27 03:17:20 +00:00
|
|
|
pub(super) fn set_event_state(&self, shorteventid: u64, shortstatehash: u64) -> Result<()> {
|
2024-05-26 21:29:19 +00:00
|
|
|
self.shorteventid_shortstatehash
|
|
|
|
|
.insert(&shorteventid.to_be_bytes(), &shortstatehash.to_be_bytes())?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-27 03:17:20 +00:00
|
|
|
pub(super) fn get_forward_extremities(&self, room_id: &RoomId) -> Result<HashSet<Arc<EventId>>> {
|
2024-05-26 21:29:19 +00:00
|
|
|
let mut prefix = room_id.as_bytes().to_vec();
|
|
|
|
|
prefix.push(0xFF);
|
|
|
|
|
|
|
|
|
|
self.roomid_pduleaves
|
|
|
|
|
.scan_prefix(prefix)
|
|
|
|
|
.map(|(_, bytes)| {
|
|
|
|
|
EventId::parse_arc(
|
|
|
|
|
utils::string_from_bytes(&bytes)
|
|
|
|
|
.map_err(|_| Error::bad_database("EventID in roomid_pduleaves is invalid unicode."))?,
|
|
|
|
|
)
|
|
|
|
|
.map_err(|_| Error::bad_database("EventId in roomid_pduleaves is invalid."))
|
|
|
|
|
})
|
|
|
|
|
.collect()
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-27 03:17:20 +00:00
|
|
|
pub(super) fn set_forward_extremities(
|
2024-05-26 21:29:19 +00:00
|
|
|
&self,
|
|
|
|
|
room_id: &RoomId,
|
|
|
|
|
event_ids: Vec<OwnedEventId>,
|
2024-07-09 20:04:43 +00:00
|
|
|
_mutex_lock: &RoomMutexGuard, // Take mutex guard to make sure users get the room state mutex
|
2024-05-26 21:29:19 +00:00
|
|
|
) -> Result<()> {
|
|
|
|
|
let mut prefix = room_id.as_bytes().to_vec();
|
|
|
|
|
prefix.push(0xFF);
|
|
|
|
|
|
|
|
|
|
for (key, _) in self.roomid_pduleaves.scan_prefix(prefix.clone()) {
|
|
|
|
|
self.roomid_pduleaves.remove(&key)?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for event_id in event_ids {
|
|
|
|
|
let mut key = prefix.clone();
|
|
|
|
|
key.extend_from_slice(event_id.as_bytes());
|
|
|
|
|
self.roomid_pduleaves.insert(&key, event_id.as_bytes())?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|