2022-12-18 06:37:03 +01:00
|
|
|
use std::{collections::HashMap, sync::Arc};
|
2022-09-06 23:15:09 +02:00
|
|
|
|
2024-07-18 06:37:47 +00:00
|
|
|
use conduit::{utils, Error, PduEvent, Result};
|
|
|
|
|
use database::Map;
|
2022-10-05 20:34:31 +02:00
|
|
|
use ruma::{events::StateEventType, EventId, RoomId};
|
2022-09-06 23:15:09 +02:00
|
|
|
|
2024-07-18 06:37:47 +00:00
|
|
|
use crate::{rooms, Dep};
|
2022-09-06 23:15:09 +02:00
|
|
|
|
2024-06-28 22:51:39 +00:00
|
|
|
pub(super) struct Data {
|
|
|
|
|
eventid_shorteventid: Arc<Map>,
|
|
|
|
|
shorteventid_shortstatehash: Arc<Map>,
|
2024-07-18 06:37:47 +00:00
|
|
|
services: Services,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct Services {
|
|
|
|
|
short: Dep<rooms::short::Service>,
|
|
|
|
|
state: Dep<rooms::state::Service>,
|
|
|
|
|
state_compressor: Dep<rooms::state_compressor::Service>,
|
|
|
|
|
timeline: Dep<rooms::timeline::Service>,
|
2022-08-14 13:38:21 +02:00
|
|
|
}
|
2024-05-26 21:29:19 +00:00
|
|
|
|
2024-05-27 03:17:20 +00:00
|
|
|
impl Data {
|
2024-07-18 06:37:47 +00:00
|
|
|
pub(super) fn new(args: &crate::Args<'_>) -> Self {
|
|
|
|
|
let db = &args.db;
|
2024-05-27 03:17:20 +00:00
|
|
|
Self {
|
2024-06-28 22:51:39 +00:00
|
|
|
eventid_shorteventid: db["eventid_shorteventid"].clone(),
|
|
|
|
|
shorteventid_shortstatehash: db["shorteventid_shortstatehash"].clone(),
|
2024-07-18 06:37:47 +00:00
|
|
|
services: Services {
|
|
|
|
|
short: args.depend::<rooms::short::Service>("rooms::short"),
|
|
|
|
|
state: args.depend::<rooms::state::Service>("rooms::state"),
|
|
|
|
|
state_compressor: args.depend::<rooms::state_compressor::Service>("rooms::state_compressor"),
|
|
|
|
|
timeline: args.depend::<rooms::timeline::Service>("rooms::timeline"),
|
|
|
|
|
},
|
2024-05-27 03:17:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-26 21:29:19 +00:00
|
|
|
#[allow(unused_qualifications)] // async traits
|
2024-05-27 03:17:20 +00:00
|
|
|
pub(super) async fn state_full_ids(&self, shortstatehash: u64) -> Result<HashMap<u64, Arc<EventId>>> {
|
2024-07-18 06:37:47 +00:00
|
|
|
let full_state = self
|
|
|
|
|
.services
|
2024-05-26 21:29:19 +00:00
|
|
|
.state_compressor
|
|
|
|
|
.load_shortstatehash_info(shortstatehash)?
|
|
|
|
|
.pop()
|
|
|
|
|
.expect("there is always one layer")
|
|
|
|
|
.1;
|
|
|
|
|
let mut result = HashMap::new();
|
|
|
|
|
let mut i: u8 = 0;
|
|
|
|
|
for compressed in full_state.iter() {
|
2024-07-18 06:37:47 +00:00
|
|
|
let parsed = self
|
|
|
|
|
.services
|
2024-05-26 21:29:19 +00:00
|
|
|
.state_compressor
|
|
|
|
|
.parse_compressed_state_event(compressed)?;
|
|
|
|
|
result.insert(parsed.0, parsed.1);
|
|
|
|
|
|
|
|
|
|
i = i.wrapping_add(1);
|
|
|
|
|
if i % 100 == 0 {
|
|
|
|
|
tokio::task::yield_now().await;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(result)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[allow(unused_qualifications)] // async traits
|
2024-05-27 03:17:20 +00:00
|
|
|
pub(super) async fn state_full(
|
|
|
|
|
&self, shortstatehash: u64,
|
|
|
|
|
) -> Result<HashMap<(StateEventType, String), Arc<PduEvent>>> {
|
2024-07-18 06:37:47 +00:00
|
|
|
let full_state = self
|
|
|
|
|
.services
|
2024-05-26 21:29:19 +00:00
|
|
|
.state_compressor
|
|
|
|
|
.load_shortstatehash_info(shortstatehash)?
|
|
|
|
|
.pop()
|
|
|
|
|
.expect("there is always one layer")
|
|
|
|
|
.1;
|
|
|
|
|
|
|
|
|
|
let mut result = HashMap::new();
|
|
|
|
|
let mut i: u8 = 0;
|
|
|
|
|
for compressed in full_state.iter() {
|
2024-07-18 06:37:47 +00:00
|
|
|
let (_, eventid) = self
|
|
|
|
|
.services
|
2024-05-26 21:29:19 +00:00
|
|
|
.state_compressor
|
|
|
|
|
.parse_compressed_state_event(compressed)?;
|
2024-07-18 06:37:47 +00:00
|
|
|
if let Some(pdu) = self.services.timeline.get_pdu(&eventid)? {
|
2024-05-26 21:29:19 +00:00
|
|
|
result.insert(
|
|
|
|
|
(
|
|
|
|
|
pdu.kind.to_string().into(),
|
|
|
|
|
pdu.state_key
|
|
|
|
|
.as_ref()
|
|
|
|
|
.ok_or_else(|| Error::bad_database("State event has no state key."))?
|
|
|
|
|
.clone(),
|
|
|
|
|
),
|
|
|
|
|
pdu,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
i = i.wrapping_add(1);
|
|
|
|
|
if i % 100 == 0 {
|
|
|
|
|
tokio::task::yield_now().await;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns a single PDU from `room_id` with key (`event_type`,
|
|
|
|
|
/// `state_key`).
|
2024-05-27 03:17:20 +00:00
|
|
|
#[allow(clippy::unused_self)]
|
|
|
|
|
pub(super) fn state_get_id(
|
2024-05-26 21:29:19 +00:00
|
|
|
&self, shortstatehash: u64, event_type: &StateEventType, state_key: &str,
|
|
|
|
|
) -> Result<Option<Arc<EventId>>> {
|
2024-07-18 06:37:47 +00:00
|
|
|
let Some(shortstatekey) = self
|
|
|
|
|
.services
|
2024-05-26 21:29:19 +00:00
|
|
|
.short
|
|
|
|
|
.get_shortstatekey(event_type, state_key)?
|
|
|
|
|
else {
|
|
|
|
|
return Ok(None);
|
|
|
|
|
};
|
2024-07-18 06:37:47 +00:00
|
|
|
let full_state = self
|
|
|
|
|
.services
|
2024-05-26 21:29:19 +00:00
|
|
|
.state_compressor
|
|
|
|
|
.load_shortstatehash_info(shortstatehash)?
|
|
|
|
|
.pop()
|
|
|
|
|
.expect("there is always one layer")
|
|
|
|
|
.1;
|
|
|
|
|
Ok(full_state
|
|
|
|
|
.iter()
|
|
|
|
|
.find(|bytes| bytes.starts_with(&shortstatekey.to_be_bytes()))
|
|
|
|
|
.and_then(|compressed| {
|
2024-07-18 06:37:47 +00:00
|
|
|
self.services
|
2024-05-26 21:29:19 +00:00
|
|
|
.state_compressor
|
|
|
|
|
.parse_compressed_state_event(compressed)
|
|
|
|
|
.ok()
|
|
|
|
|
.map(|(_, id)| id)
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns a single PDU from `room_id` with key (`event_type`,
|
|
|
|
|
/// `state_key`).
|
2024-05-27 03:17:20 +00:00
|
|
|
pub(super) fn state_get(
|
2024-05-26 21:29:19 +00:00
|
|
|
&self, shortstatehash: u64, event_type: &StateEventType, state_key: &str,
|
|
|
|
|
) -> Result<Option<Arc<PduEvent>>> {
|
|
|
|
|
self.state_get_id(shortstatehash, event_type, state_key)?
|
2024-07-18 06:37:47 +00:00
|
|
|
.map_or(Ok(None), |event_id| self.services.timeline.get_pdu(&event_id))
|
2024-05-26 21:29:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns the state hash for this pdu.
|
2024-05-27 03:17:20 +00:00
|
|
|
pub(super) fn pdu_shortstatehash(&self, event_id: &EventId) -> Result<Option<u64>> {
|
2024-05-26 21:29:19 +00:00
|
|
|
self.eventid_shorteventid
|
|
|
|
|
.get(event_id.as_bytes())?
|
|
|
|
|
.map_or(Ok(None), |shorteventid| {
|
|
|
|
|
self.shorteventid_shortstatehash
|
|
|
|
|
.get(&shorteventid)?
|
|
|
|
|
.map(|bytes| {
|
|
|
|
|
utils::u64_from_bytes(&bytes).map_err(|_| {
|
|
|
|
|
Error::bad_database("Invalid shortstatehash bytes in shorteventid_shortstatehash")
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
.transpose()
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns the full room state.
|
|
|
|
|
#[allow(unused_qualifications)] // async traits
|
2024-05-27 03:17:20 +00:00
|
|
|
pub(super) async fn room_state_full(
|
|
|
|
|
&self, room_id: &RoomId,
|
|
|
|
|
) -> Result<HashMap<(StateEventType, String), Arc<PduEvent>>> {
|
2024-07-18 06:37:47 +00:00
|
|
|
if let Some(current_shortstatehash) = self.services.state.get_room_shortstatehash(room_id)? {
|
2024-05-26 21:29:19 +00:00
|
|
|
self.state_full(current_shortstatehash).await
|
|
|
|
|
} else {
|
|
|
|
|
Ok(HashMap::new())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns a single PDU from `room_id` with key (`event_type`,
|
|
|
|
|
/// `state_key`).
|
2024-05-27 03:17:20 +00:00
|
|
|
pub(super) fn room_state_get_id(
|
2024-05-26 21:29:19 +00:00
|
|
|
&self, room_id: &RoomId, event_type: &StateEventType, state_key: &str,
|
|
|
|
|
) -> Result<Option<Arc<EventId>>> {
|
2024-07-18 06:37:47 +00:00
|
|
|
if let Some(current_shortstatehash) = self.services.state.get_room_shortstatehash(room_id)? {
|
2024-05-26 21:29:19 +00:00
|
|
|
self.state_get_id(current_shortstatehash, event_type, state_key)
|
|
|
|
|
} else {
|
|
|
|
|
Ok(None)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns a single PDU from `room_id` with key (`event_type`,
|
|
|
|
|
/// `state_key`).
|
2024-05-27 03:17:20 +00:00
|
|
|
pub(super) fn room_state_get(
|
2024-05-26 21:29:19 +00:00
|
|
|
&self, room_id: &RoomId, event_type: &StateEventType, state_key: &str,
|
|
|
|
|
) -> Result<Option<Arc<PduEvent>>> {
|
2024-07-18 06:37:47 +00:00
|
|
|
if let Some(current_shortstatehash) = self.services.state.get_room_shortstatehash(room_id)? {
|
2024-05-26 21:29:19 +00:00
|
|
|
self.state_get(current_shortstatehash, event_type, state_key)
|
|
|
|
|
} else {
|
|
|
|
|
Ok(None)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|