summaryrefslogtreecommitdiffstats
path: root/framework/src/entitycache.h
diff options
context:
space:
mode:
Diffstat (limited to 'framework/src/entitycache.h')
-rw-r--r--framework/src/entitycache.h77
1 files changed, 77 insertions, 0 deletions
diff --git a/framework/src/entitycache.h b/framework/src/entitycache.h
new file mode 100644
index 00000000..485bcbdd
--- /dev/null
+++ b/framework/src/entitycache.h
@@ -0,0 +1,77 @@
1/*
2 Copyright (c) 2018 Christian Mollekopf <mollekopf@kolabsys.com>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19#pragma once
20
21#include "kube_export.h"
22#include <sink/query.h>
23#include <sink/store.h>
24#include <QSharedPointer>
25#include <QAbstractItemModel>
26
27class KUBE_EXPORT EntityCacheInterface
28{
29public:
30 typedef QSharedPointer<EntityCacheInterface> Ptr;
31 EntityCacheInterface() = default;
32 virtual ~EntityCacheInterface() = default;
33
34 virtual QVariant getProperty(const QByteArray &identifier, const QByteArray &property) const = 0;
35};
36
37template<typename DomainType, typename Property>
38class KUBE_EXPORT EntityCache : public EntityCacheInterface
39{
40public:
41 typedef QSharedPointer<EntityCache> Ptr;
42
43 EntityCache();
44 virtual ~EntityCache() = default;
45
46 virtual QVariant getProperty(const QByteArray &, const QByteArray &) const override;
47
48private:
49 QHash<QByteArray, typename DomainType::Ptr> mCache;
50 QSharedPointer<QAbstractItemModel> mModel;
51};
52
53template<typename DomainType, typename Property>
54EntityCache<DomainType, Property>::EntityCache()
55 : EntityCacheInterface()
56{
57 Sink::Query query;
58 query.request<Property>();
59 query.setFlags(Sink::Query::LiveQuery);
60 mModel = Sink::Store::loadModel<DomainType>(query);
61 QObject::connect(mModel.data(), &QAbstractItemModel::rowsInserted, mModel.data(), [this] (const QModelIndex &, int start, int end) {
62 for (int row = start; row <= end; row++) {
63 auto entity = mModel->index(row, 0, QModelIndex()).data(Sink::Store::DomainObjectRole).template value<typename DomainType::Ptr>();
64 mCache.insert(entity->identifier(), entity);
65 }
66 });
67}
68
69template<typename DomainType, typename Property>
70QVariant EntityCache<DomainType, Property>::getProperty(const QByteArray &identifier, const QByteArray &property) const
71{
72 if (auto entity = mCache.value(identifier)) {
73 return entity->getProperty(property);
74 }
75 return {};
76}
77