diff options
Diffstat (limited to 'common/entitystorage.cpp')
-rw-r--r-- | common/entitystorage.cpp | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/common/entitystorage.cpp b/common/entitystorage.cpp new file mode 100644 index 0000000..f84e9f5 --- /dev/null +++ b/common/entitystorage.cpp | |||
@@ -0,0 +1,122 @@ | |||
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 | |||
20 | #include "entitystorage.h" | ||
21 | |||
22 | static void scan(const QSharedPointer<Akonadi2::Storage> &storage, const QByteArray &key, std::function<bool(const QByteArray &key, const Akonadi2::Entity &entity)> callback) | ||
23 | { | ||
24 | storage->scan(key, [=](void *keyValue, int keySize, void *dataValue, int dataSize) -> bool { | ||
25 | //Skip internals | ||
26 | if (Akonadi2::Storage::isInternalKey(keyValue, keySize)) { | ||
27 | return true; | ||
28 | } | ||
29 | |||
30 | //Extract buffers | ||
31 | Akonadi2::EntityBuffer buffer(dataValue, dataSize); | ||
32 | |||
33 | //FIXME implement buffer.isValid() | ||
34 | // const auto resourceBuffer = Akonadi2::EntityBuffer::readBuffer<DummyEvent>(buffer.entity().resource()); | ||
35 | // const auto localBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::ApplicationDomain::Buffer::Event>(buffer.entity().local()); | ||
36 | // const auto metadataBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::Metadata>(buffer.entity().metadata()); | ||
37 | |||
38 | // if ((!resourceBuffer && !localBuffer) || !metadataBuffer) { | ||
39 | // qWarning() << "invalid buffer " << QByteArray::fromRawData(static_cast<char*>(keyValue), keySize); | ||
40 | // return true; | ||
41 | // } | ||
42 | return callback(QByteArray::fromRawData(static_cast<char*>(keyValue), keySize), buffer.entity()); | ||
43 | }, | ||
44 | [](const Akonadi2::Storage::Error &error) { | ||
45 | qWarning() << "Error during query: " << error.message; | ||
46 | }); | ||
47 | } | ||
48 | |||
49 | void EntityStorageBase::readValue(const QSharedPointer<Akonadi2::Storage> &storage, const QByteArray &key, const std::function<void(const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &)> &resultCallback) | ||
50 | { | ||
51 | scan(storage, key, [=](const QByteArray &key, const Akonadi2::Entity &entity) { | ||
52 | const auto metadataBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::Metadata>(entity.metadata()); | ||
53 | qint64 revision = metadataBuffer ? metadataBuffer->revision() : -1; | ||
54 | //This only works for a 1:1 mapping of resource to domain types. | ||
55 | //Not i.e. for tags that are stored as flags in each entity of an imap store. | ||
56 | //additional properties that don't have a 1:1 mapping (such as separately stored tags), | ||
57 | //could be added to the adaptor | ||
58 | auto domainObject = create(key, revision, mDomainTypeAdaptorFactory->createAdaptor(entity)); | ||
59 | resultCallback(domainObject); | ||
60 | return true; | ||
61 | }); | ||
62 | } | ||
63 | |||
64 | static ResultSet fullScan(const QSharedPointer<Akonadi2::Storage> &storage) | ||
65 | { | ||
66 | //TODO use a result set with an iterator, to read values on demand | ||
67 | QVector<QByteArray> keys; | ||
68 | scan(storage, QByteArray(), [=, &keys](const QByteArray &key, const Akonadi2::Entity &) { | ||
69 | keys << key; | ||
70 | return true; | ||
71 | }); | ||
72 | Trace() << "Full scan found " << keys.size() << " results"; | ||
73 | return ResultSet(keys); | ||
74 | } | ||
75 | |||
76 | ResultSet EntityStorageBase::filteredSet(const ResultSet &resultSet, const std::function<bool(const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &domainObject)> &filter, const QSharedPointer<Akonadi2::Storage> &storage, qint64 baseRevision, qint64 topRevision) | ||
77 | { | ||
78 | auto resultSetPtr = QSharedPointer<ResultSet>::create(resultSet); | ||
79 | |||
80 | //Read through the source values and return whatever matches the filter | ||
81 | std::function<bool(std::function<void(const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &)>)> generator = [this, resultSetPtr, storage, filter](std::function<void(const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &)> callback) -> bool { | ||
82 | while (resultSetPtr->next()) { | ||
83 | readValue(storage, resultSetPtr->id(), [this, filter, callback](const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &domainObject) { | ||
84 | if (filter(domainObject)) { | ||
85 | callback(domainObject); | ||
86 | } | ||
87 | }); | ||
88 | } | ||
89 | return false; | ||
90 | }; | ||
91 | return ResultSet(generator); | ||
92 | } | ||
93 | |||
94 | ResultSet EntityStorageBase::getResultSet(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::Storage> &storage, qint64 baseRevision, qint64 topRevision) | ||
95 | { | ||
96 | QSet<QByteArray> appliedFilters; | ||
97 | ResultSet resultSet = queryIndexes(query, mResourceInstanceIdentifier, appliedFilters); | ||
98 | const auto remainingFilters = query.propertyFilter.keys().toSet() - appliedFilters; | ||
99 | |||
100 | //We do a full scan if there were no indexes available to create the initial set. | ||
101 | if (appliedFilters.isEmpty()) { | ||
102 | resultSet = fullScan(storage); | ||
103 | } | ||
104 | |||
105 | auto filter = [remainingFilters, query, baseRevision, topRevision](const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &domainObject) -> bool { | ||
106 | if (topRevision > 0) { | ||
107 | Trace() << "filtering by revision " << domainObject->revision(); | ||
108 | if (domainObject->revision() < baseRevision || domainObject->revision() > topRevision) { | ||
109 | return false; | ||
110 | } | ||
111 | } | ||
112 | for (const auto &filterProperty : remainingFilters) { | ||
113 | //TODO implement other comparison operators than equality | ||
114 | if (domainObject->getProperty(filterProperty) != query.propertyFilter.value(filterProperty)) { | ||
115 | return false; | ||
116 | } | ||
117 | } | ||
118 | return true; | ||
119 | }; | ||
120 | |||
121 | return filteredSet(resultSet, filter, storage, baseRevision, topRevision); | ||
122 | } | ||