From 778b01181604dc2eae2013f2dc37db6d647b526a Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Mon, 8 May 2017 14:27:25 +0200 Subject: Gather required databases from index definitions. --- common/domain/typeimplementations.cpp | 78 +++++++++++++---- common/domain/typeimplementations.h | 5 ++ common/domain/typeimplementations_p.h | 154 ++++++++++++++++++++++++++++++++++ common/mail/threadindexer.cpp | 7 ++ common/mail/threadindexer.h | 1 + common/storage/entitystore.cpp | 73 +++++++++++----- 6 files changed, 278 insertions(+), 40 deletions(-) create mode 100644 common/domain/typeimplementations_p.h (limited to 'common') diff --git a/common/domain/typeimplementations.cpp b/common/domain/typeimplementations.cpp index a87e73d..4e61bad 100644 --- a/common/domain/typeimplementations.cpp +++ b/common/domain/typeimplementations.cpp @@ -28,6 +28,7 @@ #include "entity_generated.h" #include "mail/threadindexer.h" #include "domainadaptor.h" +#include "typeimplementations_p.h" using namespace Sink; using namespace Sink::ApplicationDomain; @@ -35,22 +36,43 @@ using namespace Sink::ApplicationDomain; #define SINK_REGISTER_SERIALIZER(MAPPER, ENTITYTYPE, PROPERTY, LOWERCASEPROPERTY) \ MAPPER.addMapping(&Sink::ApplicationDomain::Buffer::ENTITYTYPE::LOWERCASEPROPERTY, &Sink::ApplicationDomain::Buffer::ENTITYTYPE##Builder::add_##LOWERCASEPROPERTY); +typedef IndexConfig, + ValueIndex, + ValueIndex, + ValueIndex, + SortedIndex, + SecondaryIndex, + SecondaryIndex, + CustomSecondaryIndex + > MailIndexConfig; + +typedef IndexConfig, + ValueIndex + > FolderIndexConfig; + +typedef IndexConfig + > ContactIndexConfig; + +typedef IndexConfig + > AddressbookIndexConfig; + +typedef IndexConfig + > EventIndexConfig; + void TypeImplementation::configure(TypeIndex &index) { - // index.addProperty(); - /* index.addProperty(Mail::SenderName::name); */ - /* index->addProperty(Mail::Subject::name); */ - /* index->addFulltextProperty(Mail::Subject::name); */ - index.addProperty(); - index.addProperty(); - index.addPropertyWithSorting(); - index.addProperty(); - index.addProperty(); + MailIndexConfig::configure(index); +} - index.addSecondaryPropertyIndexer(); - index.addSecondaryProperty(); - index.addSecondaryProperty(); +QMap TypeImplementation::typeDatabases() +{ + return merge(QMap{{QByteArray{Mail::name} + ".main", 0}}, MailIndexConfig::databases()); } void TypeImplementation::configure(IndexPropertyMapper &indexPropertyMapper) @@ -85,10 +107,15 @@ void TypeImplementation::configure(PropertyMapper &propertyMapper) SINK_REGISTER_SERIALIZER(propertyMapper, Mail, ParentMessageId, parentMessageId); } + void TypeImplementation::configure(TypeIndex &index) { - index.addProperty(Folder::Parent::name); - index.addProperty(Folder::Name::name); + FolderIndexConfig::configure(index); +} + +QMap TypeImplementation::typeDatabases() +{ + return merge(QMap{{QByteArray{Folder::name} + ".main", 0}}, FolderIndexConfig::databases()); } void TypeImplementation::configure(PropertyMapper &propertyMapper) @@ -108,7 +135,12 @@ void TypeImplementation::configure(IndexPropertyMapper &) void TypeImplementation::configure(TypeIndex &index) { - index.addProperty(Contact::Uid::name); + ContactIndexConfig::configure(index); +} + +QMap TypeImplementation::typeDatabases() +{ + return merge(QMap{{QByteArray{Contact::name} + ".main", 0}}, ContactIndexConfig::databases()); } void TypeImplementation::configure(PropertyMapper &propertyMapper) @@ -130,8 +162,12 @@ void TypeImplementation::configure(IndexPropertyMapper &) void TypeImplementation::configure(TypeIndex &index) { - index.addProperty(Addressbook::Parent::name); - index.addProperty(Addressbook::Name::name); + AddressbookIndexConfig::configure(index); +} + +QMap TypeImplementation::typeDatabases() +{ + return merge(QMap{{QByteArray{Addressbook::name} + ".main", 0}}, AddressbookIndexConfig::databases()); } void TypeImplementation::configure(PropertyMapper &propertyMapper) @@ -148,7 +184,12 @@ void TypeImplementation::configure(IndexPropertyMapper &) void TypeImplementation::configure(TypeIndex &index) { - index.addProperty(Event::Uid::name); + EventIndexConfig::configure(index); +} + +QMap TypeImplementation::typeDatabases() +{ + return merge(QMap{{QByteArray{Event::name} + ".main", 0}}, EventIndexConfig::databases()); } void TypeImplementation::configure(PropertyMapper &propertyMapper) @@ -163,3 +204,4 @@ void TypeImplementation::configure(IndexPropertyMapper &) { } + diff --git a/common/domain/typeimplementations.h b/common/domain/typeimplementations.h index 7e4608c..d36dfc1 100644 --- a/common/domain/typeimplementations.h +++ b/common/domain/typeimplementations.h @@ -47,6 +47,7 @@ public: static void configure(TypeIndex &index); static void configure(PropertyMapper &propertyMapper); static void configure(IndexPropertyMapper &indexPropertyMapper); + static QMap typeDatabases(); }; template<> @@ -57,6 +58,7 @@ public: static void configure(TypeIndex &); static void configure(PropertyMapper &); static void configure(IndexPropertyMapper &indexPropertyMapper); + static QMap typeDatabases(); }; template<> @@ -67,6 +69,7 @@ public: static void configure(TypeIndex &); static void configure(PropertyMapper &); static void configure(IndexPropertyMapper &indexPropertyMapper); + static QMap typeDatabases(); }; template<> @@ -77,6 +80,7 @@ public: static void configure(TypeIndex &); static void configure(PropertyMapper &); static void configure(IndexPropertyMapper &indexPropertyMapper); + static QMap typeDatabases(); }; template<> @@ -87,6 +91,7 @@ public: static void configure(TypeIndex &); static void configure(PropertyMapper &); static void configure(IndexPropertyMapper &indexPropertyMapper); + static QMap typeDatabases(); }; } diff --git a/common/domain/typeimplementations_p.h b/common/domain/typeimplementations_p.h new file mode 100644 index 0000000..b7a78ac --- /dev/null +++ b/common/domain/typeimplementations_p.h @@ -0,0 +1,154 @@ +/* + * Copyright (C) 2015 Christian Mollekopf + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "typeindex.h" +#include + +template +void mergeImpl(T &map, First f) +{ + for (auto it = f.constBegin(); it != f.constEnd(); it++) { + map.insert(it.key(), it.value()); + } +} + +template +void mergeImpl(T &map, First f, Tail ...maps) +{ + for (auto it = f.constBegin(); it != f.constEnd(); it++) { + map.insert(it.key(), it.value()); + } + mergeImpl(map, maps...); +} + +template +First merge(First f, Tail ...maps) +{ + First map; + mergeImpl(f, maps...); + return map; +} + +template +class ValueIndex +{ +public: + static void configure(TypeIndex &index) + { + index.addProperty(); + } + + template + static QMap databases() + { + return {{QByteArray{EntityType::name} +".index." + Property::name, 1}}; + } +}; + + +template +class SortedIndex +{ +public: + static void configure(TypeIndex &index) + { + index.addPropertyWithSorting(); + } + + template + static QMap databases() + { + return {{QByteArray{EntityType::name} +".index." + Property::name + ".sort." + SortProperty::name, 1}}; + } +}; + +template +class SecondaryIndex +{ +public: + static void configure(TypeIndex &index) + { + index.addSecondaryProperty(); + } + + template + static QMap databases() + { + return {{QByteArray{EntityType::name} +".index." + Property::name + SecondaryProperty::name, 1}}; + } +}; + +template +class CustomSecondaryIndex +{ +public: + static void configure(TypeIndex &index) + { + index.addSecondaryPropertyIndexer(); + } + + template + static QMap databases() + { + return Indexer::databases(); + } +}; + +template +class IndexConfig +{ + template + static void applyIndex(TypeIndex &index) + { + T::configure(index); + } + + ///Apply recursively for parameter pack + template + static void applyIndex(TypeIndex &index) + { + applyIndex(index); + applyIndex(index); + } + + template + static QMap getDbs() + { + return T::template databases(); + } + + template + static QMap getDbs() + { + return merge(getDbs(), getDbs()); + } + +public: + static void configure(TypeIndex &index) + { + applyIndex(index); + } + + static QMap databases() + { + return getDbs(); + } + +}; + diff --git a/common/mail/threadindexer.cpp b/common/mail/threadindexer.cpp index d91ab5f..af96b94 100644 --- a/common/mail/threadindexer.cpp +++ b/common/mail/threadindexer.cpp @@ -145,3 +145,10 @@ void ThreadIndexer::remove(const ApplicationDomain::ApplicationDomainType &entit } +QMap ThreadIndexer::databases() +{ + return {{"mail.index.messageIdthreadId", 1}, + {"mail.index.subjectthreadId", 1}, + {"mail.index.threadIdmessageId", 1}}; +} + diff --git a/common/mail/threadindexer.h b/common/mail/threadindexer.h index 064ae71..60d0863 100644 --- a/common/mail/threadindexer.h +++ b/common/mail/threadindexer.h @@ -29,6 +29,7 @@ public: virtual void add(const ApplicationDomain::ApplicationDomainType &entity) Q_DECL_OVERRIDE; virtual void modify(const ApplicationDomain::ApplicationDomainType &old, const ApplicationDomain::ApplicationDomainType &entity) Q_DECL_OVERRIDE; virtual void remove(const ApplicationDomain::ApplicationDomainType &entity) Q_DECL_OVERRIDE; + static QMap databases(); private: void updateThreadingIndex(const QByteArray &identifier, const ApplicationDomain::ApplicationDomainType &entity, Sink::Storage::DataStore::Transaction &transaction); }; diff --git a/common/storage/entitystore.cpp b/common/storage/entitystore.cpp index 9539bec..38ff730 100644 --- a/common/storage/entitystore.cpp +++ b/common/storage/entitystore.cpp @@ -36,30 +36,59 @@ using namespace Sink; using namespace Sink::Storage; +static QMap baseDbs() +{ + return {{"revisionType", 0}, + {"revisions", 0}, + {"uids", 0}, + {"default", 0}, + {"__flagtable", 0}}; +} + +template +void mergeImpl(T &map, First f) +{ + for (auto it = f.constBegin(); it != f.constEnd(); it++) { + map.insert(it.key(), it.value()); + } +} + +template +void mergeImpl(T &map, First f, Tail ...maps) +{ + for (auto it = f.constBegin(); it != f.constEnd(); it++) { + map.insert(it.key(), it.value()); + } + mergeImpl(map, maps...); +} + +template +First merge(First f, Tail ...maps) +{ + First map; + mergeImpl(f, maps...); + return map; +} + +template +struct DbLayoutHelper { + void operator()(QMap map) const { + mergeImpl(map, ApplicationDomain::TypeImplementation::typeDatabases()); + } +}; + static Sink::Storage::DbLayout dbLayout(const QByteArray &instanceId) { - return Sink::Storage::DbLayout { - instanceId, - { - {"folder.main", 0}, - {"folder.index.name", 1}, - {"folder.index.parent", 1}, - {"mail.main", 0}, - {"mail.index.date", 1}, - {"mail.index.folder", 1}, - {"mail.index.folder.sort.date", 0}, - {"mail.index.messageId", 1}, - {"mail.index.messageIdthreadId", 1}, - {"mail.index.parentMessageId", 1}, - {"mail.index.subjectthreadId", 1}, - {"mail.index.threadIdmessageId", 1}, - {"revisionType", 0}, - {"revisions", 0}, - {"uids", 0}, - {"default", 0}, - {"__flagtable", 0} - } - }; + static auto databases = [] { + QMap map; + mergeImpl(map, ApplicationDomain::TypeImplementation::typeDatabases()); + mergeImpl(map, ApplicationDomain::TypeImplementation::typeDatabases()); + mergeImpl(map, ApplicationDomain::TypeImplementation::typeDatabases()); + mergeImpl(map, ApplicationDomain::TypeImplementation::typeDatabases()); + mergeImpl(map, ApplicationDomain::TypeImplementation::typeDatabases()); + return merge(baseDbs(), map); + }(); + return {instanceId, databases}; } -- cgit v1.2.3