conduwuit/src/database/key_value/rooms/state.rs

74 lines
2.5 KiB
Rust
Raw Normal View History

2022-10-09 17:25:06 +02:00
use ruma::{EventId, OwnedEventId, RoomId};
2022-10-05 15:33:57 +02:00
use std::collections::HashSet;
2022-10-05 20:41:05 +02:00
2022-10-05 20:34:31 +02:00
use std::sync::Arc;
use tokio::sync::MutexGuard;
2022-10-05 20:34:31 +02:00
use crate::{database::KeyValueDatabase, service, utils, Error, Result};
2022-10-05 18:36:12 +02:00
impl service::rooms::state::Data for KeyValueDatabase {
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")
})?))
})
}
2022-10-05 20:34:31 +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-10-05 20:34:31 +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())?;
Ok(())
}
2021-08-12 23:04:00 +02:00
2022-09-07 13:25:51 +02:00
fn set_event_state(&self, shorteventid: u64, shortstatehash: u64) -> Result<()> {
self.shorteventid_shortstatehash
.insert(&shorteventid.to_be_bytes(), &shortstatehash.to_be_bytes())?;
2021-08-12 23:04:00 +02:00
Ok(())
}
fn get_forward_extremities(&self, room_id: &RoomId) -> Result<HashSet<Arc<EventId>>> {
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(|_| {
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
})
.collect()
}
fn set_forward_extremities(
2021-11-27 16:35:59 +01:00
&self,
room_id: &RoomId,
2022-10-09 17:25:06 +02:00
event_ids: Vec<OwnedEventId>,
_mutex_lock: &MutexGuard<'_, ()>, // Take mutex guard to make sure users get the room state mutex
2021-11-27 16:35:59 +01:00
) -> Result<()> {
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)?;
}
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(())
}
}