##// END OF EJS Templates
Correction MR
perrinel -
r300:462cefb59d8a
parent child
Show More
@@ -1,40 +1,40
1 1 #ifndef SCIQLOP_VARIABLECACHECONTROLLER_H
2 2 #define SCIQLOP_VARIABLECACHECONTROLLER_H
3 3
4 4 #include <QObject>
5 5
6 6 #include <Data/SqpDateTime.h>
7 7
8 8 #include <QLoggingCategory>
9 9
10 10 #include <Common/spimpl.h>
11 11
12 12 class Variable;
13 13
14 14 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableCacheController)
15 15
16 16
17 17 /// This class aims to store in the cache all of the dateTime already requested to the variable.
18 18 class VariableCacheController : public QObject {
19 19 Q_OBJECT
20 20 public:
21 21 explicit VariableCacheController(QObject *parent = 0);
22 22
23 23
24 24 void addDateTime(std::shared_ptr<Variable> variable, const SqpDateTime &dateTime);
25 25
26 26 /// Return all of the SqpDataTime part of the dateTime whose are not in the cache
27 27 QVector<SqpDateTime> provideNotInCacheDateTimeList(std::shared_ptr<Variable> variable,
28 28 const SqpDateTime &dateTime);
29 29
30 30
31 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 35 private:
36 36 class VariableCacheControllerPrivate;
37 37 spimpl::unique_impl_ptr<VariableCacheControllerPrivate> impl;
38 38 };
39 39
40 40 #endif // SCIQLOP_VARIABLECACHECONTROLLER_H
@@ -1,183 +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 impl->addDateTimeRecurse(dateTime, impl->m_VariableToSqpDateTimeListMap.at(variable),
54 0);
53 try {
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 64 QVector<SqpDateTime>
60 65 VariableCacheController::provideNotInCacheDateTimeList(std::shared_ptr<Variable> variable,
61 66 const SqpDateTime &dateTime)
62 67 {
63 68 auto notInCache = QVector<SqpDateTime>{};
64 69
65 70 // This algorithm is recursif. The idea is to localise the start time then the end time in the
66 71 // list of date time request associated to the variable
67 72 // We assume that the list is ordered in a way that l(0) < l(1). We assume also a < b
68 73 // (with a & b of type SqpDateTime) means ts(b) > te(a)
69 74
70 impl->addInCacheDataByStart(dateTime, impl->m_VariableToSqpDateTimeListMap.at(variable),
71 notInCache, 0, dateTime.m_TStart);
75 try {
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 83 return notInCache;
74 84 }
75 85
76 86 QVector<SqpDateTime>
77 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 98 void VariableCacheController::VariableCacheControllerPrivate::addDateTimeRecurse(
83 99 const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList, int cacheIndex)
84 100 {
85 101 const auto dateTimeListSize = dateTimeList.count();
86 102 if (cacheIndex >= dateTimeListSize) {
87 103 dateTimeList.push_back(dateTime);
88 104 // there is no anymore interval to compore, we can just push_back it
89 105 return;
90 106 }
91 107
92 108 auto currentDateTime = dateTimeList[cacheIndex];
93 109
94 110 if (dateTime.m_TEnd < currentDateTime.m_TStart) {
95 111 // The compared one is < to current one compared, we can insert it
96 112 dateTimeList.insert(cacheIndex, dateTime);
97 113 }
98 114 else if (dateTime.m_TStart > currentDateTime.m_TEnd) {
99 115 // The compared one is > to current one compared we can comparet if to the next one
100 116 addDateTimeRecurse(dateTime, dateTimeList, ++cacheIndex);
101 117 }
102 118 else {
103 119 // Merge cases: we need to merge the two interval, remove the old one from the list then
104 120 // rerun the algo from this index with the merged interval
105 121 auto mTStart = std::min(dateTime.m_TStart, currentDateTime.m_TStart);
106 122 auto mTEnd = std::max(dateTime.m_TEnd, currentDateTime.m_TEnd);
107 123 auto mergeDateTime = SqpDateTime{mTStart, mTEnd};
108 124
109 125 dateTimeList.remove(cacheIndex);
110 126 addDateTimeRecurse(mergeDateTime, dateTimeList, cacheIndex);
111 127 }
112 128 }
113 129
114 130
115 131 void VariableCacheController::VariableCacheControllerPrivate::addInCacheDataByEnd(
116 132 const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList,
117 133 QVector<SqpDateTime> &notInCache, int cacheIndex, double currentTStart)
118 134 {
119 135 const auto dateTimeListSize = dateTimeList.count();
120 136 if (cacheIndex >= dateTimeListSize) {
121 137 if (currentTStart < dateTime.m_TEnd) {
122 138
123 139 // te localised after all other interval: The last interval is [currentTsart, te]
124 140 notInCache.push_back(SqpDateTime{currentTStart, dateTime.m_TEnd});
125 141 }
126 142 return;
127 143 }
128 144
129 145 auto currentDateTimeJ = dateTimeList[cacheIndex];
130 146 if (dateTime.m_TEnd <= currentDateTimeJ.m_TStart) {
131 147 // te localised between to interval: The last interval is [currentTsart, te]
132 148 notInCache.push_back(SqpDateTime{currentTStart, dateTime.m_TEnd});
133 149 }
134 150 else {
135 151 notInCache.push_back(SqpDateTime{currentTStart, currentDateTimeJ.m_TStart});
136 152 if (dateTime.m_TEnd > currentDateTimeJ.m_TEnd) {
137 153 // te not localised before the current interval: we need to look at the next interval
138 154 addInCacheDataByEnd(dateTime, dateTimeList, notInCache, ++cacheIndex,
139 155 currentDateTimeJ.m_TEnd);
140 156 }
141 157 }
142 158 }
143 159
144 160 void VariableCacheController::VariableCacheControllerPrivate::addInCacheDataByStart(
145 161 const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList,
146 162 QVector<SqpDateTime> &notInCache, int cacheIndex, double currentTStart)
147 163 {
148 164 const auto dateTimeListSize = dateTimeList.count();
149 165 if (cacheIndex >= dateTimeListSize) {
150 166 // ts localised after all other interval: The last interval is [ts, te]
151 167 notInCache.push_back(SqpDateTime{currentTStart, dateTime.m_TEnd});
152 168 return;
153 169 }
154 170
155 171 auto currentDateTimeI = dateTimeList[cacheIndex];
156 172 if (currentTStart < currentDateTimeI.m_TStart) {
157 173
158 174 // ts localised between to interval: let's localized te
159 175 addInCacheDataByEnd(dateTime, dateTimeList, notInCache, cacheIndex, currentTStart);
160 176 }
161 177 else if (currentTStart < currentDateTimeI.m_TEnd) {
162 178 if (dateTime.m_TEnd > currentDateTimeI.m_TEnd) {
163 179 // ts not localised before the current interval: we need to look at the next interval
164 180 // We can assume now current tstart is the last interval tend, because data between them
165 181 // are
166 182 // in the cache
167 183 addInCacheDataByStart(dateTime, dateTimeList, notInCache, ++cacheIndex,
168 184 currentDateTimeI.m_TEnd);
169 185 }
170 186 }
171 187 else {
172 188 // ts not localised before the current interval: we need to look at the next interval
173 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.at(variable);
181 qCInfo(LOG_VariableCacheController()) << tr("VariableCacheController::displayCache")
182 << variableDateTimeList;
196 auto variableDateTimeList = impl->m_VariableToSqpDateTimeListMap.find(variable);
197 if (variableDateTimeList != impl->m_VariableToSqpDateTimeListMap.end()) {
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 0
You need to be logged in to leave comments. Login now