From 0279a63a420b89a5ac7287ff8cb2ba2ead0d9a83 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Mon, 7 Dec 2015 08:03:38 +0100 Subject: Ported folder and event to TypeIndex --- common/domain/event.cpp | 35 ++++++++++++++--------------------- common/domain/folder.cpp | 47 ++++++++++++++++------------------------------- common/typeindex.cpp | 28 ++++++++++++++++++++-------- 3 files changed, 50 insertions(+), 60 deletions(-) (limited to 'common') diff --git a/common/domain/event.cpp b/common/domain/event.cpp index 3036d8e..d989efe 100644 --- a/common/domain/event.cpp +++ b/common/domain/event.cpp @@ -29,42 +29,35 @@ #include "../propertymapper.h" #include "../query.h" #include "../definitions.h" +#include "../typeindex.h" #include "event_generated.h" using namespace Akonadi2::ApplicationDomain; -ResultSet TypeImplementation::queryIndexes(const Akonadi2::Query &query, const QByteArray &resourceInstanceIdentifier, QSet &appliedFilters, Akonadi2::Storage::Transaction &transaction) +static TypeIndex &getIndex() { - QVector keys; - if (query.propertyFilter.contains("uid")) { - Index uidIndex("event.index.uid", transaction); - uidIndex.lookup(query.propertyFilter.value("uid").toByteArray(), [&](const QByteArray &value) { - keys << value; - }, - [](const Index::Error &error) { - Warning() << "Error in uid index: " << error.message; - }); - appliedFilters << "uid"; + static TypeIndex *index = 0; + if (!index) { + index = new TypeIndex("event"); + index->addProperty("uid"); } - Trace() << "Index lookup found " << keys.size() << " keys."; - return ResultSet(keys); + return *index; +} + +ResultSet TypeImplementation::queryIndexes(const Akonadi2::Query &query, const QByteArray &resourceInstanceIdentifier, QSet &appliedFilters, Akonadi2::Storage::Transaction &transaction) +{ + return getIndex().query(query, appliedFilters, transaction); } void TypeImplementation::index(const QByteArray &identifier, const BufferAdaptor &bufferAdaptor, Akonadi2::Storage::Transaction &transaction) { - const auto uid = bufferAdaptor.getProperty("uid"); - if (uid.isValid()) { - Index("event.index.uid", transaction).add(uid.toByteArray(), identifier); - } + return getIndex().add(identifier, bufferAdaptor, transaction); } void TypeImplementation::removeIndex(const QByteArray &identifier, const BufferAdaptor &bufferAdaptor, Akonadi2::Storage::Transaction &transaction) { - const auto uid = bufferAdaptor.getProperty("uid"); - if (uid.isValid()) { - Index("event.index.uid", transaction).remove(uid.toByteArray(), identifier); - } + return getIndex().remove(identifier, bufferAdaptor, transaction); } QSharedPointer::Buffer> > TypeImplementation::initializeReadPropertyMapper() diff --git a/common/domain/folder.cpp b/common/domain/folder.cpp index 80e9f8f..3cd221e 100644 --- a/common/domain/folder.cpp +++ b/common/domain/folder.cpp @@ -29,52 +29,37 @@ #include "../propertymapper.h" #include "../query.h" #include "../definitions.h" +#include "../typeindex.h" #include "folder_generated.h" using namespace Akonadi2::ApplicationDomain; -ResultSet TypeImplementation::queryIndexes(const Akonadi2::Query &query, const QByteArray &resourceInstanceIdentifier, QSet &appliedFilters, Akonadi2::Storage::Transaction &transaction) +static TypeIndex &getIndex() { - QVector keys; - if (query.propertyFilter.contains("parent")) { - Index index("folder.index.parent", transaction); - auto lookupKey = query.propertyFilter.value("parent").toByteArray(); - if (lookupKey.isEmpty()) { - lookupKey = "toplevel"; - } - index.lookup(lookupKey, [&](const QByteArray &value) { - keys << value; - }, - [](const Index::Error &error) { - Warning() << "Error in uid index: " << error.message; - }); - appliedFilters << "parent"; + static TypeIndex *index = 0; + if (!index) { + index = new TypeIndex("folder"); + index->addProperty("parent"); + index->addProperty("name"); } - Trace() << "Index lookup found " << keys.size() << " keys."; - return ResultSet(keys); + return *index; +} + +ResultSet TypeImplementation::queryIndexes(const Akonadi2::Query &query, const QByteArray &resourceInstanceIdentifier, QSet &appliedFilters, Akonadi2::Storage::Transaction &transaction) +{ + return getIndex().query(query, appliedFilters, transaction); } void TypeImplementation::index(const QByteArray &identifier, const BufferAdaptor &bufferAdaptor, Akonadi2::Storage::Transaction &transaction) { - const auto parent = bufferAdaptor.getProperty("parent"); - Trace() << "indexing " << identifier << " with parent " << parent.toByteArray(); - if (parent.isValid()) { - Q_ASSERT(!parent.toByteArray().isEmpty()); - Index("folder.index.parent", transaction).add(parent.toByteArray(), identifier); - } else { - Index("folder.index.parent", transaction).add("toplevel", identifier); - } + Trace() << "Indexing " << identifier; + getIndex().add(identifier, bufferAdaptor, transaction); } void TypeImplementation::removeIndex(const QByteArray &identifier, const BufferAdaptor &bufferAdaptor, Akonadi2::Storage::Transaction &transaction) { - const auto parent = bufferAdaptor.getProperty("parent"); - if (parent.isValid()) { - Index("folder.index.parent", transaction).remove(parent.toByteArray(), identifier); - } else { - Index("folder.index.parent", transaction).remove("toplevel", identifier); - } + getIndex().remove(identifier, bufferAdaptor, transaction); } QSharedPointer::Buffer> > TypeImplementation::initializeReadPropertyMapper() diff --git a/common/typeindex.cpp b/common/typeindex.cpp index 0a0dc33..1e41267 100644 --- a/common/typeindex.cpp +++ b/common/typeindex.cpp @@ -32,7 +32,11 @@ void TypeIndex::addProperty(const QByteArray &property) { auto indexer = [this, property](const QByteArray &identifier, const QVariant &value, Akonadi2::Storage::Transaction &transaction) { // Trace() << "Indexing " << mType + ".index." + property << value.toByteArray(); - Index(mType + ".index." + property, transaction).add(value.toByteArray(), identifier); + if (value.isValid()) { + Index(mType + ".index." + property, transaction).add(value.toByteArray(), identifier); + } else { + Index(mType + ".index." + property, transaction).add("toplevel", identifier); + } }; mIndexer.insert(property, indexer); mProperties << property; @@ -43,7 +47,11 @@ void TypeIndex::addProperty(const QByteArray &property) { auto indexer = [this, property](const QByteArray &identifier, const QVariant &value, Akonadi2::Storage::Transaction &transaction) { // Trace() << "Indexing " << mType + ".index." + property << value.toByteArray(); - Index(mType + ".index." + property, transaction).add(value.toByteArray(), identifier); + if (value.isValid()) { + Index(mType + ".index." + property, transaction).add(value.toByteArray(), identifier); + } else { + Index(mType + ".index." + property, transaction).add("toplevel", identifier); + } }; mIndexer.insert(property, indexer); mProperties << property; @@ -64,10 +72,8 @@ void TypeIndex::add(const QByteArray &identifier, const Akonadi2::ApplicationDom { for (const auto &property : mProperties) { const auto value = bufferAdaptor.getProperty(property); - if (value.isValid()) { - auto indexer = mIndexer.value(property); - indexer(identifier, value, transaction); - } + auto indexer = mIndexer.value(property); + indexer(identifier, value, transaction); } } @@ -78,6 +84,8 @@ void TypeIndex::remove(const QByteArray &identifier, const Akonadi2::Application if (value.isValid()) { //FIXME don't always convert to byte array Index(mType + ".index." + property, transaction).remove(value.toByteArray(), identifier); + } else { + Index(mType + ".index." + property, transaction).remove("toplevel", identifier); } } } @@ -88,7 +96,11 @@ ResultSet TypeIndex::query(const Akonadi2::Query &query, QSet &appli for (const auto &property : mProperties) { if (query.propertyFilter.contains(property)) { Index index(mType + ".index." + property, transaction); - index.lookup(query.propertyFilter.value(property).toByteArray(), [&](const QByteArray &value) { + auto lookupKey = query.propertyFilter.value(property).toByteArray(); + if (lookupKey.isEmpty()) { + lookupKey = "toplevel"; + } + index.lookup(lookupKey, [&](const QByteArray &value) { keys << value; }, [property](const Index::Error &error) { @@ -96,7 +108,7 @@ ResultSet TypeIndex::query(const Akonadi2::Query &query, QSet &appli }); appliedFilters << property; } - Trace() << "Index lookup found keys: " << keys.size(); + Trace() << "Index lookup on " << property << " found " << keys.size() << " keys."; return ResultSet(keys); } Trace() << "No matching index"; -- cgit v1.2.3