Skip to content

Commit

Permalink
Reformatting the TextSearchQuery.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Flixtastic committed Feb 13, 2025
1 parent aa57226 commit 5761b22
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 93 deletions.
211 changes: 118 additions & 93 deletions src/parser/TextSearchQuery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,105 +10,135 @@

namespace parsedQuery {

// ____________________________________________________________________________
void TextSearchQuery::throwSubjectVariableException(
std::string predString, const TripleComponent& subject) {

Check warning on line 15 in src/parser/TextSearchQuery.cpp

View check run for this annotation

Codecov / codecov/patch

src/parser/TextSearchQuery.cpp#L15

Added line #L15 was not covered by tests
if (!subject.isVariable()) {
throw TextSearchException(
absl::StrCat("The predicate <", std::move(predString),
"> needs a Variable as Variable as subject."));
}
}

Check warning on line 21 in src/parser/TextSearchQuery.cpp

View check run for this annotation

Codecov / codecov/patch

src/parser/TextSearchQuery.cpp#L17-L21

Added lines #L17 - L21 were not covered by tests

// ____________________________________________________________________________
void TextSearchQuery::throwSubjectAndObjectVariableException(
std::string predString, const TripleComponent& subject,
const TripleComponent& object) {

Check warning on line 26 in src/parser/TextSearchQuery.cpp

View check run for this annotation

Codecov / codecov/patch

src/parser/TextSearchQuery.cpp#L26

Added line #L26 was not covered by tests
if (!(subject.isVariable() && object.isVariable())) {
throw TextSearchException(
absl::StrCat("The predicate <", std::move(predString),
"> needs a Variable as subject and one as object."));
}
}

Check warning on line 32 in src/parser/TextSearchQuery.cpp

View check run for this annotation

Codecov / codecov/patch

src/parser/TextSearchQuery.cpp#L28-L32

Added lines #L28 - L32 were not covered by tests

// ____________________________________________________________________________
void TextSearchQuery::throwContainsWordOrEntity(
const TripleComponent& subject) {

Check warning on line 36 in src/parser/TextSearchQuery.cpp

View check run for this annotation

Codecov / codecov/patch

src/parser/TextSearchQuery.cpp#L36

Added line #L36 was not covered by tests
if (configVarToConfigs_[subject.getVariable()].isWordSearch_.has_value()) {
throw TextSearchException(
"Each search should have exactly one occurrence of either "
"<contains-word> or <contains-entity>");
}
}

Check warning on line 42 in src/parser/TextSearchQuery.cpp

View check run for this annotation

Codecov / codecov/patch

src/parser/TextSearchQuery.cpp#L38-L42

Added lines #L38 - L42 were not covered by tests

// ____________________________________________________________________________
void TextSearchQuery::predStringTextSearch(const Variable& subjectVar,
const Variable& objectVar) {

Check warning on line 46 in src/parser/TextSearchQuery.cpp

View check run for this annotation

Codecov / codecov/patch

src/parser/TextSearchQuery.cpp#L46

Added line #L46 was not covered by tests
if (configVarToConfigs_[objectVar].textVar_.has_value()) {
throw TextSearchException(
absl::StrCat("Each search should only be linked to a single text "
"Variable. The text variable was: ",
configVarToConfigs_[objectVar].textVar_.value().name(),
"The config variable was: ", objectVar.name()));
}
configVarToConfigs_[objectVar].textVar_ = subjectVar;
}

Check warning on line 55 in src/parser/TextSearchQuery.cpp

View check run for this annotation

Codecov / codecov/patch

src/parser/TextSearchQuery.cpp#L48-L55

Added lines #L48 - L55 were not covered by tests

// ____________________________________________________________________________
void TextSearchQuery::predStringContainsWord(
const Variable& subjectVar, const TripleComponent::Literal& objectLiteral) {
configVarToConfigs_[subjectVar].isWordSearch_ = true;
std::string_view literal = asStringViewUnsafe(objectLiteral.getContent());

Check warning on line 61 in src/parser/TextSearchQuery.cpp

View check run for this annotation

Codecov / codecov/patch

src/parser/TextSearchQuery.cpp#L59-L61

Added lines #L59 - L61 were not covered by tests
if (literal.empty()) {
throw TextSearchException(
"The predicate <contains-word> shouldn't have an empty literal as "
"object.");
}
configVarToConfigs_[subjectVar].word_ = literal;
}

Check warning on line 68 in src/parser/TextSearchQuery.cpp

View check run for this annotation

Codecov / codecov/patch

src/parser/TextSearchQuery.cpp#L63-L68

Added lines #L63 - L68 were not covered by tests

// ____________________________________________________________________________
void TextSearchQuery::predStringContainsEntity(const TripleComponent& subject,
const TripleComponent& object) {

Check warning on line 72 in src/parser/TextSearchQuery.cpp

View check run for this annotation

Codecov / codecov/patch

src/parser/TextSearchQuery.cpp#L72

Added line #L72 was not covered by tests
if (!subject.isVariable()) {
throw TextSearchException(
"The predicate <contains-entity> needs a Variable as subject and a "
"Literal or Variable as object.");
}
throwContainsWordOrEntity(subject);
configVarToConfigs_[subject.getVariable()].isWordSearch_ = false;

Check warning on line 79 in src/parser/TextSearchQuery.cpp

View check run for this annotation

Codecov / codecov/patch

src/parser/TextSearchQuery.cpp#L74-L79

Added lines #L74 - L79 were not covered by tests
if (object.isLiteral()) {
configVarToConfigs_[subject.getVariable()].entity_ =
std::string(asStringViewUnsafe(object.getLiteral().getContent()));

Check warning on line 82 in src/parser/TextSearchQuery.cpp

View check run for this annotation

Codecov / codecov/patch

src/parser/TextSearchQuery.cpp#L81-L82

Added lines #L81 - L82 were not covered by tests
} else if (object.isVariable()) {
configVarToConfigs_[subject.getVariable()].entity_ = object.getVariable();
} else {
throw TextSearchException(
"The predicate <contains-entity> needs a Variable as subject and a "
"Literal or Variable as object.");
}
}

Check warning on line 90 in src/parser/TextSearchQuery.cpp

View check run for this annotation

Codecov / codecov/patch

src/parser/TextSearchQuery.cpp#L84-L90

Added lines #L84 - L90 were not covered by tests

// ____________________________________________________________________________
void TextSearchQuery::predStringBindMatch(const Variable& subjectVar,
const Variable& objectVar) {

Check warning on line 94 in src/parser/TextSearchQuery.cpp

View check run for this annotation

Codecov / codecov/patch

src/parser/TextSearchQuery.cpp#L94

Added line #L94 was not covered by tests
if (configVarToConfigs_[subjectVar].varToBindMatch_.has_value()) {
throw TextSearchException(
"A text search should only contain one <bind-match>.");
}
configVarToConfigs_[subjectVar].varToBindMatch_ = objectVar;
}

Check warning on line 100 in src/parser/TextSearchQuery.cpp

View check run for this annotation

Codecov / codecov/patch

src/parser/TextSearchQuery.cpp#L96-L100

Added lines #L96 - L100 were not covered by tests

// ____________________________________________________________________________
void TextSearchQuery::predStringBindScore(const Variable& subjectVar,
const Variable& objectVar) {

Check warning on line 104 in src/parser/TextSearchQuery.cpp

View check run for this annotation

Codecov / codecov/patch

src/parser/TextSearchQuery.cpp#L104

Added line #L104 was not covered by tests
if (configVarToConfigs_[subjectVar].varToBindScore_.has_value()) {
throw TextSearchException(
"A text search should only contain one <bind-score>.");
}
configVarToConfigs_[subjectVar].varToBindScore_ = objectVar;
}

Check warning on line 110 in src/parser/TextSearchQuery.cpp

View check run for this annotation

Codecov / codecov/patch

src/parser/TextSearchQuery.cpp#L106-L110

Added lines #L106 - L110 were not covered by tests

// ____________________________________________________________________________
void TextSearchQuery::addParameter(const SparqlTriple& triple) {
auto simpleTriple = triple.getSimple();
TripleComponent subject = simpleTriple.s_;
TripleComponent predicate = simpleTriple.p_;
TripleComponent object = simpleTriple.o_;
const TripleComponent& subject = simpleTriple.s_;
const TripleComponent& predicate = simpleTriple.p_;
const TripleComponent& object = simpleTriple.o_;

Check warning on line 117 in src/parser/TextSearchQuery.cpp

View check run for this annotation

Codecov / codecov/patch

src/parser/TextSearchQuery.cpp#L113-L117

Added lines #L113 - L117 were not covered by tests

auto predString = extractParameterName(predicate, TEXT_SEARCH_IRI);

Check warning on line 119 in src/parser/TextSearchQuery.cpp

View check run for this annotation

Codecov / codecov/patch

src/parser/TextSearchQuery.cpp#L119

Added line #L119 was not covered by tests

auto throwVariableException = [predString, subject, object]() {
if (!(subject.isVariable() && object.isVariable())) {
throw TextSearchException(
absl::StrCat("The predicate <", predString,
"> needs a Variable as subject and one as object."));
}
};

if (predString == "text-search") {
if (!(subject.isVariable() && object.isVariable())) {
throw TextSearchException(
"The predicate <text-search> needs a Variable as subject and one as "
"object.");
}
if (configVarToConfigs_[object.getVariable()].textVar_.has_value()) {
throw TextSearchException(
"Each search should be linked to only on text Variable.");
}
configVarToConfigs_[object.getVariable()].textVar_ = subject.getVariable();
throwSubjectAndObjectVariableException("text-search", subject, object);
predStringTextSearch(subject.getVariable(), object.getVariable());

Check warning on line 123 in src/parser/TextSearchQuery.cpp

View check run for this annotation

Codecov / codecov/patch

src/parser/TextSearchQuery.cpp#L122-L123

Added lines #L122 - L123 were not covered by tests
} else if (predString == "contains-word") {
if (!(subject.isVariable() && object.isLiteral())) {
throw TextSearchException(
"The predicate <contains-word> needs a Variable as subject and a "
"Literal as object.");
}
if (configVarToConfigs_[subject.getVariable()].isWordSearch_.has_value()) {
if (configVarToConfigs_[subject.getVariable()].isWordSearch_.value()) {
throw TextSearchException(
"One search should only contain one <contains-word>.");
} else {
throw TextSearchException(
"One search should contain either <contains-word> or "
"<contains-entity>.");
}
}
configVarToConfigs_[subject.getVariable()].isWordSearch_ = true;
std::string_view literal = object.getLiteral().toStringRepresentation();
literal = std::string(literal.substr(1, literal.size() - 2));
if (literal.empty()) {
throwSubjectVariableException("contains-word", subject);
throwContainsWordOrEntity(subject);

Check warning on line 126 in src/parser/TextSearchQuery.cpp

View check run for this annotation

Codecov / codecov/patch

src/parser/TextSearchQuery.cpp#L125-L126

Added lines #L125 - L126 were not covered by tests
if (!object.isLiteral()) {
throw TextSearchException(
"The predicate <contains-word> shouldn't have an empty literal as "
"object.");
"The predicate <contains-word> only accepts a literal as object.");
}
configVarToConfigs_[subject.getVariable()].word_ = literal;
predStringContainsWord(subject.getVariable(), object.getLiteral());

Check warning on line 131 in src/parser/TextSearchQuery.cpp

View check run for this annotation

Codecov / codecov/patch

src/parser/TextSearchQuery.cpp#L128-L131

Added lines #L128 - L131 were not covered by tests
} else if (predString == "contains-entity") {
if (!subject.isVariable()) {
throw TextSearchException(
"The predicate <contains-entity> needs a Variable as subject and a "
"Literal or Variable as object.");
}
if (configVarToConfigs_[subject.getVariable()].isWordSearch_.has_value()) {
if (configVarToConfigs_[subject.getVariable()].isWordSearch_.value()) {
throw TextSearchException(
"One search should contain either <contains-word> or "
"<contains-entity>.");
} else {
throw TextSearchException(
"One search should only contain one <contains-entity>.");
}
}
configVarToConfigs_[subject.getVariable()].isWordSearch_ = false;
if (object.isLiteral()) {
std::string_view literal = object.getLiteral().toStringRepresentation();
configVarToConfigs_[subject.getVariable()].entity_ =
std::string(literal.substr(1, literal.size() - 2));
} else if (object.isVariable()) {
configVarToConfigs_[subject.getVariable()].entity_ = object.getVariable();
} else {
throw TextSearchException(
"The predicate <contains-entity> needs a Variable as subject and a "
"Literal or Variable as object.");
}
throwSubjectVariableException("contains-word", subject);
throwContainsWordOrEntity(subject);
predStringContainsEntity(subject, object);

Check warning on line 135 in src/parser/TextSearchQuery.cpp

View check run for this annotation

Codecov / codecov/patch

src/parser/TextSearchQuery.cpp#L133-L135

Added lines #L133 - L135 were not covered by tests
} else if (predString == "bind-match") {
throwVariableException();
if (configVarToConfigs_[subject.getVariable()]
.varToBindMatch_.has_value()) {
throw TextSearchException(
"A text search should only contain one <bind-match>.");
}
configVarToConfigs_[subject.getVariable()].varToBindMatch_ =
object.getVariable();
throwSubjectAndObjectVariableException("bind-match", subject, object);
predStringBindMatch(subject.getVariable(), object.getVariable());

Check warning on line 138 in src/parser/TextSearchQuery.cpp

View check run for this annotation

Codecov / codecov/patch

src/parser/TextSearchQuery.cpp#L137-L138

Added lines #L137 - L138 were not covered by tests
} else if (predString == "bind-score") {
throwVariableException();
if (configVarToConfigs_[subject.getVariable()]
.varToBindScore_.has_value()) {
throw TextSearchException(
"A text search should only contain one <bind-score>.");
}
configVarToConfigs_[subject.getVariable()].varToBindScore_ =
object.getVariable();
throwSubjectAndObjectVariableException("bind-score", subject, object);
predStringBindScore(subject.getVariable(), object.getVariable());
}
}

Check warning on line 143 in src/parser/TextSearchQuery.cpp

View check run for this annotation

Codecov / codecov/patch

src/parser/TextSearchQuery.cpp#L140-L143

Added lines #L140 - L143 were not covered by tests

Expand All @@ -132,13 +162,8 @@ TextSearchQuery::toConfigs(QueryExecutionContext* qec) const {
"Text search service needs a text variable to search.");
}

