refs #649, added check for changed base URL to database reader

(continued #649 after fixes of #664)
This commit is contained in:
Klaus Basan
2016-05-27 17:55:19 +02:00
parent 8ad640704c
commit 467f37db7f
11 changed files with 59 additions and 9 deletions

View File

@@ -84,11 +84,12 @@ namespace BlackCore
//! Default value
static const BlackMisc::Network::CUrl &defaultValue();
//! First load is synchronous
static constexpr bool isPinned() { return true; }
//! Key in data cache
static const char *key() { return "dbicaoreaderurl"; }
};
} // ns
} // ns

View File

@@ -51,6 +51,7 @@ namespace BlackCore
CEntityFlags::Entity allEntities = entities;
CEntityFlags::Entity currentEntity = CEntityFlags::iterateDbEntities(allEntities); // CEntityFlags::InfoObjectEntity will be ignored
const bool hasInfoObjects = this->hasInfoObjects();
const bool changedUrl = this->hasChangedUrl(currentEntity);
while (currentEntity)
{
const CDatabaseReaderConfig config(this->getConfigForEntity(currentEntity));
@@ -63,7 +64,7 @@ namespace BlackCore
const qint64 cacheTimestamp = cacheTs.isValid() ? cacheTs.toMSecsSinceEpoch() : -1;
const qint64 latestEntityTimestamp = latestEntityTs.isValid() ? latestEntityTs.toMSecsSinceEpoch() : -1;
Q_ASSERT_X(latestEntityTimestamp > 0, Q_FUNC_INFO, "Missing timestamp");
if (cacheTimestamp >= latestEntityTimestamp && cacheTimestamp > 0)
if (!changedUrl && cacheTimestamp >= latestEntityTimestamp && cacheTimestamp > 0)
{
this->syncronizeCaches(currentEntity);
entities &= ~currentEntity; // do not load from web
@@ -72,14 +73,22 @@ namespace BlackCore
<< cacheTs.toString() << cacheTimestamp;
}
else
{
if (changedUrl)
{
CLogMessage(this).info("Data location changed, will override cache");
}
else
{
CLogMessage(this).info("Cache for %1 outdated, latest entity (%2, %3)")
<< CEntityFlags::flagToString(currentEntity)
<< latestEntityTs.toString() << latestEntityTimestamp;
}
}
}
else
{
// no info objects, server down
this->syncronizeCaches(currentEntity);
CLogMessage(this).info("No info object for %1, using cache") << CEntityFlags::flagToString(currentEntity);
}
@@ -181,6 +190,16 @@ namespace BlackCore
return this->m_config.findFirstOrDefaultForEntity(entity);
}
bool CDatabaseReader::isChangedUrl(const CUrl &oldUrl, const CUrl &currentUrl)
{
if (oldUrl.isEmpty()) { return true; }
Q_ASSERT_X(!currentUrl.isEmpty(), Q_FUNC_INFO, "No base URL");
const QString oldS(oldUrl.getFullUrl(false));
const QString currentS(currentUrl.getFullUrl(false));
return oldS != currentS;
}
bool CDatabaseReader::canConnect() const
{
QReadLocker rl(&this->m_statusLock);

View File

@@ -154,6 +154,12 @@ namespace BlackCore
//! Invalidate the caches for given entities
virtual void invalidateCaches(BlackMisc::Network::CEntityFlags::Entity entities) = 0;
//! Changed URL, means the cache values have been read from elsewhere
virtual bool hasChangedUrl(BlackMisc::Network::CEntityFlags::Entity entity) const = 0;
//! Has URL been changed? Means we load from a differrent server
static bool isChangedUrl(const BlackMisc::Network::CUrl &oldUrl, const BlackMisc::Network::CUrl &currentUrl);
private:
//! Check if terminated or error, otherwise split into array of objects
JsonDatastoreResponse transformReplyIntoDatastoreResponse(QNetworkReply *nwReply) const;

View File

@@ -263,6 +263,7 @@ namespace BlackCore
}
this->m_airlineIcaoCache.set(codes, latestTimestamp);
this->updateReaderUrl(this->getBaseUrl());
emit dataRead(CEntityFlags::AirlineIcaoEntity, CEntityFlags::ReadFinished, n);
CLogMessage(this).info("Read %1 %2 from %3") << n << CEntityFlags::flagToString(CEntityFlags::AirlineIcaoEntity) << urlString;
}
@@ -288,6 +289,7 @@ namespace BlackCore
}
this->m_countryCache.set(countries, latestTimestamp);
this->updateReaderUrl(this->getBaseUrl());
emit dataRead(CEntityFlags::CountryEntity, CEntityFlags::ReadFinished, n);
CLogMessage(this).info("Read %1 %2 from %3") << n << CEntityFlags::flagToString(CEntityFlags::CountryEntity) << urlString;
}
@@ -408,6 +410,12 @@ namespace BlackCore
}
}
bool CIcaoDataReader::hasChangedUrl(CEntityFlags::Entity entity) const
{
Q_UNUSED(entity);
return CDatabaseReader::isChangedUrl(this->m_readerUrlCache.getCopy(), this->getBaseUrl());
}
CUrl CIcaoDataReader::getAircraftIcaoUrl(bool shared) const
{
return shared ?

View File

@@ -124,6 +124,7 @@ namespace BlackCore
virtual void syncronizeCaches(BlackMisc::Network::CEntityFlags::Entity entities) override;
virtual void invalidateCaches(BlackMisc::Network::CEntityFlags::Entity entities) override;
virtual QDateTime getCacheTimestamp(BlackMisc::Network::CEntityFlags::Entity entity) override;
virtual bool hasChangedUrl(BlackMisc::Network::CEntityFlags::Entity entity) const override;
//! @}
private slots:

View File

@@ -69,6 +69,13 @@ namespace BlackCore
return QDateTime();
}
bool CInfoDataReader::hasChangedUrl(CEntityFlags::Entity entity) const
{
// not implemented
Q_UNUSED(entity);
return false;
}
void CInfoDataReader::read(CEntityFlags::Entity entities, const QDateTime &newerThan)
{
this->ps_read(entities, newerThan);

View File

@@ -62,6 +62,7 @@ namespace BlackCore
virtual void syncronizeCaches(BlackMisc::Network::CEntityFlags::Entity entities) override;
virtual void invalidateCaches(BlackMisc::Network::CEntityFlags::Entity entities) override;
virtual QDateTime getCacheTimestamp(BlackMisc::Network::CEntityFlags::Entity entity) override;
virtual bool hasChangedUrl(BlackMisc::Network::CEntityFlags::Entity entity) const override;
//! @}
private slots:

View File

@@ -443,6 +443,12 @@ namespace BlackCore
return QDateTime();
}
bool CModelDataReader::hasChangedUrl(CEntityFlags::Entity entity) const
{
Q_UNUSED(entity);
return CDatabaseReader::isChangedUrl(CUrl(), this->getBaseUrl());
}
CUrl CModelDataReader::getBaseUrl() const
{
const CUrl baseUrl(sApp->getGlobalSetup().getDbModelReaderUrl());

View File

@@ -129,6 +129,7 @@ namespace BlackCore
virtual void syncronizeCaches(BlackMisc::Network::CEntityFlags::Entity entities) override;
virtual void invalidateCaches(BlackMisc::Network::CEntityFlags::Entity entities) override;
virtual QDateTime getCacheTimestamp(BlackMisc::Network::CEntityFlags::Entity entity) override;
virtual bool hasChangedUrl(BlackMisc::Network::CEntityFlags::Entity entity) const override;
//! @}
private slots:

View File

@@ -107,14 +107,14 @@ namespace BlackMisc
return m_query;
}
QString CUrl::getFullUrl() const
QString CUrl::getFullUrl(bool withQuery) const
{
if (m_host.isEmpty()) { return ""; }
QString qn(m_host);
if (!hasDefaultPort() && hasPort()) { qn = qn.append(":").append(QString::number(m_port)); }
if (hasPath()) { qn = qn.append("/").append(m_path).replace("//", "/"); }
if (hasQuery()) { qn = qn.append("?").append(m_query); }
if (hasQuery() && withQuery) { qn = qn.append("?").append(m_query); }
if (hasScheme()) { qn = QString(this->getScheme()).append("://").append(qn); }
return qn;
}

View File

@@ -110,7 +110,7 @@ namespace BlackMisc
bool isEmpty() const;
//! Qualified name
QString getFullUrl() const;
QString getFullUrl(bool withQuery = true) const;
//! Set full URL
void setFullUrl(const QString &fullUrl);