|
|
@ -1,24 +1,18 @@
|
|
|
|
|
|
|
|
mod data;
|
|
|
|
|
|
|
|
pub use data::Data;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use crate::service::*;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub struct Service<D: Data> {
|
|
|
|
|
|
|
|
db: D,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl Service<_> {
|
|
|
|
/// Builds a StateMap by iterating over all keys that start
|
|
|
|
/// Builds a StateMap by iterating over all keys that start
|
|
|
|
/// with state_hash, this gives the full state for the given state_hash.
|
|
|
|
/// with state_hash, this gives the full state for the given state_hash.
|
|
|
|
#[tracing::instrument(skip(self))]
|
|
|
|
#[tracing::instrument(skip(self))]
|
|
|
|
pub async fn state_full_ids(&self, shortstatehash: u64) -> Result<BTreeMap<u64, Arc<EventId>>> {
|
|
|
|
pub async fn state_full_ids(&self, shortstatehash: u64) -> Result<BTreeMap<u64, Arc<EventId>>> {
|
|
|
|
let full_state = self
|
|
|
|
self.db.state_full_ids(shortstatehash)
|
|
|
|
.load_shortstatehash_info(shortstatehash)?
|
|
|
|
|
|
|
|
.pop()
|
|
|
|
|
|
|
|
.expect("there is always one layer")
|
|
|
|
|
|
|
|
.1;
|
|
|
|
|
|
|
|
let mut result = BTreeMap::new();
|
|
|
|
|
|
|
|
let mut i = 0;
|
|
|
|
|
|
|
|
for compressed in full_state.into_iter() {
|
|
|
|
|
|
|
|
let parsed = self.parse_compressed_state_event(compressed)?;
|
|
|
|
|
|
|
|
result.insert(parsed.0, parsed.1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
i += 1;
|
|
|
|
|
|
|
|
if i % 100 == 0 {
|
|
|
|
|
|
|
|
tokio::task::yield_now().await;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[tracing::instrument(skip(self))]
|
|
|
|
#[tracing::instrument(skip(self))]
|
|
|
@ -26,36 +20,7 @@
|
|
|
|
&self,
|
|
|
|
&self,
|
|
|
|
shortstatehash: u64,
|
|
|
|
shortstatehash: u64,
|
|
|
|
) -> Result<HashMap<(StateEventType, String), Arc<PduEvent>>> {
|
|
|
|
) -> Result<HashMap<(StateEventType, String), Arc<PduEvent>>> {
|
|
|
|
let full_state = self
|
|
|
|
self.db.state_full(shortstatehash)
|
|
|
|
.load_shortstatehash_info(shortstatehash)?
|
|
|
|
|
|
|
|
.pop()
|
|
|
|
|
|
|
|
.expect("there is always one layer")
|
|
|
|
|
|
|
|
.1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut result = HashMap::new();
|
|
|
|
|
|
|
|
let mut i = 0;
|
|
|
|
|
|
|
|
for compressed in full_state {
|
|
|
|
|
|
|
|
let (_, eventid) = self.parse_compressed_state_event(compressed)?;
|
|
|
|
|
|
|
|
if let Some(pdu) = self.get_pdu(&eventid)? {
|
|
|
|
|
|
|
|
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 += 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`).
|
|
|
|
/// Returns a single PDU from `room_id` with key (`event_type`, `state_key`).
|
|
|
@ -66,23 +31,7 @@
|
|
|
|
event_type: &StateEventType,
|
|
|
|
event_type: &StateEventType,
|
|
|
|
state_key: &str,
|
|
|
|
state_key: &str,
|
|
|
|
) -> Result<Option<Arc<EventId>>> {
|
|
|
|
) -> Result<Option<Arc<EventId>>> {
|
|
|
|
let shortstatekey = match self.get_shortstatekey(event_type, state_key)? {
|
|
|
|
self.db.state_get_id(shortstatehash, event_type, state_key)
|
|
|
|
Some(s) => s,
|
|
|
|
|
|
|
|
None => return Ok(None),
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
let full_state = self
|
|
|
|
|
|
|
|
.load_shortstatehash_info(shortstatehash)?
|
|
|
|
|
|
|
|
.pop()
|
|
|
|
|
|
|
|
.expect("there is always one layer")
|
|
|
|
|
|
|
|
.1;
|
|
|
|
|
|
|
|
Ok(full_state
|
|
|
|
|
|
|
|
.into_iter()
|
|
|
|
|
|
|
|
.find(|bytes| bytes.starts_with(&shortstatekey.to_be_bytes()))
|
|
|
|
|
|
|
|
.and_then(|compressed| {
|
|
|
|
|
|
|
|
self.parse_compressed_state_event(compressed)
|
|
|
|
|
|
|
|
.ok()
|
|
|
|
|
|
|
|
.map(|(_, id)| id)
|
|
|
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Returns a single PDU from `room_id` with key (`event_type`, `state_key`).
|
|
|
|
/// Returns a single PDU from `room_id` with key (`event_type`, `state_key`).
|
|
|
@ -93,26 +42,12 @@
|
|
|
|
event_type: &StateEventType,
|
|
|
|
event_type: &StateEventType,
|
|
|
|
state_key: &str,
|
|
|
|
state_key: &str,
|
|
|
|
) -> Result<Option<Arc<PduEvent>>> {
|
|
|
|
) -> Result<Option<Arc<PduEvent>>> {
|
|
|
|
self.state_get_id(shortstatehash, event_type, state_key)?
|
|
|
|
self.db.pdu_state_get(event_id)
|
|
|
|
.map_or(Ok(None), |event_id| self.get_pdu(&event_id))
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Returns the state hash for this pdu.
|
|
|
|
/// Returns the state hash for this pdu.
|
|
|
|
pub fn pdu_shortstatehash(&self, event_id: &EventId) -> Result<Option<u64>> {
|
|
|
|
pub fn pdu_shortstatehash(&self, event_id: &EventId) -> Result<Option<u64>> {
|
|
|
|
self.eventid_shorteventid
|
|
|
|
self.db.pdu_shortstatehash(event_id)
|
|
|
|
.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.
|
|
|
|
/// Returns the full room state.
|
|
|
@ -121,11 +56,7 @@
|
|
|
|
&self,
|
|
|
|
&self,
|
|
|
|
room_id: &RoomId,
|
|
|
|
room_id: &RoomId,
|
|
|
|
) -> Result<HashMap<(StateEventType, String), Arc<PduEvent>>> {
|
|
|
|
) -> Result<HashMap<(StateEventType, String), Arc<PduEvent>>> {
|
|
|
|
if let Some(current_shortstatehash) = self.current_shortstatehash(room_id)? {
|
|
|
|
self.db.room_state_full(room_id)
|
|
|
|
self.state_full(current_shortstatehash).await
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
Ok(HashMap::new())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Returns a single PDU from `room_id` with key (`event_type`, `state_key`).
|
|
|
|
/// Returns a single PDU from `room_id` with key (`event_type`, `state_key`).
|
|
|
@ -136,11 +67,7 @@
|
|
|
|
event_type: &StateEventType,
|
|
|
|
event_type: &StateEventType,
|
|
|
|
state_key: &str,
|
|
|
|
state_key: &str,
|
|
|
|
) -> Result<Option<Arc<EventId>>> {
|
|
|
|
) -> Result<Option<Arc<EventId>>> {
|
|
|
|
if let Some(current_shortstatehash) = self.current_shortstatehash(room_id)? {
|
|
|
|
self.db.room_state_get_id(room_id, event_type, state_key)
|
|
|
|
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`).
|
|
|
|
/// Returns a single PDU from `room_id` with key (`event_type`, `state_key`).
|
|
|
@ -151,10 +78,6 @@
|
|
|
|
event_type: &StateEventType,
|
|
|
|
event_type: &StateEventType,
|
|
|
|
state_key: &str,
|
|
|
|
state_key: &str,
|
|
|
|
) -> Result<Option<Arc<PduEvent>>> {
|
|
|
|
) -> Result<Option<Arc<PduEvent>>> {
|
|
|
|
if let Some(current_shortstatehash) = self.current_shortstatehash(room_id)? {
|
|
|
|
self.db.room_state_get(room_id, event_type, state_key)
|
|
|
|
self.state_get(current_shortstatehash, event_type, state_key)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
Ok(None)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|