From dab9389ca4d586f3d02a65304e6082fcd64c4542 Mon Sep 17 00:00:00 2001 From: Tor Egge Date: Fri, 1 Apr 2022 12:14:38 +0200 Subject: Factor out SingleEnumSearchContext and SingleNumericEnumSearchContext from SingleNumericEnumAttribute. --- .../src/vespa/searchlib/attribute/CMakeLists.txt | 1 + .../attribute/single_enum_search_context.h | 52 ++++++++++++++++++++++ .../attribute/single_enum_search_context.hpp | 44 ++++++++++++++++++ .../single_numeric_enum_search_context.cpp | 14 ++++++ .../attribute/single_numeric_enum_search_context.h | 23 ++++++++++ .../single_numeric_enum_search_context.hpp | 23 ++++++++++ .../attribute/singlenumericenumattribute.h | 39 ---------------- .../attribute/singlenumericenumattribute.hpp | 47 +------------------ .../attribute/singlenumericpostattribute.h | 3 -- .../attribute/singlenumericpostattribute.hpp | 6 ++- 10 files changed, 163 insertions(+), 89 deletions(-) create mode 100644 searchlib/src/vespa/searchlib/attribute/single_enum_search_context.h create mode 100644 searchlib/src/vespa/searchlib/attribute/single_enum_search_context.hpp create mode 100644 searchlib/src/vespa/searchlib/attribute/single_numeric_enum_search_context.cpp create mode 100644 searchlib/src/vespa/searchlib/attribute/single_numeric_enum_search_context.h create mode 100644 searchlib/src/vespa/searchlib/attribute/single_numeric_enum_search_context.hpp 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 +class SingleEnumSearchContext : public Matcher, public SearchContext +{ +protected: + const vespalib::datastore::AtomicEntryRef* _enum_indices; + const EnumStoreT& _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& 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 + 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 + +namespace search::attribute { + +template +SingleEnumSearchContext::SingleEnumSearchContext(Matcher&& matcher, const AttributeVector& toBeSearched, const vespalib::datastore::AtomicEntryRef* enum_indices, const EnumStoreT& enum_store) + : Matcher(std::move(matcher)), + SearchContext(toBeSearched), + _enum_indices(enum_indices), + _enum_store(enum_store) +{ +} + +template +bool +SingleEnumSearchContext::valid() const +{ + return this->isValid(); +} + +template +std::unique_ptr +SingleEnumSearchContext::createFilterIterator(fef::TermFieldMatchData* matchData, bool strict) +{ + if (!valid()) { + return std::make_unique(); + } + if (getIsFilter()) { + return strict + ? std::make_unique>(*this, matchData) + : std::make_unique>(*this, matchData); + } + return strict + ? std::make_unique>(*this, matchData) + : std::make_unique>(*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; +template class SingleNumericEnumSearchContext; +template class SingleNumericEnumSearchContext; +template class SingleNumericEnumSearchContext; +template class SingleNumericEnumSearchContext; +template class SingleNumericEnumSearchContext; + +} 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 +class SingleNumericEnumSearchContext : public SingleEnumSearchContext> +{ +public: + SingleNumericEnumSearchContext(std::unique_ptr qTerm, const AttributeVector& toBeSearched, const vespalib::datastore::AtomicEntryRef* enum_indices, const EnumStoreT& 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 +SingleNumericEnumSearchContext::SingleNumericEnumSearchContext(std::unique_ptr qTerm, const AttributeVector& toBeSearched, const vespalib::datastore::AtomicEntryRef* enum_indices, const EnumStoreT& enum_store) + : SingleEnumSearchContext>(NumericRangeMatcher(*qTerm, true), toBeSearched, enum_indices, enum_store) +{ +} + +template +Int64Range +SingleNumericEnumSearchContext::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, public attribute::SearchContext - { - protected: - const SingleValueNumericEnumAttribute & _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 - 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 #include #include @@ -157,51 +158,7 @@ SingleValueNumericEnumAttribute::getSearch(QueryTermSimple::UP qTerm, const attribute::SearchContextParams & params) const { (void) params; - QueryTermSimple::RangeResult res = qTerm->getRange(); - if (res.isEqual()) { - return std::make_unique(std::move(qTerm), *this); - } else { - return std::make_unique(std::move(qTerm), *this); - } -} - -template -bool -SingleValueNumericEnumAttribute::SingleSearchContext::valid() const -{ - return this->isValid(); -} - -template -SingleValueNumericEnumAttribute::SingleSearchContext::SingleSearchContext(QueryTermSimpleUP qTerm, const NumericAttribute & toBeSearched) : - attribute::NumericRangeMatcher(*qTerm, true), - attribute::SearchContext(toBeSearched), - _toBeSearched(static_cast &>(toBeSearched)) -{ } - -template -Int64Range -SingleValueNumericEnumAttribute::SingleSearchContext::getAsIntegerTerm() const -{ - return this->getRange(); -} - -template -std::unique_ptr -SingleValueNumericEnumAttribute::SingleSearchContext::createFilterIterator(fef::TermFieldMatchData * matchData, - bool strict) -{ - if (!valid()) { - return std::make_unique(); - } - if (getIsFilter()) { - return strict - ? std::make_unique>(*this, matchData) - : std::make_unique>(*this, matchData); - } - return strict - ? std::make_unique>(*this, matchData) - : std::make_unique>(*this, matchData); + return std::make_unique>(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; - using SingleSearchContext = typename SingleValueNumericEnumAttribute::SingleSearchContext; - using SingleNumericSearchContext = SingleSearchContext; - using SinglePostingSearchContext = attribute::NumericPostingSearchContext; using ValueModifier = typename B::BaseClass::ValueModifier; using generation_t = typename SingleValueNumericEnumAttribute::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 SingleValueNumericPostingAttribute::getSearch(QueryTermSimple::UP qTerm, const attribute::SearchContextParams & params) const { - SingleNumericSearchContext base_sc(std::move(qTerm), *this); - return std::make_unique(std::move(base_sc), params, *this); + using BaseSC = attribute::SingleNumericEnumSearchContext; + using SC = attribute::NumericPostingSearchContext; + BaseSC base_sc(std::move(qTerm), *this, &this->_enumIndices.acquire_elem_ref(0), this->_enumStore); + return std::make_unique(std::move(base_sc), params, *this); } } // namespace search -- cgit v1.2.3