Skip to content
Browse files

Optimise findBestMatch() so that an exact title match is found we avo…

…id an expensive title comparison search
  • Loading branch information...
1 parent d2d2791 commit d3dca98e05c6c49d1271fca0d19946cdbd413d7f @stuartm stuartm committed Jan 5, 2013
Showing with 51 additions and 17 deletions.
  1. +49 −15 mythtv/libs/libmythmetadata/metadatadownload.cpp
  2. +2 −2 mythtv/libs/libmythmetadata/metadatadownload.h
View
64 mythtv/libs/libmythmetadata/metadatadownload.cpp
@@ -130,12 +130,20 @@ void MetadataDownload::run()
// If we're in automatic mode, we need to make
// these decisions on our own. Pass to title match.
- if (list.at(0)->GetAutomatic() && list.count() > 1)
+ if (list.at(0)->GetAutomatic() && list.count() > 1
+ && list.at(0)->GetStep() == kLookupSearch)
{
- if (!findBestMatch(list, lookup->GetTitle()))
- QCoreApplication::postEvent(m_parent,
- new MetadataLookupFailure(MetadataLookupList() << lookup));
- continue;
+ MetadataLookup *bestLookup = findBestMatch(list, lookup->GetTitle());
+ if (bestLookup)
+ {
+ MetadataLookup *newlookup = bestLookup;
+ newlookup->SetStep(kLookupData);
+ prependLookup(newlookup);
+ continue;
+ }
+
+ QCoreApplication::postEvent(m_parent,
+ new MetadataLookupFailure(MetadataLookupList() << lookup));
}
LOG(VB_GENERAL, LOG_INFO,
@@ -166,16 +174,45 @@ MetadataLookup* MetadataDownload::moreWork()
return result;
}
-bool MetadataDownload::findBestMatch(MetadataLookupList list,
- QString originaltitle)
+MetadataLookup* MetadataDownload::findBestMatch(MetadataLookupList list,
+ const QString &originaltitle) const
{
QStringList titles;
+ MetadataLookup *ret = NULL;
// Build a list of all the titles
+ int exactMatches = 0;
for (MetadataLookupList::const_iterator i = list.begin();
i != list.end(); ++i)
{
- titles.append((*i)->GetTitle());
+ QString title = (*i)->GetTitle();
+ if (title == originaltitle)
+ {
+ ret = (*i);
+ exactMatches++;
+ }
+
+ titles.append(title);
+ }
+
+ // If there was one or more exact matches then we can skip a more intensive
+ // and time consuming search
+ if (exactMatches > 0)
+ {
+ if (exactMatches == 1)
+ {
+ LOG(VB_GENERAL, LOG_INFO, QString("Single Exact Title Match For %1")
+ .arg(originaltitle));
+ return ret;
+ }
+ else
+ {
+ LOG(VB_GENERAL, LOG_ERR,
+ QString("Multiple exact title matches found for %1. "
+ "Need to match on other criteria.")
+ .arg(originaltitle));
+ return NULL;
+ }
}
// Apply Levenshtein distance algorithm to determine closest match
@@ -188,7 +225,7 @@ bool MetadataDownload::findBestMatch(MetadataLookupList list,
QString("No adequate match or multiple "
"matches found for %1. Update manually.")
.arg(originaltitle));
- return false;
+ return NULL;
}
LOG(VB_GENERAL, LOG_INFO, QString("Best Title Match For %1: %2")
@@ -200,15 +237,12 @@ bool MetadataDownload::findBestMatch(MetadataLookupList list,
{
if ((*i)->GetTitle() == bestTitle)
{
- MetadataLookup *newlookup = (*i);
- newlookup->SetStep(kLookupData);
-
- prependLookup(newlookup);
- return true;
+ ret = (*i);
+ break;
}
}
- return false;
+ return ret;
}
MetadataLookupList MetadataDownload::runGrabber(QString cmd, QStringList args,
View
4 mythtv/libs/libmythmetadata/metadatadownload.h
@@ -67,8 +67,8 @@ class META_PUBLIC MetadataDownload : public MThread
MetadataLookupList handleGame(MetadataLookup* lookup);
- bool findBestMatch(MetadataLookupList list,
- QString originaltitle);
+ MetadataLookup* findBestMatch(MetadataLookupList list,
+ const QString &originaltitle) const;
MetadataLookupList runGrabber(QString cmd, QStringList args,
MetadataLookup* lookup,
bool passseas = true);

0 comments on commit d3dca98

Please sign in to comment.
Something went wrong with that request. Please try again.