diff options
Diffstat (limited to 'common/typeindex.cpp')
-rw-r--r-- | common/typeindex.cpp | 68 |
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 | |||
172 | QVector<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 | |||
204 | template <> | ||
205 | void 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 | |||
210 | template <> | ||
211 | void 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 | |||
216 | template <> | ||
217 | QVector<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 | |||
228 | template <> | ||
229 | QVector<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 | } | ||