diff options
Diffstat (limited to 'common/entityreader.h')
-rw-r--r-- | common/entityreader.h | 110 |
1 files changed, 110 insertions, 0 deletions
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 @@ | |||
1 | |||
2 | /* | ||
3 | * Copyright (C) 2016 Christian Mollekopf <mollekopf@kolabsys.com> | ||
4 | * | ||
5 | * This library is free software; you can redistribute it and/or | ||
6 | * modify it under the terms of the GNU Lesser General Public | ||
7 | * License as published by the Free Software Foundation; either | ||
8 | * version 2.1 of the License, or (at your option) version 3, or any | ||
9 | * later version accepted by the membership of KDE e.V. (or its | ||
10 | * successor approved by the membership of KDE e.V.), which shall | ||
11 | * act as a proxy defined in Section 6 of version 3 of the license. | ||
12 | * | ||
13 | * This library is distributed in the hope that it will be useful, | ||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
16 | * Lesser General Public License for more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU Lesser General Public | ||
19 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
20 | */ | ||
21 | #pragma once | ||
22 | |||
23 | #include "sink_export.h" | ||
24 | #include <domainadaptor.h> | ||
25 | |||
26 | #include "storage.h" | ||
27 | #include "resultprovider.h" | ||
28 | #include "adaptorfactoryregistry.h" | ||
29 | |||
30 | namespace Sink { | ||
31 | |||
32 | namespace EntityReaderUtils { | ||
33 | SINK_EXPORT QSharedPointer<Sink::ApplicationDomain::BufferAdaptor> getLatest(const Sink::Storage::NamedDatabase &db, const QByteArray &uid, DomainTypeAdaptorFactoryInterface &adaptorFactory, qint64 &retrievedRevision); | ||
34 | SINK_EXPORT QSharedPointer<Sink::ApplicationDomain::BufferAdaptor> get(const Sink::Storage::NamedDatabase &db, const QByteArray &key, DomainTypeAdaptorFactoryInterface &adaptorFactory, qint64 &retrievedRevision); | ||
35 | SINK_EXPORT QSharedPointer<Sink::ApplicationDomain::BufferAdaptor> getPrevious(const Sink::Storage::NamedDatabase &db, const QByteArray &uid, qint64 revision, DomainTypeAdaptorFactoryInterface &adaptorFactory, qint64 &retrievedRevision); | ||
36 | }; | ||
37 | |||
38 | /** | ||
39 | * A synchronous interface to read entities from the storage. | ||
40 | * | ||
41 | * All callbacks will be called before the end of the function. | ||
42 | * The caller must ensure passed in references remain valid for the lifetime of the object. | ||
43 | * | ||
44 | * This class is meaent to be instantiated temporarily during reads on the stack. | ||
45 | * | ||
46 | * 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. | ||
47 | */ | ||
48 | template<typename DomainType> | ||
49 | class SINK_EXPORT EntityReader | ||
50 | { | ||
51 | typedef std::function<bool(const typename DomainType::Ptr &domainObject, Sink::Operation operation)> ResultCallback; | ||
52 | |||
53 | public: | ||
54 | EntityReader(const QByteArray &resourceType, const QByteArray &mResourceInstanceIdentifier, Sink::Storage::Transaction &transaction); | ||
55 | EntityReader(DomainTypeAdaptorFactoryInterface &domainTypeAdaptorFactory, const QByteArray &resourceInstanceIdentifier, Sink::Storage::Transaction &transaction); | ||
56 | |||
57 | /** | ||
58 | * Reads the latest revision of an entity identified by @param uid | ||
59 | */ | ||
60 | DomainType read(const QByteArray &uid) const; | ||
61 | |||
62 | /** | ||
63 | * Reads the revision of the entity identified by @param key (uid + revision) | ||
64 | */ | ||
65 | DomainType readFromKey(const QByteArray &key) const; | ||
66 | |||
67 | /** | ||
68 | * Reads the (revision - 1) of an entity identified by @param uid | ||
69 | */ | ||
70 | DomainType readPrevious(const QByteArray &uid, qint64 revision) const; | ||
71 | |||
72 | /** | ||
73 | * Reads all entities that match @param query. | ||
74 | */ | ||
75 | void query(const Sink::Query &query, const std::function<bool(const DomainType &)> &callback); | ||
76 | |||
77 | /** | ||
78 | * Returns all entities that match @param query. | ||
79 | * | ||
80 | * @param offset and @param batchsize can be used to return paginated results. | ||
81 | */ | ||
82 | QPair<qint64, qint64> executeInitialQuery(const Sink::Query &query, int offset, int batchsize, const ResultCallback &callback); | ||
83 | |||
84 | /** | ||
85 | * Returns all changed entities that match @param query starting from @param lastRevision | ||
86 | */ | ||
87 | QPair<qint64, qint64> executeIncrementalQuery(const Sink::Query &query, qint64 lastRevision, const ResultCallback &callback); | ||
88 | |||
89 | private: | ||
90 | qint64 replaySet(ResultSet &resultSet, int offset, int batchSize, const ResultCallback &callback); | ||
91 | |||
92 | void readEntity(const Sink::Storage::NamedDatabase &db, const QByteArray &key, | ||
93 | const std::function<void(const Sink::ApplicationDomain::ApplicationDomainType::Ptr &, Sink::Operation)> &resultCallback); | ||
94 | |||
95 | ResultSet loadInitialResultSet(const Sink::Query &query, QSet<QByteArray> &remainingFilters, QByteArray &remainingSorting); | ||
96 | ResultSet loadIncrementalResultSet(qint64 baseRevision, const Sink::Query &query, QSet<QByteArray> &remainingFilters); | ||
97 | |||
98 | ResultSet filterAndSortSet(ResultSet &resultSet, const std::function<bool(const Sink::ApplicationDomain::ApplicationDomainType::Ptr &domainObject)> &filter, | ||
99 | const Sink::Storage::NamedDatabase &db, bool initialQuery, const QByteArray &sortProperty); | ||
100 | std::function<bool(const Sink::ApplicationDomain::ApplicationDomainType::Ptr &domainObject)> getFilter(const QSet<QByteArray> remainingFilters, const Sink::Query &query); | ||
101 | QPair<qint64, qint64> load(const Sink::Query &query, const std::function<ResultSet(QSet<QByteArray> &, QByteArray &)> &baseSetRetriever, bool initialQuery, int offset, int batchSize, const ResultCallback &callback); | ||
102 | |||
103 | private: | ||
104 | QByteArray mResourceInstanceIdentifier; | ||
105 | Sink::Storage::Transaction &mTransaction; | ||
106 | std::shared_ptr<DomainTypeAdaptorFactoryInterface> mDomainTypeAdaptorFactoryPtr; | ||
107 | DomainTypeAdaptorFactoryInterface &mDomainTypeAdaptorFactory; | ||
108 | }; | ||
109 | |||
110 | } | ||