diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-07-02 12:08:48 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-07-02 12:08:48 +0200 |
commit | ef8fcadb0c0ad4af055262a4dd9c37d4905cc0fa (patch) | |
tree | 441238d6b9e443212f99af6792cea8daee8ff561 /common/facade.h | |
parent | 4eb94786232aee936cd6371824764705c9359538 (diff) | |
download | sink-ef8fcadb0c0ad4af055262a4dd9c37d4905cc0fa.tar.gz sink-ef8fcadb0c0ad4af055262a4dd9c37d4905cc0fa.zip |
Generalized facade code.
A default implementation is now nothing but an empty shell.
Diffstat (limited to 'common/facade.h')
-rw-r--r-- | common/facade.h | 128 |
1 files changed, 126 insertions, 2 deletions
diff --git a/common/facade.h b/common/facade.h index d9ec0c9..e32ee96 100644 --- a/common/facade.h +++ b/common/facade.h | |||
@@ -31,6 +31,8 @@ | |||
31 | #include "domainadaptor.h" | 31 | #include "domainadaptor.h" |
32 | #include "entitybuffer.h" | 32 | #include "entitybuffer.h" |
33 | #include "log.h" | 33 | #include "log.h" |
34 | #include "storage.h" | ||
35 | #include "resultset.h" | ||
34 | 36 | ||
35 | /** | 37 | /** |
36 | * A QueryRunner runs a query and updates the corresponding result set. | 38 | * A QueryRunner runs a query and updates the corresponding result set. |
@@ -107,7 +109,8 @@ public: | |||
107 | GenericFacade(const QByteArray &resourceIdentifier, const QSharedPointer<DomainTypeAdaptorFactoryInterface<DomainType> > &adaptorFactory = QSharedPointer<DomainTypeAdaptorFactoryInterface<DomainType> >()) | 109 | GenericFacade(const QByteArray &resourceIdentifier, const QSharedPointer<DomainTypeAdaptorFactoryInterface<DomainType> > &adaptorFactory = QSharedPointer<DomainTypeAdaptorFactoryInterface<DomainType> >()) |
108 | : Akonadi2::StoreFacade<DomainType>(), | 110 | : Akonadi2::StoreFacade<DomainType>(), |
109 | mResourceAccess(new ResourceAccess(resourceIdentifier)), | 111 | mResourceAccess(new ResourceAccess(resourceIdentifier)), |
110 | mDomainTypeAdaptorFactory(adaptorFactory) | 112 | mDomainTypeAdaptorFactory(adaptorFactory), |
113 | mResourceInstanceIdentifier(resourceIdentifier) | ||
111 | { | 114 | { |
112 | } | 115 | } |
113 | 116 | ||
@@ -214,12 +217,133 @@ protected: | |||
214 | return KAsync::null<void>(); | 217 | return KAsync::null<void>(); |
215 | } | 218 | } |
216 | 219 | ||
217 | virtual KAsync::Job<qint64> load(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::ResultProvider<Akonadi2::ApplicationDomain::Event::Ptr> > &resultProvider, qint64 oldRevision, qint64 newRevision) { return KAsync::null<qint64>(); }; | 220 | static void scan(const QSharedPointer<Akonadi2::Storage> &storage, const QByteArray &key, std::function<bool(const QByteArray &key, const Akonadi2::Entity &entity)> callback) |
221 | { | ||
222 | storage->scan(key, [=](void *keyValue, int keySize, void *dataValue, int dataSize) -> bool { | ||
223 | //Skip internals | ||
224 | if (Akonadi2::Storage::isInternalKey(keyValue, keySize)) { | ||
225 | return true; | ||
226 | } | ||
227 | |||
228 | //Extract buffers | ||
229 | Akonadi2::EntityBuffer buffer(dataValue, dataSize); | ||
230 | |||
231 | //FIXME implement buffer.isValid() | ||
232 | // const auto resourceBuffer = Akonadi2::EntityBuffer::readBuffer<DummyEvent>(buffer.entity().resource()); | ||
233 | // const auto localBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::ApplicationDomain::Buffer::Event>(buffer.entity().local()); | ||
234 | // const auto metadataBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::Metadata>(buffer.entity().metadata()); | ||
235 | |||
236 | // if ((!resourceBuffer && !localBuffer) || !metadataBuffer) { | ||
237 | // qWarning() << "invalid buffer " << QByteArray::fromRawData(static_cast<char*>(keyValue), keySize); | ||
238 | // return true; | ||
239 | // } | ||
240 | return callback(QByteArray::fromRawData(static_cast<char*>(keyValue), keySize), buffer.entity()); | ||
241 | }, | ||
242 | [](const Akonadi2::Storage::Error &error) { | ||
243 | qWarning() << "Error during query: " << error.message; | ||
244 | }); | ||
245 | } | ||
246 | |||
247 | static void readValue(const QSharedPointer<Akonadi2::Storage> &storage, const QByteArray &key, const std::function<void(const typename DomainType::Ptr &)> &resultCallback, const QSharedPointer<DomainTypeAdaptorFactoryInterface<DomainType> > &adaptorFactory) | ||
248 | { | ||
249 | scan(storage, key, [=](const QByteArray &key, const Akonadi2::Entity &entity) { | ||
250 | const auto metadataBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::Metadata>(entity.metadata()); | ||
251 | qint64 revision = metadataBuffer ? metadataBuffer->revision() : -1; | ||
252 | //This only works for a 1:1 mapping of resource to domain types. | ||
253 | //Not i.e. for tags that are stored as flags in each entity of an imap store. | ||
254 | //additional properties that don't have a 1:1 mapping (such as separately stored tags), | ||
255 | //could be added to the adaptor | ||
256 | auto domainObject = QSharedPointer<DomainType>::create("org.kde.dummy.instance1", key, revision, adaptorFactory->createAdaptor(entity)); | ||
257 | resultCallback(domainObject); | ||
258 | return true; | ||
259 | }); | ||
260 | } | ||
261 | |||
262 | static ResultSet fullScan(const QSharedPointer<Akonadi2::Storage> &storage) | ||
263 | { | ||
264 | //TODO use a result set with an iterator, to read values on demand | ||
265 | QVector<QByteArray> keys; | ||
266 | scan(storage, QByteArray(), [=, &keys](const QByteArray &key, const Akonadi2::Entity &) { | ||
267 | keys << key; | ||
268 | return true; | ||
269 | }); | ||
270 | return ResultSet(keys); | ||
271 | } | ||
272 | |||
273 | static ResultSet filteredSet(const ResultSet &resultSet, const std::function<bool(const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &domainObject)> &filter, const QSharedPointer<Akonadi2::Storage> &storage, const QSharedPointer<DomainTypeAdaptorFactoryInterface<DomainType> > &adaptorFactory) | ||
274 | { | ||
275 | auto resultSetPtr = QSharedPointer<ResultSet>::create(resultSet); | ||
276 | |||
277 | //Read through the source values and return whatever matches the filter | ||
278 | std::function<bool(std::function<void(const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &)>)> generator = [resultSetPtr, storage, adaptorFactory, filter](std::function<void(const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &)> callback) -> bool { | ||
279 | while (resultSetPtr->next()) { | ||
280 | readValue(storage, resultSetPtr->id(), [filter, callback](const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &domainObject) { | ||
281 | if (filter(domainObject)) { | ||
282 | callback(domainObject); | ||
283 | } | ||
284 | }, adaptorFactory); | ||
285 | } | ||
286 | return false; | ||
287 | }; | ||
288 | return ResultSet(generator); | ||
289 | } | ||
218 | 290 | ||
291 | static ResultSet getResultSet(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::Storage> &storage, const QSharedPointer<DomainTypeAdaptorFactoryInterface<DomainType> > &adaptorFactory, const QByteArray &resourceInstanceIdentifier) | ||
292 | { | ||
293 | QSet<QByteArray> appliedFilters; | ||
294 | ResultSet resultSet = Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::queryIndexes(query, resourceInstanceIdentifier, appliedFilters); | ||
295 | const auto remainingFilters = query.propertyFilter.keys().toSet() - appliedFilters; | ||
296 | |||
297 | //We do a full scan if there were no indexes available to create the initial set. | ||
298 | if (appliedFilters.isEmpty()) { | ||
299 | resultSet = fullScan(storage); | ||
300 | } | ||
301 | |||
302 | auto filter = [remainingFilters, query](const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &domainObject) -> bool { | ||
303 | for (const auto &filterProperty : remainingFilters) { | ||
304 | //TODO implement other comparison operators than equality | ||
305 | if (domainObject->getProperty(filterProperty) != query.propertyFilter.value(filterProperty)) { | ||
306 | return false; | ||
307 | } | ||
308 | } | ||
309 | return true; | ||
310 | }; | ||
311 | |||
312 | return filteredSet(resultSet, filter, storage, adaptorFactory); | ||
313 | } | ||
314 | |||
315 | virtual KAsync::Job<qint64> load(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::ResultProvider<typename DomainType::Ptr> > &resultProvider, qint64 oldRevision, qint64 newRevision) | ||
316 | { | ||
317 | return KAsync::start<qint64>([=]() -> qint64 { | ||
318 | auto storage = QSharedPointer<Akonadi2::Storage>::create(Akonadi2::Store::storageLocation(), mResourceInstanceIdentifier); | ||
319 | storage->setDefaultErrorHandler([](const Akonadi2::Storage::Error &error) { | ||
320 | Warning() << "Error during query: " << error.store << error.message; | ||
321 | }); | ||
322 | |||
323 | storage->startTransaction(Akonadi2::Storage::ReadOnly); | ||
324 | //TODO start transaction on indexes as well | ||
325 | const qint64 revision = storage->maxRevision(); | ||
326 | |||
327 | auto resultSet = getResultSet(query, storage, mDomainTypeAdaptorFactory, mResourceInstanceIdentifier); | ||
328 | |||
329 | // TODO only emit changes and don't replace everything | ||
330 | resultProvider->clear(); | ||
331 | auto resultCallback = std::bind(&Akonadi2::ResultProvider<typename DomainType::Ptr>::add, resultProvider, std::placeholders::_1); | ||
332 | while(resultSet.next([resultCallback](const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &value) -> bool { | ||
333 | resultCallback(Akonadi2::ApplicationDomain::ApplicationDomainType::getInMemoryRepresentation<DomainType>(value)); | ||
334 | return true; | ||
335 | })){}; | ||
336 | storage->abortTransaction(); | ||
337 | return revision; | ||
338 | }); | ||
339 | } | ||
340 | |||
341 | private: | ||
219 | protected: | 342 | protected: |
220 | //TODO use one resource access instance per application => make static | 343 | //TODO use one resource access instance per application => make static |
221 | QSharedPointer<Akonadi2::ResourceAccess> mResourceAccess; | 344 | QSharedPointer<Akonadi2::ResourceAccess> mResourceAccess; |
222 | QSharedPointer<DomainTypeAdaptorFactoryInterface<DomainType> > mDomainTypeAdaptorFactory; | 345 | QSharedPointer<DomainTypeAdaptorFactoryInterface<DomainType> > mDomainTypeAdaptorFactory; |
346 | QByteArray mResourceInstanceIdentifier; | ||
223 | }; | 347 | }; |
224 | 348 | ||
225 | } | 349 | } |