optimize with SmallString; consolidate related re-exports

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2025-02-08 00:16:37 +00:00 committed by strawberry
parent ecc9099127
commit b872f8e593
39 changed files with 113 additions and 96 deletions

16
Cargo.lock generated
View file

@ -822,6 +822,8 @@ dependencies = [
"serde_json", "serde_json",
"serde_regex", "serde_regex",
"serde_yaml", "serde_yaml",
"smallstr",
"smallvec",
"thiserror 2.0.11", "thiserror 2.0.11",
"tikv-jemalloc-ctl", "tikv-jemalloc-ctl",
"tikv-jemalloc-sys", "tikv-jemalloc-sys",
@ -839,7 +841,6 @@ dependencies = [
name = "conduwuit_database" name = "conduwuit_database"
version = "0.5.0" version = "0.5.0"
dependencies = [ dependencies = [
"arrayvec",
"async-channel", "async-channel",
"conduwuit_core", "conduwuit_core",
"const-str", "const-str",
@ -850,7 +851,6 @@ dependencies = [
"rust-rocksdb-uwu", "rust-rocksdb-uwu",
"serde", "serde",
"serde_json", "serde_json",
"smallvec",
"tokio", "tokio",
"tracing", "tracing",
] ]
@ -902,7 +902,6 @@ dependencies = [
name = "conduwuit_service" name = "conduwuit_service"
version = "0.5.0" version = "0.5.0"
dependencies = [ dependencies = [
"arrayvec",
"async-trait", "async-trait",
"base64 0.22.1", "base64 0.22.1",
"blurhash", "blurhash",
@ -929,7 +928,6 @@ dependencies = [
"serde_json", "serde_json",
"serde_yaml", "serde_yaml",
"sha2", "sha2",
"smallvec",
"termimad", "termimad",
"tokio", "tokio",
"tracing", "tracing",
@ -4275,6 +4273,16 @@ dependencies = [
"autocfg", "autocfg",
] ]
[[package]]
name = "smallstr"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "63b1aefdf380735ff8ded0b15f31aab05daf1f70216c01c02a12926badd1df9d"
dependencies = [
"serde",
"smallvec",
]
[[package]] [[package]]
name = "smallvec" name = "smallvec"
version = "1.13.2" version = "1.13.2"

View file

@ -40,6 +40,10 @@ features = [
"write", "write",
] ]
[workspace.dependencies.smallstr]
version = "0.3"
features = ["ffi", "std", "union"]
[workspace.dependencies.const-str] [workspace.dependencies.const-str]
version = "0.5.7" version = "0.5.7"

View file

@ -2,7 +2,7 @@ use std::collections::BTreeMap;
use axum::extract::State; use axum::extract::State;
use conduwuit::{ use conduwuit::{
debug_info, debug_warn, err, error, info, pdu::PduBuilder, warn, Err, Error, Result, debug_info, debug_warn, err, error, info, pdu::PduBuilder, warn, Err, Error, Result, StateKey,
}; };
use futures::FutureExt; use futures::FutureExt;
use ruma::{ use ruma::{
@ -198,7 +198,7 @@ pub(crate) async fn create_room_route(
event_type: TimelineEventType::RoomCreate, event_type: TimelineEventType::RoomCreate,
content: to_raw_value(&create_content) content: to_raw_value(&create_content)
.expect("create event content serialization"), .expect("create event content serialization"),
state_key: Some(String::new()), state_key: Some(StateKey::new()),
..Default::default() ..Default::default()
}, },
sender_user, sender_user,
@ -267,7 +267,7 @@ pub(crate) async fn create_room_route(
event_type: TimelineEventType::RoomPowerLevels, event_type: TimelineEventType::RoomPowerLevels,
content: to_raw_value(&power_levels_content) content: to_raw_value(&power_levels_content)
.expect("serialized power_levels event content"), .expect("serialized power_levels event content"),
state_key: Some(String::new()), state_key: Some(StateKey::new()),
..Default::default() ..Default::default()
}, },
sender_user, sender_user,
@ -371,7 +371,7 @@ pub(crate) async fn create_room_route(
} }
// Implicit state key defaults to "" // Implicit state key defaults to ""
pdu_builder.state_key.get_or_insert_with(String::new); pdu_builder.state_key.get_or_insert_with(StateKey::new);
// Silently skip encryption events if they are not allowed // Silently skip encryption events if they are not allowed
if pdu_builder.event_type == TimelineEventType::RoomEncryption if pdu_builder.event_type == TimelineEventType::RoomEncryption

View file

@ -1,7 +1,7 @@
use std::cmp::max; use std::cmp::max;
use axum::extract::State; use axum::extract::State;
use conduwuit::{err, info, pdu::PduBuilder, Error, Result}; use conduwuit::{err, info, pdu::PduBuilder, Error, Result, StateKey};
use futures::StreamExt; use futures::StreamExt;
use ruma::{ use ruma::{
api::client::{error::ErrorKind, room::upgrade_room}, api::client::{error::ErrorKind, room::upgrade_room},
@ -77,7 +77,7 @@ pub(crate) async fn upgrade_room_route(
.rooms .rooms
.timeline .timeline
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder::state(String::new(), &RoomTombstoneEventContent { PduBuilder::state(StateKey::new(), &RoomTombstoneEventContent {
body: "This room has been replaced".to_owned(), body: "This room has been replaced".to_owned(),
replacement_room: replacement_room.clone(), replacement_room: replacement_room.clone(),
}), }),
@ -159,7 +159,7 @@ pub(crate) async fn upgrade_room_route(
content: to_raw_value(&create_event_content) content: to_raw_value(&create_event_content)
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
unsigned: None, unsigned: None,
state_key: Some(String::new()), state_key: Some(StateKey::new()),
redacts: None, redacts: None,
timestamp: None, timestamp: None,
}, },
@ -188,7 +188,7 @@ pub(crate) async fn upgrade_room_route(
}) })
.expect("event is valid, we just created it"), .expect("event is valid, we just created it"),
unsigned: None, unsigned: None,
state_key: Some(sender_user.to_string()), state_key: Some(sender_user.as_str().into()),
redacts: None, redacts: None,
timestamp: None, timestamp: None,
}, },
@ -217,7 +217,7 @@ pub(crate) async fn upgrade_room_route(
PduBuilder { PduBuilder {
event_type: event_type.to_string().into(), event_type: event_type.to_string().into(),
content: event_content, content: event_content,
state_key: Some(String::new()), state_key: Some(StateKey::new()),
..Default::default() ..Default::default()
}, },
sender_user, sender_user,
@ -272,7 +272,7 @@ pub(crate) async fn upgrade_room_route(
.rooms .rooms
.timeline .timeline
.build_and_append_pdu( .build_and_append_pdu(
PduBuilder::state(String::new(), &RoomPowerLevelsEventContent { PduBuilder::state(StateKey::new(), &RoomPowerLevelsEventContent {
events_default: new_level, events_default: new_level,
invite: new_level, invite: new_level,
..power_levels_event_content ..power_levels_event_content

View file

@ -172,7 +172,7 @@ async fn send_state_event_for_key_helper(
PduBuilder { PduBuilder {
event_type: event_type.to_string().into(), event_type: event_type.to_string().into(),
content: serde_json::from_str(json.json().get())?, content: serde_json::from_str(json.json().get())?,
state_key: Some(String::from(state_key)), state_key: Some(state_key.into()),
timestamp, timestamp,
..Default::default() ..Default::default()
}, },

View file

@ -441,7 +441,7 @@ async fn handle_left_room(
kind: RoomMember, kind: RoomMember,
content: serde_json::from_str(r#"{"membership":"leave"}"#) content: serde_json::from_str(r#"{"membership":"leave"}"#)
.expect("this is valid JSON"), .expect("this is valid JSON"),
state_key: Some(sender_user.to_string()), state_key: Some(sender_user.as_str().into()),
unsigned: None, unsigned: None,
// The following keys are dropped on conversion // The following keys are dropped on conversion
room_id: room_id.clone(), room_id: room_id.clone(),

View file

@ -29,7 +29,7 @@ use ruma::{
TimelineEventType::*, TimelineEventType::*,
}, },
serde::Raw, serde::Raw,
uint, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomId, OwnedUserId, RoomId, UInt, uint, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomId, RoomId, UInt, UserId,
}; };
use service::rooms::read_receipt::pack_receipts; use service::rooms::read_receipt::pack_receipts;
@ -258,12 +258,9 @@ pub(crate) async fn sync_events_v4_route(
continue; continue;
}; };
if pdu.kind == RoomMember { if pdu.kind == RoomMember {
if let Some(state_key) = &pdu.state_key { if let Some(Ok(user_id)) =
let user_id = pdu.state_key.as_deref().map(UserId::parse)
OwnedUserId::parse(state_key.clone()).map_err(|_| { {
Error::bad_database("Invalid UserId in member PDU.")
})?;
if user_id == *sender_user { if user_id == *sender_user {
continue; continue;
} }
@ -275,18 +272,18 @@ pub(crate) async fn sync_events_v4_route(
if !share_encrypted_room( if !share_encrypted_room(
&services, &services,
sender_user, sender_user,
&user_id, user_id,
Some(room_id), Some(room_id),
) )
.await .await
{ {
device_list_changes.insert(user_id); device_list_changes.insert(user_id.to_owned());
} }
}, },
| MembershipState::Leave => { | MembershipState::Leave => {
// Write down users that have left encrypted rooms we // Write down users that have left encrypted rooms we
// are in // are in
left_encrypted_users.insert(user_id); left_encrypted_users.insert(user_id.to_owned());
}, },
| _ => {}, | _ => {},
} }

View file

@ -25,7 +25,7 @@ use ruma::{
}, },
serde::Raw, serde::Raw,
state_res::TypeStateKey, state_res::TypeStateKey,
uint, DeviceId, OwnedEventId, OwnedRoomId, OwnedUserId, RoomId, UInt, UserId, uint, DeviceId, OwnedEventId, OwnedRoomId, RoomId, UInt, UserId,
}; };
use service::{rooms::read_receipt::pack_receipts, PduCount}; use service::{rooms::read_receipt::pack_receipts, PduCount};
@ -765,13 +765,9 @@ async fn collect_e2ee<'a>(
continue; continue;
}; };
if pdu.kind == TimelineEventType::RoomMember { if pdu.kind == TimelineEventType::RoomMember {
if let Some(state_key) = &pdu.state_key { if let Some(Ok(user_id)) = pdu.state_key.as_deref().map(UserId::parse)
let user_id = {
OwnedUserId::parse(state_key.clone()).map_err(|_| { if user_id == sender_user {
Error::bad_database("Invalid UserId in member PDU.")
})?;
if user_id == *sender_user {
continue; continue;
} }
@ -782,18 +778,18 @@ async fn collect_e2ee<'a>(
if !share_encrypted_room( if !share_encrypted_room(
&services, &services,
sender_user, sender_user,
&user_id, user_id,
Some(room_id), Some(room_id),
) )
.await .await
{ {
device_list_changes.insert(user_id); device_list_changes.insert(user_id.to_owned());
} }
}, },
| MembershipState::Leave => { | MembershipState::Leave => {
// Write down users that have left encrypted rooms we // Write down users that have left encrypted rooms we
// are in // are in
left_encrypted_users.insert(user_id); left_encrypted_users.insert(user_id.to_owned());
}, },
| _ => {}, | _ => {},
} }

View file

@ -92,6 +92,8 @@ serde_json.workspace = true
serde_regex.workspace = true serde_regex.workspace = true
serde_yaml.workspace = true serde_yaml.workspace = true
serde.workspace = true serde.workspace = true
smallvec.workspace = true
smallstr.workspace = true
thiserror.workspace = true thiserror.workspace = true
tikv-jemallocator.optional = true tikv-jemallocator.optional = true
tikv-jemallocator.workspace = true tikv-jemallocator.workspace = true

View file

@ -10,14 +10,17 @@ pub mod pdu;
pub mod server; pub mod server;
pub mod utils; pub mod utils;
pub use ::arrayvec;
pub use ::http; pub use ::http;
pub use ::ruma; pub use ::ruma;
pub use ::smallstr;
pub use ::smallvec;
pub use ::toml; pub use ::toml;
pub use ::tracing; pub use ::tracing;
pub use config::Config; pub use config::Config;
pub use error::Error; pub use error::Error;
pub use info::{rustc_flags_capture, version, version::version}; pub use info::{rustc_flags_capture, version, version::version};
pub use pdu::{Event, PduBuilder, PduCount, PduEvent, PduId, RawPduId}; pub use pdu::{Event, PduBuilder, PduCount, PduEvent, PduId, RawPduId, StateKey};
pub use server::Server; pub use server::Server;
pub use utils::{ctor, dtor, implement, result, result::Result}; pub use utils::{ctor, dtor, implement, result, result::Result};

View file

@ -7,6 +7,8 @@ use ruma::{
use serde::Deserialize; use serde::Deserialize;
use serde_json::value::{to_raw_value, RawValue as RawJsonValue}; use serde_json::value::{to_raw_value, RawValue as RawJsonValue};
use super::StateKey;
/// Build the start of a PDU in order to add it to the Database. /// Build the start of a PDU in order to add it to the Database.
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct Builder { pub struct Builder {
@ -17,7 +19,7 @@ pub struct Builder {
pub unsigned: Option<Unsigned>, pub unsigned: Option<Unsigned>,
pub state_key: Option<String>, pub state_key: Option<StateKey>,
pub redacts: Option<OwnedEventId>, pub redacts: Option<OwnedEventId>,
@ -29,15 +31,16 @@ pub struct Builder {
type Unsigned = BTreeMap<String, serde_json::Value>; type Unsigned = BTreeMap<String, serde_json::Value>;
impl Builder { impl Builder {
pub fn state<T>(state_key: String, content: &T) -> Self pub fn state<S, T>(state_key: S, content: &T) -> Self
where where
T: EventContent<EventType = StateEventType>, T: EventContent<EventType = StateEventType>,
S: Into<StateKey>,
{ {
Self { Self {
event_type: content.event_type().into(), event_type: content.event_type().into(),
content: to_raw_value(content) content: to_raw_value(content)
.expect("Builder failed to serialize state event content to RawValue"), .expect("Builder failed to serialize state event content to RawValue"),
state_key: Some(state_key), state_key: Some(state_key.into()),
..Self::default() ..Self::default()
} }
} }

View file

@ -8,6 +8,7 @@ mod id;
mod raw_id; mod raw_id;
mod redact; mod redact;
mod relation; mod relation;
mod state_key;
mod strip; mod strip;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
@ -17,7 +18,7 @@ use std::cmp::Ordering;
use ruma::{ use ruma::{
events::TimelineEventType, CanonicalJsonObject, CanonicalJsonValue, EventId, OwnedEventId, events::TimelineEventType, CanonicalJsonObject, CanonicalJsonValue, EventId, OwnedEventId,
OwnedRoomId, OwnedUserId, UInt, OwnedRoomId, OwnedServerName, OwnedUserId, UInt,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::value::RawValue as RawJsonValue; use serde_json::value::RawValue as RawJsonValue;
@ -29,6 +30,7 @@ pub use self::{
event_id::*, event_id::*,
id::*, id::*,
raw_id::*, raw_id::*,
state_key::{ShortStateKey, StateKey},
Count as PduCount, Id as PduId, Pdu as PduEvent, RawId as RawPduId, Count as PduCount, Id as PduId, Pdu as PduEvent, RawId as RawPduId,
}; };
use crate::Result; use crate::Result;
@ -40,13 +42,13 @@ pub struct Pdu {
pub room_id: OwnedRoomId, pub room_id: OwnedRoomId,
pub sender: OwnedUserId, pub sender: OwnedUserId,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub origin: Option<String>, pub origin: Option<OwnedServerName>,
pub origin_server_ts: UInt, pub origin_server_ts: UInt,
#[serde(rename = "type")] #[serde(rename = "type")]
pub kind: TimelineEventType, pub kind: TimelineEventType,
pub content: Box<RawJsonValue>, pub content: Box<RawJsonValue>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub state_key: Option<String>, pub state_key: Option<StateKey>,
pub prev_events: Vec<OwnedEventId>, pub prev_events: Vec<OwnedEventId>,
pub depth: UInt, pub depth: UInt,
pub auth_events: Vec<OwnedEventId>, pub auth_events: Vec<OwnedEventId>,

View file

@ -0,0 +1,8 @@
use smallstr::SmallString;
use super::ShortId;
pub type StateKey = SmallString<[u8; INLINE_SIZE]>;
pub type ShortStateKey = ShortId;
const INLINE_SIZE: usize = 48;

View file

@ -34,7 +34,6 @@ zstd_compression = [
] ]
[dependencies] [dependencies]
arrayvec.workspace = true
async-channel.workspace = true async-channel.workspace = true
conduwuit-core.workspace = true conduwuit-core.workspace = true
const-str.workspace = true const-str.workspace = true
@ -45,7 +44,6 @@ minicbor-serde.workspace = true
rust-rocksdb.workspace = true rust-rocksdb.workspace = true
serde.workspace = true serde.workspace = true
serde_json.workspace = true serde_json.workspace = true
smallvec.workspace = true
tokio.workspace = true tokio.workspace = true
tracing.workspace = true tracing.workspace = true

View file

@ -1,5 +1,6 @@
use arrayvec::ArrayVec; use conduwuit::{
use conduwuit::{checked, debug::DebugInspect, err, utils::string, Error, Result}; arrayvec::ArrayVec, checked, debug::DebugInspect, err, utils::string, Error, Result,
};
use serde::{ use serde::{
de, de,
de::{DeserializeSeed, Visitor}, de::{DeserializeSeed, Visitor},

View file

@ -1,6 +1,5 @@
use conduwuit::Result; use conduwuit::{smallvec::SmallVec, Result};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use smallvec::SmallVec;
use crate::{de, ser}; use crate::{de, ser};

View file

@ -1,7 +1,7 @@
use std::{convert::AsRef, fmt::Debug, future::Future, io::Write, sync::Arc}; use std::{convert::AsRef, fmt::Debug, future::Future, io::Write, sync::Arc};
use arrayvec::ArrayVec;
use conduwuit::{ use conduwuit::{
arrayvec::ArrayVec,
err, implement, err, implement,
utils::{future::TryExtExt, result::FlatOk}, utils::{future::TryExtExt, result::FlatOk},
Result, Result,

View file

@ -5,8 +5,7 @@
use std::{convert::AsRef, fmt::Debug, io::Write}; use std::{convert::AsRef, fmt::Debug, io::Write};
use arrayvec::ArrayVec; use conduwuit::{arrayvec::ArrayVec, implement};
use conduwuit::implement;
use rocksdb::WriteBatchWithTransaction; use rocksdb::WriteBatchWithTransaction;
use serde::Serialize; use serde::Serialize;

View file

@ -1,7 +1,6 @@
use std::{convert::AsRef, fmt::Debug, io::Write, sync::Arc}; use std::{convert::AsRef, fmt::Debug, io::Write, sync::Arc};
use arrayvec::ArrayVec; use conduwuit::{arrayvec::ArrayVec, implement, Result};
use conduwuit::{implement, Result};
use futures::Future; use futures::Future;
use serde::Serialize; use serde::Serialize;

View file

@ -1,7 +1,6 @@
use std::{convert::AsRef, fmt::Debug, io::Write}; use std::{convert::AsRef, fmt::Debug, io::Write};
use arrayvec::ArrayVec; use conduwuit::{arrayvec::ArrayVec, implement};
use conduwuit::implement;
use serde::Serialize; use serde::Serialize;
use crate::{keyval::KeyBuf, ser, util::or_else}; use crate::{keyval::KeyBuf, ser, util::or_else};

View file

@ -14,6 +14,7 @@ use async_channel::{QueueStrategy, Receiver, RecvError, Sender};
use conduwuit::{ use conduwuit::{
debug, debug_warn, err, error, implement, debug, debug_warn, err, error, implement,
result::DebugInspect, result::DebugInspect,
smallvec::SmallVec,
trace, trace,
utils::sys::compute::{get_affinity, nth_core_available, set_affinity}, utils::sys::compute::{get_affinity, nth_core_available, set_affinity},
Error, Result, Server, Error, Result, Server,
@ -21,7 +22,6 @@ use conduwuit::{
use futures::{channel::oneshot, TryFutureExt}; use futures::{channel::oneshot, TryFutureExt};
use oneshot::Sender as ResultSender; use oneshot::Sender as ResultSender;
use rocksdb::Direction; use rocksdb::Direction;
use smallvec::SmallVec;
use self::configure::configure; use self::configure::configure;
use crate::{keyval::KeyBuf, stream, Handle, Map}; use crate::{keyval::KeyBuf, stream, Handle, Map};

View file

@ -2,8 +2,10 @@
use std::fmt::Debug; use std::fmt::Debug;
use arrayvec::ArrayVec; use conduwuit::{
use conduwuit::ruma::{serde::Raw, EventId, RoomId, UserId}; arrayvec::ArrayVec,
ruma::{serde::Raw, EventId, RoomId, UserId},
};
use serde::Serialize; use serde::Serialize;
use crate::{ use crate::{

View file

@ -47,7 +47,6 @@ zstd_compression = [
blurhashing = ["dep:image","dep:blurhash"] blurhashing = ["dep:image","dep:blurhash"]
[dependencies] [dependencies]
arrayvec.workspace = true
async-trait.workspace = true async-trait.workspace = true
base64.workspace = true base64.workspace = true
bytes.workspace = true bytes.workspace = true
@ -75,7 +74,6 @@ serde_json.workspace = true
serde.workspace = true serde.workspace = true
serde_yaml.workspace = true serde_yaml.workspace = true
sha2.workspace = true sha2.workspace = true
smallvec.workspace = true
termimad.workspace = true termimad.workspace = true
termimad.optional = true termimad.optional = true
tokio.workspace = true tokio.workspace = true

View file

@ -507,8 +507,10 @@ async fn fix_referencedevents_missing_sep(services: &Services) -> Result {
} }
async fn fix_readreceiptid_readreceipt_duplicates(services: &Services) -> Result { async fn fix_readreceiptid_readreceipt_duplicates(services: &Services) -> Result {
use conduwuit::arrayvec::ArrayString;
use ruma::identifiers_validation::MAX_BYTES; use ruma::identifiers_validation::MAX_BYTES;
type ArrayId = arrayvec::ArrayString<MAX_BYTES>;
type ArrayId = ArrayString<MAX_BYTES>;
type Key<'a> = (&'a RoomId, u64, &'a UserId); type Key<'a> = (&'a RoomId, u64, &'a UserId);
warn!("Fixing undeleted entries in readreceiptid_readreceipt..."); warn!("Fixing undeleted entries in readreceiptid_readreceipt...");

View file

@ -1,7 +1,7 @@
use std::{net::IpAddr, sync::Arc, time::SystemTime}; use std::{net::IpAddr, sync::Arc, time::SystemTime};
use arrayvec::ArrayVec;
use conduwuit::{ use conduwuit::{
arrayvec::ArrayVec,
at, err, implement, at, err, implement,
utils::{math::Expected, rand, stream::TryIgnore}, utils::{math::Expected, rand, stream::TryIgnore},
Result, Result,

View file

@ -4,8 +4,7 @@ use std::{
net::{IpAddr, SocketAddr}, net::{IpAddr, SocketAddr},
}; };
use arrayvec::ArrayString; use conduwuit::{arrayvec::ArrayString, utils::math::Expected};
use conduwuit::utils::math::Expected;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)] #[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]

View file

@ -6,8 +6,7 @@ mod tests;
use std::sync::Arc; use std::sync::Arc;
use arrayvec::ArrayString; use conduwuit::{arrayvec::ArrayString, utils::MutexMap, Result, Server};
use conduwuit::{utils::MutexMap, Result, Server};
use self::{cache::Cache, dns::Resolver}; use self::{cache::Cache, dns::Resolver};
use crate::{client, Dep}; use crate::{client, Dep};

View file

@ -6,10 +6,8 @@ use std::{
use conduwuit::{debug, debug_info, err, implement, trace, warn, Err, Error, PduEvent, Result}; use conduwuit::{debug, debug_info, err, implement, trace, warn, Err, Error, PduEvent, Result};
use futures::{future::ready, TryFutureExt}; use futures::{future::ready, TryFutureExt};
use ruma::{ use ruma::{
api::client::error::ErrorKind, api::client::error::ErrorKind, events::StateEventType, state_res, CanonicalJsonObject,
events::StateEventType, CanonicalJsonValue, EventId, RoomId, ServerName,
state_res::{self, EventTypeExt},
CanonicalJsonObject, CanonicalJsonValue, EventId, RoomId, ServerName,
}; };
use super::{check_room_id, get_room_version_id, to_room_version}; use super::{check_room_id, get_room_version_id, to_room_version};
@ -123,7 +121,7 @@ pub(super) async fn handle_outlier_pdu<'a>(
// The original create event must be in the auth events // The original create event must be in the auth events
if !matches!( if !matches!(
auth_events auth_events
.get(&(StateEventType::RoomCreate, String::new())) .get(&(StateEventType::RoomCreate, String::new().into()))
.map(AsRef::as_ref), .map(AsRef::as_ref),
Some(_) | None Some(_) | None
) { ) {
@ -134,7 +132,7 @@ pub(super) async fn handle_outlier_pdu<'a>(
} }
let state_fetch = |ty: &'static StateEventType, sk: &str| { let state_fetch = |ty: &'static StateEventType, sk: &str| {
let key = ty.with_state_key(sk); let key = (ty.to_owned(), sk.into());
ready(auth_events.get(&key)) ready(auth_events.get(&key))
}; };

View file

@ -64,6 +64,7 @@ pub async fn resolve_state(
.multi_get_statekey_from_short(shortstatekeys) .multi_get_statekey_from_short(shortstatekeys)
.zip(event_ids) .zip(event_ids)
.ready_filter_map(|(ty_sk, id)| Some((ty_sk.ok()?, id))) .ready_filter_map(|(ty_sk, id)| Some((ty_sk.ok()?, id)))
.map(|((ty, sk), id)| ((ty, sk.as_str().to_owned()), id))
.collect() .collect()
}) })
.map(Ok::<_, Error>) .map(Ok::<_, Error>)

View file

@ -172,6 +172,7 @@ async fn state_at_incoming_fork(
.short .short
.get_statekey_from_short(*k) .get_statekey_from_short(*k)
.map_ok(|(ty, sk)| ((ty, sk), id.clone())) .map_ok(|(ty, sk)| ((ty, sk), id.clone()))
.map_ok(|((ty, sk), id)| ((ty, sk.as_str().to_owned()), id))
}) })
.ready_filter_map(Result::ok) .ready_filter_map(Result::ok)
.collect() .collect()

View file

@ -1,7 +1,7 @@
use std::{mem::size_of, sync::Arc}; use std::{mem::size_of, sync::Arc};
use arrayvec::ArrayVec;
use conduwuit::{ use conduwuit::{
arrayvec::ArrayVec,
result::LogErr, result::LogErr,
utils::{ utils::{
stream::{TryIgnore, WidebandExt}, stream::{TryIgnore, WidebandExt},

View file

@ -1,7 +1,7 @@
use std::sync::Arc; use std::sync::Arc;
use arrayvec::ArrayVec;
use conduwuit::{ use conduwuit::{
arrayvec::ArrayVec,
implement, implement,
utils::{ utils::{
set, set,

View file

@ -1,7 +1,7 @@
use std::{borrow::Borrow, fmt::Debug, mem::size_of_val, sync::Arc}; use std::{borrow::Borrow, fmt::Debug, mem::size_of_val, sync::Arc};
pub use conduwuit::pdu::{ShortEventId, ShortId, ShortRoomId}; pub use conduwuit::pdu::{ShortEventId, ShortId, ShortRoomId, ShortStateKey};
use conduwuit::{err, implement, utils, utils::IterStream, Result}; use conduwuit::{err, implement, utils, utils::IterStream, Result, StateKey};
use database::{Deserialized, Get, Map, Qry}; use database::{Deserialized, Get, Map, Qry};
use futures::{Stream, StreamExt}; use futures::{Stream, StreamExt};
use ruma::{events::StateEventType, EventId, RoomId}; use ruma::{events::StateEventType, EventId, RoomId};
@ -28,7 +28,6 @@ struct Services {
} }
pub type ShortStateHash = ShortId; pub type ShortStateHash = ShortId;
pub type ShortStateKey = ShortId;
impl crate::Service for Service { impl crate::Service for Service {
fn build(args: crate::Args<'_>) -> Result<Arc<Self>> { fn build(args: crate::Args<'_>) -> Result<Arc<Self>> {
@ -181,7 +180,7 @@ where
pub async fn get_statekey_from_short( pub async fn get_statekey_from_short(
&self, &self,
shortstatekey: ShortStateKey, shortstatekey: ShortStateKey,
) -> Result<(StateEventType, String)> { ) -> Result<(StateEventType, StateKey)> {
const BUFSIZE: usize = size_of::<ShortStateKey>(); const BUFSIZE: usize = size_of::<ShortStateKey>();
self.db self.db
@ -200,7 +199,7 @@ pub async fn get_statekey_from_short(
pub fn multi_get_statekey_from_short<'a, S>( pub fn multi_get_statekey_from_short<'a, S>(
&'a self, &'a self,
shortstatekey: S, shortstatekey: S,
) -> impl Stream<Item = Result<(StateEventType, String)>> + Send + 'a ) -> impl Stream<Item = Result<(StateEventType, StateKey)>> + Send + 'a
where where
S: Stream<Item = ShortStateKey> + Send + 'a, S: Stream<Item = ShortStateKey> + Send + 'a,
{ {

View file

@ -1,6 +1,6 @@
use std::borrow::Borrow; use std::borrow::Borrow;
use conduwuit::{err, implement, PduEvent, Result}; use conduwuit::{err, implement, PduEvent, Result, StateKey};
use futures::{Stream, StreamExt, TryFutureExt}; use futures::{Stream, StreamExt, TryFutureExt};
use ruma::{events::StateEventType, EventId, RoomId}; use ruma::{events::StateEventType, EventId, RoomId};
use serde::Deserialize; use serde::Deserialize;
@ -27,7 +27,7 @@ where
pub fn room_state_full<'a>( pub fn room_state_full<'a>(
&'a self, &'a self,
room_id: &'a RoomId, room_id: &'a RoomId,
) -> impl Stream<Item = Result<((StateEventType, String), PduEvent)>> + Send + 'a { ) -> impl Stream<Item = Result<((StateEventType, StateKey), PduEvent)>> + Send + 'a {
self.services self.services
.state .state
.get_room_shortstatehash(room_id) .get_room_shortstatehash(room_id)

View file

@ -6,7 +6,7 @@ use conduwuit::{
result::FlatOk, result::FlatOk,
stream::{BroadbandExt, IterStream, ReadyExt, TryExpect}, stream::{BroadbandExt, IterStream, ReadyExt, TryExpect},
}, },
PduEvent, Result, PduEvent, Result, StateKey,
}; };
use database::Deserialized; use database::Deserialized;
use futures::{future::try_join, pin_mut, FutureExt, Stream, StreamExt, TryFutureExt}; use futures::{future::try_join, pin_mut, FutureExt, Stream, StreamExt, TryFutureExt};
@ -192,7 +192,7 @@ pub fn state_keys_with_ids<'a, Id>(
&'a self, &'a self,
shortstatehash: ShortStateHash, shortstatehash: ShortStateHash,
event_type: &'a StateEventType, event_type: &'a StateEventType,
) -> impl Stream<Item = (String, Id)> + Send + 'a ) -> impl Stream<Item = (StateKey, Id)> + Send + 'a
where where
Id: for<'de> Deserialize<'de> + Send + Sized + ToOwned + 'a, Id: for<'de> Deserialize<'de> + Send + Sized + ToOwned + 'a,
<Id as ToOwned>::Owned: Borrow<EventId>, <Id as ToOwned>::Owned: Borrow<EventId>,
@ -200,7 +200,7 @@ where
let state_keys_with_short_ids = self let state_keys_with_short_ids = self
.state_keys_with_shortids(shortstatehash, event_type) .state_keys_with_shortids(shortstatehash, event_type)
.unzip() .unzip()
.map(|(ssks, sids): (Vec<String>, Vec<u64>)| (ssks, sids)) .map(|(ssks, sids): (Vec<StateKey>, Vec<u64>)| (ssks, sids))
.shared(); .shared();
let state_keys = state_keys_with_short_ids let state_keys = state_keys_with_short_ids
@ -230,7 +230,7 @@ pub fn state_keys_with_shortids<'a>(
&'a self, &'a self,
shortstatehash: ShortStateHash, shortstatehash: ShortStateHash,
event_type: &'a StateEventType, event_type: &'a StateEventType,
) -> impl Stream<Item = (String, ShortEventId)> + Send + 'a { ) -> impl Stream<Item = (StateKey, ShortEventId)> + Send + 'a {
let short_ids = self let short_ids = self
.state_full_shortids(shortstatehash) .state_full_shortids(shortstatehash)
.expect_ok() .expect_ok()
@ -267,7 +267,7 @@ pub fn state_keys<'a>(
&'a self, &'a self,
shortstatehash: ShortStateHash, shortstatehash: ShortStateHash,
event_type: &'a StateEventType, event_type: &'a StateEventType,
) -> impl Stream<Item = String> + Send + 'a { ) -> impl Stream<Item = StateKey> + Send + 'a {
let short_ids = self let short_ids = self
.state_full_shortids(shortstatehash) .state_full_shortids(shortstatehash)
.expect_ok() .expect_ok()
@ -314,7 +314,7 @@ pub fn state_added(
pub fn state_full( pub fn state_full(
&self, &self,
shortstatehash: ShortStateHash, shortstatehash: ShortStateHash,
) -> impl Stream<Item = ((StateEventType, String), PduEvent)> + Send + '_ { ) -> impl Stream<Item = ((StateEventType, StateKey), PduEvent)> + Send + '_ {
self.state_full_pdus(shortstatehash) self.state_full_pdus(shortstatehash)
.ready_filter_map(|pdu| { .ready_filter_map(|pdu| {
Some(((pdu.kind.to_string().into(), pdu.state_key.clone()?), pdu)) Some(((pdu.kind.to_string().into(), pdu.state_key.clone()?), pdu))

View file

@ -175,7 +175,7 @@ pub async fn user_can_invite(
.timeline .timeline
.create_hash_and_sign_event( .create_hash_and_sign_event(
PduBuilder::state( PduBuilder::state(
target_user.into(), target_user.as_str(),
&RoomMemberEventContent::new(MembershipState::Invite), &RoomMemberEventContent::new(MembershipState::Invite),
), ),
sender, sender,

View file

@ -5,8 +5,8 @@ use std::{
sync::{Arc, Mutex}, sync::{Arc, Mutex},
}; };
use arrayvec::ArrayVec;
use conduwuit::{ use conduwuit::{
arrayvec::ArrayVec,
at, checked, err, expected, utils, at, checked, err, expected, utils,
utils::{bytes, math::usize_from_f64, stream::IterStream}, utils::{bytes, math::usize_from_f64, stream::IterStream},
Result, Result,

View file

@ -38,7 +38,7 @@ use ruma::{
push::{Action, Ruleset, Tweak}, push::{Action, Ruleset, Tweak},
state_res::{self, Event, RoomVersion}, state_res::{self, Event, RoomVersion},
uint, CanonicalJsonObject, CanonicalJsonValue, EventId, OwnedEventId, OwnedRoomId, uint, CanonicalJsonObject, CanonicalJsonValue, EventId, OwnedEventId, OwnedRoomId,
OwnedServerName, OwnedUserId, RoomId, RoomVersionId, ServerName, UserId, OwnedServerName, RoomId, RoomVersionId, ServerName, UserId,
}; };
use serde::Deserialize; use serde::Deserialize;
use serde_json::value::{to_raw_value, RawValue as RawJsonValue}; use serde_json::value::{to_raw_value, RawValue as RawJsonValue};
@ -387,10 +387,10 @@ impl Service {
if pdu.kind == TimelineEventType::RoomMember { if pdu.kind == TimelineEventType::RoomMember {
if let Some(state_key) = &pdu.state_key { if let Some(state_key) = &pdu.state_key {
let target_user_id = OwnedUserId::parse(state_key)?; let target_user_id = UserId::parse(state_key)?;
if self.services.users.is_active_local(&target_user_id).await { if self.services.users.is_active_local(target_user_id).await {
push_target.insert(target_user_id); push_target.insert(target_user_id.to_owned());
} }
} }
} }

View file

@ -13,6 +13,7 @@ use std::{
use async_trait::async_trait; use async_trait::async_trait;
use conduwuit::{ use conduwuit::{
debug, debug_warn, err, error, debug, debug_warn, err, error,
smallvec::SmallVec,
utils::{available_parallelism, math::usize_from_u64_truncated, ReadyExt, TryReadyExt}, utils::{available_parallelism, math::usize_from_u64_truncated, ReadyExt, TryReadyExt},
warn, Result, Server, warn, Result, Server,
}; };
@ -21,7 +22,6 @@ use ruma::{
api::{appservice::Registration, OutgoingRequest}, api::{appservice::Registration, OutgoingRequest},
RoomId, ServerName, UserId, RoomId, ServerName, UserId,
}; };
use smallvec::SmallVec;
use tokio::{task, task::JoinSet}; use tokio::{task, task::JoinSet};
use self::data::Data; use self::data::Data;