mirror of
https://gitlab.computer.surgery/matrix/grapevine.git
synced 2025-12-16 15:21:24 +01:00
move eventidshort_cache to service
This commit is contained in:
parent
095ee483ac
commit
2b2b4169df
5 changed files with 49 additions and 39 deletions
|
|
@ -13,7 +13,7 @@ use ruma::{
|
|||
push_rules::PushRulesEvent, GlobalAccountDataEventType, StateEventType,
|
||||
},
|
||||
push::Ruleset,
|
||||
EventId, OwnedEventId, OwnedRoomId, OwnedUserId, RoomId, UserId,
|
||||
EventId, OwnedRoomId, OwnedUserId, RoomId, UserId,
|
||||
};
|
||||
use tracing::{debug, error, info, info_span, warn, Instrument};
|
||||
|
||||
|
|
@ -236,7 +236,6 @@ pub(crate) struct KeyValueDatabase {
|
|||
pub(super) senderkey_pusher: Arc<dyn KvTree>,
|
||||
|
||||
// Uncategorized trees
|
||||
pub(super) eventidshort_cache: Mutex<LruCache<OwnedEventId, ShortEventId>>,
|
||||
pub(super) statekeyshort_cache:
|
||||
Mutex<LruCache<(StateEventType, String), ShortStateKey>>,
|
||||
pub(super) shortstatekey_cache:
|
||||
|
|
@ -464,14 +463,6 @@ impl KeyValueDatabase {
|
|||
global: builder.open_tree("global")?,
|
||||
server_signingkeys: builder.open_tree("server_signingkeys")?,
|
||||
|
||||
#[allow(
|
||||
clippy::as_conversions,
|
||||
clippy::cast_sign_loss,
|
||||
clippy::cast_possible_truncation
|
||||
)]
|
||||
eventidshort_cache: Mutex::new(LruCache::new(
|
||||
(100_000.0 * config.cache_capacity_modifier) as usize,
|
||||
)),
|
||||
#[allow(
|
||||
clippy::as_conversions,
|
||||
clippy::cast_sign_loss,
|
||||
|
|
|
|||
|
|
@ -19,43 +19,26 @@ impl service::rooms::short::Data for KeyValueDatabase {
|
|||
fn get_or_create_shorteventid(
|
||||
&self,
|
||||
event_id: &EventId,
|
||||
) -> Result<ShortEventId> {
|
||||
let lookup = Lookup::CreateEventIdToShort;
|
||||
|
||||
if let Some(short) =
|
||||
self.eventidshort_cache.lock().unwrap().get_mut(event_id)
|
||||
{
|
||||
METRICS.record_lookup(lookup, FoundIn::Cache);
|
||||
return Ok(*short);
|
||||
}
|
||||
|
||||
let short = if let Some(shorteventid) =
|
||||
) -> Result<(ShortEventId, bool)> {
|
||||
let (short, created) = if let Some(shorteventid) =
|
||||
self.eventid_shorteventid.get(event_id.as_bytes())?
|
||||
{
|
||||
METRICS.record_lookup(lookup, FoundIn::Database);
|
||||
let shorteventid =
|
||||
utils::u64_from_bytes(&shorteventid).map_err(|_| {
|
||||
Error::bad_database("Invalid shorteventid in db.")
|
||||
})?;
|
||||
|
||||
utils::u64_from_bytes(&shorteventid).map_err(|_| {
|
||||
Error::bad_database("Invalid shorteventid in db.")
|
||||
})?
|
||||
(shorteventid, false)
|
||||
} else {
|
||||
METRICS.record_lookup(lookup, FoundIn::Nothing);
|
||||
|
||||
let shorteventid = services().globals.next_count()?;
|
||||
self.eventid_shorteventid
|
||||
.insert(event_id.as_bytes(), &shorteventid.to_be_bytes())?;
|
||||
self.shorteventid_eventid
|
||||
.insert(&shorteventid.to_be_bytes(), event_id.as_bytes())?;
|
||||
shorteventid
|
||||
(shorteventid, true)
|
||||
};
|
||||
|
||||
let short = ShortEventId::new(short);
|
||||
|
||||
self.eventidshort_cache
|
||||
.lock()
|
||||
.unwrap()
|
||||
.insert(event_id.to_owned(), short);
|
||||
|
||||
Ok(short)
|
||||
Ok((ShortEventId::new(short), created))
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self), fields(cache_result))]
|
||||
|
|
|
|||
|
|
@ -112,6 +112,14 @@ impl Services {
|
|||
{
|
||||
(100_000.0 * config.cache_capacity_modifier) as usize
|
||||
},
|
||||
#[allow(
|
||||
clippy::as_conversions,
|
||||
clippy::cast_sign_loss,
|
||||
clippy::cast_possible_truncation
|
||||
)]
|
||||
{
|
||||
(100_000.0 * config.cache_capacity_modifier) as usize
|
||||
},
|
||||
),
|
||||
state: rooms::state::Service {
|
||||
db,
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use lru_cache::LruCache;
|
||||
use ruma::{events::StateEventType, EventId, RoomId};
|
||||
use ruma::{events::StateEventType, EventId, OwnedEventId, RoomId};
|
||||
|
||||
use crate::{
|
||||
observability::{FoundIn, Lookup, METRICS},
|
||||
|
|
@ -38,18 +38,23 @@ pub(crate) use data::Data;
|
|||
pub(crate) struct Service {
|
||||
db: &'static dyn Data,
|
||||
shorteventid_cache: Mutex<LruCache<ShortEventId, Arc<EventId>>>,
|
||||
eventidshort_cache: Mutex<LruCache<OwnedEventId, ShortEventId>>,
|
||||
}
|
||||
|
||||
impl Service {
|
||||
pub(crate) fn new(
|
||||
db: &'static dyn Data,
|
||||
shorteventid_cache_size: usize,
|
||||
eventidshort_cache_size: usize,
|
||||
) -> Self {
|
||||
Self {
|
||||
db,
|
||||
shorteventid_cache: Mutex::new(LruCache::new(
|
||||
shorteventid_cache_size,
|
||||
)),
|
||||
eventidshort_cache: Mutex::new(LruCache::new(
|
||||
eventidshort_cache_size,
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -57,7 +62,29 @@ impl Service {
|
|||
&self,
|
||||
event_id: &EventId,
|
||||
) -> Result<ShortEventId> {
|
||||
self.db.get_or_create_shorteventid(event_id)
|
||||
let lookup = Lookup::CreateEventIdToShort;
|
||||
|
||||
if let Some(short) =
|
||||
self.eventidshort_cache.lock().unwrap().get_mut(event_id)
|
||||
{
|
||||
METRICS.record_lookup(lookup, FoundIn::Cache);
|
||||
return Ok(*short);
|
||||
}
|
||||
|
||||
let (short, created) = self.db.get_or_create_shorteventid(event_id)?;
|
||||
|
||||
if created {
|
||||
METRICS.record_lookup(lookup, FoundIn::Nothing);
|
||||
} else {
|
||||
METRICS.record_lookup(lookup, FoundIn::Database);
|
||||
}
|
||||
|
||||
self.eventidshort_cache
|
||||
.lock()
|
||||
.unwrap()
|
||||
.insert(event_id.to_owned(), short);
|
||||
|
||||
Ok(short)
|
||||
}
|
||||
|
||||
pub(crate) fn get_shortstatekey(
|
||||
|
|
|
|||
|
|
@ -6,10 +6,11 @@ use super::{ShortEventId, ShortRoomId, ShortStateHash, ShortStateKey};
|
|||
use crate::Result;
|
||||
|
||||
pub(crate) trait Data: Send + Sync {
|
||||
/// The returned bool indicates whether it was created
|
||||
fn get_or_create_shorteventid(
|
||||
&self,
|
||||
event_id: &EventId,
|
||||
) -> Result<ShortEventId>;
|
||||
) -> Result<(ShortEventId, bool)>;
|
||||
|
||||
fn get_shortstatekey(
|
||||
&self,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue