move lasttimelinecount_cache to service

This commit is contained in:
Charles Hall 2024-10-08 14:40:54 -07:00
parent 107f4521e0
commit ce7efc1eff
No known key found for this signature in database
GPG key ID: 7B8E0645816E07CF
4 changed files with 41 additions and 65 deletions

View file

@ -1,59 +1,18 @@
use std::{collections::hash_map, mem::size_of, sync::Arc};
use std::{mem::size_of, sync::Arc};
use ruma::{
api::client::error::ErrorKind, CanonicalJsonObject, EventId, OwnedUserId,
RoomId, UserId,
};
use service::rooms::timeline::PduCount;
use tracing::error;
use crate::{
database::KeyValueDatabase,
observability::{FoundIn, Lookup, METRICS},
service::{self, rooms::timeline::PduId},
services, utils, Error, PduEvent, Result,
};
impl service::rooms::timeline::Data for KeyValueDatabase {
#[tracing::instrument(skip(self))]
fn last_timeline_count(
&self,
sender_user: &UserId,
room_id: &RoomId,
) -> Result<PduCount> {
let lookup = Lookup::LastTimelineCount;
match self
.lasttimelinecount_cache
.lock()
.unwrap()
.entry(room_id.to_owned())
{
hash_map::Entry::Vacant(v) => {
if let Some(last_count) = self
.pdus_until(sender_user, room_id, PduCount::MAX)?
.find_map(|x| match x {
Ok(x) => Some(x),
Err(error) => {
error!(%error, "Bad pdu in pdus_since");
None
}
})
{
METRICS.record_lookup(lookup, FoundIn::Database);
Ok(*v.insert(last_count.0))
} else {
METRICS.record_lookup(lookup, FoundIn::Nothing);
Ok(PduCount::Normal(0))
}
}
hash_map::Entry::Occupied(o) => {
METRICS.record_lookup(lookup, FoundIn::Cache);
Ok(*o.get())
}
}
}
/// Returns the `count` of this pdu's id.
fn get_pdu_count(&self, event_id: &EventId) -> Result<Option<PduCount>> {
self.eventid_pduid
@ -180,7 +139,6 @@ impl service::rooms::timeline::Data for KeyValueDatabase {
pdu_id: &PduId,
pdu: &PduEvent,
json: &CanonicalJsonObject,
count: u64,
) -> Result<()> {
self.pduid_pdu.insert(
pdu_id.as_bytes(),
@ -188,11 +146,6 @@ impl service::rooms::timeline::Data for KeyValueDatabase {
.expect("CanonicalJsonObject is always a valid"),
)?;
self.lasttimelinecount_cache
.lock()
.unwrap()
.insert(pdu.room_id.clone(), PduCount::Normal(count));
self.eventid_pduid
.insert(pdu.event_id.as_bytes(), pdu_id.as_bytes())?;
self.eventid_outlierpdu.remove(pdu.event_id.as_bytes())?;