diff options
Diffstat (limited to 'common/store.cpp')
-rw-r--r-- | common/store.cpp | 55 |
1 files changed, 27 insertions, 28 deletions
diff --git a/common/store.cpp b/common/store.cpp index 1162a18..a58287b 100644 --- a/common/store.cpp +++ b/common/store.cpp | |||
@@ -36,8 +36,7 @@ | |||
36 | #include "storage.h" | 36 | #include "storage.h" |
37 | #include "log.h" | 37 | #include "log.h" |
38 | 38 | ||
39 | #undef DEBUG_AREA | 39 | SINK_DEBUG_AREA("store") |
40 | #define DEBUG_AREA "client.store" | ||
41 | 40 | ||
42 | namespace Sink { | 41 | namespace Sink { |
43 | 42 | ||
@@ -88,24 +87,24 @@ static QMap<QByteArray, QByteArray> getResources(const QList<QByteArray> &resour | |||
88 | } | 87 | } |
89 | resources.insert(res, configuredResources.value(res)); | 88 | resources.insert(res, configuredResources.value(res)); |
90 | } else { | 89 | } else { |
91 | Warning() << "Resource is not existing: " << res; | 90 | SinkWarning() << "Resource is not existing: " << res; |
92 | } | 91 | } |
93 | } | 92 | } |
94 | } | 93 | } |
95 | Trace() << "Found resources: " << resources; | 94 | SinkTrace() << "Found resources: " << resources; |
96 | return resources; | 95 | return resources; |
97 | } | 96 | } |
98 | 97 | ||
99 | template <class DomainType> | 98 | template <class DomainType> |
100 | QSharedPointer<QAbstractItemModel> Store::loadModel(Query query) | 99 | QSharedPointer<QAbstractItemModel> Store::loadModel(Query query) |
101 | { | 100 | { |
102 | Trace() << "Query: " << ApplicationDomain::getTypeName<DomainType>(); | 101 | SinkTrace() << "Query: " << ApplicationDomain::getTypeName<DomainType>(); |
103 | Trace() << " Requested: " << query.requestedProperties; | 102 | SinkTrace() << " Requested: " << query.requestedProperties; |
104 | Trace() << " Filter: " << query.propertyFilter; | 103 | SinkTrace() << " Filter: " << query.propertyFilter; |
105 | Trace() << " Parent: " << query.parentProperty; | 104 | SinkTrace() << " Parent: " << query.parentProperty; |
106 | Trace() << " Ids: " << query.ids; | 105 | SinkTrace() << " Ids: " << query.ids; |
107 | Trace() << " IsLive: " << query.liveQuery; | 106 | SinkTrace() << " IsLive: " << query.liveQuery; |
108 | Trace() << " Sorting: " << query.sortProperty; | 107 | SinkTrace() << " Sorting: " << query.sortProperty; |
109 | auto model = QSharedPointer<ModelResult<DomainType, typename DomainType::Ptr>>::create(query, query.requestedProperties); | 108 | auto model = QSharedPointer<ModelResult<DomainType, typename DomainType::Ptr>>::create(query, query.requestedProperties); |
110 | 109 | ||
111 | //* Client defines lifetime of model | 110 | //* Client defines lifetime of model |
@@ -123,16 +122,16 @@ QSharedPointer<QAbstractItemModel> Store::loadModel(Query query) | |||
123 | const auto resourceType = resources.value(resourceInstanceIdentifier); | 122 | const auto resourceType = resources.value(resourceInstanceIdentifier); |
124 | auto facade = FacadeFactory::instance().getFacade<DomainType>(resourceType, resourceInstanceIdentifier); | 123 | auto facade = FacadeFactory::instance().getFacade<DomainType>(resourceType, resourceInstanceIdentifier); |
125 | if (facade) { | 124 | if (facade) { |
126 | Trace() << "Trying to fetch from resource " << resourceInstanceIdentifier; | 125 | SinkTrace() << "Trying to fetch from resource " << resourceInstanceIdentifier; |
127 | auto result = facade->load(query); | 126 | auto result = facade->load(query); |
128 | if (result.second) { | 127 | if (result.second) { |
129 | aggregatingEmitter->addEmitter(result.second); | 128 | aggregatingEmitter->addEmitter(result.second); |
130 | } else { | 129 | } else { |
131 | Warning() << "Null emitter for resource " << resourceInstanceIdentifier; | 130 | SinkWarning() << "Null emitter for resource " << resourceInstanceIdentifier; |
132 | } | 131 | } |
133 | result.first.template then<void>([&future]() { future.setFinished(); }).exec(); | 132 | result.first.template then<void>([&future]() { future.setFinished(); }).exec(); |
134 | } else { | 133 | } else { |
135 | Trace() << "Couldn' find a facade for " << resourceInstanceIdentifier; | 134 | SinkTrace() << "Couldn' find a facade for " << resourceInstanceIdentifier; |
136 | // Ignore the error and carry on | 135 | // Ignore the error and carry on |
137 | future.setFinished(); | 136 | future.setFinished(); |
138 | } | 137 | } |
@@ -164,7 +163,7 @@ KAsync::Job<void> Store::create(const DomainType &domainObject) | |||
164 | { | 163 | { |
165 | // Potentially move to separate thread as well | 164 | // Potentially move to separate thread as well |
166 | auto facade = getFacade<DomainType>(domainObject.resourceInstanceIdentifier()); | 165 | auto facade = getFacade<DomainType>(domainObject.resourceInstanceIdentifier()); |
167 | return facade->create(domainObject).template then<void>([facade]() {}, [](int errorCode, const QString &error) { Warning() << "Failed to create"; }); | 166 | return facade->create(domainObject).template then<void>([facade]() {}, [](int errorCode, const QString &error) { SinkWarning() << "Failed to create"; }); |
168 | } | 167 | } |
169 | 168 | ||
170 | template <class DomainType> | 169 | template <class DomainType> |
@@ -172,7 +171,7 @@ KAsync::Job<void> Store::modify(const DomainType &domainObject) | |||
172 | { | 171 | { |
173 | // Potentially move to separate thread as well | 172 | // Potentially move to separate thread as well |
174 | auto facade = getFacade<DomainType>(domainObject.resourceInstanceIdentifier()); | 173 | auto facade = getFacade<DomainType>(domainObject.resourceInstanceIdentifier()); |
175 | return facade->modify(domainObject).template then<void>([facade]() {}, [](int errorCode, const QString &error) { Warning() << "Failed to modify"; }); | 174 | return facade->modify(domainObject).template then<void>([facade]() {}, [](int errorCode, const QString &error) { SinkWarning() << "Failed to modify"; }); |
176 | } | 175 | } |
177 | 176 | ||
178 | template <class DomainType> | 177 | template <class DomainType> |
@@ -180,7 +179,7 @@ KAsync::Job<void> Store::remove(const DomainType &domainObject) | |||
180 | { | 179 | { |
181 | // Potentially move to separate thread as well | 180 | // Potentially move to separate thread as well |
182 | auto facade = getFacade<DomainType>(domainObject.resourceInstanceIdentifier()); | 181 | auto facade = getFacade<DomainType>(domainObject.resourceInstanceIdentifier()); |
183 | return facade->remove(domainObject).template then<void>([facade]() {}, [](int errorCode, const QString &error) { Warning() << "Failed to remove"; }); | 182 | return facade->remove(domainObject).template then<void>([facade]() {}, [](int errorCode, const QString &error) { SinkWarning() << "Failed to remove"; }); |
184 | } | 183 | } |
185 | 184 | ||
186 | KAsync::Job<void> Store::removeDataFromDisk(const QByteArray &identifier) | 185 | KAsync::Job<void> Store::removeDataFromDisk(const QByteArray &identifier) |
@@ -188,28 +187,28 @@ KAsync::Job<void> Store::removeDataFromDisk(const QByteArray &identifier) | |||
188 | // All databases are going to become invalid, nuke the environments | 187 | // All databases are going to become invalid, nuke the environments |
189 | // TODO: all clients should react to a notification the resource | 188 | // TODO: all clients should react to a notification the resource |
190 | Sink::Storage::clearEnv(); | 189 | Sink::Storage::clearEnv(); |
191 | Trace() << "Remove data from disk " << identifier; | 190 | SinkTrace() << "Remove data from disk " << identifier; |
192 | auto time = QSharedPointer<QTime>::create(); | 191 | auto time = QSharedPointer<QTime>::create(); |
193 | time->start(); | 192 | time->start(); |
194 | auto resourceAccess = ResourceAccessFactory::instance().getAccess(identifier, ResourceConfig::getResourceType(identifier)); | 193 | auto resourceAccess = ResourceAccessFactory::instance().getAccess(identifier, ResourceConfig::getResourceType(identifier)); |
195 | resourceAccess->open(); | 194 | resourceAccess->open(); |
196 | return resourceAccess->sendCommand(Sink::Commands::RemoveFromDiskCommand) | 195 | return resourceAccess->sendCommand(Sink::Commands::RemoveFromDiskCommand) |
197 | .then<void>([resourceAccess, time]() { Trace() << "Remove from disk complete." << Log::TraceTime(time->elapsed()); }); | 196 | .then<void>([resourceAccess, time]() { SinkTrace() << "Remove from disk complete." << Log::TraceTime(time->elapsed()); }); |
198 | } | 197 | } |
199 | 198 | ||
200 | KAsync::Job<void> Store::synchronize(const Sink::Query &query) | 199 | KAsync::Job<void> Store::synchronize(const Sink::Query &query) |
201 | { | 200 | { |
202 | Trace() << "synchronize" << query.resources; | 201 | SinkTrace() << "synchronize" << query.resources; |
203 | auto resources = getResources(query.resources, query.accounts).keys(); | 202 | auto resources = getResources(query.resources, query.accounts).keys(); |
204 | //FIXME only necessary because each doesn't propagate errors | 203 | //FIXME only necessary because each doesn't propagate errors |
205 | auto error = new bool; | 204 | auto error = new bool; |
206 | return KAsync::iterate(resources) | 205 | return KAsync::iterate(resources) |
207 | .template each<void, QByteArray>([query, error](const QByteArray &resource, KAsync::Future<void> &future) { | 206 | .template each<void, QByteArray>([query, error](const QByteArray &resource, KAsync::Future<void> &future) { |
208 | Trace() << "Synchronizing " << resource; | 207 | SinkTrace() << "Synchronizing " << resource; |
209 | auto resourceAccess = ResourceAccessFactory::instance().getAccess(resource, ResourceConfig::getResourceType(resource)); | 208 | auto resourceAccess = ResourceAccessFactory::instance().getAccess(resource, ResourceConfig::getResourceType(resource)); |
210 | resourceAccess->open(); | 209 | resourceAccess->open(); |
211 | resourceAccess->synchronizeResource(true, false).then<void>([resourceAccess, &future]() {Trace() << "synced."; future.setFinished(); }, | 210 | resourceAccess->synchronizeResource(true, false).then<void>([resourceAccess, &future]() {SinkTrace() << "synced."; future.setFinished(); }, |
212 | [&future, error](int errorCode, QString msg) { *error = true; Warning() << "Error during sync."; future.setError(errorCode, msg); }).exec(); | 211 | [&future, error](int errorCode, QString msg) { *error = true; SinkWarning() << "Error during sync."; future.setError(errorCode, msg); }).exec(); |
213 | }).then<void>([error](KAsync::Future<void> &future) { | 212 | }).then<void>([error](KAsync::Future<void> &future) { |
214 | if (*error) { | 213 | if (*error) { |
215 | future.setError(1, "Error during sync."); | 214 | future.setError(1, "Error during sync."); |
@@ -306,25 +305,25 @@ QList<DomainType> Store::read(const Sink::Query &q) | |||
306 | auto resources = getResources(query.resources, query.accounts, ApplicationDomain::getTypeName<DomainType>()); | 305 | auto resources = getResources(query.resources, query.accounts, ApplicationDomain::getTypeName<DomainType>()); |
307 | auto aggregatingEmitter = AggregatingResultEmitter<typename DomainType::Ptr>::Ptr::create(); | 306 | auto aggregatingEmitter = AggregatingResultEmitter<typename DomainType::Ptr>::Ptr::create(); |
308 | aggregatingEmitter->onAdded([&list](const typename DomainType::Ptr &value){ | 307 | aggregatingEmitter->onAdded([&list](const typename DomainType::Ptr &value){ |
309 | Trace() << "Found value: " << value->identifier(); | 308 | SinkTrace() << "Found value: " << value->identifier(); |
310 | list << *value; | 309 | list << *value; |
311 | }); | 310 | }); |
312 | for (const auto resourceInstanceIdentifier : resources.keys()) { | 311 | for (const auto resourceInstanceIdentifier : resources.keys()) { |
313 | const auto resourceType = resources.value(resourceInstanceIdentifier); | 312 | const auto resourceType = resources.value(resourceInstanceIdentifier); |
314 | Trace() << "Looking for " << resourceType << resourceInstanceIdentifier; | 313 | SinkTrace() << "Looking for " << resourceType << resourceInstanceIdentifier; |
315 | auto facade = FacadeFactory::instance().getFacade<DomainType>(resourceType, resourceInstanceIdentifier); | 314 | auto facade = FacadeFactory::instance().getFacade<DomainType>(resourceType, resourceInstanceIdentifier); |
316 | if (facade) { | 315 | if (facade) { |
317 | Trace() << "Trying to fetch from resource " << resourceInstanceIdentifier; | 316 | SinkTrace() << "Trying to fetch from resource " << resourceInstanceIdentifier; |
318 | auto result = facade->load(query); | 317 | auto result = facade->load(query); |
319 | if (result.second) { | 318 | if (result.second) { |
320 | aggregatingEmitter->addEmitter(result.second); | 319 | aggregatingEmitter->addEmitter(result.second); |
321 | } else { | 320 | } else { |
322 | Warning() << "Null emitter for resource " << resourceInstanceIdentifier; | 321 | SinkWarning() << "Null emitter for resource " << resourceInstanceIdentifier; |
323 | } | 322 | } |
324 | result.first.exec(); | 323 | result.first.exec(); |
325 | aggregatingEmitter->fetch(typename DomainType::Ptr()); | 324 | aggregatingEmitter->fetch(typename DomainType::Ptr()); |
326 | } else { | 325 | } else { |
327 | Trace() << "Couldn't find a facade for " << resourceInstanceIdentifier; | 326 | SinkTrace() << "Couldn't find a facade for " << resourceInstanceIdentifier; |
328 | // Ignore the error and carry on | 327 | // Ignore the error and carry on |
329 | } | 328 | } |
330 | } | 329 | } |