Auto status change to "Under Review"
@@ -1,40 +1,40 | |||||
1 | #ifndef SCIQLOP_VARIABLECACHECONTROLLER_H |
|
1 | #ifndef SCIQLOP_VARIABLECACHECONTROLLER_H | |
2 | #define SCIQLOP_VARIABLECACHECONTROLLER_H |
|
2 | #define SCIQLOP_VARIABLECACHECONTROLLER_H | |
3 |
|
3 | |||
4 | #include <QObject> |
|
4 | #include <QObject> | |
5 |
|
5 | |||
6 | #include <Data/SqpDateTime.h> |
|
6 | #include <Data/SqpDateTime.h> | |
7 |
|
7 | |||
8 | #include <QLoggingCategory> |
|
8 | #include <QLoggingCategory> | |
9 |
|
9 | |||
10 | #include <Common/spimpl.h> |
|
10 | #include <Common/spimpl.h> | |
11 |
|
11 | |||
12 | class Variable; |
|
12 | class Variable; | |
13 |
|
13 | |||
14 | Q_DECLARE_LOGGING_CATEGORY(LOG_VariableCacheController) |
|
14 | Q_DECLARE_LOGGING_CATEGORY(LOG_VariableCacheController) | |
15 |
|
15 | |||
16 |
|
16 | |||
17 | /// This class aims to store in the cache all of the dateTime already requested to the variable. |
|
17 | /// This class aims to store in the cache all of the dateTime already requested to the variable. | |
18 | class VariableCacheController : public QObject { |
|
18 | class VariableCacheController : public QObject { | |
19 | Q_OBJECT |
|
19 | Q_OBJECT | |
20 | public: |
|
20 | public: | |
21 | explicit VariableCacheController(QObject *parent = 0); |
|
21 | explicit VariableCacheController(QObject *parent = 0); | |
22 |
|
22 | |||
23 |
|
23 | |||
24 | void addDateTime(std::shared_ptr<Variable> variable, const SqpDateTime &dateTime); |
|
24 | void addDateTime(std::shared_ptr<Variable> variable, const SqpDateTime &dateTime); | |
25 |
|
25 | |||
26 | /// Return all of the SqpDataTime part of the dateTime whose are not in the cache |
|
26 | /// Return all of the SqpDataTime part of the dateTime whose are not in the cache | |
27 | QVector<SqpDateTime> provideNotInCacheDateTimeList(std::shared_ptr<Variable> variable, |
|
27 | QVector<SqpDateTime> provideNotInCacheDateTimeList(std::shared_ptr<Variable> variable, | |
28 | const SqpDateTime &dateTime); |
|
28 | const SqpDateTime &dateTime); | |
29 |
|
29 | |||
30 |
|
30 | |||
31 | QVector<SqpDateTime> dateCacheList(std::shared_ptr<Variable> variable) const noexcept; |
|
31 | QVector<SqpDateTime> dateCacheList(std::shared_ptr<Variable> variable) const noexcept; | |
32 |
|
32 | |||
33 | void displayCache(std::shared_ptr<Variable> variable); |
|
33 | void displayCache(std::shared_ptr<Variable> variable) const; | |
34 |
|
34 | |||
35 | private: |
|
35 | private: | |
36 | class VariableCacheControllerPrivate; |
|
36 | class VariableCacheControllerPrivate; | |
37 | spimpl::unique_impl_ptr<VariableCacheControllerPrivate> impl; |
|
37 | spimpl::unique_impl_ptr<VariableCacheControllerPrivate> impl; | |
38 | }; |
|
38 | }; | |
39 |
|
39 | |||
40 | #endif // SCIQLOP_VARIABLECACHECONTROLLER_H |
|
40 | #endif // SCIQLOP_VARIABLECACHECONTROLLER_H |
@@ -1,183 +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 | impl->addDateTimeRecurse(dateTime, impl->m_VariableToSqpDateTimeListMap.at(variable), |
|
53 | try { | |
54 | 0); |
|
54 | impl->addDateTimeRecurse(dateTime, | |
|
55 | impl->m_VariableToSqpDateTimeListMap.at(variable), 0); | |||
|
56 | } | |||
|
57 | catch (const std::out_of_range &e) { | |||
|
58 | qCInfo(LOG_VariableCacheController()) << e.what(); | |||
|
59 | } | |||
55 | } |
|
60 | } | |
56 | } |
|
61 | } | |
57 | } |
|
62 | } | |
58 |
|
63 | |||
59 | QVector<SqpDateTime> |
|
64 | QVector<SqpDateTime> | |
60 | VariableCacheController::provideNotInCacheDateTimeList(std::shared_ptr<Variable> variable, |
|
65 | VariableCacheController::provideNotInCacheDateTimeList(std::shared_ptr<Variable> variable, | |
61 | const SqpDateTime &dateTime) |
|
66 | const SqpDateTime &dateTime) | |
62 | { |
|
67 | { | |
63 | auto notInCache = QVector<SqpDateTime>{}; |
|
68 | auto notInCache = QVector<SqpDateTime>{}; | |
64 |
|
69 | |||
65 | // 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 | |
66 | // list of date time request associated to the variable |
|
71 | // list of date time request associated to the variable | |
67 | // 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 | |
68 | // (with a & b of type SqpDateTime) means ts(b) > te(a) |
|
73 | // (with a & b of type SqpDateTime) means ts(b) > te(a) | |
69 |
|
74 | |||
70 | impl->addInCacheDataByStart(dateTime, impl->m_VariableToSqpDateTimeListMap.at(variable), |
|
75 | try { | |
71 | notInCache, 0, dateTime.m_TStart); |
|
76 | impl->addInCacheDataByStart(dateTime, impl->m_VariableToSqpDateTimeListMap.at(variable), | |
|
77 | notInCache, 0, dateTime.m_TStart); | |||
|
78 | } | |||
|
79 | catch (const std::out_of_range &e) { | |||
|
80 | qCInfo(LOG_VariableCacheController()) << e.what(); | |||
|
81 | } | |||
72 |
|
82 | |||
73 | return notInCache; |
|
83 | return notInCache; | |
74 | } |
|
84 | } | |
75 |
|
85 | |||
76 | QVector<SqpDateTime> |
|
86 | QVector<SqpDateTime> | |
77 | VariableCacheController::dateCacheList(std::shared_ptr<Variable> variable) const noexcept |
|
87 | VariableCacheController::dateCacheList(std::shared_ptr<Variable> variable) const noexcept | |
78 | { |
|
88 | { | |
79 | return impl->m_VariableToSqpDateTimeListMap.at(variable); |
|
89 | try { | |
|
90 | return impl->m_VariableToSqpDateTimeListMap.at(variable); | |||
|
91 | } | |||
|
92 | catch (const std::out_of_range &e) { | |||
|
93 | qCInfo(LOG_VariableCacheController()) << e.what(); | |||
|
94 | return QVector<SqpDateTime>{}; | |||
|
95 | } | |||
80 | } |
|
96 | } | |
81 |
|
97 | |||
82 | void VariableCacheController::VariableCacheControllerPrivate::addDateTimeRecurse( |
|
98 | void VariableCacheController::VariableCacheControllerPrivate::addDateTimeRecurse( | |
83 | const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList, int cacheIndex) |
|
99 | const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList, int cacheIndex) | |
84 | { |
|
100 | { | |
85 | const auto dateTimeListSize = dateTimeList.count(); |
|
101 | const auto dateTimeListSize = dateTimeList.count(); | |
86 | if (cacheIndex >= dateTimeListSize) { |
|
102 | if (cacheIndex >= dateTimeListSize) { | |
87 | dateTimeList.push_back(dateTime); |
|
103 | dateTimeList.push_back(dateTime); | |
88 | // 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 | |
89 | return; |
|
105 | return; | |
90 | } |
|
106 | } | |
91 |
|
107 | |||
92 | auto currentDateTime = dateTimeList[cacheIndex]; |
|
108 | auto currentDateTime = dateTimeList[cacheIndex]; | |
93 |
|
109 | |||
94 | if (dateTime.m_TEnd < currentDateTime.m_TStart) { |
|
110 | if (dateTime.m_TEnd < currentDateTime.m_TStart) { | |
95 | // 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 | |
96 | dateTimeList.insert(cacheIndex, dateTime); |
|
112 | dateTimeList.insert(cacheIndex, dateTime); | |
97 | } |
|
113 | } | |
98 | else if (dateTime.m_TStart > currentDateTime.m_TEnd) { |
|
114 | else if (dateTime.m_TStart > currentDateTime.m_TEnd) { | |
99 | // 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 | |
100 | addDateTimeRecurse(dateTime, dateTimeList, ++cacheIndex); |
|
116 | addDateTimeRecurse(dateTime, dateTimeList, ++cacheIndex); | |
101 | } |
|
117 | } | |
102 | else { |
|
118 | else { | |
103 | // 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 | |
104 | // rerun the algo from this index with the merged interval |
|
120 | // rerun the algo from this index with the merged interval | |
105 | auto mTStart = std::min(dateTime.m_TStart, currentDateTime.m_TStart); |
|
121 | auto mTStart = std::min(dateTime.m_TStart, currentDateTime.m_TStart); | |
106 | auto mTEnd = std::max(dateTime.m_TEnd, currentDateTime.m_TEnd); |
|
122 | auto mTEnd = std::max(dateTime.m_TEnd, currentDateTime.m_TEnd); | |
107 | auto mergeDateTime = SqpDateTime{mTStart, mTEnd}; |
|
123 | auto mergeDateTime = SqpDateTime{mTStart, mTEnd}; | |
108 |
|
124 | |||
109 | dateTimeList.remove(cacheIndex); |
|
125 | dateTimeList.remove(cacheIndex); | |
110 | addDateTimeRecurse(mergeDateTime, dateTimeList, cacheIndex); |
|
126 | addDateTimeRecurse(mergeDateTime, dateTimeList, cacheIndex); | |
111 | } |
|
127 | } | |
112 | } |
|
128 | } | |
113 |
|
129 | |||
114 |
|
130 | |||
115 | void VariableCacheController::VariableCacheControllerPrivate::addInCacheDataByEnd( |
|
131 | void VariableCacheController::VariableCacheControllerPrivate::addInCacheDataByEnd( | |
116 | const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList, |
|
132 | const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList, | |
117 | QVector<SqpDateTime> ¬InCache, int cacheIndex, double currentTStart) |
|
133 | QVector<SqpDateTime> ¬InCache, int cacheIndex, double currentTStart) | |
118 | { |
|
134 | { | |
119 | const auto dateTimeListSize = dateTimeList.count(); |
|
135 | const auto dateTimeListSize = dateTimeList.count(); | |
120 | if (cacheIndex >= dateTimeListSize) { |
|
136 | if (cacheIndex >= dateTimeListSize) { | |
121 | if (currentTStart < dateTime.m_TEnd) { |
|
137 | if (currentTStart < dateTime.m_TEnd) { | |
122 |
|
138 | |||
123 | // 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] | |
124 | notInCache.push_back(SqpDateTime{currentTStart, dateTime.m_TEnd}); |
|
140 | notInCache.push_back(SqpDateTime{currentTStart, dateTime.m_TEnd}); | |
125 | } |
|
141 | } | |
126 | return; |
|
142 | return; | |
127 | } |
|
143 | } | |
128 |
|
144 | |||
129 | auto currentDateTimeJ = dateTimeList[cacheIndex]; |
|
145 | auto currentDateTimeJ = dateTimeList[cacheIndex]; | |
130 | if (dateTime.m_TEnd <= currentDateTimeJ.m_TStart) { |
|
146 | if (dateTime.m_TEnd <= currentDateTimeJ.m_TStart) { | |
131 | // te localised between to interval: The last interval is [currentTsart, te] |
|
147 | // te localised between to interval: The last interval is [currentTsart, te] | |
132 | notInCache.push_back(SqpDateTime{currentTStart, dateTime.m_TEnd}); |
|
148 | notInCache.push_back(SqpDateTime{currentTStart, dateTime.m_TEnd}); | |
133 | } |
|
149 | } | |
134 | else { |
|
150 | else { | |
135 | notInCache.push_back(SqpDateTime{currentTStart, currentDateTimeJ.m_TStart}); |
|
151 | notInCache.push_back(SqpDateTime{currentTStart, currentDateTimeJ.m_TStart}); | |
136 | if (dateTime.m_TEnd > currentDateTimeJ.m_TEnd) { |
|
152 | if (dateTime.m_TEnd > currentDateTimeJ.m_TEnd) { | |
137 | // 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 | |
138 | addInCacheDataByEnd(dateTime, dateTimeList, notInCache, ++cacheIndex, |
|
154 | addInCacheDataByEnd(dateTime, dateTimeList, notInCache, ++cacheIndex, | |
139 | currentDateTimeJ.m_TEnd); |
|
155 | currentDateTimeJ.m_TEnd); | |
140 | } |
|
156 | } | |
141 | } |
|
157 | } | |
142 | } |
|
158 | } | |
143 |
|
159 | |||
144 | void VariableCacheController::VariableCacheControllerPrivate::addInCacheDataByStart( |
|
160 | void VariableCacheController::VariableCacheControllerPrivate::addInCacheDataByStart( | |
145 | const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList, |
|
161 | const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList, | |
146 | QVector<SqpDateTime> ¬InCache, int cacheIndex, double currentTStart) |
|
162 | QVector<SqpDateTime> ¬InCache, int cacheIndex, double currentTStart) | |
147 | { |
|
163 | { | |
148 | const auto dateTimeListSize = dateTimeList.count(); |
|
164 | const auto dateTimeListSize = dateTimeList.count(); | |
149 | if (cacheIndex >= dateTimeListSize) { |
|
165 | if (cacheIndex >= dateTimeListSize) { | |
150 | // 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] | |
151 | notInCache.push_back(SqpDateTime{currentTStart, dateTime.m_TEnd}); |
|
167 | notInCache.push_back(SqpDateTime{currentTStart, dateTime.m_TEnd}); | |
152 | return; |
|
168 | return; | |
153 | } |
|
169 | } | |
154 |
|
170 | |||
155 | auto currentDateTimeI = dateTimeList[cacheIndex]; |
|
171 | auto currentDateTimeI = dateTimeList[cacheIndex]; | |
156 | if (currentTStart < currentDateTimeI.m_TStart) { |
|
172 | if (currentTStart < currentDateTimeI.m_TStart) { | |
157 |
|
173 | |||
158 | // ts localised between to interval: let's localized te |
|
174 | // ts localised between to interval: let's localized te | |
159 | addInCacheDataByEnd(dateTime, dateTimeList, notInCache, cacheIndex, currentTStart); |
|
175 | addInCacheDataByEnd(dateTime, dateTimeList, notInCache, cacheIndex, currentTStart); | |
160 | } |
|
176 | } | |
161 | else if (currentTStart < currentDateTimeI.m_TEnd) { |
|
177 | else if (currentTStart < currentDateTimeI.m_TEnd) { | |
162 | if (dateTime.m_TEnd > currentDateTimeI.m_TEnd) { |
|
178 | if (dateTime.m_TEnd > currentDateTimeI.m_TEnd) { | |
163 | // 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 | |
164 | // 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 | |
165 | // are |
|
181 | // are | |
166 | // in the cache |
|
182 | // in the cache | |
167 | addInCacheDataByStart(dateTime, dateTimeList, notInCache, ++cacheIndex, |
|
183 | addInCacheDataByStart(dateTime, dateTimeList, notInCache, ++cacheIndex, | |
168 | currentDateTimeI.m_TEnd); |
|
184 | currentDateTimeI.m_TEnd); | |
169 | } |
|
185 | } | |
170 | } |
|
186 | } | |
171 | else { |
|
187 | else { | |
172 | // 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 | |
173 | addInCacheDataByStart(dateTime, dateTimeList, notInCache, ++cacheIndex, currentTStart); |
|
189 | addInCacheDataByStart(dateTime, dateTimeList, notInCache, ++cacheIndex, currentTStart); | |
174 | } |
|
190 | } | |
175 | } |
|
191 | } | |
176 |
|
192 | |||
177 |
|
193 | |||
178 | void VariableCacheController::displayCache(std::shared_ptr<Variable> variable) |
|
194 | void VariableCacheController::displayCache(std::shared_ptr<Variable> variable) const | |
179 | { |
|
195 | { | |
180 |
auto variableDateTimeList = impl->m_VariableToSqpDateTimeListMap. |
|
196 | auto variableDateTimeList = impl->m_VariableToSqpDateTimeListMap.find(variable); | |
181 | qCInfo(LOG_VariableCacheController()) << tr("VariableCacheController::displayCache") |
|
197 | if (variableDateTimeList != impl->m_VariableToSqpDateTimeListMap.end()) { | |
182 | << variableDateTimeList; |
|
198 | qCInfo(LOG_VariableCacheController()) << tr("VariableCacheController::displayCache") | |
|
199 | << variableDateTimeList->second; | |||
|
200 | } | |||
|
201 | else { | |||
|
202 | qCWarning(LOG_VariableCacheController()) | |||
|
203 | << tr("Cannot display a variable that is not in the cache"); | |||
|
204 | } | |||
183 | } |
|
205 | } |
General Comments 1
You need to be logged in to leave comments.
Login now