From 7f1298bf3f5859a187b18a8ccf61d0c8de3159e0 Mon Sep 17 00:00:00 2001 From: Volodymyr Yavdoshenko Date: Fri, 9 May 2025 18:57:55 +0300 Subject: [PATCH] fix: review comments --- src/core/search/ast_expr.cc | 12 ------------ src/core/search/ast_expr.h | 30 +++++++++--------------------- src/core/search/lexer.lex | 4 ++-- src/core/search/search.cc | 28 ++++++++++++++-------------- src/core/search/tag_types.h | 13 +++++++++++++ 5 files changed, 38 insertions(+), 49 deletions(-) create mode 100644 src/core/search/tag_types.h diff --git a/src/core/search/ast_expr.cc b/src/core/search/ast_expr.cc index 3c7fc6669..30fdfcac1 100644 --- a/src/core/search/ast_expr.cc +++ b/src/core/search/ast_expr.cc @@ -16,18 +16,6 @@ using namespace std; namespace dfly::search { -AstTermNode::AstTermNode(string term) : term{std::move(term)} { -} - -AstPrefixNode::AstPrefixNode(string prefix) : prefix{std::move(prefix)} { -} - -AstSuffixNode::AstSuffixNode(string suffix) : suffix{std::move(suffix)} { -} - -AstInfixNode::AstInfixNode(string infix) : infix{std::move(infix)} { -} - AstRangeNode::AstRangeNode(double lo, bool lo_excl, double hi, bool hi_excl) : lo{lo_excl ? nextafter(lo, hi) : lo}, hi{hi_excl ? nextafter(hi, lo) : hi} { } diff --git a/src/core/search/ast_expr.h b/src/core/search/ast_expr.h index 4df1fc5c9..be911d696 100644 --- a/src/core/search/ast_expr.h +++ b/src/core/search/ast_expr.h @@ -12,6 +12,7 @@ #include #include "core/search/base.h" +#include "core/search/tag_types.h" namespace dfly { @@ -25,30 +26,17 @@ struct AstStarNode {}; // Matches all documents where this field has a non-null value struct AstStarFieldNode {}; -// Matches terms in text fields -struct AstTermNode { - explicit AstTermNode(std::string term); +template struct AstAffixNode { + explicit AstAffixNode(std::string affix) : affix{std::move(affix)} { + } - std::string term; + std::string affix; }; -struct AstPrefixNode { - explicit AstPrefixNode(std::string prefix); - - std::string prefix; -}; - -struct AstSuffixNode { - explicit AstSuffixNode(std::string suffix); - - std::string suffix; -}; - -struct AstInfixNode { - explicit AstInfixNode(std::string infix); - - std::string infix; -}; +using AstTermNode = AstAffixNode; +using AstPrefixNode = AstAffixNode; +using AstSuffixNode = AstAffixNode; +using AstInfixNode = AstAffixNode; // Matches numeric range struct AstRangeNode { diff --git a/src/core/search/lexer.lex b/src/core/search/lexer.lex index 8cc33a9ee..c8a91fbbe 100644 --- a/src/core/search/lexer.lex +++ b/src/core/search/lexer.lex @@ -1,6 +1,7 @@ %top{ // Our lexer need to know about Parser::symbol_type #include "core/search/parser.hh" + #include "core/search/tag_types.h" // Include TagType enum } %{ @@ -25,8 +26,7 @@ // A number symbol corresponding to the value in S. using dfly::search::Parser; using namespace std; - - enum class TagType { PREFIX, SUFFIX, INFIX, REGULAR }; + using dfly::search::TagType; Parser::symbol_type make_StringLit(string_view src, const Parser::location_type& loc); Parser::symbol_type make_Tag(string_view src, TagType type, const Parser::location_type& loc); diff --git a/src/core/search/search.cc b/src/core/search/search.cc index 6b90babcc..b4534a858 100644 --- a/src/core/search/search.cc +++ b/src/core/search/search.cc @@ -118,16 +118,16 @@ struct ProfileBuilder { string GetNodeInfo(const AstNode& node) { struct NodeFormatter { void operator()(std::string* out, const AstPrefixNode& node) const { - out->append(node.prefix); + out->append(node.affix); } void operator()(std::string* out, const AstSuffixNode& node) const { - out->append(node.suffix); + out->append(node.affix); } void operator()(std::string* out, const AstInfixNode& node) const { - out->append(node.infix); + out->append(node.affix); } void operator()(std::string* out, const AstTermNode& node) const { - out->append(node.term); + out->append(node.affix); } void operator()(std::string* out, const AstTagsNode::TagValue& value) const { visit([this, out](const auto& n) { this->operator()(out, n); }, value); @@ -135,10 +135,10 @@ struct ProfileBuilder { }; Overloaded node_info{ [](monostate) -> string { return ""s; }, - [](const AstTermNode& n) { return absl::StrCat("Term{", n.term, "}"); }, - [](const AstPrefixNode& n) { return absl::StrCat("Prefix{", n.prefix, "}"); }, - [](const AstSuffixNode& n) { return absl::StrCat("Suffix{", n.suffix, "}"); }, - [](const AstInfixNode& n) { return absl::StrCat("Infix{", n.infix, "}"); }, + [](const AstTermNode& n) { return absl::StrCat("Term{", n.affix, "}"); }, + [](const AstPrefixNode& n) { return absl::StrCat("Prefix{", n.affix, "}"); }, + [](const AstSuffixNode& n) { return absl::StrCat("Suffix{", n.affix, "}"); }, + [](const AstInfixNode& n) { return absl::StrCat("Infix{", n.affix, "}"); }, [](const AstRangeNode& n) { return absl::StrCat("Range{", n.lo, "<>", n.hi, "}"); }, [](const AstLogicalNode& n) { auto op = n.op == AstLogicalNode::AND ? "and" : "or"; @@ -301,7 +301,7 @@ struct BasicSearch { // "term": access field's text index or unify results from all text indices if no field is set IndexResult Search(const AstTermNode& node, string_view active_field) { - std::string term = node.term; + std::string term = node.affix; bool strip_whitespace = true; if (auto synonyms = indices_->GetSynonyms(); synonyms) { @@ -363,7 +363,7 @@ struct BasicSearch { } auto mapping = [&node, this](TextIndex* index) { - return CollectPrefixMatches(index, node.prefix); + return CollectPrefixMatches(index, node.affix); }; return UnifyResults(GetSubResults(indices, mapping), LogicOp::OR); } @@ -422,16 +422,16 @@ struct BasicSearch { return IndexResult{}; Overloaded ov{[tag_index](const AstTermNode& term) -> IndexResult { - return tag_index->Matching(term.term); + return tag_index->Matching(term.affix); }, [tag_index, this](const AstPrefixNode& prefix) { - return CollectPrefixMatches(tag_index, prefix.prefix); + return CollectPrefixMatches(tag_index, prefix.affix); }, [tag_index, this](const AstSuffixNode& suffix) { - return CollectSuffixMatches(tag_index, suffix.suffix); + return CollectSuffixMatches(tag_index, suffix.affix); }, [tag_index, this](const AstInfixNode& infix) { - return CollectInfixMatches(tag_index, infix.infix); + return CollectInfixMatches(tag_index, infix.affix); }}; auto mapping = [ov](const auto& tag) { return visit(ov, tag); }; return UnifyResults(GetSubResults(node.tags, mapping), LogicOp::OR); diff --git a/src/core/search/tag_types.h b/src/core/search/tag_types.h new file mode 100644 index 000000000..42166ca9b --- /dev/null +++ b/src/core/search/tag_types.h @@ -0,0 +1,13 @@ +// Copyright 2023, DragonflyDB authors. All rights reserved. +// See LICENSE for licensing terms. +// + +#pragma once + +namespace dfly { +namespace search { + +enum class TagType { PREFIX, SUFFIX, INFIX, REGULAR }; + +} // namespace search +} // namespace dfly