mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-30 20:15:35 +08:00
refs #781, use admit in database reader
* added amit functions in readers * cacheHasChanged functions (needed because data of caches are loaded in background) * pinned small caches * CDatabaseReader::readInBackgroundThread uses admitCaches now
This commit is contained in:
@@ -42,8 +42,8 @@ namespace BlackCore
|
||||
//! Trait for DB distributor cache
|
||||
struct TDbDistributorCache : public BlackMisc::TDataTrait<BlackMisc::Simulation::CDistributorList>
|
||||
{
|
||||
//! Defer loading (no currently small)
|
||||
static constexpr bool isDeferred() { return false; }
|
||||
//! First load is synchronous, distributors is a small cache
|
||||
static constexpr bool isPinned() { return true; }
|
||||
|
||||
//! Key in data cache
|
||||
static const char *key() { return "dbdistributorcache"; }
|
||||
@@ -83,8 +83,8 @@ namespace BlackCore
|
||||
//! Trait for DB countries
|
||||
struct TDbCountryCache : public BlackMisc::TDataTrait<BlackMisc::CCountryList>
|
||||
{
|
||||
//! Defer loading (no currently small)
|
||||
static constexpr bool isDeferred() { return false; }
|
||||
//! First load is synchronous, countries is a small cache
|
||||
static constexpr bool isPinned() { return true; }
|
||||
|
||||
//! Key in data cache
|
||||
static const char *key() { return "dbcountrycache"; }
|
||||
|
||||
@@ -47,6 +47,11 @@ namespace BlackCore
|
||||
if (entities.testFlag(CEntityFlags::AirportEntity)) { this->m_airportCache.synchronize(); }
|
||||
}
|
||||
|
||||
void CAirportDataReader::admitCaches(CEntityFlags::Entity entities)
|
||||
{
|
||||
if (entities.testFlag(CEntityFlags::AirportEntity)) { this->m_airportCache.admit(); }
|
||||
}
|
||||
|
||||
void CAirportDataReader::invalidateCaches(CEntityFlags::Entity entities)
|
||||
{
|
||||
if (entities.testFlag(CEntityFlags::AirportEntity)) { CDataCache::instance()->clearAllValues(this->m_airportCache.getKey()); }
|
||||
@@ -125,7 +130,7 @@ namespace BlackCore
|
||||
|
||||
void CAirportDataReader::ps_airportCacheChanged()
|
||||
{
|
||||
// void
|
||||
this->cacheHasChanged(CEntityFlags::AirportEntity);
|
||||
}
|
||||
|
||||
void CAirportDataReader::ps_baseUrlCacheChanged()
|
||||
|
||||
@@ -41,10 +41,11 @@ namespace BlackCore
|
||||
// base class overrides
|
||||
virtual QDateTime getCacheTimestamp(BlackMisc::Network::CEntityFlags::Entity entities) const override;
|
||||
virtual int getCacheCount(BlackMisc::Network::CEntityFlags::Entity entity) const override;
|
||||
virtual void synchronizeCaches(BlackMisc::Network::CEntityFlags::Entity entities) override;
|
||||
virtual void admitCaches(BlackMisc::Network::CEntityFlags::Entity entities) override;
|
||||
|
||||
protected:
|
||||
// base class overrides
|
||||
virtual void synchronizeCaches(BlackMisc::Network::CEntityFlags::Entity entities) override;
|
||||
virtual void invalidateCaches(BlackMisc::Network::CEntityFlags::Entity entities) override;
|
||||
virtual bool hasChangedUrl(BlackMisc::Network::CEntityFlags::Entity entity) const override;
|
||||
|
||||
|
||||
@@ -74,7 +74,7 @@ namespace BlackCore
|
||||
Q_ASSERT_X(latestEntityTimestamp >= 0, Q_FUNC_INFO, "Missing timestamp");
|
||||
if (!changedUrl && cacheTimestamp >= latestEntityTimestamp && cacheTimestamp >= 0 && latestEntityTimestamp >= 0)
|
||||
{
|
||||
this->synchronizeCaches(currentEntity);
|
||||
this->admitCaches(currentEntity);
|
||||
entities &= ~currentEntity; // do not load from web database
|
||||
cachedEntities |= currentEntity; // read from cache
|
||||
CLogMessage(this).info("Using cache for %1 (%2, %3)") << currentEntityName << cacheTs.toString() << cacheTimestamp;
|
||||
@@ -94,7 +94,7 @@ namespace BlackCore
|
||||
else
|
||||
{
|
||||
// no info objects, server down
|
||||
this->synchronizeCaches(currentEntity);
|
||||
this->admitCaches(currentEntity);
|
||||
const int c = this->getCacheCount(currentEntity);
|
||||
CLogMessage(this).info("No info object for %1, using cache with %2 objects") << currentEntityName << c;
|
||||
entities &= ~currentEntity; // do not load from web database
|
||||
@@ -105,7 +105,7 @@ namespace BlackCore
|
||||
}
|
||||
|
||||
// signals for the cached entities
|
||||
this->emitReadSignalPerSingleCachedEntity(cachedEntities);
|
||||
this->emitReadSignalPerSingleCachedEntity(cachedEntities, true);
|
||||
|
||||
// Real read from DB
|
||||
this->startReadFromDbInBackgroundThread(entities, newerThan);
|
||||
@@ -211,17 +211,23 @@ namespace BlackCore
|
||||
return this->m_config.findFirstOrDefaultForEntity(entity);
|
||||
}
|
||||
|
||||
void CDatabaseReader::emitReadSignalPerSingleCachedEntity(CEntityFlags::Entity cachedEntities)
|
||||
CEntityFlags::Entity CDatabaseReader::emitReadSignalPerSingleCachedEntity(CEntityFlags::Entity cachedEntities, bool onlyIfHasData)
|
||||
{
|
||||
if (cachedEntities == CEntityFlags::NoEntity) { return; }
|
||||
if (cachedEntities == CEntityFlags::NoEntity) { return CEntityFlags::NoEntity; }
|
||||
CEntityFlags::Entity emitted = CEntityFlags::NoEntity;
|
||||
CEntityFlags::Entity cachedEntitiesToEmit = cachedEntities;
|
||||
CEntityFlags::Entity currentCachedEntity = CEntityFlags::iterateDbEntities(cachedEntitiesToEmit);
|
||||
while (currentCachedEntity)
|
||||
{
|
||||
const int c = this->getCacheCount(currentCachedEntity);
|
||||
emit dataRead(currentCachedEntity, CEntityFlags::ReadFinished, c);
|
||||
if (!onlyIfHasData || c > 0)
|
||||
{
|
||||
emit dataRead(currentCachedEntity, CEntityFlags::ReadFinished, c);
|
||||
emitted |= currentCachedEntity;
|
||||
}
|
||||
currentCachedEntity = CEntityFlags::iterateDbEntities(cachedEntitiesToEmit);
|
||||
}
|
||||
return emitted;
|
||||
}
|
||||
|
||||
bool CDatabaseReader::isChangedUrl(const CUrl &oldUrl, const CUrl ¤tUrl)
|
||||
@@ -307,6 +313,11 @@ namespace BlackCore
|
||||
return sApp->getGlobalSetup().getSwiftSharedUrls().getRandomWorkingUrl();
|
||||
}
|
||||
|
||||
void CDatabaseReader::cacheHasChanged(CEntityFlags::Entity entities)
|
||||
{
|
||||
this->emitReadSignalPerSingleCachedEntity(entities, false);
|
||||
}
|
||||
|
||||
bool CDatabaseReader::canPingSwiftServer()
|
||||
{
|
||||
const CUrl url(getDbUrl());
|
||||
|
||||
@@ -154,8 +154,8 @@ namespace BlackCore
|
||||
protected:
|
||||
CDatabaseReaderConfigList m_config; //!< DB reder configuration
|
||||
QString m_statusMessage; //!< Returned status message from watchdog
|
||||
bool m_1stReplyReceived = false; //!< Successful connection? Does not mean data / authorizations are correct
|
||||
QNetworkReply::NetworkError m_1stReplyStatus = QNetworkReply::UnknownServerError; //!< Successful connection?
|
||||
bool m_1stReplyReceived = false; //!< Successful connection? Does not mean data / authorizations are correct
|
||||
mutable QReadWriteLock m_statusLock; //!< Lock
|
||||
|
||||
//! Constructor
|
||||
@@ -172,7 +172,7 @@ namespace BlackCore
|
||||
CDatabaseReaderConfig getConfigForEntity(BlackMisc::Network::CEntityFlags::Entity entity) const;
|
||||
|
||||
//! Split into single entity and send dataRead signal
|
||||
void emitReadSignalPerSingleCachedEntity(BlackMisc::Network::CEntityFlags::Entity cachedEntities);
|
||||
BlackMisc::Network::CEntityFlags::Entity emitReadSignalPerSingleCachedEntity(BlackMisc::Network::CEntityFlags::Entity cachedEntities, bool onlyIfHasData);
|
||||
|
||||
//! DB base URL
|
||||
static const BlackMisc::Network::CUrl &getDbUrl();
|
||||
@@ -185,12 +185,18 @@ namespace BlackCore
|
||||
//! Synchronize caches for given entities
|
||||
virtual void synchronizeCaches(BlackMisc::Network::CEntityFlags::Entity entities) = 0;
|
||||
|
||||
//! Admit caches for given entities
|
||||
virtual void admitCaches(BlackMisc::Network::CEntityFlags::Entity entities) = 0;
|
||||
|
||||
//! 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;
|
||||
|
||||
//! Cache for given entity has changed
|
||||
virtual void cacheHasChanged(BlackMisc::Network::CEntityFlags::Entity entities);
|
||||
|
||||
//! Has URL been changed? Means we load from a differrent server
|
||||
static bool isChangedUrl(const BlackMisc::Network::CUrl &oldUrl, const BlackMisc::Network::CUrl ¤tUrl);
|
||||
//! @}
|
||||
|
||||
@@ -181,17 +181,17 @@ namespace BlackCore
|
||||
|
||||
void CIcaoDataReader::ps_aircraftIcaoCacheChanged()
|
||||
{
|
||||
// void
|
||||
this->cacheHasChanged(CEntityFlags::AircraftIcaoEntity);
|
||||
}
|
||||
|
||||
void CIcaoDataReader::ps_airlineIcaoCacheChanged()
|
||||
{
|
||||
// void
|
||||
this->cacheHasChanged(CEntityFlags::AirlineIcaoEntity);
|
||||
}
|
||||
|
||||
void CIcaoDataReader::ps_countryCacheChanged()
|
||||
{
|
||||
// void
|
||||
this->cacheHasChanged(CEntityFlags::CountryEntity);
|
||||
}
|
||||
|
||||
void CIcaoDataReader::ps_baseUrlCacheChanged()
|
||||
@@ -400,6 +400,13 @@ namespace BlackCore
|
||||
if (entities.testFlag(CEntityFlags::CountryEntity)) { this->m_countryCache.synchronize(); }
|
||||
}
|
||||
|
||||
void CIcaoDataReader::admitCaches(CEntityFlags::Entity entities)
|
||||
{
|
||||
if (entities.testFlag(CEntityFlags::AircraftIcaoEntity)) { this->m_aircraftIcaoCache.admit(); }
|
||||
if (entities.testFlag(CEntityFlags::AirlineIcaoEntity)) { this->m_airlineIcaoCache.admit(); }
|
||||
if (entities.testFlag(CEntityFlags::CountryEntity)) { this->m_countryCache.admit(); }
|
||||
}
|
||||
|
||||
void CIcaoDataReader::invalidateCaches(CEntityFlags::Entity entities)
|
||||
{
|
||||
if (entities.testFlag(CEntityFlags::AircraftIcaoEntity)) { CDataCache::instance()->clearAllValues(this->m_aircraftIcaoCache.getKey()); }
|
||||
|
||||
@@ -118,6 +118,7 @@ namespace BlackCore
|
||||
virtual QDateTime getCacheTimestamp(BlackMisc::Network::CEntityFlags::Entity entity) const override;
|
||||
virtual int getCacheCount(BlackMisc::Network::CEntityFlags::Entity entity) const override;
|
||||
virtual void synchronizeCaches(BlackMisc::Network::CEntityFlags::Entity entities) override;
|
||||
virtual void admitCaches(BlackMisc::Network::CEntityFlags::Entity entities) override;
|
||||
|
||||
protected:
|
||||
// cache handling for base class
|
||||
|
||||
@@ -56,6 +56,12 @@ namespace BlackCore
|
||||
Q_UNUSED(entities);
|
||||
}
|
||||
|
||||
void CInfoDataReader::admitCaches(CEntityFlags::Entity entities)
|
||||
{
|
||||
// no caching used here
|
||||
Q_UNUSED(entities);
|
||||
}
|
||||
|
||||
void CInfoDataReader::invalidateCaches(CEntityFlags::Entity entities)
|
||||
{
|
||||
// no caching used here
|
||||
|
||||
@@ -51,6 +51,8 @@ namespace BlackCore
|
||||
// cache handling for base class
|
||||
virtual QDateTime getCacheTimestamp(BlackMisc::Network::CEntityFlags::Entity entity) const override;
|
||||
virtual int getCacheCount(BlackMisc::Network::CEntityFlags::Entity entity) const override;
|
||||
virtual void synchronizeCaches(BlackMisc::Network::CEntityFlags::Entity entities) override;
|
||||
virtual void admitCaches(BlackMisc::Network::CEntityFlags::Entity entities) override;
|
||||
|
||||
public slots:
|
||||
//! Allow to call CInfoDataReader::ps_read directly, special for info objects
|
||||
@@ -58,7 +60,6 @@ namespace BlackCore
|
||||
|
||||
protected:
|
||||
// cache handling for base class
|
||||
virtual void synchronizeCaches(BlackMisc::Network::CEntityFlags::Entity entities) override;
|
||||
virtual void invalidateCaches(BlackMisc::Network::CEntityFlags::Entity entities) override;
|
||||
virtual bool hasChangedUrl(BlackMisc::Network::CEntityFlags::Entity entity) const override;
|
||||
|
||||
|
||||
@@ -217,17 +217,17 @@ namespace BlackCore
|
||||
|
||||
void CModelDataReader::ps_liveryCacheChanged()
|
||||
{
|
||||
// void
|
||||
this->cacheHasChanged(CEntityFlags::LiveryEntity);
|
||||
}
|
||||
|
||||
void CModelDataReader::ps_modelCacheChanged()
|
||||
{
|
||||
// void
|
||||
this->cacheHasChanged(CEntityFlags::ModelEntity);
|
||||
}
|
||||
|
||||
void CModelDataReader::ps_distributorCacheChanged()
|
||||
{
|
||||
// void
|
||||
this->cacheHasChanged(CEntityFlags::DistributorEntity);
|
||||
}
|
||||
|
||||
void CModelDataReader::ps_baseUrlCacheChanged()
|
||||
@@ -387,7 +387,7 @@ namespace BlackCore
|
||||
{
|
||||
CLiveryList liveries;
|
||||
liveries.convertFromJson(liveriesJson);
|
||||
int c = liveries.size();
|
||||
const int c = liveries.size();
|
||||
this->m_liveryCache.set(liveries);
|
||||
|
||||
emit dataRead(CEntityFlags::LiveryEntity, CEntityFlags::ReadFinished, c);
|
||||
@@ -402,7 +402,7 @@ namespace BlackCore
|
||||
{
|
||||
CAircraftModelList models;
|
||||
models.convertFromJson(Json::jsonObjectFromString(modelsJson));
|
||||
int c = models.size();
|
||||
const int c = models.size();
|
||||
this->m_modelCache.set(models);
|
||||
|
||||
emit dataRead(CEntityFlags::ModelEntity, CEntityFlags::ReadFinished, c);
|
||||
@@ -417,7 +417,7 @@ namespace BlackCore
|
||||
{
|
||||
CDistributorList distributors;
|
||||
distributors.convertFromJson(Json::jsonObjectFromString(distributorsJson));
|
||||
int c = distributors.size();
|
||||
const int c = distributors.size();
|
||||
this->m_distributorCache.set(distributors);
|
||||
|
||||
emit dataRead(CEntityFlags::DistributorEntity, CEntityFlags::ReadFinished, c);
|
||||
@@ -473,6 +473,13 @@ namespace BlackCore
|
||||
if (entities.testFlag(CEntityFlags::DistributorEntity)) { this->m_distributorCache.synchronize(); }
|
||||
}
|
||||
|
||||
void CModelDataReader::admitCaches(CEntityFlags::Entity entities)
|
||||
{
|
||||
if (entities.testFlag(CEntityFlags::LiveryEntity)) { this->m_liveryCache.admit(); }
|
||||
if (entities.testFlag(CEntityFlags::ModelEntity)) { this->m_modelCache.admit(); }
|
||||
if (entities.testFlag(CEntityFlags::DistributorEntity)) { this->m_distributorCache.admit(); }
|
||||
}
|
||||
|
||||
void CModelDataReader::invalidateCaches(CEntityFlags::Entity entities)
|
||||
{
|
||||
if (entities.testFlag(CEntityFlags::LiveryEntity)) { CDataCache::instance()->clearAllValues(this->m_liveryCache.getKey()); }
|
||||
|
||||
@@ -128,6 +128,7 @@ namespace BlackCore
|
||||
virtual QDateTime getCacheTimestamp(BlackMisc::Network::CEntityFlags::Entity entity) const override;
|
||||
virtual int getCacheCount(BlackMisc::Network::CEntityFlags::Entity entity) const override;
|
||||
virtual void synchronizeCaches(BlackMisc::Network::CEntityFlags::Entity entities) override;
|
||||
virtual void admitCaches(BlackMisc::Network::CEntityFlags::Entity entities) override;
|
||||
|
||||
protected:
|
||||
// cache handling for base class
|
||||
|
||||
@@ -172,8 +172,18 @@ namespace BlackCore
|
||||
|
||||
void CWebDataServices::synchronizeDbCaches(CEntityFlags::Entity entities)
|
||||
{
|
||||
if (this->m_infoDataReader) { this->m_infoDataReader->synchronizeCaches(entities); }
|
||||
if (this->m_modelDataReader) { this->m_modelDataReader->synchronizeCaches(entities); }
|
||||
if (this->m_icaoDataReader) { this->m_icaoDataReader->synchronizeCaches(entities); }
|
||||
if (this->m_airportDataReader) { this->m_airportDataReader->synchronizeCaches(entities); }
|
||||
}
|
||||
|
||||
void CWebDataServices::admitDbCaches(CEntityFlags::Entity entities)
|
||||
{
|
||||
if (this->m_infoDataReader) { this->m_infoDataReader->admitCaches(entities); }
|
||||
if (this->m_modelDataReader) { this->m_modelDataReader->admitCaches(entities); }
|
||||
if (this->m_icaoDataReader) { this->m_icaoDataReader->admitCaches(entities); }
|
||||
if (this->m_airportDataReader) { this->m_airportDataReader->admitCaches(entities); }
|
||||
}
|
||||
|
||||
CEntityFlags::Entity CWebDataServices::triggerRead(CEntityFlags::Entity whatToRead, const QDateTime &newerThan)
|
||||
|
||||
@@ -339,6 +339,9 @@ namespace BlackCore
|
||||
//! Synchronize all DB caches
|
||||
void synchronizeDbCaches(BlackMisc::Network::CEntityFlags::Entity entities);
|
||||
|
||||
//! Admit all DB caches
|
||||
void admitDbCaches(BlackMisc::Network::CEntityFlags::Entity entities);
|
||||
|
||||
//! Write data to disk (mainly for testing scenarios)
|
||||
bool writeDbDataToDisk(const QString &dir) const;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user