summaryrefslogtreecommitdiffstats
path: root/common/typeindex.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2017-02-11 17:57:27 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2017-02-13 19:42:39 +0100
commit335251d0420523ac8b9997c802f6850c427aaf01 (patch)
treea2d8ce0b941568c071e498683c6f1c2563ce2626 /common/typeindex.cpp
parentc2f3b5bae5a32d5b3573ed8256bf45231631751a (diff)
downloadsink-335251d0420523ac8b9997c802f6850c427aaf01.tar.gz
sink-335251d0420523ac8b9997c802f6850c427aaf01.zip
Fixed index removals
Diffstat (limited to 'common/typeindex.cpp')
-rw-r--r--common/typeindex.cpp70
1 files changed, 40 insertions, 30 deletions
diff --git a/common/typeindex.cpp b/common/typeindex.cpp
index a65c676..a0d6d43 100644
--- a/common/typeindex.cpp
+++ b/common/typeindex.cpp
@@ -72,9 +72,13 @@ QByteArray TypeIndex::indexName(const QByteArray &property, const QByteArray &so
72template <> 72template <>
73void TypeIndex::addProperty<QByteArray>(const QByteArray &property) 73void TypeIndex::addProperty<QByteArray>(const QByteArray &property)
74{ 74{
75 auto indexer = [this, property](const QByteArray &identifier, const QVariant &value, Sink::Storage::DataStore::Transaction &transaction) { 75 auto indexer = [this, property](bool add, const QByteArray &identifier, const QVariant &value, Sink::Storage::DataStore::Transaction &transaction) {
76 // SinkTraceCtx(mLogCtx) << "Indexing " << mType + ".index." + property << value.toByteArray(); 76 // SinkTraceCtx(mLogCtx) << "Indexing " << mType + ".index." + property << value.toByteArray();
77 Index(indexName(property), transaction).add(getByteArray(value), identifier); 77 if (add) {
78 Index(indexName(property), transaction).add(getByteArray(value), identifier);
79 } else {
80 Index(indexName(property), transaction).remove(getByteArray(value), identifier);
81 }
78 }; 82 };
79 mIndexer.insert(property, indexer); 83 mIndexer.insert(property, indexer);
80 mProperties << property; 84 mProperties << property;
@@ -83,9 +87,13 @@ void TypeIndex::addProperty<QByteArray>(const QByteArray &property)
83template <> 87template <>
84void TypeIndex::addProperty<QString>(const QByteArray &property) 88void TypeIndex::addProperty<QString>(const QByteArray &property)
85{ 89{
86 auto indexer = [this, property](const QByteArray &identifier, const QVariant &value, Sink::Storage::DataStore::Transaction &transaction) { 90 auto indexer = [this, property](bool add, const QByteArray &identifier, const QVariant &value, Sink::Storage::DataStore::Transaction &transaction) {
87 // SinkTraceCtx(mLogCtx) << "Indexing " << mType + ".index." + property << value.toByteArray(); 91 // SinkTraceCtx(mLogCtx) << "Indexing " << mType + ".index." + property << value.toByteArray();
88 Index(indexName(property), transaction).add(getByteArray(value), identifier); 92 if (add) {
93 Index(indexName(property), transaction).add(getByteArray(value), identifier);
94 } else {
95 Index(indexName(property), transaction).remove(getByteArray(value), identifier);
96 }
89 }; 97 };
90 mIndexer.insert(property, indexer); 98 mIndexer.insert(property, indexer);
91 mProperties << property; 99 mProperties << property;
@@ -94,9 +102,13 @@ void TypeIndex::addProperty<QString>(const QByteArray &property)
94template <> 102template <>
95void TypeIndex::addProperty<QDateTime>(const QByteArray &property) 103void TypeIndex::addProperty<QDateTime>(const QByteArray &property)
96{ 104{
97 auto indexer = [this, property](const QByteArray &identifier, const QVariant &value, Sink::Storage::DataStore::Transaction &transaction) { 105 auto indexer = [this, property](bool add, const QByteArray &identifier, const QVariant &value, Sink::Storage::DataStore::Transaction &transaction) {
98 //SinkTraceCtx(mLogCtx) << "Indexing " << mType + ".index." + property << getByteArray(value); 106 //SinkTraceCtx(mLogCtx) << "Indexing " << mType + ".index." + property << getByteArray(value);
99 Index(indexName(property), transaction).add(getByteArray(value), identifier); 107 if (add) {
108 Index(indexName(property), transaction).add(getByteArray(value), identifier);
109 } else {
110 Index(indexName(property), transaction).remove(getByteArray(value), identifier);
111 }
100 }; 112 };
101 mIndexer.insert(property, indexer); 113 mIndexer.insert(property, indexer);
102 mProperties << property; 114 mProperties << property;
@@ -111,10 +123,15 @@ void TypeIndex::addProperty<ApplicationDomain::Reference>(const QByteArray &prop
111template <> 123template <>
112void TypeIndex::addPropertyWithSorting<QByteArray, QDateTime>(const QByteArray &property, const QByteArray &sortProperty) 124void TypeIndex::addPropertyWithSorting<QByteArray, QDateTime>(const QByteArray &property, const QByteArray &sortProperty)
113{ 125{
114 auto indexer = [=](const QByteArray &identifier, const QVariant &value, const QVariant &sortValue, Sink::Storage::DataStore::Transaction &transaction) { 126 auto indexer = [=](bool add, const QByteArray &identifier, const QVariant &value, const QVariant &sortValue, Sink::Storage::DataStore::Transaction &transaction) {
115 const auto date = sortValue.toDateTime(); 127 const auto date = sortValue.toDateTime();
116 const auto propertyValue = getByteArray(value); 128 const auto propertyValue = getByteArray(value);
117 Index(indexName(property, sortProperty), transaction).add(propertyValue + toSortableByteArray(date), identifier); 129 SinkWarning() << "Adding sorted value " << indexName(property, sortProperty);
130 if (add) {
131 Index(indexName(property, sortProperty), transaction).add(propertyValue + toSortableByteArray(date), identifier);
132 } else {
133 Index(indexName(property, sortProperty), transaction).remove(propertyValue + toSortableByteArray(date), identifier);
134 }
118 }; 135 };
119 mSortIndexer.insert(property + sortProperty, indexer); 136 mSortIndexer.insert(property + sortProperty, indexer);
120 mSortedProperties.insert(property, sortProperty); 137 mSortedProperties.insert(property, sortProperty);
@@ -126,45 +143,38 @@ void TypeIndex::addPropertyWithSorting<ApplicationDomain::Reference, QDateTime>(
126 addPropertyWithSorting<QByteArray, QDateTime>(property, sortProperty); 143 addPropertyWithSorting<QByteArray, QDateTime>(property, sortProperty);
127} 144}
128 145
129void TypeIndex::add(const QByteArray &identifier, const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Storage::DataStore::Transaction &transaction) 146void TypeIndex::updateIndex(bool add, const QByteArray &identifier, const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Storage::DataStore::Transaction &transaction)
130{ 147{
131 SinkTrace() << "add " << identifier;
132 for (const auto &property : mProperties) { 148 for (const auto &property : mProperties) {
133 const auto value = entity.getProperty(property); 149 const auto value = entity.getProperty(property);
134 auto indexer = mIndexer.value(property); 150 auto indexer = mIndexer.value(property);
135 indexer(identifier, value, transaction); 151 indexer(add, identifier, value, transaction);
136 } 152 }
137 for (auto it = mSortedProperties.constBegin(); it != mSortedProperties.constEnd(); it++) { 153 for (auto it = mSortedProperties.constBegin(); it != mSortedProperties.constEnd(); it++) {
138 const auto value = entity.getProperty(it.key()); 154 const auto value = entity.getProperty(it.key());
139 const auto sortValue = entity.getProperty(it.value()); 155 const auto sortValue = entity.getProperty(it.value());
140 auto indexer = mSortIndexer.value(it.key() + it.value()); 156 auto indexer = mSortIndexer.value(it.key() + it.value());
141 indexer(identifier, value, sortValue, transaction); 157 indexer(add, identifier, value, sortValue, transaction);
142 } 158 }
143 for (const auto &indexer : mCustomIndexer) { 159 for (const auto &indexer : mCustomIndexer) {
144 indexer->setup(this, &transaction); 160 indexer->setup(this, &transaction);
145 indexer->add(entity); 161 if (add) {
162 indexer->add(entity);
163 } else {
164 indexer->remove(entity);
165 }
146 } 166 }
167
168}
169
170void TypeIndex::add(const QByteArray &identifier, const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Storage::DataStore::Transaction &transaction)
171{
172 updateIndex(true, identifier, entity, transaction);
147} 173}
148 174
149void TypeIndex::remove(const QByteArray &identifier, const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Storage::DataStore::Transaction &transaction) 175void TypeIndex::remove(const QByteArray &identifier, const Sink::ApplicationDomain::ApplicationDomainType &entity, Sink::Storage::DataStore::Transaction &transaction)
150{ 176{
151 for (const auto &property : mProperties) { 177 updateIndex(false, identifier, entity, transaction);
152 const auto value = entity.getProperty(property);
153 Index(indexName(property), transaction).remove(getByteArray(value), identifier);
154 }
155 for (auto it = mSortedProperties.constBegin(); it != mSortedProperties.constEnd(); it++) {
156 const auto propertyValue = entity.getProperty(it.key());
157 const auto sortValue = entity.getProperty(it.value());
158 if (sortValue.type() == QVariant::DateTime) {
159 Index(indexName(it.key(), it.value()), transaction).remove(propertyValue.toByteArray() + toSortableByteArray(sortValue.toDateTime()), identifier);
160 } else {
161 Index(indexName(it.key(), it.value()), transaction).remove(propertyValue.toByteArray() + sortValue.toByteArray(), identifier);
162 }
163 }
164 for (const auto &indexer : mCustomIndexer) {
165 indexer->setup(this, &transaction);
166 indexer->remove(entity);
167 }
168} 178}
169 179
170static QVector<QByteArray> indexLookup(Index &index, QueryBase::Comparator filter) 180static QVector<QByteArray> indexLookup(Index &index, QueryBase::Comparator filter)