Check warning on line 163 in src/parser/TextSearchQuery.cpp

View check run for this annotation

Codecov / codecov/patch

src/parser/TextSearchQuery.cpp#L161-L163

Added lines #L161 - L163 were not covered by tests
if (conf.isWordSearch_.value()) {
auto it = potentialTermsForCvar.find(conf.textVar_.value());
if (it == potentialTermsForCvar.end()) {
potentialTermsForCvar.insert(
{conf.textVar_.value(), {conf.word_.value()}});
} else {
it->second.push_back(conf.word_.value());
}
potentialTermsForCvar[conf.textVar_.value()].push_back(
conf.word_.value());
}
}

Check warning on line 168 in src/parser/TextSearchQuery.cpp

View check run for this annotation

Codecov / codecov/patch

src/parser/TextSearchQuery.cpp#L165-L168

Added lines #L165 - L168 were not covered by tests
// Second pass to create all configs
Expand Down
28 changes: 28 additions & 0 deletions src/parser/TextSearchQuery.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,34 @@ struct TextSearchQuery : MagicServiceQuery {
std::vector<std::variant<TextIndexScanForWordConfiguration,
TextIndexScanForEntityConfiguration>>
toConfigs(QueryExecutionContext* qec) const;

// Helper functions for addParameter

// Checks if subject is a variable. If not throws exception.
void throwSubjectVariableException(std::string predString,
const TripleComponent& subject);
// Checks if object and subject are variables. If not throws exception.
void throwSubjectAndObjectVariableException(std::string predString,
const TripleComponent& subject,
const TripleComponent& object);
// Checks if query already encountered <contains-word> or <contains-entity>
// before this. If yes throws exception.
void throwContainsWordOrEntity(const TripleComponent& subject);

void predStringTextSearch(const Variable& subjectVar,
const Variable& objectVar);

void predStringContainsWord(const Variable& subjectVar,
const TripleComponent::Literal& objectLiteral);

void predStringContainsEntity(const TripleComponent& subject,
const TripleComponent& object);

void predStringBindMatch(const Variable& subjectVar,
const Variable& objectVar);

void predStringBindScore(const Variable& subjectVar,
const Variable& objectVar);
};

} // namespace parsedQuery

0 comments on commit 5761b22

Please sign in to comment.