Commit 0d25bc4a authored by Richard Braakman's avatar Richard Braakman

[performance] look up address resolve requests in a QHash

This change does not affect normal performance but it guards
against bad performance when there are many resolve requests.

Thanks to Matthew Vogt for the idea.
parent e2ab0615
......@@ -663,25 +663,23 @@ void SeasideCache::unregisterResolveListener(ResolveListener *listener)
if (!instancePtr)
return;
QList<ResolveData>::iterator it = instancePtr->m_resolveAddresses.begin();
QHash<QContactFetchRequest *, ResolveData>::iterator it = instancePtr->m_resolveAddresses.begin();
while (it != instancePtr->m_resolveAddresses.end()) {
if (it->listener == listener) {
if (it->fetchRequest) {
it->fetchRequest->cancel();
delete it->fetchRequest;
}
if (it.value().listener == listener) {
it.key()->cancel();
delete it.key();
it = instancePtr->m_resolveAddresses.erase(it);
} else {
++it;
}
}
it = instancePtr->m_unknownAddresses.begin();
while (it != instancePtr->m_unknownAddresses.end()) {
if (it->listener == listener) {
it = instancePtr->m_unknownAddresses.erase(it);
QList<ResolveData>::iterator it2 = instancePtr->m_unknownAddresses.begin();
while (it2 != instancePtr->m_unknownAddresses.end()) {
if (it2->listener == listener) {
it2 = instancePtr->m_unknownAddresses.erase(it2);
} else {
++it;
++it2;
}
}
}
......@@ -2664,53 +2662,52 @@ void SeasideCache::addressRequestStateChanged(QContactAbstractRequest::State sta
applyContactUpdates(request->contacts(), queryDetailTypes);
// now figure out which address was being resolved and resolve it
QList<ResolveData>::iterator it = instancePtr->m_resolveAddresses.begin();
for ( ; it != instancePtr->m_resolveAddresses.end(); ++it) {
if (request == it->fetchRequest) {
ResolveData data(*it);
if (data.first == QString()) {
// We have now queried this phone number
m_resolvedPhoneNumbers.insert(minimizePhoneNumber(data.second));
}
QHash<QContactFetchRequest *, ResolveData>::iterator it = instancePtr->m_resolveAddresses.find(request);
if (it == instancePtr->m_resolveAddresses.end()) {
qWarning() << "Got stateChanged for unknown request";
return;
}
CacheItem *item = 0;
QList<QContact> resolvedContacts(data.fetchRequest->contacts());
delete data.fetchRequest;
data.fetchRequest = 0;
ResolveData data(it.value());
if (data.first == QString()) {
// We have now queried this phone number
m_resolvedPhoneNumbers.insert(minimizePhoneNumber(data.second));
}
if (!resolvedContacts.isEmpty()) {
if (resolvedContacts.count() == 1) {
item = itemById(apiId(resolvedContacts.first()), false);
} else {
// Lookup the result in our updated indexes
if (data.first == QString()) {
item = itemByPhoneNumber(data.second, false);
} else if (data.second == QString()) {
item = itemByEmailAddress(data.first, false);
} else {
item = itemByOnlineAccount(data.first, data.second, false);
}
}
} else {
// This address is unknown - keep it for later resolution
if (data.first == QString()) {
// Compare this phone number in minimized form
data.compare = minimizePhoneNumber(data.second);
} else if (data.second == QString()) {
// Compare this email address in lowercased form
data.compare = data.first.toLower();
} else {
// Compare this account URI in lowercased form
data.compare = data.second.toLower();
}
CacheItem *item = 0;
const QList<QContact> &resolvedContacts = it.key()->contacts();
m_unknownAddresses.append(data);
if (!resolvedContacts.isEmpty()) {
if (resolvedContacts.count() == 1) {
item = itemById(apiId(resolvedContacts.first()), false);
} else {
// Lookup the result in our updated indexes
if (data.first == QString()) {
item = itemByPhoneNumber(data.second, false);
} else if (data.second == QString()) {
item = itemByEmailAddress(data.first, false);
} else {
item = itemByOnlineAccount(data.first, data.second, false);
}
data.listener->addressResolved(data.first, data.second, item);
m_resolveAddresses.erase(it);
return;
}
} else {
// This address is unknown - keep it for later resolution
if (data.first == QString()) {
// Compare this phone number in minimized form
data.compare = minimizePhoneNumber(data.second);
} else if (data.second == QString()) {
// Compare this email address in lowercased form
data.compare = data.first.toLower();
} else {
// Compare this account URI in lowercased form
data.compare = data.second.toLower();
}
m_unknownAddresses.append(data);
}
data.listener->addressResolved(data.first, data.second, item);
delete it.key();
m_resolveAddresses.erase(it);
}
void SeasideCache::makePopulated(FilterType filter)
......@@ -3080,7 +3077,6 @@ void SeasideCache::resolveAddress(ResolveListener *listener, const QString &firs
data.second = second;
data.requireComplete = requireComplete;
data.listener = listener;
data.fetchRequest = 0;
// Is this address a known-unknown?
bool knownUnknown = false;
......@@ -3098,7 +3094,6 @@ void SeasideCache::resolveAddress(ResolveListener *listener, const QString &firs
} else {
QContactFetchRequest *request = new QContactFetchRequest(this);
request->setManager(manager());
data.fetchRequest = request;
if (first.isEmpty()) {
// Search for phone number
......@@ -3130,7 +3125,7 @@ void SeasideCache::resolveAddress(ResolveListener *listener, const QString &firs
request->setFetchHint(requireComplete ? basicFetchHint() : favoriteFetchHint(m_fetchTypes | m_extraFetchTypes));
connect(request, SIGNAL(stateChanged(QContactAbstractRequest::State)),
this, SLOT(addressRequestStateChanged(QContactAbstractRequest::State)));
m_resolveAddresses.append(data);
m_resolveAddresses[request] = data;
request->start();
}
}
......
......@@ -472,9 +472,8 @@ private:
QString compare;
bool requireComplete;
ResolveListener *listener;
QContactFetchRequest *fetchRequest;
};
QList<ResolveData> m_resolveAddresses;
QHash<QContactFetchRequest *, ResolveData> m_resolveAddresses;
QList<ResolveData> m_unknownResolveAddresses;
QList<ResolveData> m_unknownAddresses;
QSet<QString> m_resolvedPhoneNumbers;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment