Commit 65e74d07 authored by Maik Qualmann's avatar Maik Qualmann

use QHash for image ids and album ids to speed up similarity search with MySQL

The building of the cache could be reduced here with 30000 fingerprints
of over 30 seconds on under 300ms. The image search or sketch needed
minutes before, are now ready in less than a second.
parent 6b907e2e
......@@ -4264,6 +4264,33 @@ QList<qlonglong> CoreDB::getAllItems() const
return items;
}
QHash<qlonglong, QPair<int, int> > CoreDB::getAllItemsWithAlbum() const
{
QList<QVariant> values;
d->db->execSql(QString::fromUtf8("SELECT Images.id, Albums.albumRoot, Albums.id "
"FROM Images "
" LEFT JOIN Albums ON Albums.id=Images.album "
" WHERE Images.status<3;"),
&values);
QHash<qlonglong, QPair<int, int> > itemAlbumHash;
for (QList<QVariant>::const_iterator it = values.constBegin() ; it != values.constEnd() ; )
{
qlonglong id = (*it).toLongLong();
++it;
int albumRoot = (*it).toInt();
++it;
int album = (*it).toInt();
++it;
itemAlbumHash[id] = qMakePair(albumRoot, album);
}
return itemAlbumHash;
}
QList<ItemScanInfo> CoreDB::getItemScanInfos(int albumID) const
{
QList<QVariant> values;
......
......@@ -637,6 +637,13 @@ public:
*/
QList<qlonglong> getAllItems() const;
/**
* Returns all ids of items with album ids in images table.
* QPair.first == albumRootID
* QPair.second == albumID
*/
QHash<qlonglong, QPair<int, int> > getAllItemsWithAlbum() const;
/**
* Returns the id of the item with the given filename in
* the album with the given id.
......
......@@ -162,7 +162,7 @@ public:
albumCache = nullptr;
useSignatureCache = false;
signatureQuery = QString::fromUtf8("SELECT M.imageid, M.matrix FROM ImageHaarMatrix AS M;");
signatureQuery = QString::fromUtf8("SELECT imageid, matrix FROM ImageHaarMatrix;");
}
~Private()
......@@ -253,17 +253,16 @@ public:
return;
}
const QHash<qlonglong, QPair<int, int> >& itemAlbumHash = CoreDbAccess().db()->getAllItemsWithAlbum();
while (query.next())
{
imageid = query.value(0).toLongLong();
// Get the album id and status of the item with the ItemShortInfo.
ItemShortInfo info = CoreDbAccess().db()->getItemShortInfo(imageid);
if (!info.isNull() && info.albumID > 0)
if (itemAlbumHash.contains(imageid))
{
blob.read(query.value(1).toByteArray(), &targetSig);
albumid = info.albumID;
albumid = itemAlbumHash.value(imageid).second;
signatureCache[imageid] = targetSig;
albumCache[imageid] = albumid;
}
......@@ -747,31 +746,28 @@ QMap<qlonglong, double> HaarIface::searchDatabase(Haar::SignatureData* const que
return scores;
}
const QHash<qlonglong, QPair<int, int> >& itemAlbumHash = CoreDbAccess().db()->getAllItemsWithAlbum();
// We don't use SimilarityDb's convenience calls, as the result set is large
// and we try to avoid copying in a temporary QList<QVariant>
int albumRootId = 0;
while (query.next())
{
imageid = query.value(0).toLongLong();
// Get the album id, album root id and status of the item with the ItemShortInfo.
ItemShortInfo info = CoreDbAccess().db()->getItemShortInfo(imageid);
if (!info.isNull() && info.albumID > 0)
if (itemAlbumHash.contains(imageid))
{
QPair<int, int> albumPair = itemAlbumHash.value(imageid);
if (filterByAlbumRoots)
{
albumRootId = info.albumRootID;
if (!d->albumRootsToSearch.contains(albumRootId))
if (!d->albumRootsToSearch.contains(albumPair.first))
{
continue;
}
}
blob.read(query.value(1).toByteArray(), &targetSig);
albumid = info.albumID;
albumid = albumPair.second;
if (d->useSignatureCache)
{
......
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