2024-08-08 17:18:30 +00:00
|
|
|
use std::sync::Arc;
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-08-08 17:18:30 +00:00
|
|
|
use conduit::{
|
|
|
|
|
utils::{stream::TryIgnore, ReadyExt},
|
|
|
|
|
Result,
|
|
|
|
|
};
|
|
|
|
|
use database::{Database, Deserialized, Interfix, Map};
|
|
|
|
|
use ruma::{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_shortstatehash: Arc<Map>,
|
2024-08-08 17:18:30 +00:00
|
|
|
pub(super) roomid_pduleaves: 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_shortstatehash: db["roomid_shortstatehash"].clone(),
|
2024-08-08 17:18:30 +00:00
|
|
|
roomid_pduleaves: db["roomid_pduleaves"].clone(),
|
2024-05-27 03:17:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-08 17:18:30 +00:00
|
|
|
pub(super) async fn get_room_shortstatehash(&self, room_id: &RoomId) -> Result<u64> {
|
|
|
|
|
self.roomid_shortstatehash.qry(room_id).await.deserialized()
|
2024-05-26 21:29:19 +00:00
|
|
|
}
|
|
|
|
|
|
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-08-08 17:18:30 +00:00
|
|
|
) {
|
2024-05-26 21:29:19 +00:00
|
|
|
self.roomid_shortstatehash
|
2024-08-08 17:18:30 +00:00
|
|
|
.insert(room_id.as_bytes(), &new_shortstatehash.to_be_bytes());
|
2024-05-26 21:29:19 +00:00
|
|
|
}
|
|
|
|
|
|
2024-08-08 17:18:30 +00:00
|
|
|
pub(super) fn set_event_state(&self, shorteventid: u64, shortstatehash: u64) {
|
2024-05-26 21:29:19 +00:00
|
|
|
self.shorteventid_shortstatehash
|
2024-08-08 17:18:30 +00:00
|
|
|
.insert(&shorteventid.to_be_bytes(), &shortstatehash.to_be_bytes());
|
2024-05-26 21:29:19 +00:00
|
|
|
}
|
|
|
|
|
|
2024-08-08 17:18:30 +00:00
|
|
|
pub(super) async 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-08-08 17:18:30 +00:00
|
|
|
) {
|
|
|
|
|
let prefix = (room_id, Interfix);
|
|
|
|
|
self.roomid_pduleaves
|
|
|
|
|
.keys_raw_prefix(&prefix)
|
|
|
|
|
.ignore_err()
|
|
|
|
|
.ready_for_each(|key| self.roomid_pduleaves.remove(key))
|
|
|
|
|
.await;
|
|
|
|
|
|
2024-05-26 21:29:19 +00:00
|
|
|
let mut prefix = room_id.as_bytes().to_vec();
|
|
|
|
|
prefix.push(0xFF);
|
|
|
|
|
for event_id in event_ids {
|
|
|
|
|
let mut key = prefix.clone();
|
|
|
|
|
key.extend_from_slice(event_id.as_bytes());
|
2024-08-08 17:18:30 +00:00
|
|
|
self.roomid_pduleaves.insert(&key, event_id.as_bytes());
|
2024-05-26 21:29:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|