outdent auth_chain Service impl

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2025-01-20 09:05:49 +00:00
parent 4c0ae8c2f7
commit 610129d162

View file

@ -7,14 +7,14 @@ use std::{
};
use conduwuit::{
at, debug, debug_error, trace,
at, debug, debug_error, implement, trace,
utils::{
stream::{ReadyExt, TryBroadbandExt},
IterStream,
},
validated, warn, Err, Result,
};
use futures::{Stream, StreamExt, TryStreamExt};
use futures::{Stream, StreamExt, TryFutureExt, TryStreamExt};
use ruma::{EventId, OwnedEventId, RoomId};
use self::data::Data;
@ -44,7 +44,7 @@ impl crate::Service for Service {
fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
}
impl Service {
#[implement(Service)]
pub async fn event_ids_iter<'a, I>(
&'a self,
room_id: &RoomId,
@ -62,6 +62,7 @@ impl Service {
Ok(stream)
}
#[implement(Service)]
pub async fn get_event_ids<'a, I>(
&'a self,
room_id: &RoomId,
@ -82,6 +83,7 @@ impl Service {
Ok(event_ids)
}
#[implement(Service)]
#[tracing::instrument(name = "auth_chain", level = "debug", skip_all)]
pub async fn get_auth_chain<'a, I>(
&'a self,
@ -115,7 +117,7 @@ impl Service {
"start",
);
let full_auth_chain: Vec<_> = buckets
let full_auth_chain: Vec<ShortEventId> = buckets
.into_iter()
.try_stream()
.broad_and_then(|chunk| async move {
@ -148,11 +150,14 @@ impl Service {
Ok(auth_chain)
})
.try_collect()
.await?;
let mut chunk_cache: Vec<_> = chunk_cache.into_iter().flatten().collect();
.map_ok(|chunk_cache: Vec<_>| chunk_cache.into_iter().flatten().collect())
.map_ok(|mut chunk_cache: Vec<_>| {
chunk_cache.sort_unstable();
chunk_cache.dedup();
chunk_cache
})
.await?;
self.cache_auth_chain_vec(chunk_key, chunk_cache.as_slice());
debug!(
chunk_cache_length = ?chunk_cache.len(),
@ -163,11 +168,14 @@ impl Service {
Ok(chunk_cache)
})
.try_collect()
.await?;
let mut full_auth_chain: Vec<_> = full_auth_chain.into_iter().flatten().collect();
.map_ok(|auth_chain: Vec<_>| auth_chain.into_iter().flatten().collect())
.map_ok(|mut full_auth_chain: Vec<_>| {
full_auth_chain.sort_unstable();
full_auth_chain.dedup();
full_auth_chain
})
.await?;
debug!(
chain_length = ?full_auth_chain.len(),
elapsed = ?started.elapsed(),
@ -177,6 +185,7 @@ impl Service {
Ok(full_auth_chain)
}
#[implement(Service)]
#[tracing::instrument(name = "inner", level = "trace", skip(self, room_id))]
async fn get_auth_chain_inner(
&self,
@ -211,11 +220,7 @@ impl Service {
.await;
if found.insert(sauthevent) {
trace!(
?event_id,
?auth_event,
"adding auth event to processing queue"
);
trace!(?event_id, ?auth_event, "adding auth event to processing queue");
todo.push_back(auth_event.clone());
}
@ -227,11 +232,13 @@ impl Service {
Ok(found.into_iter().collect())
}
#[implement(Service)]
#[inline]
pub async fn get_cached_eventid_authchain(&self, key: &[u64]) -> Result<Arc<[ShortEventId]>> {
self.db.get_cached_eventid_authchain(key).await
}
#[implement(Service)]
#[tracing::instrument(skip_all, level = "debug")]
pub fn cache_auth_chain(&self, key: Vec<u64>, auth_chain: &HashSet<ShortEventId>) {
let val: Arc<[ShortEventId]> = auth_chain.iter().copied().collect();
@ -239,6 +246,7 @@ impl Service {
self.db.cache_auth_chain(key, val);
}
#[implement(Service)]
#[tracing::instrument(skip_all, level = "debug")]
pub fn cache_auth_chain_vec(&self, key: Vec<u64>, auth_chain: &[ShortEventId]) {
let val: Arc<[ShortEventId]> = auth_chain.iter().copied().collect();
@ -246,11 +254,12 @@ impl Service {
self.db.cache_auth_chain(key, val);
}
#[implement(Service)]
pub fn get_cache_usage(&self) -> (usize, usize) {
let cache = self.db.auth_chain_cache.lock().expect("locked");
(cache.len(), cache.capacity())
}
#[implement(Service)]
pub fn clear_cache(&self) { self.db.auth_chain_cache.lock().expect("locked").clear(); }
}