diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-07-27 23:00:23 +0200 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2015-07-27 23:22:10 +0200 |
commit | 7bd184460952932a237dc6f8bea7a8cd220afadf (patch) | |
tree | dd520096627edd9c7c06d7d1a52b30bb0876be22 | |
parent | 60514ebd866c8c49e90e501198588e2806c5fe7b (diff) | |
download | sink-7bd184460952932a237dc6f8bea7a8cd220afadf.tar.gz sink-7bd184460952932a237dc6f8bea7a8cd220afadf.zip |
Abstracted the storage so the facade can be tested.
-rw-r--r-- | common/CMakeLists.txt | 1 | ||||
-rw-r--r-- | common/entitystorage.cpp | 122 | ||||
-rw-r--r-- | common/entitystorage.h | 138 | ||||
-rw-r--r-- | common/facade.h | 8 | ||||
-rw-r--r-- | tests/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/genericfacadetest.cpp | 80 |
6 files changed, 244 insertions, 106 deletions
diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index a69c62c..b242256 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt | |||
@@ -13,6 +13,7 @@ endif (STORAGE_unqlite) | |||
13 | set(command_SRCS | 13 | set(command_SRCS |
14 | log.cpp | 14 | log.cpp |
15 | entitybuffer.cpp | 15 | entitybuffer.cpp |
16 | entitystorage.cpp | ||
16 | clientapi.cpp | 17 | clientapi.cpp |
17 | facadefactory.cpp | 18 | facadefactory.cpp |
18 | commands.cpp | 19 | commands.cpp |
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 | } | ||
diff --git a/common/entitystorage.h b/common/entitystorage.h index 6a41e0e..a62d474 100644 --- a/common/entitystorage.h +++ b/common/entitystorage.h | |||
@@ -31,124 +31,59 @@ | |||
31 | /** | 31 | /** |
32 | * Wraps storage, entity adaptor factory and indexes into one. | 32 | * Wraps storage, entity adaptor factory and indexes into one. |
33 | */ | 33 | */ |
34 | template <typename DomainType> | 34 | class EntityStorageBase |
35 | class EntityStorage | ||
36 | { | 35 | { |
37 | 36 | protected: | |
38 | public: | 37 | EntityStorageBase(const QByteArray &instanceIdentifier, const DomainTypeAdaptorFactoryInterface::Ptr &adaptorFactory) |
39 | EntityStorage(const QByteArray &instanceIdentifier, const DomainTypeAdaptorFactoryInterface::Ptr &adaptorFactory) | ||
40 | : mResourceInstanceIdentifier(instanceIdentifier), | 38 | : mResourceInstanceIdentifier(instanceIdentifier), |
41 | mDomainTypeAdaptorFactory(adaptorFactory) | 39 | mDomainTypeAdaptorFactory(adaptorFactory) |
42 | { | 40 | { |
43 | 41 | ||
44 | } | 42 | } |
45 | 43 | ||
46 | private: | 44 | virtual Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr create(const QByteArray &key, qint64 revision, const QSharedPointer<Akonadi2::ApplicationDomain::BufferAdaptor> &adaptor) = 0; |
47 | static void scan(const QSharedPointer<Akonadi2::Storage> &storage, const QByteArray &key, std::function<bool(const QByteArray &key, const Akonadi2::Entity &entity)> callback) | 45 | virtual Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr copy(const Akonadi2::ApplicationDomain::ApplicationDomainType &) = 0; |
48 | { | 46 | virtual ResultSet queryIndexes(const Akonadi2::Query &query, const QByteArray &resourceInstanceIdentifier, QSet<QByteArray> &appliedFilters) = 0; |
49 | storage->scan(key, [=](void *keyValue, int keySize, void *dataValue, int dataSize) -> bool { | 47 | |
50 | //Skip internals | 48 | void readValue(const QSharedPointer<Akonadi2::Storage> &storage, const QByteArray &key, const std::function<void(const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &)> &resultCallback); |
51 | if (Akonadi2::Storage::isInternalKey(keyValue, keySize)) { | 49 | ResultSet filteredSet(const ResultSet &resultSet, const std::function<bool(const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &domainObject)> &filter, const QSharedPointer<Akonadi2::Storage> &storage, qint64 baseRevision, qint64 topRevision); |
52 | return true; | 50 | ResultSet getResultSet(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::Storage> &storage, qint64 baseRevision, qint64 topRevision); |
53 | } | 51 | |
54 | 52 | protected: | |
55 | //Extract buffers | 53 | QByteArray mResourceInstanceIdentifier; |
56 | Akonadi2::EntityBuffer buffer(dataValue, dataSize); | 54 | DomainTypeAdaptorFactoryInterface::Ptr mDomainTypeAdaptorFactory; |
57 | 55 | }; | |
58 | //FIXME implement buffer.isValid() | 56 | |
59 | // const auto resourceBuffer = Akonadi2::EntityBuffer::readBuffer<DummyEvent>(buffer.entity().resource()); | 57 | template<typename DomainType> |
60 | // const auto localBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::ApplicationDomain::Buffer::Event>(buffer.entity().local()); | 58 | class EntityStorage : public EntityStorageBase |
61 | // const auto metadataBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::Metadata>(buffer.entity().metadata()); | 59 | { |
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 | 60 | ||
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) | 61 | public: |
62 | EntityStorage(const QByteArray &instanceIdentifier, const DomainTypeAdaptorFactoryInterface::Ptr &adaptorFactory) | ||
63 | : EntityStorageBase(instanceIdentifier, adaptorFactory) | ||
75 | { | 64 | { |
76 | scan(storage, key, [=](const QByteArray &key, const Akonadi2::Entity &entity) { | 65 | |
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 | } | 66 | } |
88 | 67 | ||
89 | static ResultSet fullScan(const QSharedPointer<Akonadi2::Storage> &storage) | 68 | protected: |
69 | Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr create(const QByteArray &key, qint64 revision, const QSharedPointer<Akonadi2::ApplicationDomain::BufferAdaptor> &adaptor) Q_DECL_OVERRIDE | ||
90 | { | 70 | { |
91 | //TODO use a result set with an iterator, to read values on demand | 71 | return DomainType::Ptr::create(mResourceInstanceIdentifier, key, revision, adaptor); |
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 | } | 72 | } |
100 | 73 | ||
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) | 74 | Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr copy(const Akonadi2::ApplicationDomain::ApplicationDomainType &object) Q_DECL_OVERRIDE |
102 | { | 75 | { |
103 | auto resultSetPtr = QSharedPointer<ResultSet>::create(resultSet); | 76 | return Akonadi2::ApplicationDomain::ApplicationDomainType::getInMemoryRepresentation<DomainType>(object); |
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 | } | 77 | } |
118 | 78 | ||
119 | static ResultSet getResultSet(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::Storage> &storage, const DomainTypeAdaptorFactoryInterface::Ptr &adaptorFactory, const QByteArray &resourceInstanceIdentifier, qint64 baseRevision, qint64 topRevision) | 79 | ResultSet queryIndexes(const Akonadi2::Query &query, const QByteArray &resourceInstanceIdentifier, QSet<QByteArray> &appliedFilters) Q_DECL_OVERRIDE |
120 | { | 80 | { |
121 | QSet<QByteArray> appliedFilters; | 81 | return Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::queryIndexes(query, resourceInstanceIdentifier, 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 | } | 82 | } |
148 | 83 | ||
149 | public: | 84 | public: |
150 | 85 | ||
151 | void read(const Akonadi2::Query &query, const QPair<qint64, qint64> &revisionRange, const QSharedPointer<Akonadi2::ResultProvider<typename DomainType::Ptr> > &resultProvider) | 86 | virtual void read(const Akonadi2::Query &query, const QPair<qint64, qint64> &revisionRange, const QSharedPointer<Akonadi2::ResultProvider<typename DomainType::Ptr> > &resultProvider) |
152 | { | 87 | { |
153 | auto storage = QSharedPointer<Akonadi2::Storage>::create(Akonadi2::Store::storageLocation(), mResourceInstanceIdentifier); | 88 | auto storage = QSharedPointer<Akonadi2::Storage>::create(Akonadi2::Store::storageLocation(), mResourceInstanceIdentifier); |
154 | storage->setDefaultErrorHandler([](const Akonadi2::Storage::Error &error) { | 89 | storage->setDefaultErrorHandler([](const Akonadi2::Storage::Error &error) { |
@@ -159,17 +94,14 @@ public: | |||
159 | //TODO start transaction on indexes as well | 94 | //TODO start transaction on indexes as well |
160 | 95 | ||
161 | Log() << "Querying" << revisionRange.first << revisionRange.second; | 96 | Log() << "Querying" << revisionRange.first << revisionRange.second; |
162 | auto resultSet = getResultSet(query, storage, mDomainTypeAdaptorFactory, mResourceInstanceIdentifier, revisionRange.first, revisionRange.second); | 97 | auto resultSet = getResultSet(query, storage, revisionRange.first, revisionRange.second); |
163 | auto resultCallback = std::bind(&Akonadi2::ResultProvider<typename DomainType::Ptr>::add, resultProvider, std::placeholders::_1); | 98 | while(resultSet.next([this, resultProvider](const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &value) -> bool { |
164 | while(resultSet.next([resultCallback](const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &value) -> bool { | 99 | auto cloned = copy(*value); |
165 | resultCallback(Akonadi2::ApplicationDomain::ApplicationDomainType::getInMemoryRepresentation<DomainType>(*value)); | 100 | resultProvider->add(cloned.template staticCast<DomainType>()); |
166 | return true; | 101 | return true; |
167 | })){}; | 102 | })){}; |
168 | //TODO replay removals and modifications | 103 | //TODO replay removals and modifications |
169 | storage->abortTransaction(); | 104 | storage->abortTransaction(); |
170 | } | 105 | } |
171 | 106 | ||
172 | private: | ||
173 | DomainTypeAdaptorFactoryInterface::Ptr mDomainTypeAdaptorFactory; | ||
174 | QByteArray mResourceInstanceIdentifier; | ||
175 | }; | 107 | }; |
diff --git a/common/facade.h b/common/facade.h index ef3bbbc..254f671 100644 --- a/common/facade.h +++ b/common/facade.h | |||
@@ -112,9 +112,10 @@ public: | |||
112 | * @param resourceIdentifier is the identifier of the resource instance | 112 | * @param resourceIdentifier is the identifier of the resource instance |
113 | * @param adaptorFactory is the adaptor factory used to generate the mappings from domain to resource types and vice versa | 113 | * @param adaptorFactory is the adaptor factory used to generate the mappings from domain to resource types and vice versa |
114 | */ | 114 | */ |
115 | GenericFacade(const QByteArray &resourceIdentifier, const DomainTypeAdaptorFactoryInterface::Ptr &adaptorFactory = DomainTypeAdaptorFactoryInterface::Ptr()) | 115 | GenericFacade(const QByteArray &resourceIdentifier, const DomainTypeAdaptorFactoryInterface::Ptr &adaptorFactory = DomainTypeAdaptorFactoryInterface::Ptr(), const QSharedPointer<EntityStorage<DomainType> > storage = QSharedPointer<EntityStorage<DomainType> >()) |
116 | : Akonadi2::StoreFacade<DomainType>(), | 116 | : Akonadi2::StoreFacade<DomainType>(), |
117 | mResourceAccess(new ResourceAccess(resourceIdentifier)), | 117 | mResourceAccess(new ResourceAccess(resourceIdentifier)), |
118 | mStorage(storage ? storage : QSharedPointer<EntityStorage<DomainType> >::create(resourceIdentifier, adaptorFactory)), | ||
118 | mDomainTypeAdaptorFactory(adaptorFactory), | 119 | mDomainTypeAdaptorFactory(adaptorFactory), |
119 | mResourceInstanceIdentifier(resourceIdentifier) | 120 | mResourceInstanceIdentifier(resourceIdentifier) |
120 | { | 121 | { |
@@ -257,11 +258,11 @@ protected: | |||
257 | } | 258 | } |
258 | 259 | ||
259 | 260 | ||
261 | private: | ||
260 | virtual KAsync::Job<qint64> load(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::ResultProvider<typename DomainType::Ptr> > &resultProvider, qint64 oldRevision, qint64 newRevision) | 262 | virtual KAsync::Job<qint64> load(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::ResultProvider<typename DomainType::Ptr> > &resultProvider, qint64 oldRevision, qint64 newRevision) |
261 | { | 263 | { |
262 | return KAsync::start<qint64>([=]() -> qint64 { | 264 | return KAsync::start<qint64>([=]() -> qint64 { |
263 | EntityStorage<DomainType> storage(mResourceInstanceIdentifier, mDomainTypeAdaptorFactory); | 265 | mStorage->read(query, qMakePair(oldRevision, newRevision), resultProvider); |
264 | storage.read(query, qMakePair(oldRevision, newRevision), resultProvider); | ||
265 | return newRevision; | 266 | return newRevision; |
266 | }); | 267 | }); |
267 | } | 268 | } |
@@ -269,6 +270,7 @@ protected: | |||
269 | protected: | 270 | protected: |
270 | //TODO use one resource access instance per application & per resource | 271 | //TODO use one resource access instance per application & per resource |
271 | QSharedPointer<Akonadi2::ResourceAccess> mResourceAccess; | 272 | QSharedPointer<Akonadi2::ResourceAccess> mResourceAccess; |
273 | QSharedPointer<EntityStorage<DomainType> > mStorage; | ||
272 | DomainTypeAdaptorFactoryInterface::Ptr mDomainTypeAdaptorFactory; | 274 | DomainTypeAdaptorFactoryInterface::Ptr mDomainTypeAdaptorFactory; |
273 | QByteArray mResourceInstanceIdentifier; | 275 | QByteArray mResourceInstanceIdentifier; |
274 | }; | 276 | }; |
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index eb4d606..6a52101 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt | |||
@@ -40,6 +40,7 @@ auto_tests ( | |||
40 | messagequeuetest | 40 | messagequeuetest |
41 | indextest | 41 | indextest |
42 | genericresourcetest | 42 | genericresourcetest |
43 | genericfacadetest | ||
43 | ) | 44 | ) |
44 | 45 | ||
45 | target_link_libraries(dummyresourcetest akonadi2_resource_dummy) | 46 | target_link_libraries(dummyresourcetest akonadi2_resource_dummy) |
diff --git a/tests/genericfacadetest.cpp b/tests/genericfacadetest.cpp new file mode 100644 index 0000000..fa086d1 --- /dev/null +++ b/tests/genericfacadetest.cpp | |||
@@ -0,0 +1,80 @@ | |||
1 | #include <QtTest> | ||
2 | |||
3 | #include <QString> | ||
4 | |||
5 | #include <common/facade.h> | ||
6 | #include <common/domainadaptor.h> | ||
7 | #include <common/resultprovider.h> | ||
8 | #include <common/synclistresult.h> | ||
9 | |||
10 | //Replace with something different | ||
11 | #include "event_generated.h" | ||
12 | |||
13 | class TestEventAdaptorFactory : public DomainTypeAdaptorFactory<Akonadi2::ApplicationDomain::Event, Akonadi2::ApplicationDomain::Buffer::Event, Akonadi2::ApplicationDomain::Buffer::EventBuilder> | ||
14 | { | ||
15 | public: | ||
16 | TestEventAdaptorFactory() | ||
17 | : DomainTypeAdaptorFactory() | ||
18 | { | ||
19 | } | ||
20 | |||
21 | virtual ~TestEventAdaptorFactory() {}; | ||
22 | }; | ||
23 | |||
24 | class TestEntityStorage : public EntityStorage<Akonadi2::ApplicationDomain::Event> | ||
25 | { | ||
26 | public: | ||
27 | using EntityStorage::EntityStorage; | ||
28 | virtual void read(const Akonadi2::Query &query, const QPair<qint64, qint64> &revisionRange, const QSharedPointer<Akonadi2::ResultProvider<Akonadi2::ApplicationDomain::Event::Ptr> > &resultProvider) Q_DECL_OVERRIDE | ||
29 | { | ||
30 | for (const auto &res : mResults) { | ||
31 | resultProvider->add(res); | ||
32 | } | ||
33 | } | ||
34 | |||
35 | QList<Akonadi2::ApplicationDomain::Event::Ptr> mResults; | ||
36 | }; | ||
37 | |||
38 | class TestResourceFacade : public Akonadi2::GenericFacade<Akonadi2::ApplicationDomain::Event> | ||
39 | { | ||
40 | public: | ||
41 | TestResourceFacade(const QByteArray &instanceIdentifier, const QSharedPointer<EntityStorage<Akonadi2::ApplicationDomain::Event> > storage) | ||
42 | : Akonadi2::GenericFacade<Akonadi2::ApplicationDomain::Event>(instanceIdentifier, QSharedPointer<TestEventAdaptorFactory>::create(), storage) | ||
43 | { | ||
44 | |||
45 | } | ||
46 | virtual ~TestResourceFacade() | ||
47 | { | ||
48 | |||
49 | } | ||
50 | }; | ||
51 | |||
52 | class GenericFacadeTest : public QObject | ||
53 | { | ||
54 | Q_OBJECT | ||
55 | private Q_SLOTS: | ||
56 | |||
57 | void testLoad() | ||
58 | { | ||
59 | Akonadi2::Query query; | ||
60 | query.liveQuery = false; | ||
61 | auto resultSet = QSharedPointer<Akonadi2::ResultProvider<Akonadi2::ApplicationDomain::Event::Ptr> >::create(); | ||
62 | |||
63 | auto storage = QSharedPointer<TestEntityStorage>::create("identifier", QSharedPointer<TestEventAdaptorFactory>::create()); | ||
64 | storage->mResults << Akonadi2::ApplicationDomain::Event::Ptr::create(); | ||
65 | TestResourceFacade facade("identifier", storage); | ||
66 | |||
67 | async::SyncListResult<Akonadi2::ApplicationDomain::Event::Ptr> result(resultSet->emitter()); | ||
68 | |||
69 | facade.load(query, resultSet).exec().waitForFinished(); | ||
70 | resultSet->initialResultSetComplete(); | ||
71 | |||
72 | //We have to wait for the events that deliver the results to be processed by the eventloop | ||
73 | result.exec(); | ||
74 | |||
75 | QCOMPARE(result.size(), 1); | ||
76 | } | ||
77 | }; | ||
78 | |||
79 | QTEST_MAIN(GenericFacadeTest) | ||
80 | #include "genericfacadetest.moc" | ||