diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-11-27 17:30:04 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-11-27 17:30:04 +0100 |
commit | 5b41b26a349967acf2197f9f9228526193fd826e (patch) | |
tree | 166452bcc0757564deefe233bf031d2ccb0564d2 /common/queryrunner.h | |
parent | 13af56e436f49df32d3b2f6f223cf1dec2eabaac (diff) | |
download | sink-5b41b26a349967acf2197f9f9228526193fd826e.tar.gz sink-5b41b26a349967acf2197f9f9228526193fd826e.zip |
Introduced a QueryRunner object
The QueryRunner object lives for the duration of the query (so just
for the initial query for non-live queries, and for the lifetime of the
result model for live queries).
It's supposed to handle all the threading internally and decouple the
lifetime of the facade.
Diffstat (limited to 'common/queryrunner.h')
-rw-r--r-- | common/queryrunner.h | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/common/queryrunner.h b/common/queryrunner.h new file mode 100644 index 0000000..e2af9de --- /dev/null +++ b/common/queryrunner.h | |||
@@ -0,0 +1,107 @@ | |||
1 | /* | ||
2 | Copyright (c) 2015 Christian Mollekopf <mollekopf@kolabsys.com> | ||
3 | |||
4 | This library is free software; you can redistribute it and/or modify it | ||
5 | under the terms of the GNU Library General Public License as published by | ||
6 | the Free Software Foundation; either version 2 of the License, or (at your | ||
7 | option) any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, but WITHOUT | ||
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | ||
12 | License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Library General Public License | ||
15 | along with this library; see the file COPYING.LIB. If not, write to the | ||
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
17 | 02110-1301, USA. | ||
18 | */ | ||
19 | |||
20 | #pragma once | ||
21 | |||
22 | #include <QObject> | ||
23 | #include "facadeinterface.h" | ||
24 | #include "resourceaccess.h" | ||
25 | #include "resultprovider.h" | ||
26 | #include "domaintypeadaptorfactoryinterface.h" | ||
27 | #include "storage.h" | ||
28 | #include "query.h" | ||
29 | |||
30 | /** | ||
31 | * A QueryRunner runs a query and updates the corresponding result set. | ||
32 | * | ||
33 | * The lifetime of the QueryRunner is defined by the resut set (otherwise it's doing useless work), | ||
34 | * and by how long a result set must be updated. If the query is one off the runner dies after the execution, | ||
35 | * otherwise it lives on the react to changes and updates the corresponding result set. | ||
36 | * | ||
37 | * QueryRunner has to keep ResourceAccess alive in order to keep getting updates. | ||
38 | */ | ||
39 | |||
40 | class QueryRunnerBase : public QObject | ||
41 | { | ||
42 | Q_OBJECT | ||
43 | protected: | ||
44 | typedef std::function<KAsync::Job<void>()> QueryFunction; | ||
45 | |||
46 | /** | ||
47 | * Set the query to run | ||
48 | */ | ||
49 | void setQuery(const QueryFunction &query) | ||
50 | { | ||
51 | queryFunction = query; | ||
52 | } | ||
53 | |||
54 | |||
55 | protected slots: | ||
56 | /** | ||
57 | * Rerun query with new revision | ||
58 | */ | ||
59 | void revisionChanged(qint64 newRevision) | ||
60 | { | ||
61 | Trace() << "New revision: " << newRevision; | ||
62 | run().exec(); | ||
63 | } | ||
64 | |||
65 | private: | ||
66 | /** | ||
67 | * Starts query | ||
68 | */ | ||
69 | KAsync::Job<void> run(qint64 newRevision = 0) | ||
70 | { | ||
71 | return queryFunction(); | ||
72 | } | ||
73 | |||
74 | QueryFunction queryFunction; | ||
75 | }; | ||
76 | |||
77 | template<typename DomainType> | ||
78 | class QueryRunner : public QueryRunnerBase | ||
79 | { | ||
80 | public: | ||
81 | QueryRunner(const Akonadi2::Query &query, const Akonadi2::ResourceAccessInterface::Ptr &, const QByteArray &instanceIdentifier, const DomainTypeAdaptorFactoryInterface::Ptr &, const QByteArray &bufferType); | ||
82 | |||
83 | typename Akonadi2::ResultEmitter<typename DomainType::Ptr>::Ptr emitter(); | ||
84 | |||
85 | private: | ||
86 | static void replaySet(ResultSet &resultSet, Akonadi2::ResultProviderInterface<typename DomainType::Ptr> &resultProvider); | ||
87 | |||
88 | void readEntity(const Akonadi2::Storage::NamedDatabase &db, const QByteArray &key, const std::function<void(const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &, Akonadi2::Operation)> &resultCallback); | ||
89 | |||
90 | ResultSet loadInitialResultSet(const Akonadi2::Query &query, Akonadi2::Storage::Transaction &transaction, QSet<QByteArray> &remainingFilters); | ||
91 | ResultSet loadIncrementalResultSet(qint64 baseRevision, const Akonadi2::Query &query, Akonadi2::Storage::Transaction &transaction, QSet<QByteArray> &remainingFilters); | ||
92 | |||
93 | ResultSet filterSet(const ResultSet &resultSet, const std::function<bool(const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &domainObject)> &filter, const Akonadi2::Storage::NamedDatabase &db, bool initialQuery); | ||
94 | std::function<bool(const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &domainObject)> getFilter(const QSet<QByteArray> remainingFilters, const Akonadi2::Query &query); | ||
95 | qint64 load(const Akonadi2::Query &query, const std::function<ResultSet(Akonadi2::Storage::Transaction &, QSet<QByteArray> &)> &baseSetRetriever, Akonadi2::ResultProviderInterface<typename DomainType::Ptr> &resultProvider, bool initialQuery); | ||
96 | qint64 executeIncrementalQuery(const Akonadi2::Query &query, Akonadi2::ResultProviderInterface<typename DomainType::Ptr> &resultProvider); | ||
97 | qint64 executeInitialQuery(const Akonadi2::Query &query, const typename DomainType::Ptr &parent, Akonadi2::ResultProviderInterface<typename DomainType::Ptr> &resultProvider); | ||
98 | |||
99 | private: | ||
100 | QSharedPointer<Akonadi2::ResultProvider<typename DomainType::Ptr> > mResultProvider; | ||
101 | QSharedPointer<Akonadi2::ResourceAccessInterface> mResourceAccess; | ||
102 | DomainTypeAdaptorFactoryInterface::Ptr mDomainTypeAdaptorFactory; | ||
103 | QByteArray mResourceInstanceIdentifier; | ||
104 | QByteArray mBufferType; | ||
105 | Akonadi2::Query mQuery; | ||
106 | }; | ||
107 | |||