From be8dba1827ec54ec11d9a3ef143db9ad7f7f38df Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Mon, 26 Sep 2016 11:58:38 +0200 Subject: The threading reduction is working. --- common/typeindex.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) (limited to 'common/typeindex.cpp') 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 TypeIndex::query(const Sink::Query &query, QSet SinkTrace() << "No matching index"; return keys; } + +QVector TypeIndex::lookup(const QByteArray &property, const QVariant &value, Sink::Storage::Transaction &transaction) +{ + SinkTrace() << "Index lookup on property: " << property << mSecondaryProperties.keys() << mProperties; + if (mProperties.contains(property)) { + QVector keys; + Index index(indexName(property), transaction); + const auto lookupKey = getByteArray(value); + index.lookup( + lookupKey, [&](const QByteArray &value) { keys << value; }, [property](const Index::Error &error) { SinkWarning() << "Error in index: " << error.message << property; }); + SinkTrace() << "Index lookup on " << property << " found " << keys.size() << " keys."; + return keys; + } else if (mSecondaryProperties.contains(property)) { + //Lookups on secondary indexes first lookup the key, and then lookup the results again to resolve to entity id's + QVector keys; + auto resultProperty = mSecondaryProperties.value(property); + + QVector secondaryKeys; + Index index(indexName(property + resultProperty), transaction); + const auto lookupKey = getByteArray(value); + index.lookup( + lookupKey, [&](const QByteArray &value) { secondaryKeys << value; }, [property](const Index::Error &error) { SinkWarning() << "Error in index: " << error.message << property; }); + SinkTrace() << "Looked up secondary keys: " << secondaryKeys; + for (const auto &secondary : secondaryKeys) { + keys += lookup(resultProperty, secondary, transaction); + } + return keys; + } else { + SinkWarning() << "Tried to lookup " << property << " but couldn't find value"; + } + return QVector(); +} + +template <> +void TypeIndex::index(const QByteArray &leftName, const QByteArray &rightName, const QVariant &leftValue, const QVariant &rightValue, Sink::Storage::Transaction &transaction) +{ + Index(indexName(leftName + rightName), transaction).add(getByteArray(leftValue), getByteArray(rightValue)); +} + +template <> +void TypeIndex::index(const QByteArray &leftName, const QByteArray &rightName, const QVariant &leftValue, const QVariant &rightValue, Sink::Storage::Transaction &transaction) +{ + Index(indexName(leftName + rightName), transaction).add(getByteArray(leftValue), getByteArray(rightValue)); +} + +template <> +QVector TypeIndex::secondaryLookup(const QByteArray &leftName, const QByteArray &rightName, const QVariant &value, Sink::Storage::Transaction &transaction) +{ + QVector keys; + Index index(indexName(leftName + rightName), transaction); + const auto lookupKey = getByteArray(value); + index.lookup( + lookupKey, [&](const QByteArray &value) { keys << value; }, [=](const Index::Error &error) { SinkWarning() << "Error in index: " << error.message << value; }); + + return keys; +} + +template <> +QVector TypeIndex::secondaryLookup(const QByteArray &leftName, const QByteArray &rightName, const QVariant &value, Sink::Storage::Transaction &transaction) +{ + QVector keys; + Index index(indexName(leftName + rightName), transaction); + const auto lookupKey = getByteArray(value); + index.lookup( + lookupKey, [&](const QByteArray &value) { keys << value; }, [=](const Index::Error &error) { SinkWarning() << "Error in index: " << error.message << value; }); + + return keys; +} -- cgit v1.2.3