2024-07-02 22:40:58 +00:00
|
|
|
use std::{
|
|
|
|
mem::size_of,
|
|
|
|
sync::{Arc, Mutex},
|
|
|
|
};
|
2022-09-06 23:15:09 +02:00
|
|
|
|
2024-09-25 03:52:28 +00:00
|
|
|
use conduit::{err, utils, utils::math::usize_from_f64, Err, Result};
|
2024-07-18 06:37:47 +00:00
|
|
|
use database::Map;
|
2024-07-02 22:40:58 +00:00
|
|
|
use lru_cache::LruCache;
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-05-27 03:17:20 +00:00
|
|
|
pub(super) struct Data {
|
2024-06-28 22:51:39 +00:00
|
|
|
shorteventid_authchain: Arc<Map>,
|
2024-07-02 22:40:58 +00:00
|
|
|
pub(super) auth_chain_cache: Mutex<LruCache<Vec<u64>, Arc<[u64]>>>,
|
2022-07-10 17:23:26 +02:00
|
|
|
}
|
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;
|
2024-07-02 22:40:58 +00:00
|
|
|
let cache_size = f64::from(config.auth_chain_cache_capacity);
|
2024-07-13 16:46:13 -04:00
|
|
|
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(),
|
2024-07-02 22:40:58 +00:00
|
|
|
auth_chain_cache: Mutex::new(LruCache::new(cache_size)),
|
2024-05-27 03:17:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-25 03:52:28 +00:00
|
|
|
pub(super) async fn get_cached_eventid_authchain(&self, key: &[u64]) -> Result<Arc<[u64]>> {
|
|
|
|
debug_assert!(!key.is_empty(), "auth_chain key must not be empty");
|
|
|
|
|
2024-05-26 21:29:19 +00:00
|
|
|
// Check RAM cache
|
2024-09-25 03:52:28 +00:00
|
|
|
if let Some(result) = self
|
|
|
|
.auth_chain_cache
|
|
|
|
.lock()
|
|
|
|
.expect("cache locked")
|
|
|
|
.get_mut(key)
|
|
|
|
{
|
|
|
|
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()
|
|
|
|
.expect("cache locked")
|
|
|
|
.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-09-25 03:52:28 +00:00
|
|
|
pub(super) fn cache_auth_chain(&self, key: Vec<u64>, auth_chain: Arc<[u64]>) {
|
|
|
|
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
|
2024-07-02 22:40:58 +00:00
|
|
|
self.auth_chain_cache
|
2024-05-26 21:29:19 +00:00
|
|
|
.lock()
|
2024-09-25 03:52:28 +00:00
|
|
|
.expect("cache locked")
|
2024-05-26 21:29:19 +00:00
|
|
|
.insert(key, auth_chain);
|
|
|
|
}
|
|
|
|
}
|