From 0991561c9630fcb89b9666b8d57c2b7ea592fec6 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Wed, 10 Feb 2016 10:47:12 +0100 Subject: Moved Store to separate file --- common/CMakeLists.txt | 2 + common/clientapi.cpp | 210 +--------------------------------------- common/clientapi.h | 64 +------------ common/store.cpp | 259 ++++++++++++++++++++++++++++++++++++++++++++++++++ common/store.h | 101 ++++++++++++++++++++ 5 files changed, 365 insertions(+), 271 deletions(-) create mode 100644 common/store.cpp create mode 100644 common/store.h diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index a165820..02335ad 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -33,6 +33,7 @@ set(storage_SRCS storage_lmdb.cpp) set(storage_LIBS lmdb) set(command_SRCS + store.cpp modelresult.cpp definitions.cpp log.cpp @@ -102,6 +103,7 @@ install(TARGETS ${PROJECT_NAME} add_clang_static_analysis(${PROJECT_NAME}) install(FILES + store.h clientapi.h domain/applicationdomaintype.h query.h diff --git a/common/clientapi.cpp b/common/clientapi.cpp index cbbfdd8..01411c2 100644 --- a/common/clientapi.cpp +++ b/common/clientapi.cpp @@ -46,115 +46,6 @@ namespace Sink { -QString Store::storageLocation() -{ - return Sink::storageLocation(); -} - -static QList getResources(const QList &resourceFilter, const QByteArray &type) -{ - //Return the global resource (signified by an empty name) for types that don't eblong to a specific resource - if (type == "sinkresource") { - return QList() << ""; - } - QList resources; - const auto configuredResources = ResourceConfig::getResources(); - if (resourceFilter.isEmpty()) { - for (const auto &res : configuredResources.keys()) { - //TODO filter by entity type - resources << res; - } - } else { - for (const auto &res : resourceFilter) { - if (configuredResources.contains(res)) { - resources << res; - } else { - qWarning() << "Resource is not existing: " << res; - } - } - } - Trace() << "Found resources: " << resources; - return resources; -} - -template -QSharedPointer Store::loadModel(Query query) -{ - Trace() << "Query: "; - Trace() << " Requested: " << query.requestedProperties; - Trace() << " Filter: " << query.propertyFilter; - Trace() << " Parent: " << query.parentProperty; - Trace() << " Ids: " << query.ids; - Trace() << " IsLive: " << query.liveQuery; - auto model = QSharedPointer >::create(query, query.requestedProperties); - - //* Client defines lifetime of model - //* The model lifetime defines the duration of live-queries - //* The facade needs to life for the duration of any calls being made (assuming we get rid of any internal callbacks - //* The emitter needs to live or the duration of query (respectively, the model) - //* The result provider needs to live for as long as results are provided (until the last thread exits). - - // Query all resources and aggregate results - auto resources = getResources(query.resources, ApplicationDomain::getTypeName()); - auto aggregatingEmitter = AggregatingResultEmitter::Ptr::create(); - model->setEmitter(aggregatingEmitter); - KAsync::iterate(resources) - .template each([query, aggregatingEmitter](const QByteArray &resource, KAsync::Future &future) { - auto facade = FacadeFactory::instance().getFacade(resourceName(resource), resource); - if (facade) { - Trace() << "Trying to fetch from resource " << resource; - auto result = facade->load(query); - aggregatingEmitter->addEmitter(result.second); - result.first.template then([&future](){future.setFinished();}).exec(); - } else { - Trace() << "Couldn' find a facade for " << resource; - //Ignore the error and carry on - future.setFinished(); - } - }).exec(); - model->fetchMore(QModelIndex()); - - return model; -} - -template -static std::shared_ptr > getFacade(const QByteArray &resourceInstanceIdentifier) -{ - if (auto facade = FacadeFactory::instance().getFacade(resourceName(resourceInstanceIdentifier), resourceInstanceIdentifier)) { - return facade; - } - return std::make_shared >(); -} - -template -KAsync::Job Store::create(const DomainType &domainObject) { - //Potentially move to separate thread as well - auto facade = getFacade(domainObject.resourceInstanceIdentifier()); - return facade->create(domainObject).template then([facade](){}, [](int errorCode, const QString &error) { - Warning() << "Failed to create"; - }); -} - -template -KAsync::Job Store::modify(const DomainType &domainObject) -{ - //Potentially move to separate thread as well - auto facade = getFacade(domainObject.resourceInstanceIdentifier()); - return facade->modify(domainObject).template then([facade](){}, [](int errorCode, const QString &error) { - Warning() << "Failed to modify"; - }); -} - -template -KAsync::Job Store::remove(const DomainType &domainObject) -{ - //Potentially move to separate thread as well - auto facade = getFacade(domainObject.resourceInstanceIdentifier()); - return facade->remove(domainObject).template then([facade](){}, [](int errorCode, const QString &error) { - Warning() << "Failed to remove"; - }); -} - KAsync::Job ResourceControl::shutdown(const QByteArray &identifier) { Trace() << "shutdown " << identifier; @@ -190,37 +81,6 @@ KAsync::Job ResourceControl::start(const QByteArray &identifier) }); } -KAsync::Job Store::removeDataFromDisk(const QByteArray &identifier) -{ - //All databases are going to become invalid, nuke the environments - //TODO: all clients should react to a notification the resource - Sink::Storage::clearEnv(); - Trace() << "Remove data from disk " << identifier; - auto time = QSharedPointer::create(); - time->start(); - auto resourceAccess = QSharedPointer::create(identifier); - resourceAccess->open(); - return resourceAccess->sendCommand(Sink::Commands::RemoveFromDiskCommand).then([resourceAccess, time]() { - Trace() << "Remove from disk complete." << Log::TraceTime(time->elapsed()); - }); -} - -KAsync::Job Store::synchronize(const Sink::Query &query) -{ - Trace() << "synchronize" << query.resources; - return KAsync::iterate(query.resources) - .template each([query](const QByteArray &resource, KAsync::Future &future) { - Trace() << "Synchronizing " << resource; - auto resourceAccess = QSharedPointer::create(resource); - resourceAccess->open(); - resourceAccess->synchronizeResource(true, false).then([&future, resourceAccess]() { - future.setFinished(); - }).exec(); - }) - //FIXME JOBAPI this is only required because we don't care about the return value of each (and each shouldn't even have a return value) - .template then([](){}); -} - KAsync::Job ResourceControl::flushMessageQueue(const QByteArrayList &resourceIdentifier) { Trace() << "flushMessageQueue" << resourceIdentifier; @@ -242,67 +102,6 @@ KAsync::Job ResourceControl::flushReplayQueue(const QByteArrayList &resour return flushMessageQueue(resourceIdentifier); } -template -KAsync::Job Store::fetchOne(const Sink::Query &query) -{ - return KAsync::start([query](KAsync::Future &future) { - //FIXME We could do this more elegantly if composed jobs would have the correct type (In that case we'd simply return the value from then continuation, and could avoid the outer job entirely) - fetch(query, 1) - .template then >([&future](const QList &list){ - future.setValue(*list.first()); - future.setFinished(); - }, [&future](int errorCode, const QString &errorMessage) { - future.setError(errorCode, errorMessage); - future.setFinished(); - }).exec(); - }); -} - -template -KAsync::Job > Store::fetchAll(const Sink::Query &query) -{ - return fetch(query); -} - -template -KAsync::Job > Store::fetch(const Sink::Query &query, int minimumAmount) -{ - auto model = loadModel(query); - auto list = QSharedPointer >::create(); - auto context = QSharedPointer::create(); - return KAsync::start >([model, list, context, minimumAmount](KAsync::Future > &future) { - if (model->rowCount() >= 1) { - for (int i = 0; i < model->rowCount(); i++) { - list->append(model->index(i, 0, QModelIndex()).data(Sink::Store::DomainObjectRole).template value()); - } - } else { - QObject::connect(model.data(), &QAbstractItemModel::rowsInserted, context.data(), [model, &future, list](const QModelIndex &index, int start, int end) { - for (int i = start; i <= end; i++) { - list->append(model->index(i, 0, QModelIndex()).data(Sink::Store::DomainObjectRole).template value()); - } - }); - QObject::connect(model.data(), &QAbstractItemModel::dataChanged, context.data(), [model, &future, list, minimumAmount](const QModelIndex &, const QModelIndex &, const QVector &roles) { - if (roles.contains(ModelResult::ChildrenFetchedRole)) { - if (list->size() < minimumAmount) { - future.setError(1, "Not enough values."); - } else { - future.setValue(*list); - } - future.setFinished(); - } - }); - } - if (model->data(QModelIndex(), ModelResult::ChildrenFetchedRole).toBool()) { - if (list->size() < minimumAmount) { - future.setError(1, "Not enough values."); - } else { - future.setValue(*list); - } - future.setFinished(); - } - }); -} - template KAsync::Job ResourceControl::inspect(const Inspection &inspectionCommand) { @@ -371,14 +170,7 @@ void Notifier::registerHandler(std::function handler d->handler << handler; } -#define REGISTER_TYPE(T) template KAsync::Job Store::remove(const T &domainObject); \ - template KAsync::Job Store::create(const T &domainObject); \ - template KAsync::Job Store::modify(const T &domainObject); \ - template QSharedPointer Store::loadModel(Query query); \ - template KAsync::Job ResourceControl::inspect(const Inspection &); \ - template KAsync::Job Store::fetchOne(const Query &); \ - template KAsync::Job > Store::fetchAll(const Query &); \ - template KAsync::Job > Store::fetch(const Query &, int); \ +#define REGISTER_TYPE(T) template KAsync::Job ResourceControl::inspect(const Inspection &); \ REGISTER_TYPE(ApplicationDomain::Event); REGISTER_TYPE(ApplicationDomain::Mail); diff --git a/common/clientapi.h b/common/clientapi.h index 2b49826..a1d2469 100644 --- a/common/clientapi.h +++ b/common/clientapi.h @@ -26,6 +26,8 @@ #include +#include "store.h" + #include "query.h" #include "inspection.h" #include "applicationdomaintype.h" @@ -49,68 +51,6 @@ private: }; -/** - * Store interface used in the client API. - */ -namespace Store { - -QString SINK_EXPORT storageLocation(); - -enum Roles { - DomainObjectRole = Qt::UserRole + 1, //Must be the same as in ModelResult - ChildrenFetchedRole, - DomainObjectBaseRole -}; - -/** - * Asynchronusly load a dataset with tree structure information - */ -template -QSharedPointer SINK_EXPORT loadModel(Query query); - -/** - * Create a new entity. - */ -template -KAsync::Job SINK_EXPORT create(const DomainType &domainObject); - -/** - * Modify an entity. - * - * This includes moving etc. since these are also simple settings on a property. - */ -template -KAsync::Job SINK_EXPORT modify(const DomainType &domainObject); - -/** - * Remove an entity. - */ -template -KAsync::Job SINK_EXPORT remove(const DomainType &domainObject); - -/** - * Synchronize data to local cache. - */ -KAsync::Job SINK_EXPORT synchronize(const Sink::Query &query); - -/** - * Removes all resource data from disk. - * - * This will not touch the configuration. All commands that that arrived at the resource before this command will be dropped. All commands that arrived later will be executed. - */ -KAsync::Job SINK_EXPORT removeDataFromDisk(const QByteArray &resourceIdentifier); - -template -KAsync::Job SINK_EXPORT fetchOne(const Sink::Query &query); - -template -KAsync::Job > SINK_EXPORT fetchAll(const Sink::Query &query); - -template -KAsync::Job > SINK_EXPORT fetch(const Sink::Query &query, int minimumAmount = 0); - -}; - namespace ResourceControl { template diff --git a/common/store.cpp b/common/store.cpp new file mode 100644 index 0000000..7f93202 --- /dev/null +++ b/common/store.cpp @@ -0,0 +1,259 @@ +/* + * Copyright (C) 2015 Christian Mollekopf + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) version 3, or any + * later version accepted by the membership of KDE e.V. (or its + * successor approved by the membership of KDE e.V.), which shall + * act as a proxy defined in Section 6 of version 3 of the license. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + */ + +#include "store.h" + +#include +#include +#include +#include + +#include "resourceaccess.h" +#include "commands.h" +#include "resourcefacade.h" +#include "definitions.h" +#include "resourceconfig.h" +#include "facadefactory.h" +#include "modelresult.h" +#include "storage.h" +#include "log.h" + +#undef DEBUG_AREA +#define DEBUG_AREA "client.store" + +namespace Sink +{ + +QString Store::storageLocation() +{ + return Sink::storageLocation(); +} + +static QList getResources(const QList &resourceFilter, const QByteArray &type) +{ + //Return the global resource (signified by an empty name) for types that don't eblong to a specific resource + if (type == "sinkresource") { + return QList() << ""; + } + QList resources; + const auto configuredResources = ResourceConfig::getResources(); + if (resourceFilter.isEmpty()) { + for (const auto &res : configuredResources.keys()) { + //TODO filter by entity type + resources << res; + } + } else { + for (const auto &res : resourceFilter) { + if (configuredResources.contains(res)) { + resources << res; + } else { + qWarning() << "Resource is not existing: " << res; + } + } + } + Trace() << "Found resources: " << resources; + return resources; +} + +template +QSharedPointer Store::loadModel(Query query) +{ + Trace() << "Query: "; + Trace() << " Requested: " << query.requestedProperties; + Trace() << " Filter: " << query.propertyFilter; + Trace() << " Parent: " << query.parentProperty; + Trace() << " Ids: " << query.ids; + Trace() << " IsLive: " << query.liveQuery; + auto model = QSharedPointer >::create(query, query.requestedProperties); + + //* Client defines lifetime of model + //* The model lifetime defines the duration of live-queries + //* The facade needs to life for the duration of any calls being made (assuming we get rid of any internal callbacks + //* The emitter needs to live or the duration of query (respectively, the model) + //* The result provider needs to live for as long as results are provided (until the last thread exits). + + // Query all resources and aggregate results + auto resources = getResources(query.resources, ApplicationDomain::getTypeName()); + auto aggregatingEmitter = AggregatingResultEmitter::Ptr::create(); + model->setEmitter(aggregatingEmitter); + KAsync::iterate(resources) + .template each([query, aggregatingEmitter](const QByteArray &resource, KAsync::Future &future) { + auto facade = FacadeFactory::instance().getFacade(resourceName(resource), resource); + if (facade) { + Trace() << "Trying to fetch from resource " << resource; + auto result = facade->load(query); + aggregatingEmitter->addEmitter(result.second); + result.first.template then([&future](){future.setFinished();}).exec(); + } else { + Trace() << "Couldn' find a facade for " << resource; + //Ignore the error and carry on + future.setFinished(); + } + }).exec(); + model->fetchMore(QModelIndex()); + + return model; +} + +template +static std::shared_ptr > getFacade(const QByteArray &resourceInstanceIdentifier) +{ + if (auto facade = FacadeFactory::instance().getFacade(resourceName(resourceInstanceIdentifier), resourceInstanceIdentifier)) { + return facade; + } + return std::make_shared >(); +} + +template +KAsync::Job Store::create(const DomainType &domainObject) { + //Potentially move to separate thread as well + auto facade = getFacade(domainObject.resourceInstanceIdentifier()); + return facade->create(domainObject).template then([facade](){}, [](int errorCode, const QString &error) { + Warning() << "Failed to create"; + }); +} + +template +KAsync::Job Store::modify(const DomainType &domainObject) +{ + //Potentially move to separate thread as well + auto facade = getFacade(domainObject.resourceInstanceIdentifier()); + return facade->modify(domainObject).template then([facade](){}, [](int errorCode, const QString &error) { + Warning() << "Failed to modify"; + }); +} + +template +KAsync::Job Store::remove(const DomainType &domainObject) +{ + //Potentially move to separate thread as well + auto facade = getFacade(domainObject.resourceInstanceIdentifier()); + return facade->remove(domainObject).template then([facade](){}, [](int errorCode, const QString &error) { + Warning() << "Failed to remove"; + }); +} + +KAsync::Job Store::removeDataFromDisk(const QByteArray &identifier) +{ + //All databases are going to become invalid, nuke the environments + //TODO: all clients should react to a notification the resource + Sink::Storage::clearEnv(); + Trace() << "Remove data from disk " << identifier; + auto time = QSharedPointer::create(); + time->start(); + auto resourceAccess = QSharedPointer::create(identifier); + resourceAccess->open(); + return resourceAccess->sendCommand(Sink::Commands::RemoveFromDiskCommand).then([resourceAccess, time]() { + Trace() << "Remove from disk complete." << Log::TraceTime(time->elapsed()); + }); +} + +KAsync::Job Store::synchronize(const Sink::Query &query) +{ + Trace() << "synchronize" << query.resources; + return KAsync::iterate(query.resources) + .template each([query](const QByteArray &resource, KAsync::Future &future) { + Trace() << "Synchronizing " << resource; + auto resourceAccess = QSharedPointer::create(resource); + resourceAccess->open(); + resourceAccess->synchronizeResource(true, false).then([&future, resourceAccess]() { + future.setFinished(); + }).exec(); + }) + //FIXME JOBAPI this is only required because we don't care about the return value of each (and each shouldn't even have a return value) + .template then([](){}); +} + +template +KAsync::Job Store::fetchOne(const Sink::Query &query) +{ + return KAsync::start([query](KAsync::Future &future) { + //FIXME We could do this more elegantly if composed jobs would have the correct type (In that case we'd simply return the value from then continuation, and could avoid the outer job entirely) + fetch(query, 1) + .template then >([&future](const QList &list){ + future.setValue(*list.first()); + future.setFinished(); + }, [&future](int errorCode, const QString &errorMessage) { + future.setError(errorCode, errorMessage); + future.setFinished(); + }).exec(); + }); +} + +template +KAsync::Job > Store::fetchAll(const Sink::Query &query) +{ + return fetch(query); +} + +template +KAsync::Job > Store::fetch(const Sink::Query &query, int minimumAmount) +{ + auto model = loadModel(query); + auto list = QSharedPointer >::create(); + auto context = QSharedPointer::create(); + return KAsync::start >([model, list, context, minimumAmount](KAsync::Future > &future) { + if (model->rowCount() >= 1) { + for (int i = 0; i < model->rowCount(); i++) { + list->append(model->index(i, 0, QModelIndex()).data(Sink::Store::DomainObjectRole).template value()); + } + } else { + QObject::connect(model.data(), &QAbstractItemModel::rowsInserted, context.data(), [model, &future, list](const QModelIndex &index, int start, int end) { + for (int i = start; i <= end; i++) { + list->append(model->index(i, 0, QModelIndex()).data(Sink::Store::DomainObjectRole).template value()); + } + }); + QObject::connect(model.data(), &QAbstractItemModel::dataChanged, context.data(), [model, &future, list, minimumAmount](const QModelIndex &, const QModelIndex &, const QVector &roles) { + if (roles.contains(ModelResult::ChildrenFetchedRole)) { + if (list->size() < minimumAmount) { + future.setError(1, "Not enough values."); + } else { + future.setValue(*list); + } + future.setFinished(); + } + }); + } + if (model->data(QModelIndex(), ModelResult::ChildrenFetchedRole).toBool()) { + if (list->size() < minimumAmount) { + future.setError(1, "Not enough values."); + } else { + future.setValue(*list); + } + future.setFinished(); + } + }); +} + +#define REGISTER_TYPE(T) template KAsync::Job Store::remove(const T &domainObject); \ + template KAsync::Job Store::create(const T &domainObject); \ + template KAsync::Job Store::modify(const T &domainObject); \ + template QSharedPointer Store::loadModel(Query query); \ + template KAsync::Job Store::fetchOne(const Query &); \ + template KAsync::Job > Store::fetchAll(const Query &); \ + template KAsync::Job > Store::fetch(const Query &, int); \ + +REGISTER_TYPE(ApplicationDomain::Event); +REGISTER_TYPE(ApplicationDomain::Mail); +REGISTER_TYPE(ApplicationDomain::Folder); +REGISTER_TYPE(ApplicationDomain::SinkResource); + +} // namespace Sink + diff --git a/common/store.h b/common/store.h new file mode 100644 index 0000000..6696833 --- /dev/null +++ b/common/store.h @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2015 Christian Mollekopf + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) version 3, or any + * later version accepted by the membership of KDE e.V. (or its + * successor approved by the membership of KDE e.V.), which shall + * act as a proxy defined in Section 6 of version 3 of the license. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . + */ + +#pragma once + +#include "sink_export.h" +#include +#include + +#include + +#include "query.h" +#include "applicationdomaintype.h" + +class QAbstractItemModel; + +namespace Sink { + +/** + * The unified Sink Store. + * + * This is the primary interface for clients to interact with Sink. + * It provides a unified store where all data provided by various resources can be accessed and modified. + */ +namespace Store { + +QString SINK_EXPORT storageLocation(); + +enum Roles { + DomainObjectRole = Qt::UserRole + 1, //Must be the same as in ModelResult + ChildrenFetchedRole, + DomainObjectBaseRole +}; + +/** + * Asynchronusly load a dataset with tree structure information + */ +template +QSharedPointer SINK_EXPORT loadModel(Query query); + +/** + * Create a new entity. + */ +template +KAsync::Job SINK_EXPORT create(const DomainType &domainObject); + +/** + * Modify an entity. + * + * This includes moving etc. since these are also simple settings on a property. + */ +template +KAsync::Job SINK_EXPORT modify(const DomainType &domainObject); + +/** + * Remove an entity. + */ +template +KAsync::Job SINK_EXPORT remove(const DomainType &domainObject); + +/** + * Synchronize data to local cache. + */ +KAsync::Job SINK_EXPORT synchronize(const Sink::Query &query); + +/** + * Removes all resource data from disk. + * + * This will not touch the configuration. All commands that that arrived at the resource before this command will be dropped. All commands that arrived later will be executed. + */ +KAsync::Job SINK_EXPORT removeDataFromDisk(const QByteArray &resourceIdentifier); + +template +KAsync::Job SINK_EXPORT fetchOne(const Sink::Query &query); + +template +KAsync::Job > SINK_EXPORT fetchAll(const Sink::Query &query); + +template +KAsync::Job > SINK_EXPORT fetch(const Sink::Query &query, int minimumAmount = 0); + + } +} + -- cgit v1.2.3