diff options
Diffstat (limited to 'common/entitystorage.h')
-rw-r--r-- | common/entitystorage.h | 175 |
1 files changed, 175 insertions, 0 deletions
diff --git a/common/entitystorage.h b/common/entitystorage.h new file mode 100644 index 0000000..6a41e0e --- /dev/null +++ b/common/entitystorage.h | |||
@@ -0,0 +1,175 @@ | |||
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 "clientapi.h" | ||
22 | |||
23 | #include <QByteArray> | ||
24 | |||
25 | #include "domainadaptor.h" | ||
26 | #include "entitybuffer.h" | ||
27 | #include "log.h" | ||
28 | #include "storage.h" | ||
29 | #include "resultset.h" | ||
30 | |||
31 | /** | ||
32 | * Wraps storage, entity adaptor factory and indexes into one. | ||
33 | */ | ||
34 | template <typename DomainType> | ||
35 | class EntityStorage | ||
36 | { | ||
37 | |||
38 | public: | ||
39 | EntityStorage(const QByteArray &instanceIdentifier, const DomainTypeAdaptorFactoryInterface::Ptr &adaptorFactory) | ||
40 | : mResourceInstanceIdentifier(instanceIdentifier), | ||
41 | mDomainTypeAdaptorFactory(adaptorFactory) | ||
42 | { | ||
43 | |||
44 | } | ||
45 | |||
46 | private: | ||
47 | static void scan(const QSharedPointer<Akonadi2::Storage> &storage, const QByteArray &key, std::function<bool(const QByteArray &key, const Akonadi2::Entity &entity)> callback) | ||
48 | { | ||
49 | storage->scan(key, [=](void *keyValue, int keySize, void *dataValue, int dataSize) -> bool { | ||
50 | //Skip internals | ||
51 | if (Akonadi2::Storage::isInternalKey(keyValue, keySize)) { | ||
52 | return true; | ||
53 | } | ||
54 | |||
55 | //Extract buffers | ||
56 | Akonadi2::EntityBuffer buffer(dataValue, dataSize); | ||
57 | |||
58 | //FIXME implement buffer.isValid() | ||
59 | // const auto resourceBuffer = Akonadi2::EntityBuffer::readBuffer<DummyEvent>(buffer.entity().resource()); | ||
60 | // const auto localBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::ApplicationDomain::Buffer::Event>(buffer.entity().local()); | ||
61 | // const auto metadataBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::Metadata>(buffer.entity().metadata()); | ||
62 | |||
63 | // if ((!resourceBuffer && !localBuffer) || !metadataBuffer) { | ||
64 | // qWarning() << "invalid buffer " << QByteArray::fromRawData(static_cast<char*>(keyValue), keySize); | ||
65 | // return true; | ||
66 | // } | ||
67 | return callback(QByteArray::fromRawData(static_cast<char*>(keyValue), keySize), buffer.entity()); | ||
68 | }, | ||
69 | [](const Akonadi2::Storage::Error &error) { | ||
70 | qWarning() << "Error during query: " << error.message; | ||
71 | }); | ||
72 | } | ||
73 | |||
74 | static void readValue(const QSharedPointer<Akonadi2::Storage> &storage, const QByteArray &key, const std::function<void(const typename DomainType::Ptr &)> &resultCallback, const DomainTypeAdaptorFactoryInterface::Ptr &adaptorFactory, const QByteArray &instanceIdentifier) | ||
75 | { | ||
76 | scan(storage, key, [=](const QByteArray &key, const Akonadi2::Entity &entity) { | ||
77 | const auto metadataBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::Metadata>(entity.metadata()); | ||
78 | qint64 revision = metadataBuffer ? metadataBuffer->revision() : -1; | ||
79 | //This only works for a 1:1 mapping of resource to domain types. | ||
80 | //Not i.e. for tags that are stored as flags in each entity of an imap store. | ||
81 | //additional properties that don't have a 1:1 mapping (such as separately stored tags), | ||
82 | //could be added to the adaptor | ||
83 | auto domainObject = QSharedPointer<DomainType>::create(instanceIdentifier, key, revision, adaptorFactory->createAdaptor(entity)); | ||
84 | resultCallback(domainObject); | ||
85 | return true; | ||
86 | }); | ||
87 | } | ||
88 | |||
89 | static ResultSet fullScan(const QSharedPointer<Akonadi2::Storage> &storage) | ||
90 | { | ||
91 | //TODO use a result set with an iterator, to read values on demand | ||
92 | QVector<QByteArray> keys; | ||
93 | scan(storage, QByteArray(), [=, &keys](const QByteArray &key, const Akonadi2::Entity &) { | ||
94 | keys << key; | ||
95 | return true; | ||
96 | }); | ||
97 | Trace() << "Full scan found " << keys.size() << " results"; | ||
98 | return ResultSet(keys); | ||
99 | } | ||
100 | |||
101 | static ResultSet filteredSet(const ResultSet &resultSet, const std::function<bool(const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &domainObject)> &filter, const QSharedPointer<Akonadi2::Storage> &storage, const DomainTypeAdaptorFactoryInterface::Ptr &adaptorFactory, qint64 baseRevision, qint64 topRevision, const QByteArray &instanceIdentifier) | ||
102 | { | ||
103 | auto resultSetPtr = QSharedPointer<ResultSet>::create(resultSet); | ||
104 | |||
105 | //Read through the source values and return whatever matches the filter | ||
106 | std::function<bool(std::function<void(const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &)>)> generator = [resultSetPtr, storage, adaptorFactory, filter, instanceIdentifier](std::function<void(const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &)> callback) -> bool { | ||
107 | while (resultSetPtr->next()) { | ||
108 | readValue(storage, resultSetPtr->id(), [filter, callback](const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &domainObject) { | ||
109 | if (filter(domainObject)) { | ||
110 | callback(domainObject); | ||
111 | } | ||
112 | }, adaptorFactory, instanceIdentifier); | ||
113 | } | ||
114 | return false; | ||
115 | }; | ||
116 | return ResultSet(generator); | ||
117 | } | ||
118 | |||
119 | static ResultSet getResultSet(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::Storage> &storage, const DomainTypeAdaptorFactoryInterface::Ptr &adaptorFactory, const QByteArray &resourceInstanceIdentifier, qint64 baseRevision, qint64 topRevision) | ||
120 | { | ||
121 | QSet<QByteArray> appliedFilters; | ||
122 | ResultSet resultSet = Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::queryIndexes(query, resourceInstanceIdentifier, appliedFilters, qMakePair(baseRevision, topRevision)); | ||
123 | const auto remainingFilters = query.propertyFilter.keys().toSet() - appliedFilters; | ||
124 | |||
125 | //We do a full scan if there were no indexes available to create the initial set. | ||
126 | if (appliedFilters.isEmpty()) { | ||
127 | resultSet = fullScan(storage); | ||
128 | } | ||
129 | |||
130 | auto filter = [remainingFilters, query, baseRevision, topRevision](const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &domainObject) -> bool { | ||
131 | if (topRevision > 0) { | ||
132 | Trace() << "filtering by revision " << domainObject->revision(); | ||
133 | if (domainObject->revision() < baseRevision || domainObject->revision() > topRevision) { | ||
134 | return false; | ||
135 | } | ||
136 | } | ||
137 | for (const auto &filterProperty : remainingFilters) { | ||
138 | //TODO implement other comparison operators than equality | ||
139 | if (domainObject->getProperty(filterProperty) != query.propertyFilter.value(filterProperty)) { | ||
140 | return false; | ||
141 | } | ||
142 | } | ||
143 | return true; | ||
144 | }; | ||
145 | |||
146 | return filteredSet(resultSet, filter, storage, adaptorFactory, baseRevision, topRevision, resourceInstanceIdentifier); | ||
147 | } | ||
148 | |||
149 | public: | ||
150 | |||
151 | void read(const Akonadi2::Query &query, const QPair<qint64, qint64> &revisionRange, const QSharedPointer<Akonadi2::ResultProvider<typename DomainType::Ptr> > &resultProvider) | ||
152 | { | ||
153 | auto storage = QSharedPointer<Akonadi2::Storage>::create(Akonadi2::Store::storageLocation(), mResourceInstanceIdentifier); | ||
154 | storage->setDefaultErrorHandler([](const Akonadi2::Storage::Error &error) { | ||
155 | Warning() << "Error during query: " << error.store << error.message; | ||
156 | }); | ||
157 | |||
158 | storage->startTransaction(Akonadi2::Storage::ReadOnly); | ||
159 | //TODO start transaction on indexes as well | ||
160 | |||
161 | Log() << "Querying" << revisionRange.first << revisionRange.second; | ||
162 | auto resultSet = getResultSet(query, storage, mDomainTypeAdaptorFactory, mResourceInstanceIdentifier, revisionRange.first, revisionRange.second); | ||
163 | auto resultCallback = std::bind(&Akonadi2::ResultProvider<typename DomainType::Ptr>::add, resultProvider, std::placeholders::_1); | ||
164 | while(resultSet.next([resultCallback](const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &value) -> bool { | ||
165 | resultCallback(Akonadi2::ApplicationDomain::ApplicationDomainType::getInMemoryRepresentation<DomainType>(*value)); | ||
166 | return true; | ||
167 | })){}; | ||
168 | //TODO replay removals and modifications | ||
169 | storage->abortTransaction(); | ||
170 | } | ||
171 | |||
172 | private: | ||
173 | DomainTypeAdaptorFactoryInterface::Ptr mDomainTypeAdaptorFactory; | ||
174 | QByteArray mResourceInstanceIdentifier; | ||
175 | }; | ||