From 555c373a0c4dfe386dcd2c88ae9548d95e307409 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Thu, 9 Jun 2016 17:27:29 +0200 Subject: Moved query logic to EntityReader to make it reusable in the resource. --- common/entityreader.h | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 common/entityreader.h (limited to 'common/entityreader.h') diff --git a/common/entityreader.h b/common/entityreader.h new file mode 100644 index 0000000..a479679 --- /dev/null +++ b/common/entityreader.h @@ -0,0 +1,110 @@ + +/* + * Copyright (C) 2016 Christian Mollekopf + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) version 3, or any + * later version accepted by the membership of KDE e.V. (or its + * successor approved by the membership of KDE e.V.), which shall + * act as a proxy defined in Section 6 of version 3 of the license. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + */ +#pragma once + +#include "sink_export.h" +#include + +#include "storage.h" +#include "resultprovider.h" +#include "adaptorfactoryregistry.h" + +namespace Sink { + +namespace EntityReaderUtils { + SINK_EXPORT QSharedPointer getLatest(const Sink::Storage::NamedDatabase &db, const QByteArray &uid, DomainTypeAdaptorFactoryInterface &adaptorFactory, qint64 &retrievedRevision); + SINK_EXPORT QSharedPointer get(const Sink::Storage::NamedDatabase &db, const QByteArray &key, DomainTypeAdaptorFactoryInterface &adaptorFactory, qint64 &retrievedRevision); + SINK_EXPORT QSharedPointer getPrevious(const Sink::Storage::NamedDatabase &db, const QByteArray &uid, qint64 revision, DomainTypeAdaptorFactoryInterface &adaptorFactory, qint64 &retrievedRevision); +}; + +/** + * A synchronous interface to read entities from the storage. + * + * All callbacks will be called before the end of the function. + * The caller must ensure passed in references remain valid for the lifetime of the object. + * + * This class is meaent to be instantiated temporarily during reads on the stack. + * + * Note that all objects returned in callbacks are only valid during the execution of the callback and may start pointing into invalid memory if shallow-copied. + */ +template +class SINK_EXPORT EntityReader +{ + typedef std::function ResultCallback; + +public: + EntityReader(const QByteArray &resourceType, const QByteArray &mResourceInstanceIdentifier, Sink::Storage::Transaction &transaction); + EntityReader(DomainTypeAdaptorFactoryInterface &domainTypeAdaptorFactory, const QByteArray &resourceInstanceIdentifier, Sink::Storage::Transaction &transaction); + + /** + * Reads the latest revision of an entity identified by @param uid + */ + DomainType read(const QByteArray &uid) const; + + /** + * Reads the revision of the entity identified by @param key (uid + revision) + */ + DomainType readFromKey(const QByteArray &key) const; + + /** + * Reads the (revision - 1) of an entity identified by @param uid + */ + DomainType readPrevious(const QByteArray &uid, qint64 revision) const; + + /** + * Reads all entities that match @param query. + */ + void query(const Sink::Query &query, const std::function &callback); + + /** + * Returns all entities that match @param query. + * + * @param offset and @param batchsize can be used to return paginated results. + */ + QPair executeInitialQuery(const Sink::Query &query, int offset, int batchsize, const ResultCallback &callback); + + /** + * Returns all changed entities that match @param query starting from @param lastRevision + */ + QPair executeIncrementalQuery(const Sink::Query &query, qint64 lastRevision, const ResultCallback &callback); + +private: + qint64 replaySet(ResultSet &resultSet, int offset, int batchSize, const ResultCallback &callback); + + void readEntity(const Sink::Storage::NamedDatabase &db, const QByteArray &key, + const std::function &resultCallback); + + ResultSet loadInitialResultSet(const Sink::Query &query, QSet &remainingFilters, QByteArray &remainingSorting); + ResultSet loadIncrementalResultSet(qint64 baseRevision, const Sink::Query &query, QSet &remainingFilters); + + ResultSet filterAndSortSet(ResultSet &resultSet, const std::function &filter, + const Sink::Storage::NamedDatabase &db, bool initialQuery, const QByteArray &sortProperty); + std::function getFilter(const QSet remainingFilters, const Sink::Query &query); + QPair load(const Sink::Query &query, const std::function &, QByteArray &)> &baseSetRetriever, bool initialQuery, int offset, int batchSize, const ResultCallback &callback); + +private: + QByteArray mResourceInstanceIdentifier; + Sink::Storage::Transaction &mTransaction; + std::shared_ptr mDomainTypeAdaptorFactoryPtr; + DomainTypeAdaptorFactoryInterface &mDomainTypeAdaptorFactory; +}; + +} -- cgit v1.2.3