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