VariableCacheController.cpp
225 lines
| 9.3 KiB
| text/x-c
|
CppLexer
r209 | #include "Variable/VariableCacheController.h" | |||
#include "Variable/Variable.h" | ||||
#include <unordered_map> | ||||
r336 | #include <QThread> | |||
r271 | Q_LOGGING_CATEGORY(LOG_VariableCacheController, "VariableCacheController") | |||
r209 | struct VariableCacheController::VariableCacheControllerPrivate { | |||
r471 | std::unordered_map<std::shared_ptr<Variable>, QVector<SqpRange> > m_VariableToSqpRangeListMap; | |||
r213 | ||||
r471 | void addInCacheDataByEnd(const SqpRange &dateTime, QVector<SqpRange> &dateTimeList, | |||
QVector<SqpRange> ¬InCache, int cacheIndex, double currentTStart); | ||||
r213 | ||||
r471 | void addInCacheDataByStart(const SqpRange &dateTime, QVector<SqpRange> &dateTimeList, | |||
QVector<SqpRange> ¬InCache, int cacheIndex, double currentTStart); | ||||
r214 | ||||
r471 | void addDateTimeRecurse(const SqpRange &dateTime, QVector<SqpRange> &dateTimeList, | |||
r214 | int cacheIndex); | |||
r209 | }; | |||
VariableCacheController::VariableCacheController(QObject *parent) | ||||
r227 | : QObject{parent}, impl{spimpl::make_unique_impl<VariableCacheControllerPrivate>()} | |||
r209 | { | |||
} | ||||
void VariableCacheController::addDateTime(std::shared_ptr<Variable> variable, | ||||
r471 | const SqpRange &dateTime) | |||
r209 | { | |||
r339 | qCDebug(LOG_VariableCacheController()) << "VariableCacheController::addDateTime" | |||
<< QThread::currentThread()->objectName(); | ||||
r209 | if (variable) { | |||
r471 | auto findVariableIte = impl->m_VariableToSqpRangeListMap.find(variable); | |||
if (findVariableIte == impl->m_VariableToSqpRangeListMap.end()) { | ||||
impl->m_VariableToSqpRangeListMap[variable].push_back(dateTime); | ||||
r214 | } | |||
else { | ||||
r471 | // addDateTime modify the list<SqpRange> of the variable in a way to ensure | |||
r214 | // that the list is ordered : l(0) < l(1). We assume also a < b | |||
r471 | // (with a & b of type SqpRange) means ts(b) > te(a) | |||
r214 | ||||
// The algorithm will try the merge of two interval: | ||||
// - dateTime will be compare with the first interval of the list: | ||||
// A: if it is inferior, it will be inserted and it's finished. | ||||
// B: if it is in intersection, it will be merge then the merged one | ||||
// will be compared to the next interval. The old one is remove from the list | ||||
// C: if it is superior, we do the same with the next interval of the list | ||||
r278 | try { | |||
r471 | impl->addDateTimeRecurse(dateTime, impl->m_VariableToSqpRangeListMap.at(variable), | |||
0); | ||||
r278 | } | |||
catch (const std::out_of_range &e) { | ||||
r294 | qCWarning(LOG_VariableCacheController()) << "addDateTime" << e.what(); | |||
r278 | } | |||
r214 | } | |||
r209 | } | |||
} | ||||
r213 | ||||
Alexandre Leroux
|
r305 | void VariableCacheController::clear(std::shared_ptr<Variable> variable) noexcept | ||
{ | ||||
if (!variable) { | ||||
qCCritical(LOG_VariableCacheController()) << "Can't clear variable cache: variable is null"; | ||||
return; | ||||
} | ||||
r471 | auto nbEntries = impl->m_VariableToSqpRangeListMap.erase(variable); | |||
Alexandre Leroux
|
r305 | |||
auto clearCacheMessage | ||||
= (nbEntries != 0) | ||||
? tr("Variable cache cleared for variable %1").arg(variable->name()) | ||||
: tr("No deletion of variable cache: no cache was associated with the variable"); | ||||
qCDebug(LOG_VariableCacheController()) << clearCacheMessage; | ||||
} | ||||
r471 | QVector<SqpRange> | |||
r213 | VariableCacheController::provideNotInCacheDateTimeList(std::shared_ptr<Variable> variable, | |||
r471 | const SqpRange &dateTime) | |||
r213 | { | |||
r339 | qCDebug(LOG_VariableCacheController()) | |||
r336 | << "VariableCacheController::provideNotInCacheDateTimeList" | |||
<< QThread::currentThread()->objectName(); | ||||
r471 | auto notInCache = QVector<SqpRange>{}; | |||
r213 | ||||
// This algorithm is recursif. The idea is to localise the start time then the end time in the | ||||
// list of date time request associated to the variable | ||||
// We assume that the list is ordered in a way that l(0) < l(1). We assume also a < b | ||||
r471 | // (with a & b of type SqpRange) means ts(b) > te(a) | |||
auto it = impl->m_VariableToSqpRangeListMap.find(variable); | ||||
if (it != impl->m_VariableToSqpRangeListMap.end()) { | ||||
r294 | impl->addInCacheDataByStart(dateTime, it->second, notInCache, 0, dateTime.m_TStart); | |||
r278 | } | |||
r294 | else { | |||
notInCache << dateTime; | ||||
r278 | } | |||
r213 | ||||
return notInCache; | ||||
} | ||||
r471 | QVector<SqpRange> | |||
r214 | VariableCacheController::dateCacheList(std::shared_ptr<Variable> variable) const noexcept | |||
{ | ||||
r339 | qCDebug(LOG_VariableCacheController()) << "VariableCacheController::dateCacheList" | |||
<< QThread::currentThread()->objectName(); | ||||
r278 | try { | |||
r471 | return impl->m_VariableToSqpRangeListMap.at(variable); | |||
r278 | } | |||
catch (const std::out_of_range &e) { | ||||
r280 | qCWarning(LOG_VariableCacheController()) << e.what(); | |||
r471 | return QVector<SqpRange>{}; | |||
r278 | } | |||
r214 | } | |||
void VariableCacheController::VariableCacheControllerPrivate::addDateTimeRecurse( | ||||
r471 | const SqpRange &dateTime, QVector<SqpRange> &dateTimeList, int cacheIndex) | |||
r214 | { | |||
const auto dateTimeListSize = dateTimeList.count(); | ||||
if (cacheIndex >= dateTimeListSize) { | ||||
dateTimeList.push_back(dateTime); | ||||
// there is no anymore interval to compore, we can just push_back it | ||||
return; | ||||
} | ||||
auto currentDateTime = dateTimeList[cacheIndex]; | ||||
if (dateTime.m_TEnd < currentDateTime.m_TStart) { | ||||
// The compared one is < to current one compared, we can insert it | ||||
dateTimeList.insert(cacheIndex, dateTime); | ||||
} | ||||
else if (dateTime.m_TStart > currentDateTime.m_TEnd) { | ||||
// The compared one is > to current one compared we can comparet if to the next one | ||||
addDateTimeRecurse(dateTime, dateTimeList, ++cacheIndex); | ||||
} | ||||
else { | ||||
// Merge cases: we need to merge the two interval, remove the old one from the list then | ||||
// rerun the algo from this index with the merged interval | ||||
auto mTStart = std::min(dateTime.m_TStart, currentDateTime.m_TStart); | ||||
auto mTEnd = std::max(dateTime.m_TEnd, currentDateTime.m_TEnd); | ||||
r471 | auto mergeDateTime = SqpRange{mTStart, mTEnd}; | |||
r214 | ||||
dateTimeList.remove(cacheIndex); | ||||
addDateTimeRecurse(mergeDateTime, dateTimeList, cacheIndex); | ||||
} | ||||
} | ||||
r213 | ||||
void VariableCacheController::VariableCacheControllerPrivate::addInCacheDataByEnd( | ||||
r471 | const SqpRange &dateTime, QVector<SqpRange> &dateTimeList, QVector<SqpRange> ¬InCache, | |||
int cacheIndex, double currentTStart) | ||||
r213 | { | |||
const auto dateTimeListSize = dateTimeList.count(); | ||||
if (cacheIndex >= dateTimeListSize) { | ||||
if (currentTStart < dateTime.m_TEnd) { | ||||
// te localised after all other interval: The last interval is [currentTsart, te] | ||||
r471 | notInCache.push_back(SqpRange{currentTStart, dateTime.m_TEnd}); | |||
r213 | } | |||
return; | ||||
} | ||||
auto currentDateTimeJ = dateTimeList[cacheIndex]; | ||||
if (dateTime.m_TEnd <= currentDateTimeJ.m_TStart) { | ||||
// te localised between to interval: The last interval is [currentTsart, te] | ||||
r471 | notInCache.push_back(SqpRange{currentTStart, dateTime.m_TEnd}); | |||
r213 | } | |||
else { | ||||
r471 | notInCache.push_back(SqpRange{currentTStart, currentDateTimeJ.m_TStart}); | |||
r213 | if (dateTime.m_TEnd > currentDateTimeJ.m_TEnd) { | |||
// te not localised before the current interval: we need to look at the next interval | ||||
addInCacheDataByEnd(dateTime, dateTimeList, notInCache, ++cacheIndex, | ||||
currentDateTimeJ.m_TEnd); | ||||
} | ||||
} | ||||
} | ||||
void VariableCacheController::VariableCacheControllerPrivate::addInCacheDataByStart( | ||||
r471 | const SqpRange &dateTime, QVector<SqpRange> &dateTimeList, QVector<SqpRange> ¬InCache, | |||
int cacheIndex, double currentTStart) | ||||
r213 | { | |||
const auto dateTimeListSize = dateTimeList.count(); | ||||
if (cacheIndex >= dateTimeListSize) { | ||||
// ts localised after all other interval: The last interval is [ts, te] | ||||
r471 | notInCache.push_back(SqpRange{currentTStart, dateTime.m_TEnd}); | |||
r213 | return; | |||
} | ||||
auto currentDateTimeI = dateTimeList[cacheIndex]; | ||||
if (currentTStart < currentDateTimeI.m_TStart) { | ||||
// ts localised between to interval: let's localized te | ||||
r227 | addInCacheDataByEnd(dateTime, dateTimeList, notInCache, cacheIndex, currentTStart); | |||
r213 | } | |||
r271 | else if (currentTStart < currentDateTimeI.m_TEnd) { | |||
if (dateTime.m_TEnd > currentDateTimeI.m_TEnd) { | ||||
// ts not localised before the current interval: we need to look at the next interval | ||||
// We can assume now current tstart is the last interval tend, because data between them | ||||
// are | ||||
// in the cache | ||||
addInCacheDataByStart(dateTime, dateTimeList, notInCache, ++cacheIndex, | ||||
currentDateTimeI.m_TEnd); | ||||
} | ||||
r213 | } | |||
else { | ||||
// ts not localised before the current interval: we need to look at the next interval | ||||
addInCacheDataByStart(dateTime, dateTimeList, notInCache, ++cacheIndex, currentTStart); | ||||
} | ||||
} | ||||
r271 | ||||
r278 | void VariableCacheController::displayCache(std::shared_ptr<Variable> variable) const | |||
r271 | { | |||
r471 | auto variableDateTimeList = impl->m_VariableToSqpRangeListMap.find(variable); | |||
if (variableDateTimeList != impl->m_VariableToSqpRangeListMap.end()) { | ||||
r337 | qCInfo(LOG_VariableCacheController()) << tr("VariableCacheController::displayCache") | |||
<< variableDateTimeList->second; | ||||
r278 | } | |||
else { | ||||
qCWarning(LOG_VariableCacheController()) | ||||
<< tr("Cannot display a variable that is not in the cache"); | ||||
} | ||||
r271 | } | |||