de-arc state_full_ids

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-11-30 08:09:51 +00:00
parent b5266ad9f5
commit 4a3cc9fffa
9 changed files with 69 additions and 39 deletions

View file

@ -1,7 +1,7 @@
mod tests;
use std::{
collections::VecDeque,
collections::{HashMap, VecDeque},
fmt::{Display, Formatter},
str::FromStr,
sync::Arc,
@ -572,7 +572,7 @@ impl Service {
return Ok(None);
};
let state = self
let state: HashMap<_, Arc<_>> = self
.services
.state_accessor
.state_full_ids(current_shortstatehash)

View file

@ -1,4 +1,4 @@
use std::{collections::HashMap, sync::Arc};
use std::{borrow::Borrow, collections::HashMap, sync::Arc};
use conduit::{
at, err,
@ -8,6 +8,7 @@ use conduit::{
use database::{Deserialized, Map};
use futures::{StreamExt, TryFutureExt};
use ruma::{events::StateEventType, EventId, OwnedEventId, RoomId};
use serde::Deserialize;
use crate::{
rooms,
@ -84,7 +85,11 @@ impl Data {
Ok(full_pdus)
}
pub(super) async fn state_full_ids(&self, shortstatehash: ShortStateHash) -> Result<HashMap<u64, Arc<EventId>>> {
pub(super) async fn state_full_ids<Id>(&self, shortstatehash: ShortStateHash) -> Result<HashMap<ShortStateKey, Id>>
where
Id: for<'de> Deserialize<'de> + Send + Sized + ToOwned,
<Id as ToOwned>::Owned: Borrow<EventId>,
{
let short_ids = self.state_full_shortids(shortstatehash).await?;
let event_ids = self
@ -123,11 +128,15 @@ impl Data {
Ok(shortids)
}
/// Returns a single PDU from `room_id` with key (`event_type`,`state_key`).
#[allow(clippy::unused_self)]
pub(super) async fn state_get_id(
/// Returns a single EventId from `room_id` with key
/// (`event_type`,`state_key`).
pub(super) async fn state_get_id<Id>(
&self, shortstatehash: ShortStateHash, event_type: &StateEventType, state_key: &str,
) -> Result<Arc<EventId>> {
) -> Result<Id>
where
Id: for<'de> Deserialize<'de> + Send + Sized + ToOwned,
<Id as ToOwned>::Owned: Borrow<EventId>,
{
let shortstatekey = self
.services
.short
@ -162,7 +171,7 @@ impl Data {
&self, shortstatehash: ShortStateHash, event_type: &StateEventType, state_key: &str,
) -> Result<PduEvent> {
self.state_get_id(shortstatehash, event_type, state_key)
.and_then(|event_id| async move { self.services.timeline.get_pdu(&event_id).await })
.and_then(|event_id: OwnedEventId| async move { self.services.timeline.get_pdu(&event_id).await })
.await
}
@ -204,10 +213,15 @@ impl Data {
.await
}
/// Returns a single PDU from `room_id` with key (`event_type`,`state_key`).
pub(super) async fn room_state_get_id(
/// Returns a single EventId from `room_id` with key
/// (`event_type`,`state_key`).
pub(super) async fn room_state_get_id<Id>(
&self, room_id: &RoomId, event_type: &StateEventType, state_key: &str,
) -> Result<Arc<EventId>> {
) -> Result<Id>
where
Id: for<'de> Deserialize<'de> + Send + Sized + ToOwned,
<Id as ToOwned>::Owned: Borrow<EventId>,
{
self.services
.state
.get_room_shortstatehash(room_id)

View file

@ -1,6 +1,7 @@
mod data;
use std::{
borrow::Borrow,
collections::HashMap,
fmt::Write,
sync::{Arc, Mutex as StdMutex, Mutex},
@ -101,8 +102,12 @@ impl Service {
/// Builds a StateMap by iterating over all keys that start
/// with state_hash, this gives the full state for the given state_hash.
#[tracing::instrument(skip(self), level = "debug")]
pub async fn state_full_ids(&self, shortstatehash: ShortStateHash) -> Result<HashMap<u64, Arc<EventId>>> {
self.db.state_full_ids(shortstatehash).await
pub async fn state_full_ids<Id>(&self, shortstatehash: ShortStateHash) -> Result<HashMap<u64, Id>>
where
Id: for<'de> Deserialize<'de> + Send + Sized + ToOwned,
<Id as ToOwned>::Owned: Borrow<EventId>,
{
self.db.state_full_ids::<Id>(shortstatehash).await
}
#[inline]
@ -118,12 +123,16 @@ impl Service {
self.db.state_full(shortstatehash).await
}
/// Returns a single PDU from `room_id` with key (`event_type`,
/// Returns a single EventId from `room_id` with key (`event_type`,
/// `state_key`).
#[tracing::instrument(skip(self), level = "debug")]
pub async fn state_get_id(
pub async fn state_get_id<Id>(
&self, shortstatehash: ShortStateHash, event_type: &StateEventType, state_key: &str,
) -> Result<Arc<EventId>> {
) -> Result<Id>
where
Id: for<'de> Deserialize<'de> + Send + Sized + ToOwned,
<Id as ToOwned>::Owned: Borrow<EventId>,
{
self.db
.state_get_id(shortstatehash, event_type, state_key)
.await
@ -321,12 +330,16 @@ impl Service {
self.db.room_state_full_pdus(room_id).await
}
/// Returns a single PDU from `room_id` with key (`event_type`,
/// Returns a single EventId from `room_id` with key (`event_type`,
/// `state_key`).
#[tracing::instrument(skip(self), level = "debug")]
pub async fn room_state_get_id(
pub async fn room_state_get_id<Id>(
&self, room_id: &RoomId, event_type: &StateEventType, state_key: &str,
) -> Result<Arc<EventId>> {
) -> Result<Id>
where
Id: for<'de> Deserialize<'de> + Send + Sized + ToOwned,
<Id as ToOwned>::Owned: Borrow<EventId>,
{
self.db
.room_state_get_id(room_id, event_type, state_key)
.await