diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-11-15 12:46:26 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-11-15 12:46:26 +0100 |
commit | 972f3a4e96876e4c36162a11062e40863d88a2a1 (patch) | |
tree | e5173c26f35895e8f3386822d47b7b0d93374db5 /common/entitystorage.h | |
parent | d4b10a3de396eebc6c815093e9e1725ece270e9e (diff) | |
download | sink-972f3a4e96876e4c36162a11062e40863d88a2a1.tar.gz sink-972f3a4e96876e4c36162a11062e40863d88a2a1.zip |
Cleanup
Diffstat (limited to 'common/entitystorage.h')
-rw-r--r-- | common/entitystorage.h | 126 |
1 files changed, 0 insertions, 126 deletions
diff --git a/common/entitystorage.h b/common/entitystorage.h deleted file mode 100644 index 8e73083..0000000 --- a/common/entitystorage.h +++ /dev/null | |||
@@ -1,126 +0,0 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2014 Christian Mollekopf <chrigi_1@fastmail.fm> | ||
3 | * | ||
4 | * This program is free software; you can redistribute it and/or modify | ||
5 | * it under the terms of the GNU General Public License as published by | ||
6 | * the Free Software Foundation; either version 2 of the License, or | ||
7 | * (at your option) any later version. | ||
8 | * | ||
9 | * This program is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | * GNU General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU General Public License | ||
15 | * along with this program; if not, write to the | ||
16 | * Free Software Foundation, Inc., | ||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
18 | */ | ||
19 | #pragma once | ||
20 | |||
21 | #include <QByteArray> | ||
22 | |||
23 | #include "query.h" | ||
24 | #include "domainadaptor.h" | ||
25 | #include "entitybuffer.h" | ||
26 | #include "log.h" | ||
27 | #include "storage.h" | ||
28 | #include "resultset.h" | ||
29 | #include "resultprovider.h" | ||
30 | #include "definitions.h" | ||
31 | |||
32 | /** | ||
33 | * Wraps storage, entity adaptor factory and indexes into one. | ||
34 | * | ||
35 | */ | ||
36 | class EntityStorageBase | ||
37 | { | ||
38 | public: | ||
39 | typedef std::function<ResultSet (const Akonadi2::Query &query, Akonadi2::Storage::Transaction &transaction, QSet<QByteArray> &remainingFilters)> InitialResultLoader; | ||
40 | typedef std::function<ResultSet (qint64 baseRevision, const Akonadi2::Query &query, Akonadi2::Storage::Transaction &transaction, QSet<QByteArray> &remainingFilters)> IncrementalResultLoader; | ||
41 | typedef std::function<void(const Akonadi2::Storage::Transaction &transaction, const QByteArray &key, const std::function<void(const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &, Akonadi2::Operation)> &resultCallback)> EntityReader; | ||
42 | |||
43 | /** | ||
44 | * Returns the initial result set that still needs to be filtered. | ||
45 | * | ||
46 | * To make this efficient indexes should be chosen that are as selective as possible. | ||
47 | */ | ||
48 | InitialResultLoader loadInitialResultSet; | ||
49 | /** | ||
50 | * Returns the incremental result set that still needs to be filtered. | ||
51 | */ | ||
52 | IncrementalResultLoader loadIncrementalResultSet; | ||
53 | |||
54 | /** | ||
55 | * Loads a single entity by uid from storage. | ||
56 | */ | ||
57 | EntityReader readEntity; | ||
58 | |||
59 | protected: | ||
60 | EntityStorageBase(const QByteArray &instanceIdentifier) | ||
61 | : mResourceInstanceIdentifier(instanceIdentifier) | ||
62 | { | ||
63 | |||
64 | } | ||
65 | |||
66 | virtual Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr copy(const Akonadi2::ApplicationDomain::ApplicationDomainType &) = 0; | ||
67 | |||
68 | ResultSet getResultSet(const Akonadi2::Query &query, Akonadi2::Storage::Transaction &transaction, qint64 baseRevision); | ||
69 | |||
70 | QByteArray mResourceInstanceIdentifier; | ||
71 | |||
72 | private: | ||
73 | ResultSet filteredSet(const ResultSet &resultSet, const std::function<bool(const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &domainObject)> &filter, const Akonadi2::Storage::Transaction &transaction, bool isInitialQuery); | ||
74 | }; | ||
75 | |||
76 | template<typename DomainType> | ||
77 | class EntityStorage : public EntityStorageBase | ||
78 | { | ||
79 | |||
80 | public: | ||
81 | |||
82 | EntityStorage(const QByteArray &instanceIdentifier) | ||
83 | : EntityStorageBase(instanceIdentifier) | ||
84 | { | ||
85 | } | ||
86 | |||
87 | protected: | ||
88 | Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr copy(const Akonadi2::ApplicationDomain::ApplicationDomainType &object) Q_DECL_OVERRIDE | ||
89 | { | ||
90 | return Akonadi2::ApplicationDomain::ApplicationDomainType::getInMemoryRepresentation<DomainType>(object); | ||
91 | } | ||
92 | |||
93 | public: | ||
94 | |||
95 | virtual qint64 read(const Akonadi2::Query &query, qint64 baseRevision, const QSharedPointer<Akonadi2::ResultProvider<typename DomainType::Ptr> > &resultProvider) | ||
96 | { | ||
97 | Akonadi2::Storage storage(Akonadi2::storageLocation(), mResourceInstanceIdentifier); | ||
98 | storage.setDefaultErrorHandler([](const Akonadi2::Storage::Error &error) { | ||
99 | Warning() << "Error during query: " << error.store << error.message; | ||
100 | }); | ||
101 | |||
102 | auto transaction = storage.createTransaction(Akonadi2::Storage::ReadOnly); | ||
103 | |||
104 | Log() << "Querying" << baseRevision << Akonadi2::Storage::maxRevision(transaction); | ||
105 | auto resultSet = getResultSet(query, transaction, baseRevision); | ||
106 | while(resultSet.next([this, resultProvider](const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &value, Akonadi2::Operation operation) -> bool { | ||
107 | switch (operation) { | ||
108 | case Akonadi2::Operation_Creation: | ||
109 | Trace() << "Got creation"; | ||
110 | resultProvider->add(copy(*value).template staticCast<DomainType>()); | ||
111 | break; | ||
112 | case Akonadi2::Operation_Modification: | ||
113 | Trace() << "Got modification"; | ||
114 | resultProvider->modify(copy(*value).template staticCast<DomainType>()); | ||
115 | break; | ||
116 | case Akonadi2::Operation_Removal: | ||
117 | Trace() << "Got removal"; | ||
118 | resultProvider->remove(copy(*value).template staticCast<DomainType>()); | ||
119 | break; | ||
120 | } | ||
121 | return true; | ||
122 | })){}; | ||
123 | return Akonadi2::Storage::maxRevision(transaction); | ||
124 | } | ||
125 | |||
126 | }; | ||