diff options
Diffstat (limited to 'examples/dummyresource/facade.cpp')
-rw-r--r-- | examples/dummyresource/facade.cpp | 128 |
1 files changed, 0 insertions, 128 deletions
diff --git a/examples/dummyresource/facade.cpp b/examples/dummyresource/facade.cpp index 002b836..d6e9286 100644 --- a/examples/dummyresource/facade.cpp +++ b/examples/dummyresource/facade.cpp | |||
@@ -19,25 +19,7 @@ | |||
19 | 19 | ||
20 | #include "facade.h" | 20 | #include "facade.h" |
21 | 21 | ||
22 | #include <QDebug> | ||
23 | #include <functional> | ||
24 | |||
25 | #include "common/resourceaccess.h" | ||
26 | #include "common/commands.h" | ||
27 | #include "common/resultset.h" | ||
28 | #include "common/domain/event.h" | ||
29 | #include "dummycalendar_generated.h" | ||
30 | #include "event_generated.h" | ||
31 | #include "entity_generated.h" | ||
32 | #include "metadata_generated.h" | ||
33 | #include "domainadaptor.h" | 22 | #include "domainadaptor.h" |
34 | #include <common/entitybuffer.h> | ||
35 | #include <common/index.h> | ||
36 | #include <common/log.h> | ||
37 | |||
38 | using namespace DummyCalendar; | ||
39 | using namespace flatbuffers; | ||
40 | |||
41 | 23 | ||
42 | DummyResourceFacade::DummyResourceFacade() | 24 | DummyResourceFacade::DummyResourceFacade() |
43 | : Akonadi2::GenericFacade<Akonadi2::ApplicationDomain::Event>("org.kde.dummy.instance1", QSharedPointer<DummyEventAdaptorFactory>::create()) | 25 | : Akonadi2::GenericFacade<Akonadi2::ApplicationDomain::Event>("org.kde.dummy.instance1", QSharedPointer<DummyEventAdaptorFactory>::create()) |
@@ -48,113 +30,3 @@ DummyResourceFacade::~DummyResourceFacade() | |||
48 | { | 30 | { |
49 | } | 31 | } |
50 | 32 | ||
51 | static void scan(const QSharedPointer<Akonadi2::Storage> &storage, const QByteArray &key, std::function<bool(const QByteArray &key, const Akonadi2::Entity &entity, DummyEvent const *buffer, Akonadi2::ApplicationDomain::Buffer::Event const *local, Akonadi2::Metadata const *metadata)> callback) | ||
52 | { | ||
53 | storage->scan(key, [=](void *keyValue, int keySize, void *dataValue, int dataSize) -> bool { | ||
54 | //Skip internals | ||
55 | if (Akonadi2::Storage::isInternalKey(keyValue, keySize)) { | ||
56 | return true; | ||
57 | } | ||
58 | |||
59 | //Extract buffers | ||
60 | Akonadi2::EntityBuffer buffer(dataValue, dataSize); | ||
61 | |||
62 | const auto resourceBuffer = Akonadi2::EntityBuffer::readBuffer<DummyEvent>(buffer.entity().resource()); | ||
63 | const auto localBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::ApplicationDomain::Buffer::Event>(buffer.entity().local()); | ||
64 | const auto metadataBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::Metadata>(buffer.entity().metadata()); | ||
65 | |||
66 | if ((!resourceBuffer && !localBuffer) || !metadataBuffer) { | ||
67 | qWarning() << "invalid buffer " << QByteArray::fromRawData(static_cast<char*>(keyValue), keySize); | ||
68 | return true; | ||
69 | } | ||
70 | return callback(QByteArray::fromRawData(static_cast<char*>(keyValue), keySize), buffer.entity(), resourceBuffer, localBuffer, metadataBuffer); | ||
71 | }, | ||
72 | [](const Akonadi2::Storage::Error &error) { | ||
73 | qWarning() << "Error during query: " << error.message; | ||
74 | }); | ||
75 | } | ||
76 | |||
77 | static void readValue(const QSharedPointer<Akonadi2::Storage> &storage, const QByteArray &key, const std::function<void(const Akonadi2::ApplicationDomain::Event::Ptr &)> &resultCallback, const QSharedPointer<DomainTypeAdaptorFactoryInterface<Akonadi2::ApplicationDomain::Event> > &adaptorFactory) | ||
78 | { | ||
79 | scan(storage, key, [=](const QByteArray &key, const Akonadi2::Entity &entity, DummyEvent const *buffer, Akonadi2::ApplicationDomain::Buffer::Event const *local, Akonadi2::Metadata const *metadataBuffer) { | ||
80 | qint64 revision = metadataBuffer ? metadataBuffer->revision() : -1; | ||
81 | //This only works for a 1:1 mapping of resource to domain types. | ||
82 | //Not i.e. for tags that are stored as flags in each entity of an imap store. | ||
83 | //additional properties that don't have a 1:1 mapping (such as separately stored tags), | ||
84 | //could be added to the adaptor | ||
85 | auto event = QSharedPointer<Akonadi2::ApplicationDomain::Event>::create("org.kde.dummy.instance1", key, revision, adaptorFactory->createAdaptor(entity)); | ||
86 | resultCallback(event); | ||
87 | return true; | ||
88 | }); | ||
89 | } | ||
90 | |||
91 | static ResultSet getResultSet(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::Storage> &storage, const QSharedPointer<DomainTypeAdaptorFactoryInterface<Akonadi2::ApplicationDomain::Event> > &adaptorFactory) | ||
92 | { | ||
93 | QSet<QByteArray> appliedFilters; | ||
94 | ResultSet resultSet = Akonadi2::ApplicationDomain::TypeImplementation<Akonadi2::ApplicationDomain::Event>::queryIndexes(query, "org.kde.dummy.instance1", appliedFilters); | ||
95 | const auto remainingFilters = query.propertyFilter.keys().toSet() - appliedFilters; | ||
96 | |||
97 | //We do a full scan if there were no indexes available to create the initial set. | ||
98 | //TODO use a result set with an iterator, to read values on demand | ||
99 | if (appliedFilters.isEmpty()) { | ||
100 | QVector<QByteArray> keys; | ||
101 | scan(storage, QByteArray(), [=, &keys](const QByteArray &key, const Akonadi2::Entity &entity, DummyEvent const *buffer, Akonadi2::ApplicationDomain::Buffer::Event const *local, Akonadi2::Metadata const *metadataBuffer) { | ||
102 | keys << key; | ||
103 | return true; | ||
104 | }); | ||
105 | resultSet = ResultSet(keys); | ||
106 | } | ||
107 | |||
108 | auto filter = [remainingFilters, query](const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &event) -> bool { | ||
109 | for (const auto &filterProperty : remainingFilters) { | ||
110 | //TODO implement other comparison operators than equality | ||
111 | if (event->getProperty(filterProperty) != query.propertyFilter.value(filterProperty)) { | ||
112 | return false; | ||
113 | } | ||
114 | } | ||
115 | return true; | ||
116 | }; | ||
117 | |||
118 | auto resultSetPtr = QSharedPointer<ResultSet>::create(resultSet); | ||
119 | |||
120 | //Read through the source values and return whatever matches the filter | ||
121 | std::function<bool(std::function<void(const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &)>)> generator = [resultSetPtr, storage, adaptorFactory, filter](std::function<void(const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &)> callback) -> bool { | ||
122 | while (resultSetPtr->next()) { | ||
123 | Akonadi2::ApplicationDomain::Event::Ptr event; | ||
124 | readValue(storage, resultSetPtr->id(), [filter, callback](const Akonadi2::ApplicationDomain::Event::Ptr &event) { | ||
125 | if (filter(event)) { | ||
126 | callback(event); | ||
127 | } | ||
128 | }, adaptorFactory); | ||
129 | } | ||
130 | return false; | ||
131 | }; | ||
132 | return ResultSet(generator); | ||
133 | } | ||
134 | |||
135 | //TODO generalize | ||
136 | KAsync::Job<qint64> DummyResourceFacade::load(const Akonadi2::Query &query, const QSharedPointer<Akonadi2::ResultProvider<Akonadi2::ApplicationDomain::Event::Ptr> > &resultProvider, qint64 oldRevision, qint64 newRevision) | ||
137 | { | ||
138 | return KAsync::start<qint64>([=]() { | ||
139 | auto storage = QSharedPointer<Akonadi2::Storage>::create(Akonadi2::Store::storageLocation(), "org.kde.dummy.instance1"); | ||
140 | storage->setDefaultErrorHandler([](const Akonadi2::Storage::Error &error) { | ||
141 | Warning() << "Error during query: " << error.store << error.message; | ||
142 | }); | ||
143 | |||
144 | storage->startTransaction(Akonadi2::Storage::ReadOnly); | ||
145 | //TODO start transaction on indexes as well | ||
146 | const qint64 revision = storage->maxRevision(); | ||
147 | |||
148 | auto resultSet = getResultSet(query, storage, mDomainTypeAdaptorFactory); | ||
149 | |||
150 | // TODO only emit changes and don't replace everything | ||
151 | resultProvider->clear(); | ||
152 | auto resultCallback = std::bind(&Akonadi2::ResultProvider<Akonadi2::ApplicationDomain::Event::Ptr>::add, resultProvider, std::placeholders::_1); | ||
153 | while(resultSet.next([resultCallback](const Akonadi2::ApplicationDomain::ApplicationDomainType::Ptr &value) -> bool { | ||
154 | resultCallback(Akonadi2::ApplicationDomain::ApplicationDomainType::getInMemoryRepresentation<Akonadi2::ApplicationDomain::Event>(value)); | ||
155 | })){}; | ||
156 | storage->abortTransaction(); | ||
157 | return revision; | ||
158 | }); | ||
159 | } | ||
160 | |||