Private
Public Access
1
0

ContactResolver: implement in-memory cache for positive results

This commit is contained in:
2025-06-26 18:50:58 -07:00
parent e73cf321c0
commit 9e3e6dc66f
3 changed files with 29 additions and 7 deletions

View File

@@ -1,6 +1,8 @@
pub mod eds; pub mod eds;
pub use eds::EDSContactResolverBackend; pub use eds::EDSContactResolverBackend;
use std::collections::HashMap;
pub trait ContactResolverBackend { pub trait ContactResolverBackend {
type ContactID; type ContactID;
@@ -13,6 +15,8 @@ pub type AnyContactID = String;
#[derive(Clone)] #[derive(Clone)]
pub struct ContactResolver<T: ContactResolverBackend> { pub struct ContactResolver<T: ContactResolverBackend> {
backend: T, backend: T,
display_name_cache: HashMap<AnyContactID, String>,
contact_id_cache: HashMap<String, AnyContactID>,
} }
impl<T: ContactResolverBackend> ContactResolver<T> impl<T: ContactResolverBackend> ContactResolver<T>
@@ -22,16 +26,34 @@ where
T: Default, T: Default,
{ {
pub fn new(backend: T) -> Self { pub fn new(backend: T) -> Self {
Self { backend } Self { backend, display_name_cache: HashMap::new(), contact_id_cache: HashMap::new() }
} }
pub fn resolve_contact_id(&self, address: &str) -> Option<AnyContactID> { pub fn resolve_contact_id(&mut self, address: &str) -> Option<AnyContactID> {
self.backend.resolve_contact_id(address).map(|id| id.into()) if let Some(id) = self.contact_id_cache.get(address) {
return Some(id.clone());
}
let id = self.backend.resolve_contact_id(address).map(|id| id.into());
if let Some(ref id) = id {
self.contact_id_cache.insert(address.to_string(), id.clone());
}
id
} }
pub fn get_contact_display_name(&self, contact_id: &AnyContactID) -> Option<String> { pub fn get_contact_display_name(&mut self, contact_id: &AnyContactID) -> Option<String> {
if let Some(display_name) = self.display_name_cache.get(contact_id) {
return Some(display_name.clone());
}
let backend_contact_id: T::ContactID = T::ContactID::from((*contact_id).clone()); let backend_contact_id: T::ContactID = T::ContactID::from((*contact_id).clone());
self.backend.get_contact_display_name(&backend_contact_id) let display_name = self.backend.get_contact_display_name(&backend_contact_id);
if let Some(ref display_name) = display_name {
self.display_name_cache.insert(contact_id.to_string(), display_name.clone());
}
display_name
} }
} }

View File

@@ -507,7 +507,7 @@ impl Daemon {
// Insert each conversation // Insert each conversation
let num_conversations = db_conversations.len(); let num_conversations = db_conversations.len();
let contact_resolver = ContactResolver::new(EDSContactResolverBackend::default()); let mut contact_resolver = ContactResolver::new(EDSContactResolverBackend::default());
for conversation in db_conversations { for conversation in db_conversations {
// Insert or update conversation and its participants // Insert or update conversation and its participants
database database

View File

@@ -173,7 +173,7 @@ impl DBusAgent {
.map_err(|e| MethodErr::failed(&format!("Daemon error: {}", e))) .map_err(|e| MethodErr::failed(&format!("Daemon error: {}", e)))
} }
fn resolve_participant_display_name(&self, participant: &Participant) -> String { fn resolve_participant_display_name(&mut self, participant: &Participant) -> String {
match participant { match participant {
// Me (we should use a special string here...) // Me (we should use a special string here...)
Participant::Me => "(Me)".to_string(), Participant::Me => "(Me)".to_string(),