diff options
author | Geir Storli <geirst@oath.com> | 2018-06-08 14:05:44 +0000 |
---|---|---|
committer | Geir Storli <geirst@oath.com> | 2018-06-08 14:26:52 +0000 |
commit | e424d13893a85185c4ce07deeea04e5050660d7b (patch) | |
tree | 35d1a96c51b8c12bdd2133609fed73bc0d047386 | |
parent | 29649971ad58d3c24f6393a06b063b346d1be96b (diff) |
Add common function to extract an array from a struct collection.
-rw-r--r-- | searchcore/src/vespa/searchcore/proton/attribute/document_field_extractor.cpp | 66 |
1 files changed, 29 insertions, 37 deletions
diff --git a/searchcore/src/vespa/searchcore/proton/attribute/document_field_extractor.cpp b/searchcore/src/vespa/searchcore/proton/attribute/document_field_extractor.cpp index 5bef5604e42..1aa398a23d5 100644 --- a/searchcore/src/vespa/searchcore/proton/attribute/document_field_extractor.cpp +++ b/searchcore/src/vespa/searchcore/proton/attribute/document_field_extractor.cpp @@ -164,27 +164,6 @@ DocumentFieldExtractor::getSimpleFieldValue(const FieldPath &fieldPath) return _doc.getNestedFieldValue(fieldPath.getFullRange()); } -std::unique_ptr<FieldValue> -DocumentFieldExtractor::getStructArrayFieldValue(const FieldPath &fieldPath) -{ - const auto outerFieldValue = getCachedFieldValue(fieldPath[0]); - if (outerFieldValue != nullptr && checkInherits(*outerFieldValue, ArrayFieldValue::classId)) { - const auto outerArray = static_cast<const ArrayFieldValue *>(outerFieldValue); - const auto &innerFieldPathEntry = fieldPath[1]; - auto array = makeArray(innerFieldPathEntry, outerArray->size()); - uint32_t arrayIndex = 0; - for (const auto &outerElemBase : *outerArray) { - auto &arrayElem = (*array)[arrayIndex++]; - const auto &structElem = static_cast<const StructFieldValue &>(outerElemBase); - if (!structElem.getValue(innerFieldPathEntry.getFieldRef(), arrayElem)) { - arrayElem.accept(setUndefinedValueVisitor); - } - } - return array; - } - return std::unique_ptr<FieldValue>(); -} - namespace { template <typename ExtractorFunc> @@ -203,6 +182,33 @@ getMapFieldValue(const FieldValue *outerFieldValue, const FieldPathEntry &innerE return std::unique_ptr<FieldValue>(); } +template <typename CollectionFieldValueT, typename ExtractorFunc> +std::unique_ptr<FieldValue> +extractArrayFromStructCollection(const FieldValue *outerFieldValue, const FieldPathEntry &innerEntry, ExtractorFunc &&extractor) +{ + if (outerFieldValue != nullptr && checkInherits(*outerFieldValue, CollectionFieldValueT::classId)) { + const auto *outerCollection = static_cast<const CollectionFieldValueT *>(outerFieldValue); + auto array = makeArray(innerEntry, outerCollection->size()); + uint32_t arrayIndex = 0; + for (const auto &outerElem : *outerCollection) { + auto &arrayElem = (*array)[arrayIndex++]; + const auto &structElem = static_cast<const StructFieldValue &>(*extractor(&outerElem)); + if (!structElem.getValue(innerEntry.getFieldRef(), arrayElem)) { + arrayElem.accept(setUndefinedValueVisitor); + } + } + return array; + } + return std::unique_ptr<FieldValue>(); +} + +} + +std::unique_ptr<FieldValue> +DocumentFieldExtractor::getStructArrayFieldValue(const FieldPath &fieldPath) +{ + return extractArrayFromStructCollection<ArrayFieldValue>(getCachedFieldValue(fieldPath[0]), fieldPath[1], + [](const auto *elem){ return elem; }); } std::unique_ptr<FieldValue> @@ -222,22 +228,8 @@ DocumentFieldExtractor::getPrimitiveMapFieldValue(const FieldPath &fieldPath) std::unique_ptr<document::FieldValue> DocumentFieldExtractor::getStructMapFieldValue(const FieldPath &fieldPath) { - const auto outerFieldValue = getCachedFieldValue(fieldPath[0]); - if (outerFieldValue != nullptr && checkInherits(*outerFieldValue, MapFieldValue::classId)) { - const auto outerMap = static_cast<const MapFieldValue *>(outerFieldValue); - const auto &innerFieldPathEntry = fieldPath[2]; - auto array = makeArray(innerFieldPathEntry, outerMap->size()); - uint32_t arrayIndex = 0; - for (const auto &mapElem : *outerMap) { - auto &arrayElem = (*array)[arrayIndex++]; - const auto &structElem = static_cast<const StructFieldValue &>(*mapElem.second); - if (!structElem.getValue(innerFieldPathEntry.getFieldRef(), arrayElem)) { - arrayElem.accept(setUndefinedValueVisitor); - } - } - return array; - } - return std::unique_ptr<FieldValue>(); + return extractArrayFromStructCollection<MapFieldValue>(getCachedFieldValue(fieldPath[0]), fieldPath[2], + [](const auto *elem){ return elem->second; }); } std::unique_ptr<FieldValue> |