ContactResolver: implement in-memory cache for positive results
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
pub mod eds;
|
||||
pub use eds::EDSContactResolverBackend;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub trait ContactResolverBackend {
|
||||
type ContactID;
|
||||
|
||||
@@ -13,6 +15,8 @@ pub type AnyContactID = String;
|
||||
#[derive(Clone)]
|
||||
pub struct ContactResolver<T: ContactResolverBackend> {
|
||||
backend: T,
|
||||
display_name_cache: HashMap<AnyContactID, String>,
|
||||
contact_id_cache: HashMap<String, AnyContactID>,
|
||||
}
|
||||
|
||||
impl<T: ContactResolverBackend> ContactResolver<T>
|
||||
@@ -22,16 +26,34 @@ where
|
||||
T: Default,
|
||||
{
|
||||
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> {
|
||||
self.backend.resolve_contact_id(address).map(|id| id.into())
|
||||
pub fn resolve_contact_id(&mut self, address: &str) -> Option<AnyContactID> {
|
||||
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());
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -507,7 +507,7 @@ impl Daemon {
|
||||
|
||||
// Insert each conversation
|
||||
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 {
|
||||
// Insert or update conversation and its participants
|
||||
database
|
||||
|
||||
@@ -173,7 +173,7 @@ impl DBusAgent {
|
||||
.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 {
|
||||
// Me (we should use a special string here...)
|
||||
Participant::Me => "(Me)".to_string(),
|
||||
|
||||
Reference in New Issue
Block a user