Skip to content

Commit

Permalink
Merge pull request #21958 from vespa-engine/toregge/factor-out-single…
Browse files Browse the repository at this point in the history
…-small-numeric-search-context-from-single-small-numeric-attribute

Factor out SingleSmallNumericSearchContext from SingleSmallNumericAttribute.
  • Loading branch information
baldersheim authored Apr 3, 2022
2 parents ab62199 + a9197ea commit 00641e5
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 87 deletions.
1 change: 1 addition & 0 deletions searchlib/src/vespa/searchlib/attribute/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ vespa_add_library(searchlib_attribute OBJECT
singlestringpostattribute.cpp
single_numeric_enum_search_context.cpp
single_numeric_search_context.cpp
single_small_numeric_search_context.cpp
single_string_enum_search_context.cpp
single_string_enum_hint_search_context.cpp
sourceselector.cpp
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "single_small_numeric_search_context.h"
#include "attributeiterators.hpp"
#include <vespa/searchlib/queryeval/emptysearch.h>

namespace search::attribute {

SingleSmallNumericSearchContext::SingleSmallNumericSearchContext(std::unique_ptr<QueryTermSimple> qTerm, const AttributeVector& toBeSearched, const Word* word_data, Word value_mask, uint32_t value_shift_shift, uint32_t value_shift_mask, uint32_t word_shift)
: NumericSearchContext<NumericRangeMatcher<T>>(toBeSearched, *qTerm, false),
_wordData(word_data),
_valueMask(value_mask),
_valueShiftShift(value_shift_shift),
_valueShiftMask(value_shift_mask),
_wordShift(word_shift)
{
}

std::unique_ptr<queryeval::SearchIterator>
SingleSmallNumericSearchContext::createFilterIterator(fef::TermFieldMatchData* matchData, bool strict)
{
if (!valid()) {
return std::make_unique<queryeval::EmptySearch>();
}
if (getIsFilter()) {
return strict
? std::make_unique<FilterAttributeIteratorStrict<SingleSmallNumericSearchContext>>(*this, matchData)
: std::make_unique<FilterAttributeIteratorT<SingleSmallNumericSearchContext>>(*this, matchData);
}
return strict
? std::make_unique<AttributeIteratorStrict<SingleSmallNumericSearchContext>>(*this, matchData)
: std::make_unique<AttributeIteratorT<SingleSmallNumericSearchContext>>(*this, matchData);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "numeric_search_context.h"
#include "numeric_range_matcher.h"
#include <vespa/vespalib/util/atomic.h>

namespace search::attribute {

/*
* SingleSmallNumericSearchContext handles the creation of search iterators for
* a query term on a single value small numeric attribute vector.
*/
class SingleSmallNumericSearchContext : public NumericSearchContext<NumericRangeMatcher<int8_t>>
{
private:
using Word = uint32_t;
using T = int8_t;
const Word *_wordData;
Word _valueMask;
uint32_t _valueShiftShift;
uint32_t _valueShiftMask;
uint32_t _wordShift;

int32_t onFind(DocId docId, int32_t elementId, int32_t & weight) const override {
return find(docId, elementId, weight);
}

int32_t onFind(DocId docId, int32_t elementId) const override {
return find(docId, elementId);
}

public:
SingleSmallNumericSearchContext(std::unique_ptr<QueryTermSimple> qTerm, const AttributeVector& toBeSearched, const Word* word_data, Word value_mask, uint32_t value_shift_shift, uint32_t value_shift_mask, uint32_t word_shift);

int32_t find(DocId docId, int32_t elemId, int32_t & weight) const {
if ( elemId != 0) return -1;
const Word &word = _wordData[docId >> _wordShift];
uint32_t valueShift = (docId & _valueShiftMask) << _valueShiftShift;
T v = (vespalib::atomic::load_ref_relaxed(word) >> valueShift) & _valueMask;
weight = 1;
return match(v) ? 0 : -1;
}

int32_t find(DocId docId, int32_t elemId) const {
if ( elemId != 0) return -1;
const Word &word = _wordData[docId >> _wordShift];
uint32_t valueShift = (docId & _valueShiftMask) << _valueShiftShift;
T v = (vespalib::atomic::load_ref_relaxed(word) >> valueShift) & _valueMask;
return match(v) ? 0 : -1;
}

std::unique_ptr<queryeval::SearchIterator>
createFilterIterator(fef::TermFieldMatchData* matchData, bool strict) override;
};

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "singlesmallnumericattribute.h"
#include "attributeiterators.hpp"
#include "attributevector.hpp"
#include "iattributesavetarget.h"
#include "primitivereader.h"
#include "single_small_numeric_search_context.h"
#include <vespa/searchlib/query/query_term_simple.h>
#include <vespa/searchlib/queryeval/emptysearch.h>
#include <vespa/searchlib/util/file_settings.h>
#include <vespa/vespalib/data/databuffer.h>
#include <vespa/vespalib/util/size_literals.h>
Expand Down Expand Up @@ -172,7 +171,7 @@ std::unique_ptr<attribute::SearchContext>
SingleValueSmallNumericAttribute::getSearch(std::unique_ptr<QueryTermSimple> qTerm,
const attribute::SearchContextParams &) const
{
return std::make_unique<SingleSearchContext>(std::move(qTerm), *this);
return std::make_unique<attribute::SingleSmallNumericSearchContext>(std::move(qTerm), *this, &_wordData.acquire_elem_ref(0), _valueMask, _valueShiftShift, _valueShiftMask, _wordShift);
}

void
Expand Down Expand Up @@ -208,40 +207,6 @@ SingleValueSmallNumericAttribute::getEstimatedSaveByteSize() const
return headerSize + sz;
}

bool SingleValueSmallNumericAttribute::SingleSearchContext::valid() const { return this->isValid(); }


SingleValueSmallNumericAttribute::SingleSearchContext::SingleSearchContext(std::unique_ptr<QueryTermSimple> qTerm,
const SingleValueSmallNumericAttribute & toBeSearched)
: attribute::NumericRangeMatcher<T>(*qTerm),
SearchContext(toBeSearched), _wordData(&toBeSearched._wordData.acquire_elem_ref(0)),
_valueMask(toBeSearched._valueMask),
_valueShiftShift(toBeSearched._valueShiftShift),
_valueShiftMask(toBeSearched._valueShiftMask),
_wordShift(toBeSearched._wordShift)
{ }

Int64Range
SingleValueSmallNumericAttribute::SingleSearchContext::getAsIntegerTerm() const {
return this->getRange();
}

std::unique_ptr<queryeval::SearchIterator>
SingleValueSmallNumericAttribute::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);
}

namespace {

template <typename TT>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#pragma once

#include "integerbase.h"
#include "floatbase.h"
#include "numeric_range_matcher.h"
#include "search_context.h"
#include <vespa/vespalib/util/atomic.h>
#include <vespa/vespalib/util/rcuvector.h>
Expand All @@ -15,7 +13,6 @@ namespace search {
class SingleValueSmallNumericAttribute : public IntegerAttributeTemplate<int8_t>
{
private:
// friend class attribute::SearchContext;
typedef IntegerAttributeTemplate<int8_t> B;
typedef B::BaseType T;
typedef B::DocId DocId;
Expand Down Expand Up @@ -58,53 +55,6 @@ class SingleValueSmallNumericAttribute : public IntegerAttributeTemplate<int8_t>


public:
/*
* Specialization of SearchContext
*/
class SingleSearchContext : public attribute::NumericRangeMatcher<T>, public attribute::SearchContext
{
private:
const Word *_wordData;
Word _valueMask;
uint32_t _valueShiftShift;
uint32_t _valueShiftMask;
uint32_t _wordShift;

int32_t onFind(DocId docId, int32_t elementId, int32_t & weight) const override {
return find(docId, elementId, weight);
}

int32_t onFind(DocId docId, int32_t elementId) const override {
return find(docId, elementId);
}

bool valid() const override;

public:
SingleSearchContext(std::unique_ptr<QueryTermSimple> qTerm, const SingleValueSmallNumericAttribute & toBeSearched);

int32_t find(DocId docId, int32_t elemId, int32_t & weight) const {
if ( elemId != 0) return -1;
const Word &word = _wordData[docId >> _wordShift];
uint32_t valueShift = (docId & _valueShiftMask) << _valueShiftShift;
T v = (vespalib::atomic::load_ref_relaxed(word) >> valueShift) & _valueMask;
weight = 1;
return match(v) ? 0 : -1;
}

int32_t find(DocId docId, int32_t elemId) const {
if ( elemId != 0) return -1;
const Word &word = _wordData[docId >> _wordShift];
uint32_t valueShift = (docId & _valueShiftMask) << _valueShiftShift;
T v = (vespalib::atomic::load_ref_relaxed(word) >> valueShift) & _valueMask;
return match(v) ? 0 : -1;
}

Int64Range getAsIntegerTerm() const override;

std::unique_ptr<queryeval::SearchIterator>
createFilterIterator(fef::TermFieldMatchData * matchData, bool strict) override;
};

SingleValueSmallNumericAttribute(const vespalib::string & baseFileName, const Config &c, Word valueMask,
uint32_t valueShiftShift, uint32_t valueShiftMask, uint32_t wordShift);
Expand Down

0 comments on commit 00641e5

Please sign in to comment.