2022-09-06 23:15:09 +02:00
|
|
|
use ruma::{RoomId, EventId};
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
use std::{sync::MutexGuard, collections::HashSet};
|
|
|
|
|
use std::fmt::Debug;
|
|
|
|
|
|
|
|
|
|
use crate::{service, database::KeyValueDatabase, utils, Error};
|
|
|
|
|
|
|
|
|
|
impl service::rooms::state::Data for KeyValueDatabase {
|
2022-06-25 16:12:23 +02:00
|
|
|
fn get_room_shortstatehash(&self, room_id: &RoomId) -> Result<Option<u64>> {
|
2021-03-17 22:30:25 +01: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")
|
|
|
|
|
})?))
|
|
|
|
|
})
|
2020-08-06 08:29:59 -04:00
|
|
|
}
|
|
|
|
|
|
2022-09-06 23:15:09 +02:00
|
|
|
fn set_room_state(&self, room_id: &RoomId, new_shortstatehash: u64,
|
|
|
|
|
_mutex_lock: &MutexGuard<'_, ()>, // Take mutex guard to make sure users get the room state mutex
|
2022-06-25 16:12:23 +02:00
|
|
|
) -> Result<()> {
|
2021-03-17 22:30:25 +01:00
|
|
|
self.roomid_shortstatehash
|
2021-08-12 23:04:00 +02:00
|
|
|
.insert(room_id.as_bytes(), &new_shortstatehash.to_be_bytes())?;
|
2022-06-25 16:12:23 +02:00
|
|
|
Ok(())
|
|
|
|
|
}
|
2021-08-12 23:04:00 +02:00
|
|
|
|
2022-09-06 23:15:09 +02:00
|
|
|
fn set_event_state(&self, shorteventid: Vec<u8>, shortstatehash: Vec<u8>) -> Result<()> {
|
|
|
|
|
self.shorteventid_shortstatehash
|
2022-06-25 16:12:23 +02:00
|
|
|
.insert(&shorteventid.to_be_bytes(), &shortstatehash.to_be_bytes())?;
|
2021-08-12 23:04:00 +02:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-06 23:15:09 +02:00
|
|
|
fn get_forward_extremities(&self, room_id: &RoomId) -> Result<HashSet<Arc<EventId>>> {
|
2020-08-26 11:15:52 -04:00
|
|
|
let mut prefix = room_id.as_bytes().to_vec();
|
2020-05-03 17:25:31 +02:00
|
|
|
prefix.push(0xff);
|
|
|
|
|
|
2021-03-25 23:55:40 +01:00
|
|
|
self.roomid_pduleaves
|
2021-06-12 18:40:33 +02:00
|
|
|
.scan_prefix(prefix)
|
|
|
|
|
.map(|(_, bytes)| {
|
2021-11-27 17:44:52 +01:00
|
|
|
EventId::parse_arc(utils::string_from_bytes(&bytes).map_err(|_| {
|
2021-06-17 20:34:14 +02:00
|
|
|
Error::bad_database("EventID in roomid_pduleaves is invalid unicode.")
|
|
|
|
|
})?)
|
|
|
|
|
.map_err(|_| Error::bad_database("EventId in roomid_pduleaves is invalid."))
|
2020-06-09 15:13:17 +02:00
|
|
|
})
|
2021-03-25 23:55:40 +01:00
|
|
|
.collect()
|
2020-05-03 17:25:31 +02:00
|
|
|
}
|
|
|
|
|
|
2022-09-06 23:15:09 +02:00
|
|
|
fn set_forward_extremities<'a>(
|
2021-11-27 16:35:59 +01:00
|
|
|
&self,
|
|
|
|
|
room_id: &RoomId,
|
|
|
|
|
event_ids: impl IntoIterator<Item = &'a EventId> + Debug,
|
2022-09-06 23:15:09 +02:00
|
|
|
_mutex_lock: &MutexGuard<'_, ()>, // Take mutex guard to make sure users get the room state mutex
|
2021-11-27 16:35:59 +01:00
|
|
|
) -> Result<()> {
|
2021-01-18 19:08:59 -05:00
|
|
|
let mut prefix = room_id.as_bytes().to_vec();
|
|
|
|
|
prefix.push(0xff);
|
|
|
|
|
|
2021-06-08 18:10:00 +02:00
|
|
|
for (key, _) in self.roomid_pduleaves.scan_prefix(prefix.clone()) {
|
|
|
|
|
self.roomid_pduleaves.remove(&key)?;
|
2021-01-18 19:08:59 -05:00
|
|
|
}
|
|
|
|
|
|
2021-04-05 21:46:10 +02:00
|
|
|
for event_id in event_ids {
|
2021-01-18 19:08:59 -05:00
|
|
|
let mut key = prefix.to_owned();
|
|
|
|
|
key.extend_from_slice(event_id.as_bytes());
|
|
|
|
|
self.roomid_pduleaves.insert(&key, event_id.as_bytes())?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2022-06-25 16:12:23 +02:00
|
|
|
}
|