##// END OF EJS Templates
Variable deletion (2)...
Alexandre Leroux -
r331:d786589b2e4c
parent child
Show More
@@ -1,40 +1,46
1 1 #ifndef SCIQLOP_VARIABLECACHECONTROLLER_H
2 2 #define SCIQLOP_VARIABLECACHECONTROLLER_H
3 3
4 #include <QLoggingCategory>
4 5 #include <QObject>
5 6
6 7 #include <Data/SqpDateTime.h>
7 8
8 9 #include <QLoggingCategory>
9 10
10 11 #include <Common/spimpl.h>
11 12
13 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableCacheController)
14
12 15 class Variable;
13 16
14 17 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableCacheController)
15 18
16 19
17 20 /// This class aims to store in the cache all of the dateTime already requested to the variable.
18 21 class VariableCacheController : public QObject {
19 22 Q_OBJECT
20 23 public:
21 24 explicit VariableCacheController(QObject *parent = 0);
22 25
23 26
24 27 void addDateTime(std::shared_ptr<Variable> variable, const SqpDateTime &dateTime);
25 28
29 /// Clears cache concerning a variable
30 void clear(std::shared_ptr<Variable> variable) noexcept;
31
26 32 /// Return all of the SqpDataTime part of the dateTime whose are not in the cache
27 33 QVector<SqpDateTime> provideNotInCacheDateTimeList(std::shared_ptr<Variable> variable,
28 34 const SqpDateTime &dateTime);
29 35
30 36
31 37 QVector<SqpDateTime> dateCacheList(std::shared_ptr<Variable> variable) const noexcept;
32 38
33 39 void displayCache(std::shared_ptr<Variable> variable) const;
34 40
35 41 private:
36 42 class VariableCacheControllerPrivate;
37 43 spimpl::unique_impl_ptr<VariableCacheControllerPrivate> impl;
38 44 };
39 45
40 46 #endif // SCIQLOP_VARIABLECACHECONTROLLER_H
@@ -1,77 +1,78
1 1 #ifndef SCIQLOP_VARIABLECONTROLLER_H
2 2 #define SCIQLOP_VARIABLECONTROLLER_H
3 3
4 4 #include <Data/SqpDateTime.h>
5 5
6 6 #include <QLoggingCategory>
7 7 #include <QObject>
8 8
9 9 #include <Common/spimpl.h>
10 10
11 11 class IDataProvider;
12 12 class QItemSelectionModel;
13 13 class TimeController;
14 14 class Variable;
15 15 class VariableModel;
16 16
17 17 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableController)
18 18
19 19 /**
20 20 * @brief The VariableController class aims to handle the variables in SciQlop.
21 21 */
22 22 class VariableController : public QObject {
23 23 Q_OBJECT
24 24 public:
25 25 explicit VariableController(QObject *parent = 0);
26 26 virtual ~VariableController();
27 27
28 28 VariableModel *variableModel() noexcept;
29 29 QItemSelectionModel *variableSelectionModel() noexcept;
30 30
31 31 void setTimeController(TimeController *timeController) noexcept;
32 32
33 33 /**
34 34 * Deletes from the controller the variable passed in parameter.
35 35 *
36 36 * Delete a variable includes:
37 37 * - the deletion of the provider associated with the variable
38 * - removing the cache associated with the variable
38 39 *
39 40 * @param variable the variable to delete from the controller.
40 41 */
41 42 void deleteVariable(std::shared_ptr<Variable> variable) noexcept;
42 43
43 44 /**
44 45 * Deletes from the controller the variables passed in parameter.
45 46 * @param variables the variables to delete from the controller.
46 47 * @sa deleteVariable()
47 48 */
48 49 void deleteVariables(const QVector<std::shared_ptr<Variable> > &variables) noexcept;
49 50
50 51 signals:
51 52 /// Signal emitted when a variable has been created
52 53 void variableCreated(std::shared_ptr<Variable> variable);
53 54
54 55 public slots:
55 56 /// Request the data loading of the variable whithin dateTime
56 57 void onRequestDataLoading(std::shared_ptr<Variable> variable, const SqpDateTime &dateTime);
57 58 /**
58 59 * Creates a new variable and adds it to the model
59 60 * @param name the name of the new variable
60 61 * @param provider the data provider for the new variable
61 62 */
62 63 void createVariable(const QString &name, std::shared_ptr<IDataProvider> provider) noexcept;
63 64
64 65 /// Update the temporal parameters of every selected variable to dateTime
65 66 void onDateTimeOnSelection(const SqpDateTime &dateTime);
66 67
67 68 void initialize();
68 69 void finalize();
69 70
70 71 private:
71 72 void waitForFinish();
72 73
73 74 class VariableControllerPrivate;
74 75 spimpl::unique_impl_ptr<VariableControllerPrivate> impl;
75 76 };
76 77
77 78 #endif // SCIQLOP_VARIABLECONTROLLER_H
@@ -1,204 +1,220
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 53 try {
54 54 impl->addDateTimeRecurse(dateTime,
55 55 impl->m_VariableToSqpDateTimeListMap.at(variable), 0);
56 56 }
57 57 catch (const std::out_of_range &e) {
58 58 qCWarning(LOG_VariableCacheController()) << "addDateTime" << e.what();
59 59 }
60 60 }
61 61 }
62 62 }
63 63
64 void VariableCacheController::clear(std::shared_ptr<Variable> variable) noexcept
65 {
66 if (!variable) {
67 qCCritical(LOG_VariableCacheController()) << "Can't clear variable cache: variable is null";
68 return;
69 }
70
71 auto nbEntries = impl->m_VariableToSqpDateTimeListMap.erase(variable);
72
73 auto clearCacheMessage
74 = (nbEntries != 0)
75 ? tr("Variable cache cleared for variable %1").arg(variable->name())
76 : tr("No deletion of variable cache: no cache was associated with the variable");
77 qCDebug(LOG_VariableCacheController()) << clearCacheMessage;
78 }
79
64 80 QVector<SqpDateTime>
65 81 VariableCacheController::provideNotInCacheDateTimeList(std::shared_ptr<Variable> variable,
66 82 const SqpDateTime &dateTime)
67 83 {
68 84 auto notInCache = QVector<SqpDateTime>{};
69 85
70 86 // This algorithm is recursif. The idea is to localise the start time then the end time in the
71 87 // list of date time request associated to the variable
72 88 // We assume that the list is ordered in a way that l(0) < l(1). We assume also a < b
73 89 // (with a & b of type SqpDateTime) means ts(b) > te(a)
74 90 auto it = impl->m_VariableToSqpDateTimeListMap.find(variable);
75 91 if (it != impl->m_VariableToSqpDateTimeListMap.end()) {
76 92 impl->addInCacheDataByStart(dateTime, it->second, notInCache, 0, dateTime.m_TStart);
77 93 }
78 94 else {
79 95 notInCache << dateTime;
80 96 }
81 97
82 98 return notInCache;
83 99 }
84 100
85 101 QVector<SqpDateTime>
86 102 VariableCacheController::dateCacheList(std::shared_ptr<Variable> variable) const noexcept
87 103 {
88 104 try {
89 105 return impl->m_VariableToSqpDateTimeListMap.at(variable);
90 106 }
91 107 catch (const std::out_of_range &e) {
92 108 qCWarning(LOG_VariableCacheController()) << e.what();
93 109 return QVector<SqpDateTime>{};
94 110 }
95 111 }
96 112
97 113 void VariableCacheController::VariableCacheControllerPrivate::addDateTimeRecurse(
98 114 const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList, int cacheIndex)
99 115 {
100 116 const auto dateTimeListSize = dateTimeList.count();
101 117 if (cacheIndex >= dateTimeListSize) {
102 118 dateTimeList.push_back(dateTime);
103 119 // there is no anymore interval to compore, we can just push_back it
104 120 return;
105 121 }
106 122
107 123 auto currentDateTime = dateTimeList[cacheIndex];
108 124
109 125 if (dateTime.m_TEnd < currentDateTime.m_TStart) {
110 126 // The compared one is < to current one compared, we can insert it
111 127 dateTimeList.insert(cacheIndex, dateTime);
112 128 }
113 129 else if (dateTime.m_TStart > currentDateTime.m_TEnd) {
114 130 // The compared one is > to current one compared we can comparet if to the next one
115 131 addDateTimeRecurse(dateTime, dateTimeList, ++cacheIndex);
116 132 }
117 133 else {
118 134 // Merge cases: we need to merge the two interval, remove the old one from the list then
119 135 // rerun the algo from this index with the merged interval
120 136 auto mTStart = std::min(dateTime.m_TStart, currentDateTime.m_TStart);
121 137 auto mTEnd = std::max(dateTime.m_TEnd, currentDateTime.m_TEnd);
122 138 auto mergeDateTime = SqpDateTime{mTStart, mTEnd};
123 139
124 140 dateTimeList.remove(cacheIndex);
125 141 addDateTimeRecurse(mergeDateTime, dateTimeList, cacheIndex);
126 142 }
127 143 }
128 144
129 145
130 146 void VariableCacheController::VariableCacheControllerPrivate::addInCacheDataByEnd(
131 147 const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList,
132 148 QVector<SqpDateTime> &notInCache, int cacheIndex, double currentTStart)
133 149 {
134 150 const auto dateTimeListSize = dateTimeList.count();
135 151 if (cacheIndex >= dateTimeListSize) {
136 152 if (currentTStart < dateTime.m_TEnd) {
137 153
138 154 // te localised after all other interval: The last interval is [currentTsart, te]
139 155 notInCache.push_back(SqpDateTime{currentTStart, dateTime.m_TEnd});
140 156 }
141 157 return;
142 158 }
143 159
144 160 auto currentDateTimeJ = dateTimeList[cacheIndex];
145 161 if (dateTime.m_TEnd <= currentDateTimeJ.m_TStart) {
146 162 // te localised between to interval: The last interval is [currentTsart, te]
147 163 notInCache.push_back(SqpDateTime{currentTStart, dateTime.m_TEnd});
148 164 }
149 165 else {
150 166 notInCache.push_back(SqpDateTime{currentTStart, currentDateTimeJ.m_TStart});
151 167 if (dateTime.m_TEnd > currentDateTimeJ.m_TEnd) {
152 168 // te not localised before the current interval: we need to look at the next interval
153 169 addInCacheDataByEnd(dateTime, dateTimeList, notInCache, ++cacheIndex,
154 170 currentDateTimeJ.m_TEnd);
155 171 }
156 172 }
157 173 }
158 174
159 175 void VariableCacheController::VariableCacheControllerPrivate::addInCacheDataByStart(
160 176 const SqpDateTime &dateTime, QVector<SqpDateTime> &dateTimeList,
161 177 QVector<SqpDateTime> &notInCache, int cacheIndex, double currentTStart)
162 178 {
163 179 const auto dateTimeListSize = dateTimeList.count();
164 180 if (cacheIndex >= dateTimeListSize) {
165 181 // ts localised after all other interval: The last interval is [ts, te]
166 182 notInCache.push_back(SqpDateTime{currentTStart, dateTime.m_TEnd});
167 183 return;
168 184 }
169 185
170 186 auto currentDateTimeI = dateTimeList[cacheIndex];
171 187 if (currentTStart < currentDateTimeI.m_TStart) {
172 188
173 189 // ts localised between to interval: let's localized te
174 190 addInCacheDataByEnd(dateTime, dateTimeList, notInCache, cacheIndex, currentTStart);
175 191 }
176 192 else if (currentTStart < currentDateTimeI.m_TEnd) {
177 193 if (dateTime.m_TEnd > currentDateTimeI.m_TEnd) {
178 194 // ts not localised before the current interval: we need to look at the next interval
179 195 // We can assume now current tstart is the last interval tend, because data between them
180 196 // are
181 197 // in the cache
182 198 addInCacheDataByStart(dateTime, dateTimeList, notInCache, ++cacheIndex,
183 199 currentDateTimeI.m_TEnd);
184 200 }
185 201 }
186 202 else {
187 203 // ts not localised before the current interval: we need to look at the next interval
188 204 addInCacheDataByStart(dateTime, dateTimeList, notInCache, ++cacheIndex, currentTStart);
189 205 }
190 206 }
191 207
192 208
193 209 void VariableCacheController::displayCache(std::shared_ptr<Variable> variable) const
194 210 {
195 211 auto variableDateTimeList = impl->m_VariableToSqpDateTimeListMap.find(variable);
196 212 if (variableDateTimeList != impl->m_VariableToSqpDateTimeListMap.end()) {
197 213 qCInfo(LOG_VariableCacheController()) << tr("VariableCacheController::displayCache")
198 214 << variableDateTimeList->second;
199 215 }
200 216 else {
201 217 qCWarning(LOG_VariableCacheController())
202 218 << tr("Cannot display a variable that is not in the cache");
203 219 }
204 220 }
@@ -1,200 +1,203
1 1 #include <Variable/Variable.h>
2 2 #include <Variable/VariableCacheController.h>
3 3 #include <Variable/VariableController.h>
4 4 #include <Variable/VariableModel.h>
5 5
6 6 #include <Data/DataProviderParameters.h>
7 7 #include <Data/IDataProvider.h>
8 8 #include <Data/IDataSeries.h>
9 9 #include <Time/TimeController.h>
10 10
11 11 #include <QDateTime>
12 12 #include <QMutex>
13 13 #include <QThread>
14 14 #include <QtCore/QItemSelectionModel>
15 15
16 16 #include <unordered_map>
17 17
18 18 Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController")
19 19
20 20 namespace {
21 21
22 22 /// @todo Generates default dataseries, according to the provider passed in parameter. This method
23 23 /// will be deleted when the timerange is recovered from SciQlop
24 24 std::shared_ptr<IDataSeries> generateDefaultDataSeries(const IDataProvider &provider,
25 25 const SqpDateTime &dateTime) noexcept
26 26 {
27 27 auto parameters = DataProviderParameters{dateTime};
28 28
29 29 return provider.retrieveData(parameters);
30 30 }
31 31
32 32 } // namespace
33 33
34 34 struct VariableController::VariableControllerPrivate {
35 35 explicit VariableControllerPrivate(VariableController *parent)
36 36 : m_WorkingMutex{},
37 37 m_VariableModel{new VariableModel{parent}},
38 38 m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}},
39 39 m_VariableCacheController{std::make_unique<VariableCacheController>()}
40 40 {
41 41 }
42 42
43 43 QMutex m_WorkingMutex;
44 44 /// Variable model. The VariableController has the ownership
45 45 VariableModel *m_VariableModel;
46 46 QItemSelectionModel *m_VariableSelectionModel;
47 47
48 48
49 49 TimeController *m_TimeController{nullptr};
50 50 std::unique_ptr<VariableCacheController> m_VariableCacheController;
51 51
52 52 std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> >
53 53 m_VariableToProviderMap;
54 54 };
55 55
56 56 VariableController::VariableController(QObject *parent)
57 57 : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)}
58 58 {
59 59 qCDebug(LOG_VariableController()) << tr("VariableController construction")
60 60 << QThread::currentThread();
61 61 }
62 62
63 63 VariableController::~VariableController()
64 64 {
65 65 qCDebug(LOG_VariableController()) << tr("VariableController destruction")
66 66 << QThread::currentThread();
67 67 this->waitForFinish();
68 68 }
69 69
70 70 VariableModel *VariableController::variableModel() noexcept
71 71 {
72 72 return impl->m_VariableModel;
73 73 }
74 74
75 75 QItemSelectionModel *VariableController::variableSelectionModel() noexcept
76 76 {
77 77 return impl->m_VariableSelectionModel;
78 78 }
79 79
80 80 void VariableController::setTimeController(TimeController *timeController) noexcept
81 81 {
82 82 impl->m_TimeController = timeController;
83 83 }
84 84
85 85 void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept
86 86 {
87 87 if (!variable) {
88 88 qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null";
89 89 return;
90 90 }
91 91
92 92
93 93 // Deletes provider
94 94 auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable);
95 95 qCDebug(LOG_VariableController())
96 96 << tr("Number of providers deleted for variable %1: %2")
97 97 .arg(variable->name(), QString::number(nbProvidersDeleted));
98 98
99 // Clears cache
100 impl->m_VariableCacheController->clear(variable);
101
99 102
100 103 void VariableController::deleteVariables(
101 104 const QVector<std::shared_ptr<Variable> > &variables) noexcept
102 105 {
103 106 for (auto variable : qAsConst(variables)) {
104 107 deleteVariable(variable);
105 108 }
106 109 }
107 110
108 111 void VariableController::createVariable(const QString &name,
109 112 std::shared_ptr<IDataProvider> provider) noexcept
110 113 {
111 114
112 115 if (!impl->m_TimeController) {
113 116 qCCritical(LOG_VariableController())
114 117 << tr("Impossible to create variable: The time controller is null");
115 118 return;
116 119 }
117 120
118 121
119 122 /// @todo : for the moment :
120 123 /// - the provider is only used to retrieve data from the variable for its initialization, but
121 124 /// it will be retained later
122 125 /// - default data are generated for the variable, without taking into account the timerange set
123 126 /// in sciqlop
124 127 auto dateTime = impl->m_TimeController->dateTime();
125 128 if (auto newVariable = impl->m_VariableModel->createVariable(name, dateTime)) {
126 129
127 130 // store the provider
128 131 impl->m_VariableToProviderMap[newVariable] = provider;
129 132
130 133 auto addDateTimeAcquired
131 134 = [this, newVariable](auto dataSeriesAcquired, auto dateTimeToPutInCache) {
132 135
133 136 impl->m_VariableCacheController->addDateTime(newVariable, dateTimeToPutInCache);
134 137 newVariable->setDataSeries(dataSeriesAcquired);
135 138
136 139 };
137 140
138 141 connect(provider.get(), &IDataProvider::dataProvided, addDateTimeAcquired);
139 142 this->onRequestDataLoading(newVariable, dateTime);
140 143
141 144 // notify the creation
142 145 emit variableCreated(newVariable);
143 146 }
144 147 }
145 148
146 149 void VariableController::onDateTimeOnSelection(const SqpDateTime &dateTime)
147 150 {
148 151 auto selectedRows = impl->m_VariableSelectionModel->selectedRows();
149 152
150 153 for (const auto &selectedRow : qAsConst(selectedRows)) {
151 154 if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) {
152 155 selectedVariable->setDateTime(dateTime);
153 156 this->onRequestDataLoading(selectedVariable, dateTime);
154 157 }
155 158 }
156 159 }
157 160
158 161
159 162 void VariableController::onRequestDataLoading(std::shared_ptr<Variable> variable,
160 163 const SqpDateTime &dateTime)
161 164 {
162 165 // we want to load data of the variable for the dateTime.
163 166 // First we check if the cache contains some of them.
164 167 // For the other, we ask the provider to give them.
165 168 if (variable) {
166 169
167 170 auto dateTimeListNotInCache
168 171 = impl->m_VariableCacheController->provideNotInCacheDateTimeList(variable, dateTime);
169 172
170 173 if (!dateTimeListNotInCache.empty()) {
171 174 // Ask the provider for each data on the dateTimeListNotInCache
172 175 impl->m_VariableToProviderMap.at(variable)->requestDataLoading(
173 176 std::move(dateTimeListNotInCache));
174 177 }
175 178 else {
176 179 emit variable->updated();
177 180 }
178 181 }
179 182 else {
180 183 qCCritical(LOG_VariableController()) << tr("Impossible to load data of a variable null");
181 184 }
182 185 }
183 186
184 187
185 188 void VariableController::initialize()
186 189 {
187 190 qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread();
188 191 impl->m_WorkingMutex.lock();
189 192 qCDebug(LOG_VariableController()) << tr("VariableController init END");
190 193 }
191 194
192 195 void VariableController::finalize()
193 196 {
194 197 impl->m_WorkingMutex.unlock();
195 198 }
196 199
197 200 void VariableController::waitForFinish()
198 201 {
199 202 QMutexLocker locker{&impl->m_WorkingMutex};
200 203 }
General Comments 0
You need to be logged in to leave comments. Login now