Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
Make filter/prepared smaller, important for removes
Browse files Browse the repository at this point in the history
  • Loading branch information
MBkkt committed Dec 23, 2023
1 parent 5ba9866 commit c2802a6
Show file tree
Hide file tree
Showing 27 changed files with 111 additions and 112 deletions.
2 changes: 1 addition & 1 deletion core/search/all_docs_provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

namespace irs {

FilterWithBoost::Ptr AllDocsProvider::Default(score_t boost) {
AllDocsProvider::Ptr AllDocsProvider::Default(score_t boost) {
auto filter = std::make_unique<all>();
filter->boost(boost);
return filter;
Expand Down
9 changes: 4 additions & 5 deletions core/search/all_docs_provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,12 @@ namespace irs {

class AllDocsProvider {
public:
using ProviderFunc = std::function<FilterWithBoost::Ptr(score_t)>;
using Ptr = std::unique_ptr<FilterWithBoost>;
using ProviderFunc = std::function<Ptr(score_t)>;

static FilterWithBoost::Ptr Default(score_t boost);
static Ptr Default(score_t boost);

FilterWithBoost::Ptr MakeAllDocsFilter(score_t boost) const {
return all_docs_(boost);
}
Ptr MakeAllDocsFilter(score_t boost) const { return all_docs_(boost); }

void SetProvider(ProviderFunc&& provider) {
all_docs_ = provider ? std::move(provider) : ProviderFunc{&Default};
Expand Down
2 changes: 1 addition & 1 deletion core/search/all_filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace irs {
// Filter returning all documents
class all : public FilterWithBoost {
public:
filter::prepared::ptr prepare(const PrepareContext& ctx) const final;
prepared::ptr prepare(const PrepareContext& ctx) const final;

irs::type_info::type_id type() const noexcept final {
return irs::type<all>::id();
Expand Down
12 changes: 6 additions & 6 deletions core/search/boolean_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ filter::prepared::ptr boolean_filter::prepare(const PrepareContext& ctx) const {
std::vector<const filter*> incl;
std::vector<const filter*> excl;

FilterWithBoost::Ptr all_docs_zero_boost;
FilterWithBoost::Ptr all_docs_no_boost;
AllDocsProvider::Ptr all_docs_zero_boost;
AllDocsProvider::Ptr all_docs_no_boost;

group_filters(all_docs_zero_boost, incl, excl);

Expand All @@ -89,7 +89,7 @@ filter::prepared::ptr boolean_filter::prepare(const PrepareContext& ctx) const {
return PrepareBoolean(incl, excl, ctx);
}

void boolean_filter::group_filters(FilterWithBoost::Ptr& all_docs_zero_boost,
void boolean_filter::group_filters(AllDocsProvider::Ptr& all_docs_zero_boost,
std::vector<const filter*>& incl,
std::vector<const filter*>& excl) const {
incl.reserve(size() / 2);
Expand All @@ -98,7 +98,7 @@ void boolean_filter::group_filters(FilterWithBoost::Ptr& all_docs_zero_boost,
const filter* empty_filter = nullptr;
const auto is_or = type() == irs::type<Or>::id();
for (const auto& filter : *this) {
if (irs::type<irs::empty>::id() == filter->type()) {
if (irs::type<Empty>::id() == filter->type()) {
empty_filter = filter.get();
continue;
}
Expand Down Expand Up @@ -143,7 +143,7 @@ filter::prepared::ptr And::PrepareBoolean(std::vector<const filter*>& incl,
// optimization step
// if include group empty itself or has 'empty' -> this whole conjunction is
// empty
if (incl.empty() || incl.back()->type() == irs::type<irs::empty>::id()) {
if (incl.empty() || incl.back()->type() == irs::type<Empty>::id()) {
return prepared::empty();
}

Expand Down Expand Up @@ -228,7 +228,7 @@ filter::prepared::ptr Or::PrepareBoolean(std::vector<const filter*>& incl,
return MakeAllDocsFilter(kNoBoost)->prepare(sub_ctx);
}

if (!incl.empty() && incl.back()->type() == irs::type<irs::empty>::id()) {
if (!incl.empty() && incl.back()->type() == irs::type<Empty>::id()) {
incl.pop_back();
}

Expand Down
8 changes: 2 additions & 6 deletions core/search/boolean_filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class boolean_filter : public FilterWithBoost, public AllDocsProvider {
const PrepareContext& ctx) const = 0;

private:
void group_filters(FilterWithBoost::Ptr& all_docs_zero_boost,
void group_filters(AllDocsProvider::Ptr& all_docs_zero_boost,
std::vector<const filter*>& incl,
std::vector<const filter*>& excl) const;

Expand Down Expand Up @@ -114,7 +114,7 @@ class Or final : public boolean_filter {
};

// Represents negation
class Not : public FilterWithBoost, public AllDocsProvider {
class Not : public FilterWithType<Not>, public AllDocsProvider {
public:
const filter* filter() const { return filter_.get(); }

Expand All @@ -135,10 +135,6 @@ class Not : public FilterWithBoost, public AllDocsProvider {

prepared::ptr prepare(const PrepareContext& ctx) const final;

type_info::type_id type() const noexcept final {
return irs::type<Not>::id();
}

protected:
bool equals(const irs::filter& rhs) const noexcept final;

Expand Down
4 changes: 2 additions & 2 deletions core/search/column_existence_filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ struct by_column_existence_options {

// User-side column existence filter
class by_column_existence final
: public filter_base<by_column_existence_options> {
: public FilterWithField<by_column_existence_options> {
public:
filter::prepared::ptr prepare(const PrepareContext& ctx) const final;
prepared::ptr prepare(const PrepareContext& ctx) const final;
};

} // namespace irs
6 changes: 3 additions & 3 deletions core/search/filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ EmptyQuery kEmptyQuery;
} // namespace

filter::prepared::ptr filter::prepared::empty() {
return memory::to_managed<filter::prepared>(kEmptyQuery);
return memory::to_managed<prepared>(kEmptyQuery);
}

filter::prepared::ptr empty::prepare(const PrepareContext& /*ctx*/) const {
return memory::to_managed<filter::prepared>(kEmptyQuery);
filter::prepared::ptr Empty::prepare(const PrepareContext& /*ctx*/) const {
return memory::to_managed<prepared>(kEmptyQuery);
}

} // namespace irs
39 changes: 21 additions & 18 deletions core/search/filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,17 @@ class filter {
virtual void visit(const SubReader& segment, PreparedStateVisitor& visitor,
score_t boost) const = 0;

// test only member
virtual score_t boost() const noexcept = 0;
};

using ptr = std::unique_ptr<filter>;

virtual ~filter() = default;

bool operator==(const filter& rhs) const noexcept { return equals(rhs); }
IRS_FORCE_INLINE bool operator==(const filter& rhs) const noexcept {
return equals(rhs);
}

virtual prepared::ptr prepare(const PrepareContext& ctx) const = 0;

Expand All @@ -97,8 +100,6 @@ class filter {

class FilterWithBoost : public filter {
public:
using Ptr = std::unique_ptr<FilterWithBoost>;

score_t boost() const noexcept { return boost_; }

void boost(score_t boost) noexcept { boost_ = boost; }
Expand All @@ -109,43 +110,49 @@ class FilterWithBoost : public filter {
score_t boost_ = kNoBoost;
};

template<typename Type>
class FilterWithType : public FilterWithBoost {
public:
using filter_type = Type;

type_info::type_id type() const noexcept final {
return irs::type<Type>::id();
}
};

// Convenient base class filters with options
template<typename Options>
class filter_with_options : public FilterWithBoost {
class FilterWithOptions : public FilterWithType<typename Options::filter_type> {
public:
using options_type = Options;
using filter_type = typename options_type::filter_type;

const options_type& options() const noexcept { return options_; }
options_type* mutable_options() noexcept { return &options_; }

type_info::type_id type() const noexcept final {
return irs::type<filter_type>::id();
}

protected:
bool equals(const filter& rhs) const noexcept override {
return filter::equals(rhs) &&
options_ == DownCast<filter_type>(rhs).options_;
}

private:
options_type options_;
IRS_NO_UNIQUE_ADDRESS options_type options_;
};

// Convenient base class for single field filters
template<typename Options>
class filter_base : public filter_with_options<Options> {
class FilterWithField : public FilterWithOptions<Options> {
public:
using options_type = typename filter_with_options<Options>::options_type;
using options_type = typename FilterWithOptions<Options>::options_type;
using filter_type = typename options_type::filter_type;

std::string_view field() const noexcept { return field_; }
std::string* mutable_field() noexcept { return &field_; }

protected:
bool equals(const filter& rhs) const noexcept final {
return filter_with_options<options_type>::equals(rhs) &&
return FilterWithOptions<options_type>::equals(rhs) &&
field_ == DownCast<filter_type>(rhs).field_;
}

Expand All @@ -154,13 +161,9 @@ class filter_base : public filter_with_options<Options> {
};

// Filter which returns no documents
class empty final : public FilterWithBoost {
class Empty final : public FilterWithType<Empty> {
public:
filter::prepared::ptr prepare(const PrepareContext& ctx) const final;

type_info::type_id type() const noexcept final {
return irs::type<empty>::id();
}
prepared::ptr prepare(const PrepareContext& ctx) const final;
};

struct filter_visitor;
Expand Down
12 changes: 6 additions & 6 deletions core/search/granular_range_filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,18 @@ void set_granular_term(by_granular_range_options::terms& boundary,
/// termA@0 + termA@2 + termA@5 + termA@10
/// termB@0 + termB@2 + termB@6 + termB@10
//////////////////////////////////////////////////////////////////////////////
class by_granular_range : public filter_base<by_granular_range_options> {
class by_granular_range : public FilterWithField<by_granular_range_options> {
public:
static filter::prepared::ptr prepare(const PrepareContext& ctx,
std::string_view field,
const options_type::range_type& rng,
size_t scored_terms_limit);
static prepared::ptr prepare(const PrepareContext& ctx,
std::string_view field,
const options_type::range_type& rng,
size_t scored_terms_limit);

static void visit(const SubReader& segment, const term_reader& reader,
const options_type::range_type& rng,
filter_visitor& visitor);

filter::prepared::ptr prepare(const PrepareContext& ctx) const final {
prepared::ptr prepare(const PrepareContext& ctx) const final {
return prepare(ctx.Boost(boost()), field(), options().range,
options().scored_terms_limit);
}
Expand Down
6 changes: 3 additions & 3 deletions core/search/levenshtein_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ filter::prepared::ptr by_edit_distance::prepare(
bool with_transpositions, bytes_view prefix) {
return executeLevenshtein(
max_distance, provider, with_transpositions, prefix, term,
[]() -> filter::prepared::ptr { return prepared::empty(); },
[&]() -> filter::prepared::ptr {
[]() -> prepared::ptr { return prepared::empty(); },
[&]() -> prepared::ptr {
if (!prefix.empty() && !term.empty()) {
bstring target;
target.reserve(prefix.size() + term.size());
Expand All @@ -300,7 +300,7 @@ filter::prepared::ptr by_edit_distance::prepare(
},
[&, scored_terms_limit](const parametric_description& d,
const bytes_view prefix,
const bytes_view term) -> filter::prepared::ptr {
const bytes_view term) -> prepared::ptr {
return prepare_levenshtein_filter(ctx, field, prefix, term,
scored_terms_limit, d);
});
Expand Down
5 changes: 3 additions & 2 deletions core/search/levenshtein_filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ struct by_edit_distance_options : by_edit_distance_all_options {
/// @class by_edit_distance
/// @brief user-side levenstein filter
////////////////////////////////////////////////////////////////////////////////
class by_edit_distance final : public filter_base<by_edit_distance_options> {
class by_edit_distance final
: public FilterWithField<by_edit_distance_options> {
public:
static prepared::ptr prepare(const PrepareContext& ctx,
std::string_view field, bytes_view term,
Expand All @@ -100,7 +101,7 @@ class by_edit_distance final : public filter_base<by_edit_distance_options> {

static field_visitor visitor(const by_edit_distance_all_options& options);

filter::prepared::ptr prepare(const PrepareContext& ctx) const final {
prepared::ptr prepare(const PrepareContext& ctx) const final {
auto sub_ctx = ctx;
sub_ctx.boost *= boost();
return prepare(sub_ctx, field(), options().term, options().max_terms,
Expand Down
2 changes: 1 addition & 1 deletion core/search/nested_filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ struct ByNestedOptions {
};

// Filter is capable of finding parents by the corresponding child filter.
class ByNestedFilter final : public filter_with_options<ByNestedOptions> {
class ByNestedFilter final : public FilterWithOptions<ByNestedOptions> {
public:
prepared::ptr prepare(const PrepareContext& ctx) const final;
};
Expand Down
3 changes: 2 additions & 1 deletion core/search/ngram_similarity_filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ struct by_ngram_similarity_options {
}
};

class by_ngram_similarity : public filter_base<by_ngram_similarity_options> {
class by_ngram_similarity
: public FilterWithField<by_ngram_similarity_options> {
public:
static prepared::ptr Prepare(const PrepareContext& ctx,
std::string_view field_name,
Expand Down
5 changes: 2 additions & 3 deletions core/search/phrase_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,8 @@ filter::prepared::ptr by_phrase::Prepare(const PrepareContext& ctx,
if (1 == options.size()) {
auto query =
std::visit(PrepareVisitor{ctx, field}, options.begin()->second);
if (query) {
return query;
}
IRS_ASSERT(query);
return query;
}

// prepare phrase stats (collector for each term)
Expand Down
2 changes: 1 addition & 1 deletion core/search/phrase_filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class by_phrase_options {
bool is_simple_term_only_{true};
};

class by_phrase : public filter_base<by_phrase_options> {
class by_phrase : public FilterWithField<by_phrase_options> {
public:
static prepared::ptr Prepare(const PrepareContext& ctx,
std::string_view field,
Expand Down
4 changes: 2 additions & 2 deletions core/search/prefix_filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ struct by_prefix_options : by_prefix_filter_options {
/// @class by_prefix
/// @brief user-side prefix filter
////////////////////////////////////////////////////////////////////////////////
class by_prefix : public filter_base<by_prefix_options> {
class by_prefix : public FilterWithField<by_prefix_options> {
public:
static prepared::ptr prepare(const PrepareContext& ctx,
std::string_view field, bytes_view prefix,
Expand All @@ -73,7 +73,7 @@ class by_prefix : public filter_base<by_prefix_options> {
static void visit(const SubReader& segment, const term_reader& reader,
bytes_view prefix, filter_visitor& visitor);

filter::prepared::ptr prepare(const PrepareContext& ctx) const final {
prepared::ptr prepare(const PrepareContext& ctx) const final {
auto sub_ctx = ctx;
sub_ctx.boost *= boost();
return prepare(sub_ctx, field(), options().term,
Expand Down
2 changes: 1 addition & 1 deletion core/search/proxy_filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class proxy_filter final : public filter {
public:
using cache_ptr = std::shared_ptr<proxy_query_cache>;

filter::prepared::ptr prepare(const PrepareContext& ctx) const final;
prepared::ptr prepare(const PrepareContext& ctx) const final;

template<typename Impl, typename Base = Impl, typename... Args>
std::pair<Base&, cache_ptr> set_filter(IResourceManager& memory,
Expand Down
4 changes: 2 additions & 2 deletions core/search/range_filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ struct by_range_options : by_range_filter_options {
/// @class by_range
/// @brief user-side term range filter
//////////////////////////////////////////////////////////////////////////////
class by_range : public filter_base<by_range_options> {
class by_range : public FilterWithField<by_range_options> {
public:
static prepared::ptr prepare(const PrepareContext& ctx,
std::string_view field,
Expand All @@ -78,7 +78,7 @@ class by_range : public filter_base<by_range_options> {
const options_type::range_type& rng,
filter_visitor& visitor);

filter::prepared::ptr prepare(const PrepareContext& ctx) const final {
prepared::ptr prepare(const PrepareContext& ctx) const final {
return prepare(ctx.Boost(boost()), field(), options().range,
options().scored_terms_limit);
}
Expand Down
Loading

0 comments on commit c2802a6

Please sign in to comment.