Unbox and pin database streams.

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk
2025-11-24 09:12:14 +00:00
parent 98affbdeaf
commit 71f3ccf140
26 changed files with 217 additions and 166 deletions

View File

@@ -3,7 +3,7 @@ mod room_tags;
use std::sync::Arc;
use futures::{Stream, StreamExt, TryFutureExt};
use futures::{Stream, StreamExt, TryFutureExt, pin_mut};
use ruma::{
RoomId, UserId,
events::{
@@ -175,15 +175,18 @@ pub async fn last_count<'a>(
let upper = upper.unwrap_or(u64::MAX);
let key = (room_id, user_id, upper, Interfix);
self.db
let keys = self
.db
.roomuserdataid_accountdata
.rev_keys_from(&key)
.ignore_err()
.ready_take_while(move |(room_id_, user_id_, ..): &Key<'_>| {
room_id == *room_id_ && user_id == *user_id_
})
.map(at!(2))
.next()
.map(at!(2));
pin_mut!(keys);
keys.next()
.await
.ok_or_else(|| err!(Request(NotFound("No account data found."))))
}

View File

@@ -1,6 +1,6 @@
use std::{sync::Arc, time::Duration};
use futures::StreamExt;
use futures::{StreamExt, pin_mut};
use ruma::{Mxc, OwnedMxcUri, UserId, http_headers::ContentDisposition};
use tuwunel_core::{
Err, Result, debug, debug_info, err,
@@ -109,11 +109,14 @@ impl Data {
let dim: &[u32] = &[dim.width, dim.height];
let prefix = (mxc, dim, Interfix);
let key = self
let keys = self
.mediaid_file
.keys_prefix_raw(&prefix)
.ignore_err()
.map(ToOwned::to_owned)
.map(ToOwned::to_owned);
pin_mut!(keys);
let key = keys
.next()
.await
.ok_or_else(|| err!(Request(NotFound("Media not found"))))?;

View File

@@ -1,6 +1,6 @@
use std::sync::Arc;
use futures::{FutureExt, StreamExt, pin_mut};
use futures::{FutureExt, StreamExt};
use ruma::RoomId;
use tuwunel_core::{
Result, debug,
@@ -35,6 +35,7 @@ impl Service {
.services
.state_cache
.local_users_in_room(room_id)
.boxed()
.into_future()
.map(|(next, ..)| next.as_ref().is_some());
@@ -42,10 +43,10 @@ impl Service {
.services
.state_cache
.local_users_invited_to_room(room_id)
.boxed()
.into_future()
.map(|(next, ..)| next.as_ref().is_some());
pin_mut!(has_local_users, has_local_invites);
if has_local_users.or(has_local_invites).await {
trace!(?room_id, "Not deleting with local joined or invited");
return;

View File

@@ -46,13 +46,14 @@ pub async fn exists(&self, room_id: &RoomId) -> bool {
};
// Look for PDUs in that room.
self.db
let keys = self
.db
.pduid_pdu
.keys_prefix_raw(&prefix)
.ignore_err()
.next()
.await
.is_some()
.ignore_err();
pin_mut!(keys);
keys.next().await.is_some()
}
#[implement(Service)]

View File

@@ -1,6 +1,6 @@
use std::sync::Arc;
use futures::{Stream, StreamExt, TryFutureExt};
use futures::{Stream, StreamExt, TryFutureExt, future::Either};
use ruma::{EventId, RoomId, UserId, api::Direction};
use tuwunel_core::{
PduId, Result,
@@ -86,16 +86,8 @@ pub fn get_relations<'a>(
};
match dir {
| Direction::Backward => self
.db
.tofrom_relation
.rev_raw_keys_from(start)
.boxed(),
| Direction::Forward => self
.db
.tofrom_relation
.raw_keys_from(start)
.boxed(),
| Direction::Backward => Either::Left(self.db.tofrom_relation.rev_raw_keys_from(start)),
| Direction::Forward => Either::Right(self.db.tofrom_relation.raw_keys_from(start)),
}
.ignore_err()
.ready_take_while(move |key| key.starts_with(&target))

View File

@@ -1,6 +1,6 @@
use std::{borrow::Borrow, fmt::Debug, mem::size_of_val, sync::Arc};
use futures::{FutureExt, Stream, StreamExt};
use futures::{FutureExt, Stream, StreamExt, pin_mut};
use ruma::{EventId, OwnedRoomId, RoomId, events::StateEventType};
use serde::Deserialize;
pub use tuwunel_core::matrix::{ShortEventId, ShortId, ShortRoomId, ShortStateKey};
@@ -245,10 +245,14 @@ pub async fn get_shortroomid(&self, room_id: &RoomId) -> Result<ShortRoomId> {
#[implement(Service)]
pub async fn get_roomid_from_short(&self, shortroomid_: ShortRoomId) -> Result<OwnedRoomId> {
self.db
let stream = self
.db
.roomid_shortroomid
.stream()
.ready_filter_map(Result::ok)
.ready_filter_map(Result::ok);
pin_mut!(stream);
stream
.ready_find(|&(_, shortroomid)| shortroomid == shortroomid_)
.map(|found| found.map(|(room_id, _): (&RoomId, ShortRoomId)| room_id.to_owned()))
.await

View File

@@ -108,6 +108,7 @@ pub fn room_state_keys_with_ids<'a>(
.map_ok(|shortstatehash| {
self.state_keys_with_ids(shortstatehash, event_type)
.map(Ok)
.boxed()
})
.map_err(move |e| err!(Database("Missing state for {room_id:?}: {e:?}")))
.try_flatten_stream()
@@ -127,6 +128,7 @@ pub fn room_state_keys<'a>(
.map_ok(|shortstatehash| {
self.state_keys(shortstatehash, event_type)
.map(Ok)
.boxed()
})
.map_err(move |e| err!(Database("Missing state for {room_id:?}: {e:?}")))
.try_flatten_stream()

View File

@@ -249,7 +249,6 @@ pub fn state_keys_with_shortids<'a>(
.ignore_err()
.unzip()
.map(|(ssks, sids): (Vec<u64>, Vec<u64>)| (ssks, sids))
.boxed()
.shared();
let shortstatekeys = short_ids
@@ -410,7 +409,6 @@ pub fn state_full_shortids(
.map_ok(Vec::into_iter)
.map_ok(IterStream::try_stream)
.try_flatten_stream()
.boxed()
}
#[implement(super::Service)]

View File

@@ -212,8 +212,9 @@ pub fn get_shared_rooms<'a>(
) -> impl Stream<Item = &RoomId> + Send + 'a {
use tuwunel_core::utils::set;
let a = self.rooms_joined(user_a);
let b = self.rooms_joined(user_b);
let a = self.rooms_joined(user_a).boxed();
let b = self.rooms_joined(user_b).boxed();
set::intersection_sorted_stream2(a, b)
}
@@ -415,6 +416,7 @@ pub fn user_memberships<'a>(
.then(|| {
self.rooms_joined(user_id)
.map(|room_id| (Join, room_id))
.boxed()
.into_future()
})
.into();
@@ -424,6 +426,7 @@ pub fn user_memberships<'a>(
.then(|| {
self.rooms_invited(user_id)
.map(|room_id| (Invite, room_id))
.boxed()
.into_future()
})
.into();
@@ -433,6 +436,7 @@ pub fn user_memberships<'a>(
.then(|| {
self.rooms_knocked(user_id)
.map(|room_id| (Knock, room_id))
.boxed()
.into_future()
})
.into();
@@ -442,6 +446,7 @@ pub fn user_memberships<'a>(
.then(|| {
self.rooms_left(user_id)
.map(|room_id| (Leave, room_id))
.boxed()
.into_future()
})
.into();

View File

@@ -1,6 +1,6 @@
use std::{collections::BTreeMap, mem};
use futures::{Stream, StreamExt, TryFutureExt};
use futures::{Stream, StreamExt, TryFutureExt, pin_mut};
use ruma::{
DeviceId, KeyId, OneTimeKeyAlgorithm, OneTimeKeyId, OneTimeKeyName, OwnedKeyId, RoomId, UInt,
UserId,
@@ -113,7 +113,7 @@ pub async fn take_one_time_key(
prefix.extend_from_slice(key_algorithm.as_ref().as_bytes());
prefix.push(b':');
let one_time_key = self
let one_time_keys = self
.db
.onetimekeyid_onetimekeys
.raw_stream_prefix(&prefix)
@@ -136,11 +136,13 @@ pub async fn take_one_time_key(
.unwrap();
(key, val)
})
.next()
.await;
});
one_time_key.ok_or_else(|| err!(Request(NotFound("No one-time-key found"))))
pin_mut!(one_time_keys);
one_time_keys
.next()
.await
.ok_or_else(|| err!(Request(NotFound("No one-time-key found"))))
}
#[implement(super::Service)]