Commit 89e58567 authored by Volker Krause's avatar Volker Krause
Browse files

Use cached information for location queries

parent 3b22c0d2
......@@ -18,6 +18,7 @@
#include "hafasmgatebackend.h"
#include "hafasmgateparser.h"
#include "logging.h"
#include "cache.h"
#include <KPublicTransport/Departure>
#include <KPublicTransport/DepartureReply>
......@@ -73,13 +74,14 @@ bool HafasMgateBackend::queryDeparture(DepartureReply *reply, QNetworkAccessMana
if (!locReply) {
return false;
}
QObject::connect(locReply, &QNetworkReply::finished, [this, reply, locReply, nam]() {
QObject::connect(locReply, &QNetworkReply::finished, [this, reply, locReply, locReq, nam]() {
qDebug() << locReply->request().url();
switch (locReply->error()) {
case QNetworkReply::NoError:
{
auto res = m_parser.parseLocations(locReply->readAll());
if (m_parser.error() == Reply::NoError && !res.empty()) {
Cache::addLocationCacheEntry(backendId(), locReq.cacheKey(), res);
const auto id = res[0].identifier(locationIdentifierType());
if (!id.isEmpty()) {
queryDeparture(reply, id, nam);
......@@ -87,6 +89,7 @@ bool HafasMgateBackend::queryDeparture(DepartureReply *reply, QNetworkAccessMana
addError(reply, Reply::NotFoundError, QLatin1String("Location query found no results."));
}
} else {
Cache::addNegativeLocationCacheEntry(backendId(), locReq.cacheKey());
addError(reply, m_parser.error(), m_parser.errorMessage());
}
break;
......@@ -171,8 +174,10 @@ bool HafasMgateBackend::queryLocation(LocationReply *reply, QNetworkAccessManage
{
auto res = m_parser.parseLocations(netReply->readAll());
if (m_parser.error() == Reply::NoError) {
Cache::addLocationCacheEntry(backendId(), reply->request().cacheKey(), res);
addResult(reply, std::move(res));
} else {
Cache::addNegativeLocationCacheEntry(backendId(), reply->request().cacheKey());
addError(reply, m_parser.error(), m_parser.errorMessage());
}
break;
......
......@@ -26,6 +26,7 @@
#include <KPublicTransport/Location>
#include "backends/cache.h"
#include "backends/hafasmgatebackend.h"
#include "backends/navitiabackend.h"
......@@ -215,8 +216,22 @@ LocationReply* Manager::queryLocation(const LocationRequest &req) const
qCDebug(Log) << "Skipping insecure backend:" << backend->backendId();
continue;
}
if (backend->queryLocation(reply, d->nam())) {
++pendingOps;
auto cache = Cache::lookupLocation(backend->backendId(), req.cacheKey());
switch (cache.type) {
case CacheHitType::Negative:
qCDebug(Log) << "Negative cache hit for backend" << backend->backendId();
break;
case CacheHitType::Positive:
qCDebug(Log) << "Positive cache hit for backend" << backend->backendId();
reply->addResult(std::move(cache.data));
break;
case CacheHitType::Miss:
qCDebug(Log) << "Cache miss for backend" << backend->backendId();
if (backend->queryLocation(reply, d->nam())) {
++pendingOps;
}
break;
}
}
reply->setPendingOps(pendingOps);
......
......@@ -55,7 +55,7 @@ void Reply::addError(Reply::Error error, const QString &errorMsg)
void Reply::setPendingOps(int ops)
{
Q_ASSERT(d_ptr->pendingOps == -1);
Q_ASSERT(d_ptr->pendingOps <= -1);
Q_ASSERT(ops >= 0);
d_ptr->pendingOps = ops;
if (ops == 0) {
......
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