Skip to content

Commit

Permalink
registry: Parallel searching
Browse files Browse the repository at this point in the history
  • Loading branch information
drognanar authored and mrhota committed Jul 18, 2016
1 parent 0f1b69c commit e85504a
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 19 deletions.
10 changes: 6 additions & 4 deletions src/registry/cachingsearchstrategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@
****************************************************************************/

#include "cachingsearchstrategy.h"
#include "docset.h"

#include "searchresult.h"
#include "docset.h"
#include "searchquery.h"
#include "searchresult.h"

using namespace Zeal;

const int CacheSize = 10;

CachingSearchStrategy::CachingSearchStrategy(std::unique_ptr<DocsetSearchStrategy> strategy)
: m_search(std::move(strategy)),
m_cache(CachingSearchStrategy::CacheSize)
: m_search(std::move(strategy))
, m_cache(CacheSize)
{
}

Expand Down
2 changes: 0 additions & 2 deletions src/registry/cachingsearchstrategy.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ class CachingSearchStrategy : public DocsetSearchStrategy
QList<SearchResult> searchWithCache(const QString &query, const QString &prefix);
QList<SearchResult> searchWithoutCache(const QString &query, CancellationToken token);

const static int CacheSize = 10;

std::unique_ptr<DocsetSearchStrategy> m_search;
QCache<QString, QList<SearchResult> > m_cache;
};
Expand Down
1 change: 0 additions & 1 deletion src/registry/docset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,6 @@ void Docset::countSymbols()
const QString symbolType = parseSymbolType(symbolTypeStr);
m_symbolStrings.insertMulti(symbolType, symbolTypeStr);
m_symbolCounts[symbolType] += query.value(1).toInt();
m_symbolsTotal += query.value(1).toInt();
}
}

Expand Down
1 change: 0 additions & 1 deletion src/registry/docset.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ class Docset
QMap<QString, QString> m_symbolStrings;
QMap<QString, int> m_symbolCounts;
mutable QMap<QString, QMap<QString, QString>> m_symbols;
uint64_t m_symbolsTotal;

std::unique_ptr<DocsetSearchStrategy> m_searchStrategy;
};
Expand Down
24 changes: 14 additions & 10 deletions src/registry/docsetregistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

#include "searchresult.h"

#include <functional>
#include <QtConcurrent/QtConcurrent>
#include <QDebug>
#include <QDir>
#include <QThread>
Expand Down Expand Up @@ -127,24 +129,26 @@ void DocsetRegistry::_addDocset(const QString &path)
void DocsetRegistry::search(const QString &query, CancellationToken token)
{
qRegisterMetaType<CancellationToken>("CancellationToken");
qRegisterMetaType<CancellationToken>("Zeal::CancellationToken");
QMetaObject::invokeMethod(this, "_runQueryAsync", Qt::QueuedConnection,
Q_ARG(QString, query), Q_ARG(CancellationToken, token));
}

void DocsetRegistry::_runQueryAsync(const QString &query, const CancellationToken token)
void MergeQueryResults(QList<SearchResult> &finalResult, const QList<SearchResult> &partial)
{
QList<SearchResult> results;

for (Docset *docset : docsets()) {
if (token.isCancelled())
return;
results += docset->search(query, token);
}
finalResult += partial;
}

void DocsetRegistry::_runQueryAsync(const QString &query, const CancellationToken token)
{
QFuture<QList<SearchResult> > queryResultsFuture = QtConcurrent::mappedReduced(
docsets(),
std::bind(&Docset::search, std::placeholders::_1, query, token),
&MergeQueryResults);
QList<SearchResult> results = queryResultsFuture.result();
std::sort(results.begin(), results.end());

emit queryCompleted(results);
if (!token.isCancelled())
emit queryCompleted(results);
}

// Recursively finds and adds all docsets in a given directory.
Expand Down
2 changes: 1 addition & 1 deletion src/src.pro
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
TEMPLATE = app

QT += gui widgets sql
QT += gui widgets sql concurrent
CONFIG += c++11 silent

## Build options
Expand Down

0 comments on commit e85504a

Please sign in to comment.