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