diff options
Diffstat (limited to 'examples/dummyresource/resourcefactory.cpp')
-rw-r--r-- | examples/dummyresource/resourcefactory.cpp | 89 |
1 files changed, 66 insertions, 23 deletions
diff --git a/examples/dummyresource/resourcefactory.cpp b/examples/dummyresource/resourcefactory.cpp index 8e6bd42..0a2e90b 100644 --- a/examples/dummyresource/resourcefactory.cpp +++ b/examples/dummyresource/resourcefactory.cpp | |||
@@ -48,31 +48,76 @@ static void index(const QByteArray &index, const QVariant &value, const QByteArr | |||
48 | } | 48 | } |
49 | } | 49 | } |
50 | 50 | ||
51 | DummyResource::DummyResource(const QByteArray &instanceIdentifier, const QSharedPointer<Akonadi2::Pipeline> &pipeline) | 51 | /** |
52 | : Akonadi2::GenericResource(instanceIdentifier, pipeline) | 52 | * Index types: |
53 | { | 53 | * * uid - property |
54 | auto eventFactory = QSharedPointer<DummyEventAdaptorFactory>::create(); | 54 | * |
55 | const auto resourceIdentifier = mResourceInstanceIdentifier; | 55 | * * Property can be: |
56 | * * fixed value like uid | ||
57 | * * fixed value where we want to do smaller/greater-than comparisons. (like start date) | ||
58 | * * range indexes like what date range an event affects. | ||
59 | * * group indexes like tree hierarchies as nested sets | ||
60 | */ | ||
61 | template<typename DomainType> | ||
62 | class IndexUpdater : public Akonadi2::Preprocessor { | ||
63 | public: | ||
64 | IndexUpdater(const QByteArray &index, const QByteArray &type) | ||
65 | :mIndexIdentifier(index), | ||
66 | mBufferType(type) | ||
67 | { | ||
56 | 68 | ||
57 | auto eventIndexer = new Akonadi2::SimpleProcessor("eventIndexer", [eventFactory, resourceIdentifier](const Akonadi2::PipelineState &state, const Akonadi2::Entity &entity, Akonadi2::Storage::Transaction &transaction) { | 69 | } |
58 | Akonadi2::ApplicationDomain::Event event(resourceIdentifier, Akonadi2::Storage::uidFromKey(state.key()), -1, eventFactory->createAdaptor(entity)); | 70 | |
59 | Akonadi2::ApplicationDomain::TypeImplementation<Akonadi2::ApplicationDomain::Event>::index(event, transaction); | 71 | void newEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &newEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE |
60 | index("event.index.rid", event.getProperty("remoteId"), event.identifier(), transaction); | 72 | { |
61 | }); | 73 | Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::index(uid, newEntity, transaction); |
74 | add(newEntity.getProperty("remoteId"), uid, transaction); | ||
75 | } | ||
62 | 76 | ||
63 | mPipeline->setPreprocessors(ENTITY_TYPE_EVENT, Akonadi2::Pipeline::NewPipeline, QVector<Akonadi2::Preprocessor*>() << eventIndexer); | 77 | void modifiedEntity(const QByteArray &key, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &oldEntity, const Akonadi2::ApplicationDomain::BufferAdaptor &newEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE |
64 | mPipeline->setAdaptorFactory(ENTITY_TYPE_EVENT, eventFactory); | 78 | { |
65 | //TODO cleanup indexes during removal | 79 | } |
66 | 80 | ||
81 | void deletedEntity(const QByteArray &key, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &oldEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE | ||
67 | { | 82 | { |
68 | auto mailFactory = QSharedPointer<DummyMailAdaptorFactory>::create(); | 83 | } |
69 | auto mailIndexer = new Akonadi2::SimpleProcessor("mailIndexer", [mailFactory, resourceIdentifier](const Akonadi2::PipelineState &state, const Akonadi2::Entity &entity, Akonadi2::Storage::Transaction &transaction) { | 84 | private: |
70 | Akonadi2::ApplicationDomain::Mail mail(resourceIdentifier, Akonadi2::Storage::uidFromKey(state.key()), -1, mailFactory->createAdaptor(entity)); | 85 | void add(const QVariant &value, const QByteArray &uid, Akonadi2::Storage::Transaction &transaction) |
71 | Akonadi2::ApplicationDomain::TypeImplementation<Akonadi2::ApplicationDomain::Mail>::index(mail, transaction); | 86 | { |
72 | index("mail.index.rid", mail.getProperty("remoteId"), mail.identifier(), transaction); | 87 | if (value.isValid()) { |
73 | }); | 88 | Index(mIndexIdentifier, transaction).add(value.toByteArray(), uid); |
89 | } | ||
90 | } | ||
91 | |||
92 | void remove(const QByteArray &uid, Akonadi2::Storage::Transaction &transaction) | ||
93 | { | ||
94 | //Knowning the indexed value would probably help removing the uid efficiently. Otherwise we have to execute a full scan. | ||
95 | // Index(mIndexIdentifier, transaction).remove(uid); | ||
96 | } | ||
97 | |||
98 | void modify(Akonadi2::Storage::Transaction &transaction) | ||
99 | { | ||
100 | //Knowning the indexed value would probably help removing the uid efficiently. Otherwise we have to execute a full scan. | ||
101 | // Index(mIndexIdentifier, transaction).remove(uid); | ||
102 | } | ||
74 | 103 | ||
75 | mPipeline->setPreprocessors(ENTITY_TYPE_MAIL, Akonadi2::Pipeline::NewPipeline, QVector<Akonadi2::Preprocessor*>() << mailIndexer); | 104 | QByteArray mIndexIdentifier; |
105 | QByteArray mBufferType; | ||
106 | }; | ||
107 | |||
108 | DummyResource::DummyResource(const QByteArray &instanceIdentifier, const QSharedPointer<Akonadi2::Pipeline> &pipeline) | ||
109 | : Akonadi2::GenericResource(instanceIdentifier, pipeline) | ||
110 | { | ||
111 | { | ||
112 | auto eventFactory = QSharedPointer<DummyEventAdaptorFactory>::create(); | ||
113 | auto eventIndexer = new IndexUpdater<Akonadi2::ApplicationDomain::Event>("event.index.rid", ENTITY_TYPE_EVENT); | ||
114 | mPipeline->setPreprocessors(ENTITY_TYPE_EVENT, QVector<Akonadi2::Preprocessor*>() << eventIndexer); | ||
115 | mPipeline->setAdaptorFactory(ENTITY_TYPE_EVENT, eventFactory); | ||
116 | } | ||
117 | { | ||
118 | auto mailFactory = QSharedPointer<DummyMailAdaptorFactory>::create(); | ||
119 | auto mailIndexer = new IndexUpdater<Akonadi2::ApplicationDomain::Mail>("mail.index.rid", ENTITY_TYPE_MAIL); | ||
120 | mPipeline->setPreprocessors(ENTITY_TYPE_MAIL, QVector<Akonadi2::Preprocessor*>() << mailIndexer); | ||
76 | mPipeline->setAdaptorFactory(ENTITY_TYPE_MAIL, mailFactory); | 121 | mPipeline->setAdaptorFactory(ENTITY_TYPE_MAIL, mailFactory); |
77 | } | 122 | } |
78 | } | 123 | } |
@@ -171,9 +216,7 @@ KAsync::Job<void> DummyResource::synchronizeWithSource() | |||
171 | 216 | ||
172 | void DummyResource::removeFromDisk(const QByteArray &instanceIdentifier) | 217 | void DummyResource::removeFromDisk(const QByteArray &instanceIdentifier) |
173 | { | 218 | { |
174 | Akonadi2::Storage(Akonadi2::storageLocation(), instanceIdentifier, Akonadi2::Storage::ReadWrite).removeFromDisk(); | 219 | GenericResource::removeFromDisk(instanceIdentifier); |
175 | Akonadi2::Storage(Akonadi2::storageLocation(), instanceIdentifier + ".userqueue", Akonadi2::Storage::ReadWrite).removeFromDisk(); | ||
176 | Akonadi2::Storage(Akonadi2::storageLocation(), instanceIdentifier + ".synchronizerqueue", Akonadi2::Storage::ReadWrite).removeFromDisk(); | ||
177 | Akonadi2::Storage(Akonadi2::storageLocation(), instanceIdentifier + ".event.index.uid", Akonadi2::Storage::ReadWrite).removeFromDisk(); | 220 | Akonadi2::Storage(Akonadi2::storageLocation(), instanceIdentifier + ".event.index.uid", Akonadi2::Storage::ReadWrite).removeFromDisk(); |
178 | } | 221 | } |
179 | 222 | ||