Files
continuwuity/src/service/rooms/auth_chain/data.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

81 lines
2.1 KiB
Rust
Raw Normal View History

2025-07-19 22:05:43 +01:00
use std::{mem::size_of, sync::Arc};
2022-09-06 23:15:09 +02:00
2025-07-19 22:05:43 +01:00
use conduwuit::{Err, Result, SyncMutex, err, utils, utils::math::usize_from_f64};
2024-07-18 06:37:47 +00:00
use database::Map;
use lru_cache::LruCache;
2024-03-05 19:48:54 -05:00
2024-11-02 06:12:54 +00:00
use crate::rooms::short::ShortEventId;
2024-05-27 03:17:20 +00:00
pub(super) struct Data {
2024-06-28 22:51:39 +00:00
shorteventid_authchain: Arc<Map>,
2025-07-19 22:05:43 +01:00
pub(super) auth_chain_cache: SyncMutex<LruCache<Vec<u64>, Arc<[ShortEventId]>>>,
}
2024-05-26 21:29:19 +00:00
2024-05-27 03:17:20 +00:00
impl Data {
2024-07-18 06:37:47 +00:00
pub(super) fn new(args: &crate::Args<'_>) -> Self {
let db = &args.db;
let config = &args.server.config;
let cache_size = f64::from(config.auth_chain_cache_capacity);
let cache_size = usize_from_f64(cache_size * config.cache_capacity_modifier)
.expect("valid cache size");
2024-05-27 03:17:20 +00:00
Self {
2024-06-28 22:51:39 +00:00
shorteventid_authchain: db["shorteventid_authchain"].clone(),
2025-07-19 22:05:43 +01:00
auth_chain_cache: SyncMutex::new(LruCache::new(cache_size)),
2024-05-27 03:17:20 +00:00
}
}
pub(super) async fn get_cached_eventid_authchain(
&self,
key: &[u64],
) -> Result<Arc<[ShortEventId]>> {
2024-09-25 03:52:28 +00:00
debug_assert!(!key.is_empty(), "auth_chain key must not be empty");
2024-05-26 21:29:19 +00:00
// Check RAM cache
2025-07-19 22:05:43 +01:00
if let Some(result) = self.auth_chain_cache.lock().get_mut(key) {
2024-09-25 03:52:28 +00:00
return Ok(Arc::clone(result));
2024-05-26 21:29:19 +00:00
}
// We only save auth chains for single events in the db
2024-09-25 03:52:28 +00:00
if key.len() != 1 {
return Err!(Request(NotFound("auth_chain not cached")));
}
2024-05-26 21:29:19 +00:00
2024-09-25 03:52:28 +00:00
// Check database
let chain = self
.shorteventid_authchain
.qry(&key[0])
.await
.map_err(|_| err!(Request(NotFound("auth_chain not found"))))?;
2024-05-26 21:29:19 +00:00
2024-09-25 03:52:28 +00:00
let chain = chain
.chunks_exact(size_of::<u64>())
.map(utils::u64_from_u8)
.collect::<Arc<[u64]>>();
// Cache in RAM
self.auth_chain_cache
.lock()
.insert(vec![key[0]], Arc::clone(&chain));
2024-05-26 21:29:19 +00:00
2024-09-25 03:52:28 +00:00
Ok(chain)
2024-05-26 21:29:19 +00:00
}
2024-11-02 06:12:54 +00:00
pub(super) fn cache_auth_chain(&self, key: Vec<u64>, auth_chain: Arc<[ShortEventId]>) {
2024-09-25 03:52:28 +00:00
debug_assert!(!key.is_empty(), "auth_chain key must not be empty");
2024-05-26 21:29:19 +00:00
// Only persist single events in db
if key.len() == 1 {
2024-09-25 03:52:28 +00:00
let key = key[0].to_be_bytes();
let val = auth_chain
.iter()
.flat_map(|s| s.to_be_bytes().to_vec())
.collect::<Vec<u8>>();
self.shorteventid_authchain.insert(&key, &val);
2024-05-26 21:29:19 +00:00
}
// Cache in RAM
2025-07-19 22:05:43 +01:00
self.auth_chain_cache.lock().insert(key, auth_chain);
2024-05-26 21:29:19 +00:00
}
}