2022-10-10 14:09:11 +02:00
|
|
|
use std::{collections::HashSet, sync::Arc};
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2022-10-09 17:25:06 +02:00
|
|
|
use ruma::{EventId, OwnedEventId, RoomId};
|
2022-10-05 15:33:57 +02:00
|
|
|
use tokio::sync::MutexGuard;
|
2022-09-06 23:15:09 +02:00
|
|
|
|
2024-05-26 21:29:19 +00:00
|
|
|
use crate::{utils, Error, KeyValueDatabase, Result};
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-05-09 15:59:08 -07:00
|
|
|
pub trait Data: Send + Sync {
|
2022-06-25 16:12:23 +02:00
|
|
|
/// Returns the last state hash key added to the db for the given room.
|
2022-09-07 13:25:51 +02:00
|
|
|
fn get_room_shortstatehash(&self, room_id: &RoomId) -> Result<Option<u64>>;
|
2020-08-06 08:29:59 -04:00
|
|
|
|
2024-03-22 22:44:31 -04:00
|
|
|
/// Set the state hash to a new version, but does not update `state_cache`.
|
2022-10-05 20:34:31 +02:00
|
|
|
fn set_room_state(
|
|
|
|
|
&self,
|
|
|
|
|
room_id: &RoomId,
|
|
|
|
|
new_shortstatehash: u64,
|
2022-09-07 13:25:51 +02:00
|
|
|
_mutex_lock: &MutexGuard<'_, ()>, // Take mutex guard to make sure users get the room state mutex
|
|
|
|
|
) -> Result<()>;
|
2022-06-25 16:12:23 +02:00
|
|
|
|
|
|
|
|
/// Associates a state with an event.
|
2022-09-07 13:25:51 +02:00
|
|
|
fn set_event_state(&self, shorteventid: u64, shortstatehash: u64) -> Result<()>;
|
2022-06-25 16:12:23 +02:00
|
|
|
|
2024-03-22 22:44:31 -04:00
|
|
|
/// Returns all events we would send as the `prev_events` of the next event.
|
2022-09-07 13:25:51 +02:00
|
|
|
fn get_forward_extremities(&self, room_id: &RoomId) -> Result<HashSet<Arc<EventId>>>;
|
2022-06-25 16:12:23 +02:00
|
|
|
|
|
|
|
|
/// Replace the forward extremities of the room.
|
2022-11-21 09:51:39 +01:00
|
|
|
fn set_forward_extremities(
|
2022-10-05 20:34:31 +02:00
|
|
|
&self,
|
2022-06-25 16:12:23 +02:00
|
|
|
room_id: &RoomId,
|
2022-10-09 17:25:06 +02:00
|
|
|
event_ids: Vec<OwnedEventId>,
|
2022-09-07 13:25:51 +02:00
|
|
|
_mutex_lock: &MutexGuard<'_, ()>, // Take mutex guard to make sure users get the room state mutex
|
2022-09-06 23:15:09 +02:00
|
|
|
) -> Result<()>;
|
2022-06-25 16:12:23 +02:00
|
|
|
}
|
2024-05-26 21:29:19 +00:00
|
|
|
|
|
|
|
|
impl Data for KeyValueDatabase {
|
|
|
|
|
fn get_room_shortstatehash(&self, room_id: &RoomId) -> Result<Option<u64>> {
|
|
|
|
|
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")
|
|
|
|
|
})?))
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
) -> Result<()> {
|
|
|
|
|
self.roomid_shortstatehash
|
|
|
|
|
.insert(room_id.as_bytes(), &new_shortstatehash.to_be_bytes())?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn set_event_state(&self, shorteventid: u64, shortstatehash: u64) -> Result<()> {
|
|
|
|
|
self.shorteventid_shortstatehash
|
|
|
|
|
.insert(&shorteventid.to_be_bytes(), &shortstatehash.to_be_bytes())?;
|
|
|
|
|
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(|_| Error::bad_database("EventID in roomid_pduleaves is invalid unicode."))?,
|
|
|
|
|
)
|
|
|
|
|
.map_err(|_| Error::bad_database("EventId in roomid_pduleaves is invalid."))
|
|
|
|
|
})
|
|
|
|
|
.collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn set_forward_extremities(
|
|
|
|
|
&self,
|
|
|
|
|
room_id: &RoomId,
|
|
|
|
|
event_ids: Vec<OwnedEventId>,
|
|
|
|
|
_mutex_lock: &MutexGuard<'_, ()>, // Take mutex guard to make sure users get the room state mutex
|
|
|
|
|
) -> 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(())
|
|
|
|
|
}
|
|
|
|
|
}
|