1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
/*
Copyright (c) 2015 Christian Mollekopf <mollekopf@kolabsys.com>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
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 Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#pragma once
#include "resultset.h"
#include "storage.h"
#include "query.h"
#include "log.h"
#include "indexer.h"
#include "storage/key.h"
#include <QByteArray>
namespace Sink {
namespace Storage {
class EntityStore;
}
}
class TypeIndex
{
public:
TypeIndex(const QByteArray &type, const Sink::Log::Context &);
void addProperty(const QByteArray &property);
//FIXME We currently simply serialize based on the QVariant we get and ignore the index type
template <typename T>
void addProperty(const QByteArray &property)
{
addProperty(property);
}
template <typename T>
void addSortedProperty(const QByteArray &property);
template <typename T, typename S>
void addPropertyWithSorting(const QByteArray &property, const QByteArray &sortProperty);
template <typename T, typename S>
void addPropertyWithSorting()
{
addPropertyWithSorting<typename T::Type, typename S::Type>(T::name, S::name);
}
template <typename T>
void addProperty()
{
addProperty<typename T::Type>(T::name);
}
template <typename T>
void addSortedProperty()
{
addSortedProperty<typename T::Type>(T::name);
}
template <typename Left, typename Right>
void addSecondaryProperty()
{
mSecondaryProperties.insert(Left::name, Right::name);
}
template <typename Left, typename Right, typename CustomIndexer>
void addSecondaryPropertyIndexer()
{
mCustomIndexer << CustomIndexer::Ptr::create();
}
template <typename Begin, typename End>
void addSampledPeriodIndex(const QByteArray &beginProperty, const QByteArray &endProperty);
template <typename Begin, typename End>
void addSampledPeriodIndex()
{
addSampledPeriodIndex<typename Begin::Type, typename End::Type>(Begin::name, End::name);
}
void add(const Sink::Storage::Identifier &identifier, const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Storage::DataStore::Transaction &transaction, const QByteArray &resourceInstanceId);
void modify(const Sink::Storage::Identifier &identifier, const Sink::ApplicationDomain::ApplicationDomainType &oldEntity, const Sink::ApplicationDomain::ApplicationDomainType &newEntity, Sink::Storage::DataStore::Transaction &transaction, const QByteArray &resourceInstanceId);
void remove(const Sink::Storage::Identifier &identifier, const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Storage::DataStore::Transaction &transaction, const QByteArray &resourceInstanceId);
QVector<Sink::Storage::Identifier> query(const Sink::QueryBase &query, QSet<QByteArrayList> &appliedFilters, QByteArray &appliedSorting, Sink::Storage::DataStore::Transaction &transaction, const QByteArray &resourceInstanceId);
QVector<Sink::Storage::Identifier> lookup(const QByteArray &property, const QVariant &value, Sink::Storage::DataStore::Transaction &transaction);
template <typename Left, typename Right>
QVector<QByteArray> secondaryLookup(const QVariant &value)
{
return secondaryLookup<typename Left::Type>(Left::name, Right::name, value);
}
template <typename Type>
QVector<QByteArray> secondaryLookup(const QByteArray &leftName, const QByteArray &rightName, const QVariant &value);
template <typename Left, typename Right>
void index(const QVariant &leftValue, const QVariant &rightValue, Sink::Storage::DataStore::Transaction &transaction)
{
index<typename Left::Type, typename Right::Type>(Left::name, Right::name, leftValue, rightValue, transaction);
}
template <typename LeftType, typename RightType>
void index(const QByteArray &leftName, const QByteArray &rightName, const QVariant &leftValue, const QVariant &rightValue, Sink::Storage::DataStore::Transaction &transaction);
template <typename Left, typename Right>
void unindex(const QVariant &leftValue, const QVariant &rightValue, Sink::Storage::DataStore::Transaction &transaction)
{
index<typename Left::Type, typename Right::Type>(Left::name, Right::name, leftValue, rightValue, transaction);
}
template <typename LeftType, typename RightType>
void unindex(const QByteArray &leftName, const QByteArray &rightName, const QVariant &leftValue, const QVariant &rightValue, Sink::Storage::DataStore::Transaction &transaction);
void commitTransaction();
void abortTransaction();
enum Action {
Add,
Remove
};
private:
friend class Sink::Storage::EntityStore;
void updateIndex(Action action, const Sink::Storage::Identifier &identifier, const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Storage::DataStore::Transaction &transaction, const QByteArray &resourceInstanceId);
QByteArray indexName(const QByteArray &property, const QByteArray &sortProperty = QByteArray()) const;
QByteArray sortedIndexName(const QByteArray &property) const;
QByteArray sampledPeriodIndexName(const QByteArray &rangeBeginProperty, const QByteArray &rangeEndProperty) const;
Sink::Log::Context mLogCtx;
QByteArray mType;
QByteArrayList mProperties;
QByteArrayList mSortedProperties;
QMap<QByteArray, QByteArray> mGroupedSortedProperties;
//<Property, ResultProperty>
QMap<QByteArray, QByteArray> mSecondaryProperties;
QSet<QPair<QByteArray, QByteArray>> mSampledPeriodProperties;
QList<Sink::Indexer::Ptr> mCustomIndexer;
Sink::Storage::DataStore::Transaction *mTransaction;
QHash<QByteArray, std::function<void(Action, const Sink::Storage::Identifier &identifier, const QVariant &value, Sink::Storage::DataStore::Transaction &transaction)>> mIndexer;
QHash<QByteArray, std::function<void(Action, const Sink::Storage::Identifier &identifier, const QVariant &value, Sink::Storage::DataStore::Transaction &transaction)>> mSortIndexer;
QHash<QByteArray, std::function<void(Action, const Sink::Storage::Identifier &identifier, const QVariant &value, const QVariant &sortValue, Sink::Storage::DataStore::Transaction &transaction)>> mGroupedSortIndexer;
QHash<QPair<QByteArray, QByteArray>, std::function<void(Action, const Sink::Storage::Identifier &identifier, const QVariant &begin, const QVariant &end, Sink::Storage::DataStore::Transaction &transaction)>> mSampledPeriodIndexer;
};
|