messing around with arcs

This commit is contained in:
Timo Kösters 2022-10-05 15:33:57 +02:00 committed by Nyaaori
parent face766e0f
commit cff52d7ebb
No known key found for this signature in database
GPG key ID: E7819C3ED4D1F82E
77 changed files with 598 additions and 434 deletions

View file

@ -1,8 +1,10 @@
use std::sync::Arc;
use ruma::{RoomId, RoomAliasId, api::client::error::ErrorKind};
use crate::{service, database::KeyValueDatabase, utils, Error, services, Result};
impl service::rooms::alias::Data for KeyValueDatabase {
impl service::rooms::alias::Data for Arc<KeyValueDatabase> {
fn set_alias(
&self,
alias: &RoomAliasId,

View file

@ -1,28 +1,60 @@
use std::{collections::HashSet, mem::size_of};
use std::{collections::HashSet, mem::size_of, sync::Arc};
use crate::{service, database::KeyValueDatabase, Result, utils};
impl service::rooms::auth_chain::Data for KeyValueDatabase {
fn get_cached_eventid_authchain(&self, shorteventid: u64) -> Result<Option<HashSet<u64>>> {
Ok(self.shorteventid_authchain
.get(&shorteventid.to_be_bytes())?
.map(|chain| {
chain
.chunks_exact(size_of::<u64>())
.map(|chunk| {
utils::u64_from_bytes(chunk).expect("byte length is correct")
})
.collect()
}))
impl service::rooms::auth_chain::Data for Arc<KeyValueDatabase> {
fn get_cached_eventid_authchain(&self, key: &[u64]) -> Result<Option<Arc<HashSet<u64>>>> {
// Check RAM cache
if let Some(result) = self.auth_chain_cache.lock().unwrap().get_mut(key) {
return Ok(Some(Arc::clone(result)));
}
// We only save auth chains for single events in the db
if key.len() == 1 {
// Check DB cache
let chain = self.shorteventid_authchain
.get(&key[0].to_be_bytes())?
.map(|chain| {
chain
.chunks_exact(size_of::<u64>())
.map(|chunk| {
utils::u64_from_bytes(chunk).expect("byte length is correct")
})
.collect()
});
if let Some(chain) = chain {
let chain = Arc::new(chain);
// Cache in RAM
self.auth_chain_cache
.lock()
.unwrap()
.insert(vec![key[0]], Arc::clone(&chain));
return Ok(Some(chain));
}
}
Ok(None)
}
fn cache_eventid_authchain(&self, shorteventid: u64, auth_chain: &HashSet<u64>) -> Result<()> {
self.shorteventid_authchain.insert(
&shorteventid.to_be_bytes(),
&auth_chain
.iter()
.flat_map(|s| s.to_be_bytes().to_vec())
.collect::<Vec<u8>>(),
)
fn cache_auth_chain(&self, key: Vec<u64>, auth_chain: Arc<HashSet<u64>>) -> Result<()> {
// Only persist single events in db
if key.len() == 1 {
self.shorteventid_authchain.insert(
&key[0].to_be_bytes(),
&auth_chain
.iter()
.flat_map(|s| s.to_be_bytes().to_vec())
.collect::<Vec<u8>>(),
)?;
}
// Cache in RAM
self.auth_chain_cache.lock().unwrap().insert(key, auth_chain);
Ok(())
}
}

View file

@ -1,8 +1,10 @@
use std::sync::Arc;
use ruma::RoomId;
use crate::{service, database::KeyValueDatabase, utils, Error, Result};
impl service::rooms::directory::Data for KeyValueDatabase {
impl service::rooms::directory::Data for Arc<KeyValueDatabase> {
fn set_public(&self, room_id: &RoomId) -> Result<()> {
self.publicroomids.insert(room_id.as_bytes(), &[])
}

View file

@ -2,6 +2,8 @@ mod presence;
mod typing;
mod read_receipt;
use std::sync::Arc;
use crate::{service, database::KeyValueDatabase};
impl service::rooms::edus::Data for KeyValueDatabase {}
impl service::rooms::edus::Data for Arc<KeyValueDatabase> {}

View file

@ -1,10 +1,10 @@
use std::collections::HashMap;
use std::{collections::HashMap, sync::Arc};
use ruma::{UserId, RoomId, events::presence::PresenceEvent, presence::PresenceState, UInt};
use crate::{service, database::KeyValueDatabase, utils, Error, services, Result};
impl service::rooms::edus::presence::Data for KeyValueDatabase {
impl service::rooms::edus::presence::Data for Arc<KeyValueDatabase> {
fn update_presence(
&self,
user_id: &UserId,

View file

@ -1,10 +1,10 @@
use std::mem;
use std::{mem, sync::Arc};
use ruma::{UserId, RoomId, events::receipt::ReceiptEvent, serde::Raw, signatures::CanonicalJsonObject};
use crate::{database::KeyValueDatabase, service, utils, Error, services, Result};
impl service::rooms::edus::read_receipt::Data for KeyValueDatabase {
impl service::rooms::edus::read_receipt::Data for Arc<KeyValueDatabase> {
fn readreceipt_update(
&self,
user_id: &UserId,

View file

@ -1,10 +1,10 @@
use std::collections::HashSet;
use std::{collections::HashSet, sync::Arc};
use ruma::{UserId, RoomId};
use crate::{database::KeyValueDatabase, service, utils, Error, services, Result};
impl service::rooms::edus::typing::Data for KeyValueDatabase {
impl service::rooms::edus::typing::Data for Arc<KeyValueDatabase> {
fn typing_add(
&self,
user_id: &UserId,

View file

@ -1,8 +1,10 @@
use std::sync::Arc;
use ruma::{UserId, DeviceId, RoomId};
use crate::{service, database::KeyValueDatabase, Result};
impl service::rooms::lazy_loading::Data for KeyValueDatabase {
impl service::rooms::lazy_loading::Data for Arc<KeyValueDatabase> {
fn lazy_load_was_sent_before(
&self,
user_id: &UserId,

View file

@ -1,8 +1,10 @@
use std::sync::Arc;
use ruma::RoomId;
use crate::{service, database::KeyValueDatabase, Result, services};
impl service::rooms::metadata::Data for KeyValueDatabase {
impl service::rooms::metadata::Data for Arc<KeyValueDatabase> {
fn exists(&self, room_id: &RoomId) -> Result<bool> {
let prefix = match services().rooms.short.get_shortroomid(room_id)? {
Some(b) => b.to_be_bytes().to_vec(),

View file

@ -15,6 +15,8 @@ mod state_compressor;
mod timeline;
mod user;
use std::sync::Arc;
use crate::{database::KeyValueDatabase, service};
impl service::rooms::Data for KeyValueDatabase {}
impl service::rooms::Data for Arc<KeyValueDatabase> {}

View file

@ -1,8 +1,10 @@
use std::sync::Arc;
use ruma::{EventId, signatures::CanonicalJsonObject};
use crate::{service, database::KeyValueDatabase, PduEvent, Error, Result};
impl service::rooms::outlier::Data for KeyValueDatabase {
impl service::rooms::outlier::Data for Arc<KeyValueDatabase> {
fn get_outlier_pdu_json(&self, event_id: &EventId) -> Result<Option<CanonicalJsonObject>> {
self.eventid_outlierpdu
.get(event_id.as_bytes())?

View file

@ -4,7 +4,7 @@ use ruma::{RoomId, EventId};
use crate::{service, database::KeyValueDatabase, Result};
impl service::rooms::pdu_metadata::Data for KeyValueDatabase {
impl service::rooms::pdu_metadata::Data for Arc<KeyValueDatabase> {
fn mark_as_referenced(&self, room_id: &RoomId, event_ids: &[Arc<EventId>]) -> Result<()> {
for prev in event_ids {
let mut key = room_id.as_bytes().to_vec();

View file

@ -1,10 +1,10 @@
use std::mem::size_of;
use std::{mem::size_of, sync::Arc};
use ruma::RoomId;
use crate::{service, database::KeyValueDatabase, utils, Result, services};
impl service::rooms::search::Data for KeyValueDatabase {
impl service::rooms::search::Data for Arc<KeyValueDatabase> {
fn index_pdu<'a>(&self, shortroomid: u64, pdu_id: &[u8], message_body: String) -> Result<()> {
let mut batch = message_body
.split_terminator(|c: char| !c.is_alphanumeric())

View file

@ -1,4 +1,6 @@
use std::sync::Arc;
use crate::{database::KeyValueDatabase, service};
impl service::rooms::short::Data for KeyValueDatabase {
impl service::rooms::short::Data for Arc<KeyValueDatabase> {
}

View file

@ -1,11 +1,12 @@
use ruma::{RoomId, EventId};
use tokio::sync::MutexGuard;
use std::sync::Arc;
use std::{sync::MutexGuard, collections::HashSet};
use std::collections::HashSet;
use std::fmt::Debug;
use crate::{service, database::KeyValueDatabase, utils, Error, Result};
impl service::rooms::state::Data for KeyValueDatabase {
impl service::rooms::state::Data for Arc<KeyValueDatabase> {
fn get_room_shortstatehash(&self, room_id: &RoomId) -> Result<Option<u64>> {
self.roomid_shortstatehash
.get(room_id.as_bytes())?
@ -48,7 +49,7 @@ impl service::rooms::state::Data for KeyValueDatabase {
fn set_forward_extremities<'a>(
&self,
room_id: &RoomId,
event_ids: impl IntoIterator<Item = &'a EventId> + Debug,
event_ids: &mut dyn Iterator<Item = &'a EventId>,
_mutex_lock: &MutexGuard<'_, ()>, // Take mutex guard to make sure users get the room state mutex
) -> Result<()> {
let mut prefix = room_id.as_bytes().to_vec();

View file

@ -5,7 +5,7 @@ use async_trait::async_trait;
use ruma::{EventId, events::StateEventType, RoomId};
#[async_trait]
impl service::rooms::state_accessor::Data for KeyValueDatabase {
impl service::rooms::state_accessor::Data for Arc<KeyValueDatabase> {
async fn state_full_ids(&self, shortstatehash: u64) -> Result<BTreeMap<u64, Arc<EventId>>> {
let full_state = services().rooms.state_compressor
.load_shortstatehash_info(shortstatehash)?

View file

@ -1,8 +1,10 @@
use std::sync::Arc;
use ruma::{UserId, RoomId, events::{AnyStrippedStateEvent, AnySyncStateEvent}, serde::Raw};
use crate::{service, database::KeyValueDatabase, services, Result};
impl service::rooms::state_cache::Data for KeyValueDatabase {
impl service::rooms::state_cache::Data for Arc<KeyValueDatabase> {
fn mark_as_once_joined(&self, user_id: &UserId, room_id: &RoomId) -> Result<()> {
let mut userroom_id = user_id.as_bytes().to_vec();
userroom_id.push(0xff);

View file

@ -1,8 +1,8 @@
use std::{collections::HashSet, mem::size_of};
use std::{collections::HashSet, mem::size_of, sync::Arc};
use crate::{service::{self, rooms::state_compressor::data::StateDiff}, database::KeyValueDatabase, Error, utils, Result};
impl service::rooms::state_compressor::Data for KeyValueDatabase {
impl service::rooms::state_compressor::Data for Arc<KeyValueDatabase> {
fn get_statediff(&self, shortstatehash: u64) -> Result<StateDiff> {
let value = self
.shortstatehash_statediff

View file

@ -5,7 +5,7 @@ use tracing::error;
use crate::{service, database::KeyValueDatabase, utils, Error, PduEvent, Result, services};
impl service::rooms::timeline::Data for KeyValueDatabase {
impl service::rooms::timeline::Data for Arc<KeyValueDatabase> {
fn last_timeline_count(&self, sender_user: &UserId, room_id: &RoomId) -> Result<u64> {
match self
.lasttimelinecount_cache

View file

@ -1,8 +1,10 @@
use std::sync::Arc;
use ruma::{UserId, RoomId};
use crate::{service, database::KeyValueDatabase, utils, Error, Result, services};
impl service::rooms::user::Data for KeyValueDatabase {
impl service::rooms::user::Data for Arc<KeyValueDatabase> {
fn reset_notification_counts(&self, user_id: &UserId, room_id: &RoomId) -> Result<()> {
let mut userroom_id = user_id.as_bytes().to_vec();
userroom_id.push(0xff);