Database Refactor
combine service/users data w/ mod unit split sliding sync related out of service/users instrument database entry points remove increment crap from database interface de-wrap all database get() calls de-wrap all database insert() calls de-wrap all database remove() calls refactor database interface for async streaming add query key serializer for database implement Debug for result handle add query deserializer for database add deserialization trait for option handle start a stream utils suite de-wrap/asyncify/type-query count_one_time_keys() de-wrap/asyncify users count add admin query users command suite de-wrap/asyncify users exists de-wrap/partially asyncify user filter related asyncify/de-wrap users device/keys related asyncify/de-wrap user auth/misc related asyncify/de-wrap users blurhash asyncify/de-wrap account_data get; merge Data into Service partial asyncify/de-wrap uiaa; merge Data into Service partially asyncify/de-wrap transaction_ids get; merge Data into Service partially asyncify/de-wrap key_backups; merge Data into Service asyncify/de-wrap pusher service getters; merge Data into Service asyncify/de-wrap rooms alias getters/some iterators asyncify/de-wrap rooms directory getters/iterator partially asyncify/de-wrap rooms lazy-loading partially asyncify/de-wrap rooms metadata asyncify/dewrap rooms outlier asyncify/dewrap rooms pdu_metadata dewrap/partially asyncify rooms read receipt de-wrap rooms search service de-wrap/partially asyncify rooms user service partial de-wrap rooms state_compressor de-wrap rooms state_cache de-wrap room state et al de-wrap rooms timeline service additional users device/keys related de-wrap/asyncify sender asyncify services refactor database to TryFuture/TryStream refactor services for TryFuture/TryStream asyncify api handlers additional asyncification for admin module abstract stream related; support reverse streams additional stream conversions asyncify state-res related Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
6001014078
commit
946ca364e0
203 changed files with 12202 additions and 10709 deletions
|
@ -1,7 +1,8 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use conduit::{utils, Error, Result};
|
||||
use database::{Database, Map};
|
||||
use conduit::{err, utils::stream::TryIgnore, Result};
|
||||
use database::{Database, Deserialized, Map};
|
||||
use futures::Stream;
|
||||
use ruma::api::appservice::Registration;
|
||||
|
||||
pub struct Data {
|
||||
|
@ -19,7 +20,7 @@ impl Data {
|
|||
pub(super) fn register_appservice(&self, yaml: &Registration) -> Result<String> {
|
||||
let id = yaml.id.as_str();
|
||||
self.id_appserviceregistrations
|
||||
.insert(id.as_bytes(), serde_yaml::to_string(&yaml).unwrap().as_bytes())?;
|
||||
.insert(id.as_bytes(), serde_yaml::to_string(&yaml).unwrap().as_bytes());
|
||||
|
||||
Ok(id.to_owned())
|
||||
}
|
||||
|
@ -31,24 +32,19 @@ impl Data {
|
|||
/// * `service_name` - the name you send to register the service previously
|
||||
pub(super) fn unregister_appservice(&self, service_name: &str) -> Result<()> {
|
||||
self.id_appserviceregistrations
|
||||
.remove(service_name.as_bytes())?;
|
||||
.remove(service_name.as_bytes());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_registration(&self, id: &str) -> Result<Option<Registration>> {
|
||||
pub async fn get_registration(&self, id: &str) -> Result<Registration> {
|
||||
self.id_appserviceregistrations
|
||||
.get(id.as_bytes())?
|
||||
.map(|bytes| {
|
||||
serde_yaml::from_slice(&bytes)
|
||||
.map_err(|_| Error::bad_database("Invalid registration bytes in id_appserviceregistrations."))
|
||||
})
|
||||
.transpose()
|
||||
.qry(id)
|
||||
.await
|
||||
.deserialized_json()
|
||||
.map_err(|e| err!(Database("Invalid appservice {id:?} registration: {e:?}")))
|
||||
}
|
||||
|
||||
pub(super) fn iter_ids<'a>(&'a self) -> Result<Box<dyn Iterator<Item = Result<String>> + 'a>> {
|
||||
Ok(Box::new(self.id_appserviceregistrations.iter().map(|(id, _)| {
|
||||
utils::string_from_bytes(&id)
|
||||
.map_err(|_| Error::bad_database("Invalid id bytes in id_appserviceregistrations."))
|
||||
})))
|
||||
pub(super) fn iter_ids(&self) -> impl Stream<Item = String> + Send + '_ {
|
||||
self.id_appserviceregistrations.keys().ignore_err()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,9 +2,10 @@ mod data;
|
|||
|
||||
use std::{collections::BTreeMap, sync::Arc};
|
||||
|
||||
use async_trait::async_trait;
|
||||
use conduit::{err, Result};
|
||||
use data::Data;
|
||||
use futures_util::Future;
|
||||
use futures::{Future, StreamExt, TryStreamExt};
|
||||
use regex::RegexSet;
|
||||
use ruma::{
|
||||
api::appservice::{Namespace, Registration},
|
||||
|
@ -126,13 +127,22 @@ struct Services {
|
|||
sending: Dep<sending::Service>,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl crate::Service for Service {
|
||||
fn build(args: crate::Args<'_>) -> Result<Arc<Self>> {
|
||||
let mut registration_info = BTreeMap::new();
|
||||
let db = Data::new(args.db);
|
||||
Ok(Arc::new(Self {
|
||||
db: Data::new(args.db),
|
||||
services: Services {
|
||||
sending: args.depend::<sending::Service>("sending"),
|
||||
},
|
||||
registration_info: RwLock::new(BTreeMap::new()),
|
||||
}))
|
||||
}
|
||||
|
||||
async fn worker(self: Arc<Self>) -> Result<()> {
|
||||
// Inserting registrations into cache
|
||||
for appservice in iter_ids(&db)? {
|
||||
registration_info.insert(
|
||||
for appservice in iter_ids(&self.db).await? {
|
||||
self.registration_info.write().await.insert(
|
||||
appservice.0,
|
||||
appservice
|
||||
.1
|
||||
|
@ -141,13 +151,7 @@ impl crate::Service for Service {
|
|||
);
|
||||
}
|
||||
|
||||
Ok(Arc::new(Self {
|
||||
db,
|
||||
services: Services {
|
||||
sending: args.depend::<sending::Service>("sending"),
|
||||
},
|
||||
registration_info: RwLock::new(registration_info),
|
||||
}))
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
|
||||
|
@ -155,7 +159,7 @@ impl crate::Service for Service {
|
|||
|
||||
impl Service {
|
||||
#[inline]
|
||||
pub fn all(&self) -> Result<Vec<(String, Registration)>> { iter_ids(&self.db) }
|
||||
pub async fn all(&self) -> Result<Vec<(String, Registration)>> { iter_ids(&self.db).await }
|
||||
|
||||
/// Registers an appservice and returns the ID to the caller
|
||||
pub async fn register_appservice(&self, yaml: Registration) -> Result<String> {
|
||||
|
@ -188,7 +192,8 @@ impl Service {
|
|||
// sending to the URL
|
||||
self.services
|
||||
.sending
|
||||
.cleanup_events(service_name.to_owned())?;
|
||||
.cleanup_events(service_name.to_owned())
|
||||
.await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -251,15 +256,9 @@ impl Service {
|
|||
}
|
||||
}
|
||||
|
||||
fn iter_ids(db: &Data) -> Result<Vec<(String, Registration)>> {
|
||||
db.iter_ids()?
|
||||
.filter_map(Result::ok)
|
||||
.map(move |id| {
|
||||
Ok((
|
||||
id.clone(),
|
||||
db.get_registration(&id)?
|
||||
.expect("iter_ids only returns appservices that exist"),
|
||||
))
|
||||
})
|
||||
.collect()
|
||||
async fn iter_ids(db: &Data) -> Result<Vec<(String, Registration)>> {
|
||||
db.iter_ids()
|
||||
.then(|id| async move { Ok((id.clone(), db.get_registration(&id).await?)) })
|
||||
.try_collect()
|
||||
.await
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue