##// END OF EJS Templates
Implementation of the cach interval algoritm
perrinel -
r229:527fddc441a1
parent child
Show More
@@ -0,0 +1,245
1 #include <Variable/Variable.h>
2 #include <Variable/VariableCacheController.h>
3
4 #include <QObject>
5 #include <QtTest>
6
7 #include <memory>
8
9 class TestVariableCacheController : public QObject {
10 Q_OBJECT
11
12 private slots:
13 void testProvideNotInCacheDateTimeList();
14 };
15
16
17 void TestVariableCacheController::testProvideNotInCacheDateTimeList()
18 {
19 VariableCacheController variableCacheController{};
20
21 auto ts0 = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 0, 0}};
22 auto te0 = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}};
23 auto sqp0 = SqpDateTime{static_cast<double>(ts0.toMSecsSinceEpoch()),
24 static_cast<double>(te0.toMSecsSinceEpoch())};
25
26 auto ts1 = QDateTime{QDate{2017, 01, 01}, QTime{2, 6, 0, 0}};
27 auto te1 = QDateTime{QDate{2017, 01, 01}, QTime{2, 8, 0, 0}};
28 auto sqp1 = SqpDateTime{static_cast<double>(ts1.toMSecsSinceEpoch()),
29 static_cast<double>(te1.toMSecsSinceEpoch())};
30
31 auto ts2 = QDateTime{QDate{2017, 01, 01}, QTime{2, 18, 0, 0}};
32 auto te2 = QDateTime{QDate{2017, 01, 01}, QTime{2, 20, 0, 0}};
33 auto sqp2 = SqpDateTime{static_cast<double>(ts2.toMSecsSinceEpoch()),
34 static_cast<double>(te2.toMSecsSinceEpoch())};
35
36 auto var0 = std::make_shared<Variable>("", "", "", sqp0);
37
38 variableCacheController.addDateTime(var0, sqp0);
39 variableCacheController.addDateTime(var0, sqp1);
40 variableCacheController.addDateTime(var0, sqp2);
41
42 // first case [ts,te] < ts0
43 auto ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
44 auto te = QDateTime{QDate{2017, 01, 01}, QTime{2, 1, 0, 0}};
45 auto sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
46 static_cast<double>(te.toMSecsSinceEpoch())};
47
48
49 auto notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
50
51 QCOMPARE(notInCach.size(), 1);
52 auto notInCashSqp = notInCach.first();
53 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
54 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
55
56
57 // second case ts < ts0 && ts0 < te <= te0
58 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
59 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}};
60 sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
61 static_cast<double>(te.toMSecsSinceEpoch())};
62
63
64 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
65
66 QCOMPARE(notInCach.size(), 1);
67 notInCashSqp = notInCach.first();
68 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
69 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(ts0.toMSecsSinceEpoch()));
70
71 // 3th case ts < ts0 && te0 < te <= ts1
72 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
73 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
74 sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
75 static_cast<double>(te.toMSecsSinceEpoch())};
76
77
78 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
79
80 QCOMPARE(notInCach.size(), 2);
81 notInCashSqp = notInCach.first();
82 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
83 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(ts0.toMSecsSinceEpoch()));
84
85 notInCashSqp = notInCach.at(1);
86 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(te0.toMSecsSinceEpoch()));
87 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
88
89 // 4th case ts < ts0 && ts1 < te <= te1
90 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
91 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 7, 0, 0}};
92 sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
93 static_cast<double>(te.toMSecsSinceEpoch())};
94
95
96 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
97
98 QCOMPARE(notInCach.size(), 2);
99 notInCashSqp = notInCach.first();
100 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
101 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(ts0.toMSecsSinceEpoch()));
102
103 notInCashSqp = notInCach.at(1);
104 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(te0.toMSecsSinceEpoch()));
105 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(ts1.toMSecsSinceEpoch()));
106
107 // 5th case ts < ts0 && te3 < te
108 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 0, 0, 0}};
109 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 22, 0, 0}};
110 sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
111 static_cast<double>(te.toMSecsSinceEpoch())};
112
113
114 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
115
116 QCOMPARE(notInCach.size(), 4);
117 notInCashSqp = notInCach.first();
118 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
119 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(ts0.toMSecsSinceEpoch()));
120
121 notInCashSqp = notInCach.at(1);
122 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(te0.toMSecsSinceEpoch()));
123 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(ts1.toMSecsSinceEpoch()));
124
125 notInCashSqp = notInCach.at(2);
126 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(te1.toMSecsSinceEpoch()));
127 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(ts2.toMSecsSinceEpoch()));
128
129 notInCashSqp = notInCach.at(3);
130 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(te2.toMSecsSinceEpoch()));
131 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
132
133
134 // 6th case ts2 < ts
135 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 45, 0, 0}};
136 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 47, 0, 0}};
137 sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
138 static_cast<double>(te.toMSecsSinceEpoch())};
139
140
141 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
142
143 QCOMPARE(notInCach.size(), 1);
144 notInCashSqp = notInCach.first();
145 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
146 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
147
148 // 7th case ts = te0 && te < ts1
149 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 4, 0, 0}};
150 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
151 sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
152 static_cast<double>(te.toMSecsSinceEpoch())};
153
154
155 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
156
157 QCOMPARE(notInCach.size(), 1);
158 notInCashSqp = notInCach.first();
159 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(te0.toMSecsSinceEpoch()));
160 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
161
162 // 8th case ts0 < ts < te0 && te < ts1
163 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}};
164 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
165 sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
166 static_cast<double>(te.toMSecsSinceEpoch())};
167
168
169 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
170
171 QCOMPARE(notInCach.size(), 1);
172 notInCashSqp = notInCach.first();
173 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(te0.toMSecsSinceEpoch()));
174 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
175
176 // 9th case ts0 < ts < te0 && ts1 < te < te1
177 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 3, 30, 0}};
178 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 7, 0, 0}};
179 sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
180 static_cast<double>(te.toMSecsSinceEpoch())};
181
182
183 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
184
185 QCOMPARE(notInCach.size(), 1);
186 notInCashSqp = notInCach.first();
187 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(te0.toMSecsSinceEpoch()));
188 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(ts1.toMSecsSinceEpoch()));
189
190 // 10th case te1 < ts < te < ts2
191 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 9, 0, 0}};
192 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 10, 0, 0}};
193 sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
194 static_cast<double>(te.toMSecsSinceEpoch())};
195
196
197 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
198
199 QCOMPARE(notInCach.size(), 1);
200 notInCashSqp = notInCach.first();
201 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
202 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
203
204 // 11th case te0 < ts < ts1 && te3 < te
205 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
206 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 47, 0, 0}};
207 sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
208 static_cast<double>(te.toMSecsSinceEpoch())};
209
210
211 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
212
213 QCOMPARE(notInCach.size(), 3);
214 notInCashSqp = notInCach.first();
215 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
216 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(ts1.toMSecsSinceEpoch()));
217
218 notInCashSqp = notInCach.at(1);
219 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(te1.toMSecsSinceEpoch()));
220 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(ts2.toMSecsSinceEpoch()));
221
222 notInCashSqp = notInCach.at(2);
223 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(te2.toMSecsSinceEpoch()));
224 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
225
226 // 12th case te0 < ts < ts1 && te3 < te
227 ts = QDateTime{QDate{2017, 01, 01}, QTime{2, 5, 0, 0}};
228 te = QDateTime{QDate{2017, 01, 01}, QTime{2, 10, 0, 0}};
229 sqp = SqpDateTime{static_cast<double>(ts.toMSecsSinceEpoch()),
230 static_cast<double>(te.toMSecsSinceEpoch())};
231
232 notInCach = variableCacheController.provideNotInCacheDateTimeList(var0, sqp);
233
234 QCOMPARE(notInCach.size(), 2);
235 notInCashSqp = notInCach.first();
236 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(ts.toMSecsSinceEpoch()));
237 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(ts1.toMSecsSinceEpoch()));
238
239 notInCashSqp = notInCach.at(1);
240 QCOMPARE(notInCashSqp.m_TStart, static_cast<double>(te1.toMSecsSinceEpoch()));
241 QCOMPARE(notInCashSqp.m_TEnd, static_cast<double>(te.toMSecsSinceEpoch()));
242 }
243
244 QTEST_MAIN(TestVariableCacheController)
245 #include "TestVariableCacheController.moc"
@@ -5,8 +5,16
5
5
6 struct VariableCacheController::VariableCacheControllerPrivate {
6 struct VariableCacheController::VariableCacheControllerPrivate {
7
7
8 std::unordered_map<std::shared_ptr<Variable>, std::list<SqpDateTime> >
8 std::unordered_map<std::shared_ptr<Variable>, QVector<SqpDateTime> >
9 m_VariableToSqpDateTimeListMap;
9 m_VariableToSqpDateTimeListMap;
10
11 void addInCacheDataByEnd(const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList,
12 QVector<SqpDateTime> &notInCache, int cacheIndex,
13 double currentTStart);
14
15 void addInCacheDataByStart(const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList,
16 QVector<SqpDateTime> &notInCache, int cacheIndex,
17 double currentTStart);
10 };
18 };
11
19
12
20
@@ -23,3 +31,81 void VariableCacheController::addDateTime(std::shared_ptr<Variable> variable,
23 impl->m_VariableToSqpDateTimeListMap[variable].push_back(dateTime);
31 impl->m_VariableToSqpDateTimeListMap[variable].push_back(dateTime);
24 }
32 }
25 }
33 }
34
35 QVector<SqpDateTime>
36 VariableCacheController::provideNotInCacheDateTimeList(std::shared_ptr<Variable> variable,
37 const SqpDateTime &dateTime)
38 {
39 auto notInCache = QVector<SqpDateTime>{};
40
41 // This algorithm is recursif. The idea is to localise the start time then the end time in the
42 // list of date time request associated to the variable
43 // We assume that the list is ordered in a way that l(0) < l(1). We assume also a < b
44 // (with a & b of type SqpDateTime) means ts(b) > te(a)
45
46 impl->addInCacheDataByStart(dateTime, impl->m_VariableToSqpDateTimeListMap.at(variable),
47 notInCache, 0, dateTime.m_TStart);
48
49 return notInCache;
50 }
51
52
53 void VariableCacheController::VariableCacheControllerPrivate::addInCacheDataByEnd(
54 const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList,
55 QVector<SqpDateTime> &notInCache, int cacheIndex, double currentTStart)
56 {
57 const auto dateTimeListSize = dateTimeList.count();
58 if (cacheIndex >= dateTimeListSize) {
59 if (currentTStart < dateTime.m_TEnd) {
60
61 // te localised after all other interval: The last interval is [currentTsart, te]
62 notInCache.push_back(SqpDateTime{currentTStart, dateTime.m_TEnd});
63 }
64 return;
65 }
66
67 auto currentDateTimeJ = dateTimeList[cacheIndex];
68 if (dateTime.m_TEnd <= currentDateTimeJ.m_TStart) {
69 // te localised between to interval: The last interval is [currentTsart, te]
70 notInCache.push_back(SqpDateTime{currentTStart, dateTime.m_TEnd});
71 }
72 else {
73 notInCache.push_back(SqpDateTime{currentTStart, currentDateTimeJ.m_TStart});
74 if (dateTime.m_TEnd > currentDateTimeJ.m_TEnd) {
75 // te not localised before the current interval: we need to look at the next interval
76 addInCacheDataByEnd(dateTime, dateTimeList, notInCache, ++cacheIndex,
77 currentDateTimeJ.m_TEnd);
78 }
79 }
80 }
81
82 void VariableCacheController::VariableCacheControllerPrivate::addInCacheDataByStart(
83 const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList,
84 QVector<SqpDateTime> &notInCache, int cacheIndex, double currentTStart)
85 {
86 const auto dateTimeListSize = dateTimeList.count();
87 if (cacheIndex >= dateTimeListSize) {
88 // ts localised after all other interval: The last interval is [ts, te]
89 notInCache.push_back(SqpDateTime{currentTStart, dateTime.m_TEnd});
90 return;
91 }
92
93 auto currentDateTimeI = dateTimeList[cacheIndex];
94 auto cacheIndexJ = cacheIndex;
95 if (currentTStart < currentDateTimeI.m_TStart) {
96
97 // ts localised between to interval: let's localized te
98 addInCacheDataByEnd(dateTime, dateTimeList, notInCache, cacheIndexJ, currentTStart);
99 }
100 else if (dateTime.m_TStart < currentDateTimeI.m_TEnd) {
101 // ts not localised before the current interval: we need to look at the next interval
102 // We can assume now current tstart is the last interval tend, because data between them are
103 // in the cache
104 addInCacheDataByStart(dateTime, dateTimeList, notInCache, ++cacheIndex,
105 currentDateTimeI.m_TEnd);
106 }
107 else {
108 // ts not localised before the current interval: we need to look at the next interval
109 addInCacheDataByStart(dateTime, dateTimeList, notInCache, ++cacheIndex, currentTStart);
110 }
111 }
General Comments 0
You need to be logged in to leave comments. Login now