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