diff options
author | Tor Egge <Tor.Egge@yahooinc.com> | 2022-04-01 12:14:38 +0200 |
---|---|---|
committer | Tor Egge <Tor.Egge@yahooinc.com> | 2022-04-01 12:14:38 +0200 |
commit | dab9389ca4d586f3d02a65304e6082fcd64c4542 (patch) | |
tree | d257d71ecdd8f4188224508a0ada57950f96f54f | |
parent | 1bb3dd192f4bcb91e710fe19ca54b2b8935ffb83 (diff) |
Factor out SingleEnumSearchContext and SingleNumericEnumSearchContext from
SingleNumericEnumAttribute.
10 files changed, 163 insertions, 89 deletions
diff --git a/searchlib/src/vespa/searchlib/attribute/CMakeLists.txt b/searchlib/src/vespa/searchlib/attribute/CMakeLists.txt index 8d2e278fb5d..af8df69f5e8 100644 --- a/searchlib/src/vespa/searchlib/attribute/CMakeLists.txt +++ b/searchlib/src/vespa/searchlib/attribute/CMakeLists.txt @@ -105,6 +105,7 @@ vespa_add_library(searchlib_attribute OBJECT singlesmallnumericattribute.cpp singlestringattribute.cpp singlestringpostattribute.cpp + single_numeric_enum_search_context.cpp single_numeric_search_context.cpp sourceselector.cpp stringattribute.cpp diff --git a/searchlib/src/vespa/searchlib/attribute/single_enum_search_context.h b/searchlib/src/vespa/searchlib/attribute/single_enum_search_context.h new file mode 100644 index 00000000000..69a19666fa1 --- /dev/null +++ b/searchlib/src/vespa/searchlib/attribute/single_enum_search_context.h @@ -0,0 +1,52 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#pragma once + +#include "search_context.h" +#include "enumstore.h" +#include "multi_value_mapping.h" + +namespace search::attribute { + +/* + * SingleEnumSearchContext handles the creation of search iterators for + * a query term on a single value enumerated attribute vector. + * This class should be considered to be an abstract class. + */ +template <typename T, typename Matcher> +class SingleEnumSearchContext : public Matcher, public SearchContext +{ +protected: + const vespalib::datastore::AtomicEntryRef* _enum_indices; + const EnumStoreT<T>& _enum_store; + + int32_t onFind(DocId docId, int32_t elemId, int32_t & weight) const override final { + return find(docId, elemId, weight); + } + + int32_t onFind(DocId docId, int32_t elemId) const override final { + return find(docId, elemId); + } + bool valid() const override; + +public: + SingleEnumSearchContext(Matcher&& matcher, const AttributeVector& toBeSearched, const vespalib::datastore::AtomicEntryRef* enum_indices, const EnumStoreT<T>& enum_store); + + int32_t find(DocId docId, int32_t elemId, int32_t & weight) const { + if ( elemId != 0) return -1; + T v = _enum_store.get_value(_enum_indices[docId].load_acquire()); + weight = 1; + return this->match(v) ? 0 : -1; + } + + int32_t find(DocId docId, int32_t elemId) const { + if ( elemId != 0) return -1; + T v = _enum_store.get_value(_enum_indices[docId].load_acquire()); + return this->match(v) ? 0 : -1; + } + + std::unique_ptr<queryeval::SearchIterator> + createFilterIterator(fef::TermFieldMatchData* matchData, bool strict) override; +}; + +} diff --git a/searchlib/src/vespa/searchlib/attribute/single_enum_search_context.hpp b/searchlib/src/vespa/searchlib/attribute/single_enum_search_context.hpp new file mode 100644 index 00000000000..a79572e1fff --- /dev/null +++ b/searchlib/src/vespa/searchlib/attribute/single_enum_search_context.hpp @@ -0,0 +1,44 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#pragma once + +#include "single_enum_search_context.h" +#include "attributeiterators.hpp" +#include <vespa/searchlib/queryeval/emptysearch.h> + +namespace search::attribute { + +template <typename T, typename Matcher> +SingleEnumSearchContext<T, Matcher>::SingleEnumSearchContext(Matcher&& matcher, const AttributeVector& toBeSearched, const vespalib::datastore::AtomicEntryRef* enum_indices, const EnumStoreT<T>& enum_store) + : Matcher(std::move(matcher)), + SearchContext(toBeSearched), + _enum_indices(enum_indices), + _enum_store(enum_store) +{ +} + +template <typename T, typename Matcher> +bool +SingleEnumSearchContext<T, Matcher>::valid() const +{ + return this->isValid(); +} + +template <typename T, typename Matcher> +std::unique_ptr<queryeval::SearchIterator> +SingleEnumSearchContext<T, Matcher>::createFilterIterator(fef::TermFieldMatchData* matchData, bool strict) +{ + if (!valid()) { + return std::make_unique<queryeval::EmptySearch>(); + } + if (getIsFilter()) { + return strict + ? std::make_unique<FilterAttributeIteratorStrict<SingleEnumSearchContext>>(*this, matchData) + : std::make_unique<FilterAttributeIteratorT<SingleEnumSearchContext>>(*this, matchData); + } + return strict + ? std::make_unique<AttributeIteratorStrict<SingleEnumSearchContext>>(*this, matchData) + : std::make_unique<AttributeIteratorT<SingleEnumSearchContext>>(*this, matchData); +} + +} diff --git a/searchlib/src/vespa/searchlib/attribute/single_numeric_enum_search_context.cpp b/searchlib/src/vespa/searchlib/attribute/single_numeric_enum_search_context.cpp new file mode 100644 index 00000000000..07f6aefebca --- /dev/null +++ b/searchlib/src/vespa/searchlib/attribute/single_numeric_enum_search_context.cpp @@ -0,0 +1,14 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#include "single_numeric_enum_search_context.hpp" + +namespace search::attribute { + +template class SingleNumericEnumSearchContext<int8_t>; +template class SingleNumericEnumSearchContext<int16_t>; +template class SingleNumericEnumSearchContext<int32_t>; +template class SingleNumericEnumSearchContext<int64_t>; +template class SingleNumericEnumSearchContext<float>; +template class SingleNumericEnumSearchContext<double>; + +} diff --git a/searchlib/src/vespa/searchlib/attribute/single_numeric_enum_search_context.h b/searchlib/src/vespa/searchlib/attribute/single_numeric_enum_search_context.h new file mode 100644 index 00000000000..deabb0917f9 --- /dev/null +++ b/searchlib/src/vespa/searchlib/attribute/single_numeric_enum_search_context.h @@ -0,0 +1,23 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#pragma once + +#include "single_enum_search_context.h" +#include "numeric_range_matcher.h" + +namespace search::attribute { + +/* + * SingleNumericEnumSearchContext handles the creation of search iterators for + * a query term on a single value numeric enumerated attribute vector. + */ +template <typename T> +class SingleNumericEnumSearchContext : public SingleEnumSearchContext<T, NumericRangeMatcher<T>> +{ +public: + SingleNumericEnumSearchContext(std::unique_ptr<QueryTermSimple> qTerm, const AttributeVector& toBeSearched, const vespalib::datastore::AtomicEntryRef* enum_indices, const EnumStoreT<T>& enum_store); + + Int64Range getAsIntegerTerm() const override; +}; + +} diff --git a/searchlib/src/vespa/searchlib/attribute/single_numeric_enum_search_context.hpp b/searchlib/src/vespa/searchlib/attribute/single_numeric_enum_search_context.hpp new file mode 100644 index 00000000000..6997b094f1e --- /dev/null +++ b/searchlib/src/vespa/searchlib/attribute/single_numeric_enum_search_context.hpp @@ -0,0 +1,23 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#pragma once + +#include "single_numeric_enum_search_context.h" +#include "single_enum_search_context.hpp" + +namespace search::attribute { + +template <typename T> +SingleNumericEnumSearchContext<T>::SingleNumericEnumSearchContext(std::unique_ptr<QueryTermSimple> qTerm, const AttributeVector& toBeSearched, const vespalib::datastore::AtomicEntryRef* enum_indices, const EnumStoreT<T>& enum_store) + : SingleEnumSearchContext<T, NumericRangeMatcher<T>>(NumericRangeMatcher<T>(*qTerm, true), toBeSearched, enum_indices, enum_store) +{ +} + +template <typename T> +Int64Range +SingleNumericEnumSearchContext<T>::getAsIntegerTerm() const +{ + return this->getRange(); +} + +} diff --git a/searchlib/src/vespa/searchlib/attribute/singlenumericenumattribute.h b/searchlib/src/vespa/searchlib/attribute/singlenumericenumattribute.h index 2c598b43b28..a269aec5c6b 100644 --- a/searchlib/src/vespa/searchlib/attribute/singlenumericenumattribute.h +++ b/searchlib/src/vespa/searchlib/attribute/singlenumericenumattribute.h @@ -47,45 +47,6 @@ protected: void considerArithmeticAttributeChange(const Change & c, EnumStoreBatchUpdater & inserter) override; void applyArithmeticValueChange(const Change& c, EnumStoreBatchUpdater& updater) override; - /* - * Specialization of SearchContext - */ - class SingleSearchContext : public attribute::NumericRangeMatcher<T>, public attribute::SearchContext - { - protected: - const SingleValueNumericEnumAttribute<B> & _toBeSearched; - - int32_t onFind(DocId docId, int32_t elemId, int32_t & weight) const override { - return find(docId, elemId, weight); - } - - int32_t onFind(DocId docId, int32_t elemId) const override { - return find(docId, elemId); - } - bool valid() const override; - - public: - SingleSearchContext(QueryTermSimpleUP qTerm, const NumericAttribute & toBeSearched); - - Int64Range getAsIntegerTerm() const override; - - int32_t find(DocId docId, int32_t elemId, int32_t & weight) const { - if ( elemId != 0) return -1; - T v = _toBeSearched._enumStore.get_value(_toBeSearched.getEnumIndex(docId)); - weight = 1; - return this->match(v) ? 0 : -1; - } - - int32_t find(DocId docId, int32_t elemId) const { - if ( elemId != 0) return -1; - T v = _toBeSearched._enumStore.get_value(_toBeSearched.getEnumIndex(docId)); - return this->match(v) ? 0 : -1; - } - - std::unique_ptr<queryeval::SearchIterator> - createFilterIterator(fef::TermFieldMatchData * matchData, bool strict) override; - }; - public: SingleValueNumericEnumAttribute(const vespalib::string & baseFileName, const AttributeVector::Config & c = diff --git a/searchlib/src/vespa/searchlib/attribute/singlenumericenumattribute.hpp b/searchlib/src/vespa/searchlib/attribute/singlenumericenumattribute.hpp index 3cb5e57fe8c..f5219e2e8c7 100644 --- a/searchlib/src/vespa/searchlib/attribute/singlenumericenumattribute.hpp +++ b/searchlib/src/vespa/searchlib/attribute/singlenumericenumattribute.hpp @@ -8,6 +8,7 @@ #include "primitivereader.h" #include "singleenumattribute.hpp" #include "singlenumericenumattribute.h" +#include "single_numeric_enum_search_context.h" #include <vespa/searchlib/query/query_term_simple.h> #include <vespa/searchlib/queryeval/emptysearch.h> #include <vespa/searchlib/util/fileutil.hpp> @@ -157,51 +158,7 @@ SingleValueNumericEnumAttribute<B>::getSearch(QueryTermSimple::UP qTerm, const attribute::SearchContextParams & params) const { (void) params; - QueryTermSimple::RangeResult<T> res = qTerm->getRange<T>(); - if (res.isEqual()) { - return std::make_unique<SingleSearchContext>(std::move(qTerm), *this); - } else { - return std::make_unique<SingleSearchContext>(std::move(qTerm), *this); - } -} - -template <typename B> -bool -SingleValueNumericEnumAttribute<B>::SingleSearchContext::valid() const -{ - return this->isValid(); -} - -template <typename B> -SingleValueNumericEnumAttribute<B>::SingleSearchContext::SingleSearchContext(QueryTermSimpleUP qTerm, const NumericAttribute & toBeSearched) : - attribute::NumericRangeMatcher<T>(*qTerm, true), - attribute::SearchContext(toBeSearched), - _toBeSearched(static_cast<const SingleValueNumericEnumAttribute<B> &>(toBeSearched)) -{ } - -template <typename B> -Int64Range -SingleValueNumericEnumAttribute<B>::SingleSearchContext::getAsIntegerTerm() const -{ - return this->getRange(); -} - -template <typename B> -std::unique_ptr<queryeval::SearchIterator> -SingleValueNumericEnumAttribute<B>::SingleSearchContext::createFilterIterator(fef::TermFieldMatchData * matchData, - bool strict) -{ - if (!valid()) { - return std::make_unique<queryeval::EmptySearch>(); - } - if (getIsFilter()) { - return strict - ? std::make_unique<FilterAttributeIteratorStrict<SingleSearchContext>>(*this, matchData) - : std::make_unique<FilterAttributeIteratorT<SingleSearchContext>>(*this, matchData); - } - return strict - ? std::make_unique<AttributeIteratorStrict<SingleSearchContext>>(*this, matchData) - : std::make_unique<AttributeIteratorT<SingleSearchContext>>(*this, matchData); + return std::make_unique<attribute::SingleNumericEnumSearchContext<T>>(std::move(qTerm), *this, &this->_enumIndices.acquire_elem_ref(0), this->_enumStore); } } diff --git a/searchlib/src/vespa/searchlib/attribute/singlenumericpostattribute.h b/searchlib/src/vespa/searchlib/attribute/singlenumericpostattribute.h index b91a14f5c46..720fb211e1a 100644 --- a/searchlib/src/vespa/searchlib/attribute/singlenumericpostattribute.h +++ b/searchlib/src/vespa/searchlib/attribute/singlenumericpostattribute.h @@ -46,9 +46,6 @@ private: using PostingMap = typename PostingParent::PostingMap; using QueryTermSimpleUP = AttributeVector::QueryTermSimpleUP; using SelfType = SingleValueNumericPostingAttribute<B>; - using SingleSearchContext = typename SingleValueNumericEnumAttribute<B>::SingleSearchContext; - using SingleNumericSearchContext = SingleSearchContext; - using SinglePostingSearchContext = attribute::NumericPostingSearchContext<SingleNumericSearchContext, SelfType, vespalib::btree::BTreeNoLeafData>; using ValueModifier = typename B::BaseClass::ValueModifier; using generation_t = typename SingleValueNumericEnumAttribute<B>::generation_t; diff --git a/searchlib/src/vespa/searchlib/attribute/singlenumericpostattribute.hpp b/searchlib/src/vespa/searchlib/attribute/singlenumericpostattribute.hpp index 2589f3420d1..2050f887c33 100644 --- a/searchlib/src/vespa/searchlib/attribute/singlenumericpostattribute.hpp +++ b/searchlib/src/vespa/searchlib/attribute/singlenumericpostattribute.hpp @@ -147,8 +147,10 @@ std::unique_ptr<attribute::SearchContext> SingleValueNumericPostingAttribute<B>::getSearch(QueryTermSimple::UP qTerm, const attribute::SearchContextParams & params) const { - SingleNumericSearchContext base_sc(std::move(qTerm), *this); - return std::make_unique<SinglePostingSearchContext>(std::move(base_sc), params, *this); + using BaseSC = attribute::SingleNumericEnumSearchContext<T>; + using SC = attribute::NumericPostingSearchContext<BaseSC, SelfType, vespalib::btree::BTreeNoLeafData>; + BaseSC base_sc(std::move(qTerm), *this, &this->_enumIndices.acquire_elem_ref(0), this->_enumStore); + return std::make_unique<SC>(std::move(base_sc), params, *this); } } // namespace search |