mirror of
https://gitlab.computer.surgery/matrix/grapevine.git
synced 2025-12-18 16:21:24 +01:00
move statekeyshort_cache to service
This commit is contained in:
parent
2b2b4169df
commit
190b788683
5 changed files with 79 additions and 64 deletions
|
|
@ -47,18 +47,6 @@ impl service::rooms::short::Data for KeyValueDatabase {
|
|||
event_type: &StateEventType,
|
||||
state_key: &str,
|
||||
) -> Result<Option<ShortStateKey>> {
|
||||
let lookup = Lookup::StateKeyToShort;
|
||||
|
||||
if let Some(short) = self
|
||||
.statekeyshort_cache
|
||||
.lock()
|
||||
.unwrap()
|
||||
.get_mut(&(event_type.clone(), state_key.to_owned()))
|
||||
{
|
||||
METRICS.record_lookup(lookup, FoundIn::Cache);
|
||||
return Ok(Some(*short));
|
||||
}
|
||||
|
||||
let mut db_key = event_type.to_string().as_bytes().to_vec();
|
||||
db_key.push(0xFF);
|
||||
db_key.extend_from_slice(state_key.as_bytes());
|
||||
|
|
@ -75,17 +63,6 @@ impl service::rooms::short::Data for KeyValueDatabase {
|
|||
})
|
||||
.transpose()?;
|
||||
|
||||
if let Some(s) = short {
|
||||
METRICS.record_lookup(lookup, FoundIn::Database);
|
||||
|
||||
self.statekeyshort_cache
|
||||
.lock()
|
||||
.unwrap()
|
||||
.insert((event_type.clone(), state_key.to_owned()), s);
|
||||
} else {
|
||||
METRICS.record_lookup(lookup, FoundIn::Nothing);
|
||||
}
|
||||
|
||||
Ok(short)
|
||||
}
|
||||
|
||||
|
|
@ -94,50 +71,32 @@ impl service::rooms::short::Data for KeyValueDatabase {
|
|||
&self,
|
||||
event_type: &StateEventType,
|
||||
state_key: &str,
|
||||
) -> Result<ShortStateKey> {
|
||||
let lookup = Lookup::CreateStateKeyToShort;
|
||||
|
||||
if let Some(short) = self
|
||||
.statekeyshort_cache
|
||||
.lock()
|
||||
.unwrap()
|
||||
.get_mut(&(event_type.clone(), state_key.to_owned()))
|
||||
{
|
||||
METRICS.record_lookup(lookup, FoundIn::Cache);
|
||||
return Ok(*short);
|
||||
}
|
||||
|
||||
) -> Result<(ShortStateKey, bool)> {
|
||||
let mut db_key = event_type.to_string().as_bytes().to_vec();
|
||||
db_key.push(0xFF);
|
||||
db_key.extend_from_slice(state_key.as_bytes());
|
||||
|
||||
let short = if let Some(shortstatekey) =
|
||||
let (short, created) = if let Some(shortstatekey) =
|
||||
self.statekey_shortstatekey.get(&db_key)?
|
||||
{
|
||||
METRICS.record_lookup(lookup, FoundIn::Database);
|
||||
|
||||
utils::u64_from_bytes(&shortstatekey).map_err(|_| {
|
||||
Error::bad_database("Invalid shortstatekey in db.")
|
||||
})?
|
||||
(
|
||||
utils::u64_from_bytes(&shortstatekey).map_err(|_| {
|
||||
Error::bad_database("Invalid shortstatekey in db.")
|
||||
})?,
|
||||
false,
|
||||
)
|
||||
} else {
|
||||
METRICS.record_lookup(lookup, FoundIn::Nothing);
|
||||
|
||||
let shortstatekey = services().globals.next_count()?;
|
||||
self.statekey_shortstatekey
|
||||
.insert(&db_key, &shortstatekey.to_be_bytes())?;
|
||||
self.shortstatekey_statekey
|
||||
.insert(&shortstatekey.to_be_bytes(), &db_key)?;
|
||||
shortstatekey
|
||||
(shortstatekey, true)
|
||||
};
|
||||
|
||||
let short = ShortStateKey::new(short);
|
||||
|
||||
self.statekeyshort_cache
|
||||
.lock()
|
||||
.unwrap()
|
||||
.insert((event_type.clone(), state_key.to_owned()), short);
|
||||
|
||||
Ok(short)
|
||||
Ok((short, created))
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(self))]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue