diff options
author | Tor Egge <Tor.Egge@oath.com> | 2018-01-19 13:11:59 +0000 |
---|---|---|
committer | Tor Egge <Tor.Egge@oath.com> | 2018-01-22 12:17:40 +0000 |
commit | 8deb17159a4be4666b4220896c26143c1d6234b8 (patch) | |
tree | 42d68834ac0abe80c848e2072d90985954fbab03 | |
parent | 4148debe89932119346b102a81164921af007d00 (diff) |
Add imported tensor attribute vector.
12 files changed, 221 insertions, 20 deletions
diff --git a/searchcore/src/tests/proton/attribute/attribute_manager/attribute_manager_test.cpp b/searchcore/src/tests/proton/attribute/attribute_manager/attribute_manager_test.cpp index 899dc146c8e..6da1ff890c9 100644 --- a/searchcore/src/tests/proton/attribute/attribute_manager/attribute_manager_test.cpp +++ b/searchcore/src/tests/proton/attribute/attribute_manager/attribute_manager_test.cpp @@ -24,6 +24,7 @@ LOG_SETUP("attribute_manager_test"); #include <vespa/searchlib/attribute/attributefactory.h> #include <vespa/searchlib/attribute/attributevector.hpp> #include <vespa/searchlib/attribute/imported_attribute_vector.h> +#include <vespa/searchlib/attribute/imported_attribute_vector_factory.h> #include <vespa/searchlib/attribute/integerbase.h> #include <vespa/searchlib/attribute/predicate_attribute.h> #include <vespa/searchlib/attribute/reference_attribute.h> @@ -58,6 +59,7 @@ using search::attribute::BasicType; using search::attribute::IAttributeContext; using search::attribute::IAttributeVector; using search::attribute::ImportedAttributeVector; +using search::attribute::ImportedAttributeVectorFactory; using search::attribute::ReferenceAttribute; using search::attribute::test::MockGidToLidMapperFactory; using search::index::DummyFileHeaderContext; @@ -137,7 +139,7 @@ struct ImportedAttributesRepoBuilder { refAttr->setGidToLidMapperFactory(std::make_shared<MockGidToLidMapperFactory>()); auto targetAttr = search::AttributeFactory::createAttribute(name + "_target", INT32_SINGLE); auto documentMetaStore = std::shared_ptr<search::IDocumentMetaStoreContext>(); - auto importedAttr = std::make_shared<ImportedAttributeVector>(name, refAttr, targetAttr, documentMetaStore, false); + auto importedAttr = ImportedAttributeVectorFactory::create(name, refAttr, targetAttr, documentMetaStore, false); _repo->add(name, importedAttr); } ImportedAttributesRepo::UP build() { diff --git a/searchcore/src/tests/proton/attribute/attribute_test.cpp b/searchcore/src/tests/proton/attribute/attribute_test.cpp index cb1df2df0c5..6e8191e7d40 100644 --- a/searchcore/src/tests/proton/attribute/attribute_test.cpp +++ b/searchcore/src/tests/proton/attribute/attribute_test.cpp @@ -25,6 +25,7 @@ LOG_SETUP("attribute_test"); #include <vespa/searchlib/attribute/attributevector.hpp> #include <vespa/searchlib/attribute/bitvector_search_cache.h> #include <vespa/searchlib/attribute/imported_attribute_vector.h> +#include <vespa/searchlib/attribute/imported_attribute_vector_factory.h> #include <vespa/searchlib/attribute/integerbase.h> #include <vespa/searchlib/attribute/predicate_attribute.h> #include <vespa/searchlib/attribute/singlenumericattribute.hpp> @@ -58,6 +59,7 @@ using search::TuneFileAttributes; using search::attribute::BitVectorSearchCache; using search::attribute::IAttributeVector; using search::attribute::ImportedAttributeVector; +using search::attribute::ImportedAttributeVectorFactory; using search::attribute::ReferenceAttribute; using search::index::DummyFileHeaderContext; using search::index::schema::CollectionType; @@ -744,11 +746,11 @@ TEST_F("require that attribute writer spreads write over 3 write contexts", Fixt ImportedAttributeVector::SP createImportedAttribute(const vespalib::string &name) { - auto result = std::make_shared<ImportedAttributeVector>(name, - ReferenceAttribute::SP(), - AttributeVector::SP(), - std::shared_ptr<search::IDocumentMetaStoreContext>(), - true); + auto result = ImportedAttributeVectorFactory::create(name, + ReferenceAttribute::SP(), + AttributeVector::SP(), + std::shared_ptr<search::IDocumentMetaStoreContext>(), + true); result->getSearchCache()->insert("foo", BitVectorSearchCache::Entry::SP()); return result; } diff --git a/searchcore/src/tests/proton/attribute/imported_attributes_context/imported_attributes_context_test.cpp b/searchcore/src/tests/proton/attribute/imported_attributes_context/imported_attributes_context_test.cpp index aac74e1583d..1bee7a79192 100644 --- a/searchcore/src/tests/proton/attribute/imported_attributes_context/imported_attributes_context_test.cpp +++ b/searchcore/src/tests/proton/attribute/imported_attributes_context/imported_attributes_context_test.cpp @@ -8,6 +8,7 @@ LOG_SETUP("imported_attributes_context_test"); #include <vespa/searchlib/attribute/attribute.h> #include <vespa/searchlib/attribute/attributefactory.h> #include <vespa/searchlib/attribute/imported_attribute_vector.h> +#include <vespa/searchlib/attribute/imported_attribute_vector_factory.h> #include <vespa/searchlib/test/mock_gid_to_lid_mapping.h> #include <future> @@ -17,6 +18,7 @@ using search::attribute::BasicType; using search::attribute::Config; using search::attribute::IAttributeVector; using search::attribute::ImportedAttributeVector; +using search::attribute::ImportedAttributeVectorFactory; using search::attribute::ReferenceAttribute; using search::attribute::test::MockGidToLidMapperFactory; using generation_t = AttributeVector::generation_t; @@ -72,11 +74,11 @@ struct Fixture { { } Fixture &addAttribute(const vespalib::string &name) { - auto attr = std::make_shared<ImportedAttributeVector>(name, - createReferenceAttribute(name + "_ref"), - createTargetAttribute(name + "_target"), - std::shared_ptr<search::IDocumentMetaStoreContext>(), - false); + auto attr = ImportedAttributeVectorFactory::create(name, + createReferenceAttribute(name + "_ref"), + createTargetAttribute(name + "_target"), + std::shared_ptr<search::IDocumentMetaStoreContext>(), + false); repo.add(name, attr); return *this; } diff --git a/searchcore/src/tests/proton/attribute/imported_attributes_repo/imported_attributes_repo_test.cpp b/searchcore/src/tests/proton/attribute/imported_attributes_repo/imported_attributes_repo_test.cpp index e23c308050b..ddabed210d8 100644 --- a/searchcore/src/tests/proton/attribute/imported_attributes_repo/imported_attributes_repo_test.cpp +++ b/searchcore/src/tests/proton/attribute/imported_attributes_repo/imported_attributes_repo_test.cpp @@ -7,6 +7,7 @@ LOG_SETUP("imported_attributes_repo_test"); #include <vespa/searchcommon/attribute/iattributevector.h> #include <vespa/searchcore/proton/attribute/imported_attributes_repo.h> #include <vespa/searchlib/attribute/imported_attribute_vector.h> +#include <vespa/searchlib/attribute/imported_attribute_vector_factory.h> #include <vespa/searchlib/attribute/reference_attribute.h> using proton::ImportedAttributesRepo; @@ -14,16 +15,17 @@ using search::AttributeVector; using search::attribute::BasicType; using search::attribute::Config; using search::attribute::ImportedAttributeVector; +using search::attribute::ImportedAttributeVectorFactory; using search::attribute::ReferenceAttribute; ImportedAttributeVector::SP createAttr(const vespalib::string &name) { - return std::make_shared<ImportedAttributeVector>(name, - ReferenceAttribute::SP(), - AttributeVector::SP(), - std::shared_ptr<search::IDocumentMetaStoreContext>(), - false); + return ImportedAttributeVectorFactory::create(name, + ReferenceAttribute::SP(), + AttributeVector::SP(), + std::shared_ptr<search::IDocumentMetaStoreContext>(), + false); } struct Fixture { diff --git a/searchcore/src/vespa/searchcore/proton/reference/document_db_reference_resolver.cpp b/searchcore/src/vespa/searchcore/proton/reference/document_db_reference_resolver.cpp index 9abe8181282..9be0eb2c728 100644 --- a/searchcore/src/vespa/searchcore/proton/reference/document_db_reference_resolver.cpp +++ b/searchcore/src/vespa/searchcore/proton/reference/document_db_reference_resolver.cpp @@ -9,6 +9,7 @@ #include <vespa/searchcommon/attribute/iattributevector.h> #include <vespa/searchlib/attribute/iattributemanager.h> #include <vespa/searchlib/attribute/imported_attribute_vector.h> +#include <vespa/searchlib/attribute/imported_attribute_vector_factory.h> #include <vespa/searchlib/attribute/reference_attribute.h> #include <vespa/config-imported-fields.h> #include <vespa/document/datatype/documenttype.h> @@ -22,7 +23,7 @@ using document::ReferenceDataType; using search::attribute::BasicType; using search::attribute::Config; using search::attribute::IAttributeVector; -using search::attribute::ImportedAttributeVector; +using search::attribute::ImportedAttributeVectorFactory; using search::attribute::ReferenceAttribute; using search::AttributeGuard; using search::AttributeVector; @@ -144,8 +145,7 @@ DocumentDBReferenceResolver::createImportedAttributesRepo(const IAttributeManage for (const auto &attr : _importedFieldsCfg.attribute) { ReferenceAttribute::SP refAttr = getReferenceAttribute(attr.referencefield, attrMgr); AttributeVector::SP targetAttr = getTargetDocumentDB(refAttr->getName())->getAttribute(attr.targetfield); - ImportedAttributeVector::SP importedAttr = - std::make_shared<ImportedAttributeVector>(attr.name, refAttr, targetAttr, documentMetaStore, useSearchCache); + auto importedAttr = ImportedAttributeVectorFactory::create(attr.name, refAttr, targetAttr, documentMetaStore, useSearchCache); result->add(importedAttr->getName(), importedAttr); } } diff --git a/searchlib/src/vespa/searchlib/attribute/CMakeLists.txt b/searchlib/src/vespa/searchlib/attribute/CMakeLists.txt index e99092f0206..cc6736e21c5 100644 --- a/searchlib/src/vespa/searchlib/attribute/CMakeLists.txt +++ b/searchlib/src/vespa/searchlib/attribute/CMakeLists.txt @@ -47,6 +47,7 @@ vespa_add_library(searchlib_attribute OBJECT iattributemanager.cpp iattributesavetarget.cpp imported_attribute_vector.cpp + imported_attribute_vector_factory.cpp imported_attribute_vector_read_guard.cpp imported_search_context.cpp integerbase.cpp diff --git a/searchlib/src/vespa/searchlib/attribute/imported_attribute_vector_factory.cpp b/searchlib/src/vespa/searchlib/attribute/imported_attribute_vector_factory.cpp new file mode 100644 index 00000000000..8161abe61f4 --- /dev/null +++ b/searchlib/src/vespa/searchlib/attribute/imported_attribute_vector_factory.cpp @@ -0,0 +1,51 @@ +// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#include "imported_attribute_vector_factory.h" +#include "imported_attribute_vector.h" +#include <vespa/searchlib/tensor/imported_tensor_attribute_vector.h> + +namespace search::attribute { + +using search::tensor::ImportedTensorAttributeVector; + +namespace { + +BasicType::Type getBasicType(const std::shared_ptr<AttributeVector> &attr) +{ + return attr ? attr->getBasicType() : BasicType::Type::NONE; +} + +} + +std::shared_ptr<ImportedAttributeVector> +ImportedAttributeVectorFactory::create(vespalib::stringref name, + std::shared_ptr<ReferenceAttribute> reference_attribute, + std::shared_ptr<AttributeVector> target_attribute, + std::shared_ptr<IDocumentMetaStoreContext> document_meta_store, + bool use_search_cache) +{ + switch (getBasicType(target_attribute)) { + case BasicType::Type::TENSOR: + return std::make_shared<ImportedTensorAttributeVector>(name, std::move(reference_attribute), std::move(target_attribute), std::move(document_meta_store), use_search_cache); + default: + return std::make_shared<ImportedAttributeVector>(name, std::move(reference_attribute), std::move(target_attribute), std::move(document_meta_store), use_search_cache); + } +} + + +std::shared_ptr<ImportedAttributeVector> +ImportedAttributeVectorFactory::create(vespalib::stringref name, + std::shared_ptr<ReferenceAttribute> reference_attribute, + std::shared_ptr<AttributeVector> target_attribute, + std::shared_ptr<IDocumentMetaStoreContext> document_meta_store, + std::shared_ptr<BitVectorSearchCache> search_cache) +{ + switch (getBasicType(target_attribute)) { + case BasicType::Type::TENSOR: + return std::make_shared<ImportedTensorAttributeVector>(name, std::move(reference_attribute), std::move(target_attribute), std::move(document_meta_store), std::move(search_cache)); + default: + return std::make_shared<ImportedAttributeVector>(name, std::move(reference_attribute), std::move(target_attribute), std::move(document_meta_store), std::move(search_cache)); + } +} + +} // search::attribute diff --git a/searchlib/src/vespa/searchlib/attribute/imported_attribute_vector_factory.h b/searchlib/src/vespa/searchlib/attribute/imported_attribute_vector_factory.h new file mode 100644 index 00000000000..0826a913139 --- /dev/null +++ b/searchlib/src/vespa/searchlib/attribute/imported_attribute_vector_factory.h @@ -0,0 +1,37 @@ +// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#pragma once + +#include <vespa/vespalib/stllike/string.h> +#include <memory> + +namespace search { + +class AttributeVector; +class IDocumentMetaStoreContext; + +namespace attribute { + +class BitVectorSearchCache; +class ImportedAttributeVector; +class ReferenceAttribute; + +class ImportedAttributeVectorFactory { +public: + static std::shared_ptr<ImportedAttributeVector> + create(vespalib::stringref name, + std::shared_ptr<ReferenceAttribute> reference_attribute, + std::shared_ptr<AttributeVector> target_attribute, + std::shared_ptr<IDocumentMetaStoreContext> document_meta_store, + bool use_search_cache); + + static std::shared_ptr<ImportedAttributeVector> + create(vespalib::stringref name, + std::shared_ptr<ReferenceAttribute> reference_attribute, + std::shared_ptr<AttributeVector> target_attribute, + std::shared_ptr<IDocumentMetaStoreContext> document_meta_store, + std::shared_ptr<BitVectorSearchCache> search_cache); +}; + +} // attribute +} // search diff --git a/searchlib/src/vespa/searchlib/tensor/CMakeLists.txt b/searchlib/src/vespa/searchlib/tensor/CMakeLists.txt index f0c8832d0ef..de2b01a1697 100644 --- a/searchlib/src/vespa/searchlib/tensor/CMakeLists.txt +++ b/searchlib/src/vespa/searchlib/tensor/CMakeLists.txt @@ -6,6 +6,7 @@ vespa_add_library(searchlib_tensor OBJECT dense_tensor_store.cpp generic_tensor_attribute.cpp generic_tensor_store.cpp + imported_tensor_attribute_vector.cpp tensor_attribute.cpp generic_tensor_attribute_saver.cpp tensor_store.cpp diff --git a/searchlib/src/vespa/searchlib/tensor/imported_tensor_attribute_vector.cpp b/searchlib/src/vespa/searchlib/tensor/imported_tensor_attribute_vector.cpp new file mode 100644 index 00000000000..0b1f779d5f5 --- /dev/null +++ b/searchlib/src/vespa/searchlib/tensor/imported_tensor_attribute_vector.cpp @@ -0,0 +1,64 @@ +// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#include "imported_tensor_attribute_vector.h" +#include <vespa/eval/tensor/tensor.h> + +namespace search::tensor { + +using vespalib::tensor::Tensor; + +ImportedTensorAttributeVector::ImportedTensorAttributeVector(vespalib::stringref name, + std::shared_ptr<ReferenceAttribute> reference_attribute, + std::shared_ptr<AttributeVector> target_attribute, + std::shared_ptr<IDocumentMetaStoreContext> document_meta_store, + bool use_search_cache) + : ImportedAttributeVector(name, std::move(reference_attribute), + std::move(target_attribute), + std::move(document_meta_store), + use_search_cache), + _target_tensor_attribute(dynamic_cast<const ITensorAttribute &>(*_target_attribute)) +{ +} + +ImportedTensorAttributeVector::ImportedTensorAttributeVector(vespalib::stringref name, + std::shared_ptr<ReferenceAttribute> reference_attribute, + std::shared_ptr<AttributeVector> target_attribute, + std::shared_ptr<IDocumentMetaStoreContext> document_meta_store, + std::shared_ptr<BitVectorSearchCache> search_cache) + : ImportedAttributeVector(name, std::move(reference_attribute), + std::move(target_attribute), + std::move(document_meta_store), + std::move(search_cache)), + _target_tensor_attribute(dynamic_cast<const ITensorAttribute &>(*_target_attribute)) +{ +} + +ImportedTensorAttributeVector::~ImportedTensorAttributeVector() +{ +} + +std::unique_ptr<Tensor> +ImportedTensorAttributeVector::getTensor(uint32_t docId) const +{ + return _target_tensor_attribute.getTensor(_reference_attribute->getReferencedLid(docId)); +} + +std::unique_ptr<Tensor> +ImportedTensorAttributeVector::getEmptyTensor() const +{ + return _target_tensor_attribute.getEmptyTensor(); +} + +void +ImportedTensorAttributeVector::getTensor(uint32_t docId, vespalib::tensor::MutableDenseTensorView &tensor) const +{ + _target_tensor_attribute.getTensor(_reference_attribute->getReferencedLid(docId), tensor); +} + +vespalib::eval::ValueType +ImportedTensorAttributeVector::getTensorType() const +{ + return _target_tensor_attribute.getTensorType(); +} + +} diff --git a/searchlib/src/vespa/searchlib/tensor/imported_tensor_attribute_vector.h b/searchlib/src/vespa/searchlib/tensor/imported_tensor_attribute_vector.h new file mode 100644 index 00000000000..082044155b3 --- /dev/null +++ b/searchlib/src/vespa/searchlib/tensor/imported_tensor_attribute_vector.h @@ -0,0 +1,38 @@ +// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#pragma once + +#include <vespa/searchlib/attribute/imported_attribute_vector.h> +#include "i_tensor_attribute.h" + +namespace search::tensor { + +/** + * Attribute vector for imported tensor attributes. + */ +class ImportedTensorAttributeVector : public attribute::ImportedAttributeVector, + public ITensorAttribute +{ + using ReferenceAttribute = attribute::ReferenceAttribute; + using BitVectorSearchCache = attribute::BitVectorSearchCache; + const ITensorAttribute &_target_tensor_attribute; +public: + ImportedTensorAttributeVector(vespalib::stringref name, + std::shared_ptr<ReferenceAttribute> reference_attribute, + std::shared_ptr<AttributeVector> target_attribute, + std::shared_ptr<IDocumentMetaStoreContext> document_meta_store, + bool use_search_cache); + ImportedTensorAttributeVector(vespalib::stringref name, + std::shared_ptr<ReferenceAttribute> reference_attribute, + std::shared_ptr<AttributeVector> target_attribute, + std::shared_ptr<IDocumentMetaStoreContext> document_meta_store, + std::shared_ptr<BitVectorSearchCache> search_cache); + ~ImportedTensorAttributeVector(); + + virtual std::unique_ptr<Tensor> getTensor(uint32_t docId) const override; + virtual std::unique_ptr<Tensor> getEmptyTensor() const override; + virtual void getTensor(uint32_t docId, vespalib::tensor::MutableDenseTensorView &tensor) const override; + virtual vespalib::eval::ValueType getTensorType() const override; +}; + +} // namespace search::tensor diff --git a/searchlib/src/vespa/searchlib/test/imported_attribute_fixture.h b/searchlib/src/vespa/searchlib/test/imported_attribute_fixture.h index f9e3de79144..6917e41853a 100644 --- a/searchlib/src/vespa/searchlib/test/imported_attribute_fixture.h +++ b/searchlib/src/vespa/searchlib/test/imported_attribute_fixture.h @@ -9,6 +9,7 @@ #include <vespa/searchlib/attribute/attributeguard.h> #include <vespa/searchlib/attribute/floatbase.h> #include <vespa/searchlib/attribute/imported_attribute_vector.h> +#include <vespa/searchlib/attribute/imported_attribute_vector_factory.h> #include <vespa/searchlib/attribute/integerbase.h> #include <vespa/searchlib/attribute/not_implemented_attribute.h> #include <vespa/searchlib/attribute/stringbase.h> @@ -157,7 +158,7 @@ struct ImportedAttributeFixture { std::shared_ptr<ImportedAttributeVector> create_attribute_vector_from_members(vespalib::stringref name = default_imported_attr_name()) { - return std::make_shared<ImportedAttributeVector>(name, reference_attr, target_attr, document_meta_store, use_search_cache); + return ImportedAttributeVectorFactory::create(name, reference_attr, target_attr, document_meta_store, use_search_cache); } template<typename AttrVecType> |