summaryrefslogtreecommitdiffstats
path: root/common/typeindex.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2016-09-26 11:58:38 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2016-09-26 11:58:38 +0200
commitbe8dba1827ec54ec11d9a3ef143db9ad7f7f38df (patch)
tree4c441927d9e1851126143e5f8170d48fa87df17b /common/typeindex.cpp
parent52ad48c8bd755a2fde249296d6017853538f478f (diff)
downloadsink-be8dba1827ec54ec11d9a3ef143db9ad7f7f38df.tar.gz
sink-be8dba1827ec54ec11d9a3ef143db9ad7f7f38df.zip
The threading reduction is working.
Diffstat (limited to 'common/typeindex.cpp')
-rw-r--r--common/typeindex.cpp68
1 files changed, 68 insertions, 0 deletions
diff --git a/common/typeindex.cpp b/common/typeindex.cpp
index 1b04966..f537493 100644
--- a/common/typeindex.cpp
+++ b/common/typeindex.cpp
@@ -168,3 +168,71 @@ QVector<QByteArray> TypeIndex::query(const Sink::Query &query, QSet<QByteArray>
168 SinkTrace() << "No matching index"; 168 SinkTrace() << "No matching index";
169 return keys; 169 return keys;
170} 170}
171
172QVector<QByteArray> TypeIndex::lookup(const QByteArray &property, const QVariant &value, Sink::Storage::Transaction &transaction)
173{
174 SinkTrace() << "Index lookup on property: " << property << mSecondaryProperties.keys() << mProperties;
175 if (mProperties.contains(property)) {
176 QVector<QByteArray> keys;
177 Index index(indexName(property), transaction);
178 const auto lookupKey = getByteArray(value);
179 index.lookup(
180 lookupKey, [&](const QByteArray &value) { keys << value; }, [property](const Index::Error &error) { SinkWarning() << "Error in index: " << error.message << property; });
181 SinkTrace() << "Index lookup on " << property << " found " << keys.size() << " keys.";
182 return keys;
183 } else if (mSecondaryProperties.contains(property)) {
184 //Lookups on secondary indexes first lookup the key, and then lookup the results again to resolve to entity id's
185 QVector<QByteArray> keys;
186 auto resultProperty = mSecondaryProperties.value(property);
187
188 QVector<QByteArray> secondaryKeys;
189 Index index(indexName(property + resultProperty), transaction);
190 const auto lookupKey = getByteArray(value);
191 index.lookup(
192 lookupKey, [&](const QByteArray &value) { secondaryKeys << value; }, [property](const Index::Error &error) { SinkWarning() << "Error in index: " << error.message << property; });
193 SinkTrace() << "Looked up secondary keys: " << secondaryKeys;
194 for (const auto &secondary : secondaryKeys) {
195 keys += lookup(resultProperty, secondary, transaction);
196 }
197 return keys;
198 } else {
199 SinkWarning() << "Tried to lookup " << property << " but couldn't find value";
200 }
201 return QVector<QByteArray>();
202}
203
204template <>
205void TypeIndex::index<QByteArray, QByteArray>(const QByteArray &leftName, const QByteArray &rightName, const QVariant &leftValue, const QVariant &rightValue, Sink::Storage::Transaction &transaction)
206{
207 Index(indexName(leftName + rightName), transaction).add(getByteArray(leftValue), getByteArray(rightValue));
208}
209
210template <>
211void TypeIndex::index<QString, QByteArray>(const QByteArray &leftName, const QByteArray &rightName, const QVariant &leftValue, const QVariant &rightValue, Sink::Storage::Transaction &transaction)
212{
213 Index(indexName(leftName + rightName), transaction).add(getByteArray(leftValue), getByteArray(rightValue));
214}
215
216template <>
217QVector<QByteArray> TypeIndex::secondaryLookup<QByteArray>(const QByteArray &leftName, const QByteArray &rightName, const QVariant &value, Sink::Storage::Transaction &transaction)
218{
219 QVector<QByteArray> keys;
220 Index index(indexName(leftName + rightName), transaction);
221 const auto lookupKey = getByteArray(value);
222 index.lookup(
223 lookupKey, [&](const QByteArray &value) { keys << value; }, [=](const Index::Error &error) { SinkWarning() << "Error in index: " << error.message << value; });
224
225 return keys;
226}
227
228template <>
229QVector<QByteArray> TypeIndex::secondaryLookup<QString>(const QByteArray &leftName, const QByteArray &rightName, const QVariant &value, Sink::Storage::Transaction &transaction)
230{
231 QVector<QByteArray> keys;
232 Index index(indexName(leftName + rightName), transaction);
233 const auto lookupKey = getByteArray(value);
234 index.lookup(
235 lookupKey, [&](const QByteArray &value) { keys << value; }, [=](const Index::Error &error) { SinkWarning() << "Error in index: " << error.message << value; });
236
237 return keys;
238}