summaryrefslogtreecommitdiffstats
path: root/dummyresource
diff options
context:
space:
mode:
Diffstat (limited to 'dummyresource')
-rw-r--r--dummyresource/domainadaptor.cpp91
-rw-r--r--dummyresource/domainadaptor.h6
-rw-r--r--dummyresource/facade.cpp2
-rw-r--r--dummyresource/facade.h2
4 files changed, 16 insertions, 85 deletions
diff --git a/dummyresource/domainadaptor.cpp b/dummyresource/domainadaptor.cpp
index 00af3fe..fa00bbc 100644
--- a/dummyresource/domainadaptor.cpp
+++ b/dummyresource/domainadaptor.cpp
@@ -9,49 +9,12 @@
9#include "entity_generated.h" 9#include "entity_generated.h"
10#include "metadata_generated.h" 10#include "metadata_generated.h"
11#include "domainadaptor.h" 11#include "domainadaptor.h"
12#include "log.h"
12#include <common/entitybuffer.h> 13#include <common/entitybuffer.h>
13 14
14using namespace DummyCalendar; 15using namespace DummyCalendar;
15using namespace flatbuffers; 16using namespace flatbuffers;
16 17
17/**
18 * Defines how to convert qt primitives to flatbuffer ones
19 * TODO: rename to createProperty or so?
20 */
21template <class T>
22uoffset_t extractProperty(const QVariant &, flatbuffers::FlatBufferBuilder &fbb);
23
24template <>
25uoffset_t extractProperty<QString>(const QVariant &property, flatbuffers::FlatBufferBuilder &fbb)
26{
27 if (property.isValid()) {
28 return fbb.CreateString(property.toString().toStdString()).o;
29 }
30 return 0;
31}
32
33/**
34 * Create a buffer from a domain object using the provided mappings
35 */
36template <class Builder>
37void createBufferPart(const Akonadi2::ApplicationDomain::ApplicationDomainType &domainObject, flatbuffers::FlatBufferBuilder &fbb, const WritePropertyMapper<Builder> &mapper)
38{
39 //First create a primitives such as strings using the mappings
40 QList<std::function<void(Builder &)> > propertiesToAddToResource;
41 for (const auto &property : domainObject.changedProperties()) {
42 const auto value = domainObject.getProperty(property);
43 if (mapper.hasMapping(property)) {
44 mapper.setProperty(property, domainObject.getProperty(property), propertiesToAddToResource, fbb);
45 }
46 }
47
48 //Then create all porperties using the above generated builderCalls
49 Builder builder(fbb);
50 for (auto propertyBuilder : propertiesToAddToResource) {
51 propertyBuilder(builder);
52 }
53 builder.Finish();
54}
55 18
56 19
57 20
@@ -59,7 +22,6 @@ DummyEventAdaptorFactory::DummyEventAdaptorFactory()
59 : DomainTypeAdaptorFactory() 22 : DomainTypeAdaptorFactory()
60{ 23{
61 //TODO turn this into initializeReadPropertyMapper as well? 24 //TODO turn this into initializeReadPropertyMapper as well?
62 mResourceMapper = QSharedPointer<ReadPropertyMapper<DummyEvent> >::create();
63 mResourceMapper->addMapping("summary", [](DummyEvent const *buffer) -> QVariant { 25 mResourceMapper->addMapping("summary", [](DummyEvent const *buffer) -> QVariant {
64 if (buffer->summary()) { 26 if (buffer->summary()) {
65 return QString::fromStdString(buffer->summary()->c_str()); 27 return QString::fromStdString(buffer->summary()->c_str());
@@ -76,51 +38,24 @@ DummyEventAdaptorFactory::DummyEventAdaptorFactory()
76 38
77void DummyEventAdaptorFactory::createBuffer(const Akonadi2::ApplicationDomain::Event &event, flatbuffers::FlatBufferBuilder &fbb) 39void DummyEventAdaptorFactory::createBuffer(const Akonadi2::ApplicationDomain::Event &event, flatbuffers::FlatBufferBuilder &fbb)
78{ 40{
79 // flatbuffers::FlatBufferBuilder resFbb;
80 // flatbuffers::FlatBufferBuilder localFbb;
81
82 // QList<std::function<void(DummyEventBuilder &)> > propertiesToAddToResource;
83 // QList<std::function<void(Akonadi2::ApplicationDomain::Buffer::EventBuilder &)> > propertiesToAddToLocal;
84 // for (const auto &property : event.changedProperties()) {
85 // const auto value = event.getProperty(property);
86 // if (mResourceWriteMapper && mResourceWriteMapper->hasMapping(property)) {
87 // mResourceWriteMapper->setProperty(property, value, propertiesToAddToResource, resFbb);
88 // } if (mLocalWriteMapper && mLocalWriteMapper->hasMapping(property)) {
89 // mLocalWriteMapper->setProperty(property, value, propertiesToAddToLocal, localFbb);
90 // }
91 // }
92
93 // DummyEventBuilder resBuilder(resFbb);
94 // for (auto propertyBuilder : propertiesToAddToResource) {
95 // propertyBuilder(resBuilder);
96 // }
97 // resBuilder.Finish();
98
99 // DummyEventBuilder localBuilder(localFbb);
100 // for (auto propertyBuilder : propertiesToAddToResource) {
101 // propertyBuilder(localBuilder);
102 // }
103 // localBuilder.Finish();
104
105 // TODO: how does a resource specify what goes to a local buffer and what it stores separately?
106 // flatbuffers::FlatBufferBuilder eventFbb;
107 // {
108 // auto summary = extractProperty<QString>(event.getProperty("summary"), fbb);
109 // DummyCalendar::DummyEventBuilder eventBuilder(eventFbb);
110 // eventBuilder.add_summary(summary);
111 // auto eventLocation = eventBuilder.Finish();
112 // DummyCalendar::FinishDummyEventBuffer(eventFbb, eventLocation);
113 // }
114
115 //TODO we should only copy values into the local buffer that haven't already been copied by the resource buffer
116 flatbuffers::FlatBufferBuilder localFbb; 41 flatbuffers::FlatBufferBuilder localFbb;
117 if (mLocalWriteMapper) { 42 if (mLocalWriteMapper) {
118 createBufferPart<Akonadi2::ApplicationDomain::Buffer::EventBuilder>(event, localFbb, *mLocalWriteMapper); 43 auto pos = createBufferPart<Akonadi2::ApplicationDomain::Buffer::EventBuilder, Akonadi2::ApplicationDomain::Buffer::Event>(event, localFbb, *mLocalWriteMapper);
44 Akonadi2::ApplicationDomain::Buffer::FinishEventBuffer(localFbb, pos);
45 flatbuffers::Verifier verifier(localFbb.GetBufferPointer(), localFbb.GetSize());
46 if (!verifier.VerifyBuffer<Akonadi2::ApplicationDomain::Buffer::Event>()) {
47 Warning() << "Created invalid local buffer";
48 }
119 } 49 }
120 50
121 flatbuffers::FlatBufferBuilder resFbb; 51 flatbuffers::FlatBufferBuilder resFbb;
122 if (mResourceWriteMapper) { 52 if (mResourceWriteMapper) {
123 createBufferPart<DummyEventBuilder>(event, resFbb, *mResourceWriteMapper); 53 auto pos = createBufferPart<DummyEventBuilder, DummyEvent>(event, resFbb, *mResourceWriteMapper);
54 DummyCalendar::FinishDummyEventBuffer(resFbb, pos);
55 flatbuffers::Verifier verifier(resFbb.GetBufferPointer(), resFbb.GetSize());
56 if (!verifier.VerifyBuffer<DummyEvent>()) {
57 Warning() << "Created invalid resource buffer";
58 }
124 } 59 }
125 60
126 Akonadi2::EntityBuffer::assembleEntityBuffer(fbb, 0, 0, resFbb.GetBufferPointer(), resFbb.GetSize(), localFbb.GetBufferPointer(), localFbb.GetSize()); 61 Akonadi2::EntityBuffer::assembleEntityBuffer(fbb, 0, 0, resFbb.GetBufferPointer(), resFbb.GetSize(), localFbb.GetBufferPointer(), localFbb.GetSize());
diff --git a/dummyresource/domainadaptor.h b/dummyresource/domainadaptor.h
index 39028c9..9d351e7 100644
--- a/dummyresource/domainadaptor.h
+++ b/dummyresource/domainadaptor.h
@@ -5,14 +5,10 @@
5#include "dummycalendar_generated.h" 5#include "dummycalendar_generated.h"
6#include "entity_generated.h" 6#include "entity_generated.h"
7 7
8class DummyEventAdaptorFactory : public DomainTypeAdaptorFactory<Akonadi2::ApplicationDomain::Event, Akonadi2::ApplicationDomain::Buffer::Event, DummyCalendar::DummyEvent> 8class DummyEventAdaptorFactory : public DomainTypeAdaptorFactory<Akonadi2::ApplicationDomain::Event, Akonadi2::ApplicationDomain::Buffer::Event, DummyCalendar::DummyEvent, Akonadi2::ApplicationDomain::Buffer::EventBuilder, DummyCalendar::DummyEventBuilder>
9{ 9{
10public: 10public:
11 DummyEventAdaptorFactory(); 11 DummyEventAdaptorFactory();
12 virtual ~DummyEventAdaptorFactory() {}; 12 virtual ~DummyEventAdaptorFactory() {};
13 virtual void createBuffer(const Akonadi2::ApplicationDomain::Event &event, flatbuffers::FlatBufferBuilder &fbb); 13 virtual void createBuffer(const Akonadi2::ApplicationDomain::Event &event, flatbuffers::FlatBufferBuilder &fbb);
14
15private:
16 QSharedPointer<WritePropertyMapper<DummyCalendar::DummyEventBuilder> > mResourceWriteMapper;
17 QSharedPointer<WritePropertyMapper<Akonadi2::ApplicationDomain::Buffer::EventBuilder> > mLocalWriteMapper;
18}; 14};
diff --git a/dummyresource/facade.cpp b/dummyresource/facade.cpp
index af8c187..1477fcf 100644
--- a/dummyresource/facade.cpp
+++ b/dummyresource/facade.cpp
@@ -122,7 +122,7 @@ void DummyResourceFacade::readValue(QSharedPointer<Akonadi2::Storage> storage, c
122 const auto localBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::ApplicationDomain::Buffer::Event>(buffer.entity().local()); 122 const auto localBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::ApplicationDomain::Buffer::Event>(buffer.entity().local());
123 const auto metadataBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::Metadata>(buffer.entity().metadata()); 123 const auto metadataBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::Metadata>(buffer.entity().metadata());
124 124
125 if (!resourceBuffer || !metadataBuffer) { 125 if ((!resourceBuffer && !localBuffer) || !metadataBuffer) {
126 qWarning() << "invalid buffer " << QByteArray::fromRawData(static_cast<char*>(keyValue), keySize); 126 qWarning() << "invalid buffer " << QByteArray::fromRawData(static_cast<char*>(keyValue), keySize);
127 return true; 127 return true;
128 } 128 }
diff --git a/dummyresource/facade.h b/dummyresource/facade.h
index 7bef2cc..37ed81d 100644
--- a/dummyresource/facade.h
+++ b/dummyresource/facade.h
@@ -41,5 +41,5 @@ public:
41 41
42private: 42private:
43 void readValue(QSharedPointer<Akonadi2::Storage> storage, const QByteArray &key, const std::function<void(const Akonadi2::ApplicationDomain::Event::Ptr &)> &resultCallback, std::function<bool(const std::string &key, DummyCalendar::DummyEvent const *buffer, Akonadi2::ApplicationDomain::Buffer::Event const *local)>); 43 void readValue(QSharedPointer<Akonadi2::Storage> storage, const QByteArray &key, const std::function<void(const Akonadi2::ApplicationDomain::Event::Ptr &)> &resultCallback, std::function<bool(const std::string &key, DummyCalendar::DummyEvent const *buffer, Akonadi2::ApplicationDomain::Buffer::Event const *local)>);
44 QSharedPointer<DomainTypeAdaptorFactory<Akonadi2::ApplicationDomain::Event, Akonadi2::ApplicationDomain::Buffer::Event, DummyCalendar::DummyEvent> > mFactory; 44 QSharedPointer<DomainTypeAdaptorFactory<Akonadi2::ApplicationDomain::Event, Akonadi2::ApplicationDomain::Buffer::Event, DummyCalendar::DummyEvent, Akonadi2::ApplicationDomain::Buffer::EventBuilder, DummyCalendar::DummyEventBuilder> > mFactory;
45}; 45};