##// END OF EJS Templates
Modify cache log lvl from info to warn
perrinel -
r303:72aff1132bb4
parent child
Show More
@@ -1,205 +1,205
1 1 #include "Variable/VariableCacheController.h"
2 2
3 3 #include "Variable/Variable.h"
4 4 #include <unordered_map>
5 5
6 6 Q_LOGGING_CATEGORY(LOG_VariableCacheController, "VariableCacheController")
7 7
8 8 struct VariableCacheController::VariableCacheControllerPrivate {
9 9
10 10 std::unordered_map<std::shared_ptr<Variable>, QVector<SqpDateTime> >
11 11 m_VariableToSqpDateTimeListMap;
12 12
13 13 void addInCacheDataByEnd(const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList,
14 14 QVector<SqpDateTime> &notInCache, int cacheIndex,
15 15 double currentTStart);
16 16
17 17 void addInCacheDataByStart(const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList,
18 18 QVector<SqpDateTime> &notInCache, int cacheIndex,
19 19 double currentTStart);
20 20
21 21
22 22 void addDateTimeRecurse(const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList,
23 23 int cacheIndex);
24 24 };
25 25
26 26
27 27 VariableCacheController::VariableCacheController(QObject *parent)
28 28 : QObject{parent}, impl{spimpl::make_unique_impl<VariableCacheControllerPrivate>()}
29 29 {
30 30 }
31 31
32 32 void VariableCacheController::addDateTime(std::shared_ptr<Variable> variable,
33 33 const SqpDateTime &dateTime)
34 34 {
35 35 if (variable) {
36 36 auto findVariableIte = impl->m_VariableToSqpDateTimeListMap.find(variable);
37 37 if (findVariableIte == impl->m_VariableToSqpDateTimeListMap.end()) {
38 38 impl->m_VariableToSqpDateTimeListMap[variable].push_back(dateTime);
39 39 }
40 40 else {
41 41
42 42 // addDateTime modify the list<SqpDateTime> of the variable in a way to ensure
43 43 // that the list is ordered : l(0) < l(1). We assume also a < b
44 44 // (with a & b of type SqpDateTime) means ts(b) > te(a)
45 45
46 46 // The algorithm will try the merge of two interval:
47 47 // - dateTime will be compare with the first interval of the list:
48 48 // A: if it is inferior, it will be inserted and it's finished.
49 49 // B: if it is in intersection, it will be merge then the merged one
50 50 // will be compared to the next interval. The old one is remove from the list
51 51 // C: if it is superior, we do the same with the next interval of the list
52 52
53 53 try {
54 54 impl->addDateTimeRecurse(dateTime,
55 55 impl->m_VariableToSqpDateTimeListMap.at(variable), 0);
56 56 }
57 57 catch (const std::out_of_range &e) {
58 qCInfo(LOG_VariableCacheController()) << e.what();
58 qCWarning(LOG_VariableCacheController()) << e.what();
59 59 }
60 60 }
61 61 }
62 62 }
63 63
64 64 QVector<SqpDateTime>
65 65 VariableCacheController::provideNotInCacheDateTimeList(std::shared_ptr<Variable> variable,
66 66 const SqpDateTime &dateTime)
67 67 {
68 68 auto notInCache = QVector<SqpDateTime>{};
69 69
70 70 // This algorithm is recursif. The idea is to localise the start time then the end time in the
71 71 // list of date time request associated to the variable
72 72 // We assume that the list is ordered in a way that l(0) < l(1). We assume also a < b
73 73 // (with a & b of type SqpDateTime) means ts(b) > te(a)
74 74
75 75 try {
76 76 impl->addInCacheDataByStart(dateTime, impl->m_VariableToSqpDateTimeListMap.at(variable),
77 77 notInCache, 0, dateTime.m_TStart);
78 78 }
79 79 catch (const std::out_of_range &e) {
80 qCInfo(LOG_VariableCacheController()) << e.what();
80 qCWarning(LOG_VariableCacheController()) << e.what();
81 81 }
82 82
83 83 return notInCache;
84 84 }
85 85
86 86 QVector<SqpDateTime>
87 87 VariableCacheController::dateCacheList(std::shared_ptr<Variable> variable) const noexcept
88 88 {
89 89 try {
90 90 return impl->m_VariableToSqpDateTimeListMap.at(variable);
91 91 }
92 92 catch (const std::out_of_range &e) {
93 qCInfo(LOG_VariableCacheController()) << e.what();
93 qCWarning(LOG_VariableCacheController()) << e.what();
94 94 return QVector<SqpDateTime>{};
95 95 }
96 96 }
97 97
98 98 void VariableCacheController::VariableCacheControllerPrivate::addDateTimeRecurse(
99 99 const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList, int cacheIndex)
100 100 {
101 101 const auto dateTimeListSize = dateTimeList.count();
102 102 if (cacheIndex >= dateTimeListSize) {
103 103 dateTimeList.push_back(dateTime);
104 104 // there is no anymore interval to compore, we can just push_back it
105 105 return;
106 106 }
107 107
108 108 auto currentDateTime = dateTimeList[cacheIndex];
109 109
110 110 if (dateTime.m_TEnd < currentDateTime.m_TStart) {
111 111 // The compared one is < to current one compared, we can insert it
112 112 dateTimeList.insert(cacheIndex, dateTime);
113 113 }
114 114 else if (dateTime.m_TStart > currentDateTime.m_TEnd) {
115 115 // The compared one is > to current one compared we can comparet if to the next one
116 116 addDateTimeRecurse(dateTime, dateTimeList, ++cacheIndex);
117 117 }
118 118 else {
119 119 // Merge cases: we need to merge the two interval, remove the old one from the list then
120 120 // rerun the algo from this index with the merged interval
121 121 auto mTStart = std::min(dateTime.m_TStart, currentDateTime.m_TStart);
122 122 auto mTEnd = std::max(dateTime.m_TEnd, currentDateTime.m_TEnd);
123 123 auto mergeDateTime = SqpDateTime{mTStart, mTEnd};
124 124
125 125 dateTimeList.remove(cacheIndex);
126 126 addDateTimeRecurse(mergeDateTime, dateTimeList, cacheIndex);
127 127 }
128 128 }
129 129
130 130
131 131 void VariableCacheController::VariableCacheControllerPrivate::addInCacheDataByEnd(
132 132 const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList,
133 133 QVector<SqpDateTime> &notInCache, int cacheIndex, double currentTStart)
134 134 {
135 135 const auto dateTimeListSize = dateTimeList.count();
136 136 if (cacheIndex >= dateTimeListSize) {
137 137 if (currentTStart < dateTime.m_TEnd) {
138 138
139 139 // te localised after all other interval: The last interval is [currentTsart, te]
140 140 notInCache.push_back(SqpDateTime{currentTStart, dateTime.m_TEnd});
141 141 }
142 142 return;
143 143 }
144 144
145 145 auto currentDateTimeJ = dateTimeList[cacheIndex];
146 146 if (dateTime.m_TEnd <= currentDateTimeJ.m_TStart) {
147 147 // te localised between to interval: The last interval is [currentTsart, te]
148 148 notInCache.push_back(SqpDateTime{currentTStart, dateTime.m_TEnd});
149 149 }
150 150 else {
151 151 notInCache.push_back(SqpDateTime{currentTStart, currentDateTimeJ.m_TStart});
152 152 if (dateTime.m_TEnd > currentDateTimeJ.m_TEnd) {
153 153 // te not localised before the current interval: we need to look at the next interval
154 154 addInCacheDataByEnd(dateTime, dateTimeList, notInCache, ++cacheIndex,
155 155 currentDateTimeJ.m_TEnd);
156 156 }
157 157 }
158 158 }
159 159
160 160 void VariableCacheController::VariableCacheControllerPrivate::addInCacheDataByStart(
161 161 const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList,
162 162 QVector<SqpDateTime> &notInCache, int cacheIndex, double currentTStart)
163 163 {
164 164 const auto dateTimeListSize = dateTimeList.count();
165 165 if (cacheIndex >= dateTimeListSize) {
166 166 // ts localised after all other interval: The last interval is [ts, te]
167 167 notInCache.push_back(SqpDateTime{currentTStart, dateTime.m_TEnd});
168 168 return;
169 169 }
170 170
171 171 auto currentDateTimeI = dateTimeList[cacheIndex];
172 172 if (currentTStart < currentDateTimeI.m_TStart) {
173 173
174 174 // ts localised between to interval: let's localized te
175 175 addInCacheDataByEnd(dateTime, dateTimeList, notInCache, cacheIndex, currentTStart);
176 176 }
177 177 else if (currentTStart < currentDateTimeI.m_TEnd) {
178 178 if (dateTime.m_TEnd > currentDateTimeI.m_TEnd) {
179 179 // ts not localised before the current interval: we need to look at the next interval
180 180 // We can assume now current tstart is the last interval tend, because data between them
181 181 // are
182 182 // in the cache
183 183 addInCacheDataByStart(dateTime, dateTimeList, notInCache, ++cacheIndex,
184 184 currentDateTimeI.m_TEnd);
185 185 }
186 186 }
187 187 else {
188 188 // ts not localised before the current interval: we need to look at the next interval
189 189 addInCacheDataByStart(dateTime, dateTimeList, notInCache, ++cacheIndex, currentTStart);
190 190 }
191 191 }
192 192
193 193
194 194 void VariableCacheController::displayCache(std::shared_ptr<Variable> variable) const
195 195 {
196 196 auto variableDateTimeList = impl->m_VariableToSqpDateTimeListMap.find(variable);
197 197 if (variableDateTimeList != impl->m_VariableToSqpDateTimeListMap.end()) {
198 198 qCInfo(LOG_VariableCacheController()) << tr("VariableCacheController::displayCache")
199 199 << variableDateTimeList->second;
200 200 }
201 201 else {
202 202 qCWarning(LOG_VariableCacheController())
203 203 << tr("Cannot display a variable that is not in the cache");
204 204 }
205 205 }
General Comments 0
You need to be logged in to leave comments. Login now