mirror of https://gitlab.com/famedly/conduit
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
161 lines
5.4 KiB
Rust
161 lines
5.4 KiB
Rust
4 years ago
|
/// Builds a StateMap by iterating over all keys that start
|
||
|
/// with state_hash, this gives the full state for the given state_hash.
|
||
3 years ago
|
#[tracing::instrument(skip(self))]
|
||
2 years ago
|
pub async fn state_full_ids(&self, shortstatehash: u64) -> Result<BTreeMap<u64, Arc<EventId>>> {
|
||
3 years ago
|
let full_state = self
|
||
|
.load_shortstatehash_info(shortstatehash)?
|
||
|
.pop()
|
||
|
.expect("there is always one layer")
|
||
|
.1;
|
||
2 years ago
|
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)
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
#[tracing::instrument(skip(self))]
|
||
2 years ago
|
pub async fn state_full(
|
||
3 years ago
|
&self,
|
||
|
shortstatehash: u64,
|
||
2 years ago
|
) -> Result<HashMap<(StateEventType, String), Arc<PduEvent>>> {
|
||
3 years ago
|
let full_state = self
|
||
|
.load_shortstatehash_info(shortstatehash)?
|
||
|
.pop()
|
||
|
.expect("there is always one layer")
|
||
|
.1;
|
||
2 years ago
|
|
||
|
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(
|
||
4 years ago
|
(
|
||
2 years ago
|
pdu.kind.to_string().into(),
|
||
4 years ago
|
pdu.state_key
|
||
|
.as_ref()
|
||
|
.ok_or_else(|| Error::bad_database("State event has no state key."))?
|
||
|
.clone(),
|
||
|
),
|
||
|
pdu,
|
||
2 years ago
|
);
|
||
|
}
|
||
|
|
||
|
i += 1;
|
||
|
if i % 100 == 0 {
|
||
|
tokio::task::yield_now().await;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
Ok(result)
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
/// Returns a single PDU from `room_id` with key (`event_type`, `state_key`).
|
||
3 years ago
|
#[tracing::instrument(skip(self))]
|
||
3 years ago
|
pub fn state_get_id(
|
||
4 years ago
|
&self,
|
||
3 years ago
|
shortstatehash: u64,
|
||
2 years ago
|
event_type: &StateEventType,
|
||
4 years ago
|
state_key: &str,
|
||
3 years ago
|
) -> Result<Option<Arc<EventId>>> {
|
||
3 years ago
|
let shortstatekey = match self.get_shortstatekey(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()))
|
||
3 years ago
|
.and_then(|compressed| {
|
||
|
self.parse_compressed_state_event(compressed)
|
||
|
.ok()
|
||
|
.map(|(_, id)| id)
|
||
|
}))
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
/// Returns a single PDU from `room_id` with key (`event_type`, `state_key`).
|
||
|
#[tracing::instrument(skip(self))]
|
||
|
pub fn state_get(
|
||
|
&self,
|
||
|
shortstatehash: u64,
|
||
2 years ago
|
event_type: &StateEventType,
|
||
3 years ago
|
state_key: &str,
|
||
3 years ago
|
) -> Result<Option<Arc<PduEvent>>> {
|
||
3 years ago
|
self.state_get_id(shortstatehash, event_type, state_key)?
|
||
|
.map_or(Ok(None), |event_id| self.get_pdu(&event_id))
|
||
|
}
|
||
|
|
||
4 years ago
|
/// Returns the state hash for this pdu.
|
||
3 years ago
|
pub fn pdu_shortstatehash(&self, event_id: &EventId) -> Result<Option<u64>> {
|
||
|
self.eventid_shorteventid
|
||
|
.get(event_id.as_bytes())?
|
||
|
.map_or(Ok(None), |shorteventid| {
|
||
3 years ago
|
self.shorteventid_shortstatehash
|
||
|
.get(&shorteventid)?
|
||
|
.map(|bytes| {
|
||
|
utils::u64_from_bytes(&bytes).map_err(|_| {
|
||
3 years ago
|
Error::bad_database(
|
||
|
"Invalid shortstatehash bytes in shorteventid_shortstatehash",
|
||
|
)
|
||
3 years ago
|
})
|
||
|
})
|
||
|
.transpose()
|
||
3 years ago
|
})
|
||
4 years ago
|
}
|
||
4 years ago
|
|
||
4 years ago
|
/// Returns the full room state.
|
||
3 years ago
|
#[tracing::instrument(skip(self))]
|
||
2 years ago
|
pub async fn room_state_full(
|
||
4 years ago
|
&self,
|
||
|
room_id: &RoomId,
|
||
2 years ago
|
) -> Result<HashMap<(StateEventType, String), Arc<PduEvent>>> {
|
||
3 years ago
|
if let Some(current_shortstatehash) = self.current_shortstatehash(room_id)? {
|
||
2 years ago
|
self.state_full(current_shortstatehash).await
|
||
4 years ago
|
} else {
|
||
3 years ago
|
Ok(HashMap::new())
|
||
4 years ago
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
/// Returns a single PDU from `room_id` with key (`event_type`, `state_key`).
|
||
|
#[tracing::instrument(skip(self))]
|
||
|
pub fn room_state_get_id(
|
||
|
&self,
|
||
|
room_id: &RoomId,
|
||
2 years ago
|
event_type: &StateEventType,
|
||
3 years ago
|
state_key: &str,
|
||
3 years ago
|
) -> Result<Option<Arc<EventId>>> {
|
||
3 years ago
|
if let Some(current_shortstatehash) = self.current_shortstatehash(room_id)? {
|
||
|
self.state_get_id(current_shortstatehash, event_type, state_key)
|
||
|
} else {
|
||
|
Ok(None)
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
/// Returns a single PDU from `room_id` with key (`event_type`, `state_key`).
|
||
3 years ago
|
#[tracing::instrument(skip(self))]
|
||
4 years ago
|
pub fn room_state_get(
|
||
|
&self,
|
||
|
room_id: &RoomId,
|
||
2 years ago
|
event_type: &StateEventType,
|
||
4 years ago
|
state_key: &str,
|
||
3 years ago
|
) -> Result<Option<Arc<PduEvent>>> {
|
||
3 years ago
|
if let Some(current_shortstatehash) = self.current_shortstatehash(room_id)? {
|
||
3 years ago
|
self.state_get(current_shortstatehash, event_type, state_key)
|
||
4 years ago
|
} else {
|
||
|
Ok(None)
|
||
|
}
|
||
4 years ago
|
}
|
||
|
|