Skip to content

Commit f43dff9

Browse files
committed
Some improvements from a review with Hannah.
1 parent 7f8d909 commit f43dff9

File tree

9 files changed

+74
-69
lines changed

9 files changed

+74
-69
lines changed

src/engine/ExportQueryExecutionTrees.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,9 @@ ExportQueryExecutionTrees::idToStringAndType(const Index& index, Id id,
207207
index.idToOptionalString(id.getVocabIndex());
208208
AD_CONTRACT_CHECK(entity.has_value());
209209
// TODO<joka921> make this more efficient AND more correct
210-
auto litOrIri = ad_utility::triple_component::LiteralOrIri::
211-
fromInternalRepresentation(entity.value());
210+
auto litOrIri =
211+
ad_utility::triple_component::LiteralOrIri::fromStringRepresentation(
212+
entity.value());
212213
if constexpr (onlyReturnLiterals) {
213214
if (!litOrIri.isLiteral()) {
214215
return std::nullopt;

src/parser/Iri.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,27 @@
1111

1212
namespace ad_utility::triple_component {
1313
// __________________________________________
14-
Iri::Iri(NormalizedString iri) : iri_{std::move(iri)} {}
14+
Iri::Iri(std::string iri) : iri_{std::move(iri)} {}
1515

1616
// __________________________________________
1717
Iri::Iri(const Iri& prefix, NormalizedStringView suffix)
18-
: iri_{asNormalizedStringViewUnsafe("<") +
19-
NormalizedString{prefix.getContent()} + suffix +
20-
asNormalizedStringViewUnsafe(">")} {};
18+
: iri_{absl::StrCat("<"sv, asStringViewUnsafe(prefix.getContent()),
19+
asStringViewUnsafe(suffix), ">"sv)} {};
2120

2221
// __________________________________________
2322
NormalizedStringView Iri::getContent() const {
24-
return NormalizedStringView{iri_}.substr(1, iri_.size() - 2);
23+
return asNormalizedStringViewUnsafe(iri_).substr(1, iri_.size() - 2);
2524
}
2625

2726
// __________________________________________
2827
Iri Iri::fromIriref(std::string_view stringWithBrackets) {
2928
auto first = stringWithBrackets.find('<');
3029
AD_CORRECTNESS_CHECK(first != std::string_view::npos);
3130
return Iri{
32-
asNormalizedStringViewUnsafe(stringWithBrackets.substr(0, first + 1)) +
33-
RdfEscaping::normalizeIriWithBrackets(stringWithBrackets.substr(first)) +
34-
asNormalizedStringViewUnsafe(">")};
31+
absl::StrCat(stringWithBrackets.substr(0, first + 1),
32+
asStringViewUnsafe(RdfEscaping::normalizeIriWithBrackets(
33+
stringWithBrackets.substr(first))),
34+
">"sv)};
3535
}
3636

3737
// __________________________________________
@@ -41,14 +41,14 @@ Iri Iri::fromPrefixAndSuffix(const Iri& prefix, std::string_view suffix) {
4141
}
4242

4343
// __________________________________________
44-
Iri Iri::fromStringRepresentation(std::string_view s) {
44+
Iri Iri::fromStringRepresentation(std::string s) {
4545
AD_CORRECTNESS_CHECK(s.starts_with("<") || s.starts_with("@"));
46-
return Iri{NormalizedString{asNormalizedStringViewUnsafe(s)}};
46+
return Iri{std::move(s)};
4747
}
4848

4949
// __________________________________________
50-
std::string_view Iri::toStringRepresentation() const {
51-
return asStringViewUnsafe(iri_);
52-
}
50+
const std::string& Iri::toStringRepresentation() const { return iri_; }
51+
// __________________________________________
52+
std::string& Iri::toStringRepresentation() { return iri_; }
5353

5454
} // namespace ad_utility::triple_component

src/parser/Iri.h

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,15 @@
1010

1111
namespace ad_utility::triple_component {
1212

13-
// A class to hold IRIs. It does not store the leading or trailing
14-
// angled bracket.
15-
//
16-
// E.g. For the input "<http://example.org/books/book1>",
17-
// only "http://example.org/books/book1" is to be stored in the iri_ variable.
13+
// A class to hold IRIs.
1814
class Iri {
1915
private:
20-
// Store the string value of the IRI without any leading or trailing angled
16+
// Store the string value of the IRI including the angle brackets.
2117
// brackets.
22-
NormalizedString iri_;
18+
std::string iri_;
2319

2420
// Create a new iri object
25-
explicit Iri(NormalizedString iri);
21+
explicit Iri(std::string iri);
2622

2723
// Create a new iri using a prefix
2824
Iri(const Iri& prefix, NormalizedStringView suffix);
@@ -32,13 +28,13 @@ class Iri {
3228
Iri() = default;
3329
template <typename H>
3430
friend H AbslHashValue(H h, const std::same_as<Iri> auto& iri) {
35-
return H::combine(std::move(h),
36-
asStringViewUnsafe(NormalizedStringView(iri.iri_)));
31+
return H::combine(std::move(h), iri.iri_);
3732
}
3833
bool operator==(const Iri&) const = default;
39-
static Iri fromStringRepresentation(std::string_view s);
34+
static Iri fromStringRepresentation(std::string s);
4035

41-
std::string_view toStringRepresentation() const;
36+
const std::string& toStringRepresentation() const;
37+
std::string& toStringRepresentation();
4238

4339
// Create a new iri given an iri with brackets
4440
static Iri fromIriref(std::string_view stringWithBrackets);

src/parser/Literal.cpp

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99

1010
#include "parser/LiteralOrIri.h"
1111

12-
static constexpr NormalizedChar quote{'"'};
13-
static constexpr NormalizedChar at{'@'};
14-
static constexpr NormalizedChar hat{'^'};
12+
static constexpr char quote{'"'};
13+
static constexpr char at{'@'};
14+
static constexpr char hat{'^'};
1515

1616
namespace ad_utility::triple_component {
1717
// __________________________________________
18-
Literal::Literal(NormalizedString content, size_t beginOfSuffix)
18+
Literal::Literal(std::string content, size_t beginOfSuffix)
1919
: content_{std::move(content)}, beginOfSuffix_{beginOfSuffix} {
2020
AD_CORRECTNESS_CHECK(content_.starts_with(quote));
2121
AD_CORRECTNESS_CHECK(beginOfSuffix_ >= 2);
@@ -33,7 +33,7 @@ bool Literal::hasDatatype() const { return getSuffix().starts_with(hat); }
3333

3434
// __________________________________________
3535
NormalizedStringView Literal::getContent() const {
36-
return NormalizedStringView{content_}.substr(1, beginOfSuffix_ - 2);
36+
return content().substr(1, beginOfSuffix_ - 2);
3737
}
3838

3939
// __________________________________________
@@ -42,7 +42,7 @@ NormalizedStringView Literal::getDatatype() const {
4242
AD_THROW("The literal does not have an explicit datatype.");
4343
}
4444
// We don't return the enclosing <angle brackets>
45-
NormalizedStringView result = content_;
45+
NormalizedStringView result = content();
4646
result.remove_prefix(beginOfSuffix_ + 3);
4747
result.remove_suffix(1);
4848
return result;
@@ -53,7 +53,7 @@ NormalizedStringView Literal::getLanguageTag() const {
5353
if (!hasLanguageTag()) {
5454
AD_THROW("The literal does not have an explicit language tag.");
5555
}
56-
return NormalizedStringView{content_}.substr(beginOfSuffix_ + 1);
56+
return content().substr(beginOfSuffix_ + 1);
5757
}
5858

5959
// __________________________________________
@@ -80,8 +80,9 @@ Literal Literal::literalWithoutQuotes(
8080
Literal Literal::literalWithNormalizedContent(
8181
NormalizedString normalizedRdfContent,
8282
std::optional<std::variant<Iri, string>> descriptor) {
83-
auto quotes = asNormalizedStringViewUnsafe("\"");
84-
auto actualContent = quotes + std::move(normalizedRdfContent) + quotes;
83+
auto quotes = "\""sv;
84+
auto actualContent =
85+
absl::StrCat(quotes, asStringViewUnsafe(normalizedRdfContent), quotes);
8586
auto sz = actualContent.size();
8687
auto literal = Literal{std::move(actualContent), sz};
8788
if (!descriptor.has_value()) {
@@ -104,31 +105,34 @@ Literal Literal::literalWithNormalizedContent(
104105

105106
// __________________________________________
106107
void Literal::addLanguageTag(std::string_view languageTag) {
107-
content_.push_back(at);
108-
content_.append(RdfEscaping::normalizeLanguageTag(languageTag));
108+
AD_CORRECTNESS_CHECK(!hasDatatype() && !hasLanguageTag());
109+
if (languageTag.starts_with('@')) {
110+
absl::StrAppend(&content_, languageTag);
111+
} else {
112+
absl::StrAppend(&content_, "@"sv, languageTag);
113+
}
109114
}
110115

111116
// __________________________________________
112117
void Literal::addDatatype(const Iri& datatype) {
113-
content_.append(asNormalizedStringViewUnsafe("^^"));
114-
content_.append(
115-
asNormalizedStringViewUnsafe(datatype.toStringRepresentation()));
118+
AD_CORRECTNESS_CHECK(!hasDatatype() && !hasLanguageTag());
119+
absl::StrAppend(&content_, "^^"sv, datatype.toStringRepresentation());
116120
}
117121

118122
// __________________________________________
119-
std::string_view Literal::toStringRepresentation() const {
120-
return asStringViewUnsafe(content_);
121-
}
123+
const std::string& Literal::toStringRepresentation() const { return content_; }
124+
125+
// __________________________________________
126+
std::string& Literal::toStringRepresentation() { return content_; }
122127

123128
// __________________________________________
124-
Literal Literal::fromStringRepresentation(std::string_view internal) {
129+
Literal Literal::fromStringRepresentation(std::string internal) {
125130
// TODO<joka921> This is a little dangerous as there might be quotes in the
126131
// IRI which might lead to unexpected results here.
127132
AD_CORRECTNESS_CHECK(internal.starts_with('"'));
128133
auto endIdx = internal.rfind('"');
129134
AD_CORRECTNESS_CHECK(endIdx > 0);
130-
return Literal{NormalizedString{asNormalizedStringViewUnsafe(internal)},
131-
endIdx + 1};
135+
return Literal{std::move(internal), endIdx + 1};
132136
}
133137

134138
} // namespace ad_utility::triple_component

src/parser/Literal.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ class Literal {
1616
// For example `"Hello World"@en` or `"With"Quote"^^<someDatatype>` (note
1717
// that the quote in the middle is unescaped because this is the normalized
1818
// form that QLever stores.
19-
NormalizedString content_;
19+
std::string content_;
2020
// The position after the closing `"`, so either the size of the string, or
2121
// the position of the `@` or `^^` for literals with language tags or
2222
// datatypes.
2323
std::size_t beginOfSuffix_;
2424

2525
// Create a new literal without any descriptor
26-
explicit Literal(NormalizedString content, size_t beginOfSuffix_);
26+
explicit Literal(std::string content, size_t beginOfSuffix_);
2727

2828
// Similar to `fromEscapedRdfLiteral`, except the rdfContent is expected to
2929
// already be normalized
@@ -33,22 +33,27 @@ class Literal {
3333

3434
// Internal helper function. Return either the empty string (for a plain
3535
// literal), `@langtag` or `^^<datatypeIri>`.
36-
NormalizedStringView getSuffix() const {
37-
NormalizedStringView result = content_;
36+
std::string_view getSuffix() const {
37+
std::string_view result = content_;
3838
result.remove_prefix(beginOfSuffix_);
3939
return result;
4040
}
4141

42+
NormalizedStringView content() const {
43+
return asNormalizedStringViewUnsafe(content_);
44+
}
45+
4246
public:
4347
template <typename H>
4448
friend H AbslHashValue(H h, const std::same_as<Literal> auto& literal) {
45-
return H::combine(std::move(h), asStringViewUnsafe(literal.content_));
49+
return H::combine(std::move(h), literal.content_);
4650
}
4751
bool operator==(const Literal&) const = default;
4852

49-
std::string_view toStringRepresentation() const;
53+
const std::string& toStringRepresentation() const;
54+
std::string& toStringRepresentation();
5055

51-
static Literal fromStringRepresentation(std::string_view internal);
56+
static Literal fromStringRepresentation(std::string internal);
5257

5358
// Return true if the literal has an assigned language tag
5459
bool hasLanguageTag() const;

src/parser/LiteralOrIri.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,21 @@ class LiteralOrIri {
3737
// Create a new LiteralOrIri based on an Iri object
3838
explicit LiteralOrIri(Iri iri);
3939

40-
std::string toInternalRepresentation() const {
41-
auto impl = [](const auto& val) {
42-
return absl::StrCat(val.toStringRepresentation());
40+
const std::string& toStringRepresentation() const {
41+
auto impl = [](const auto& val) -> decltype(auto) {
42+
return val.toStringRepresentation();
4343
};
4444
return std::visit(impl, data_);
4545
}
4646

47-
static LiteralOrIri fromInternalRepresentation(std::string_view internal) {
47+
static LiteralOrIri fromStringRepresentation(std::string internal) {
4848
char tag = internal.front();
4949
if (tag == iriPrefixChar) {
50-
return LiteralOrIri{Iri::fromStringRepresentation(internal)};
50+
return LiteralOrIri{Iri::fromStringRepresentation(std::move(internal))};
5151
} else {
5252
AD_CORRECTNESS_CHECK(tag == literalPrefixChar);
53-
return LiteralOrIri{Literal::fromStringRepresentation(internal)};
53+
return LiteralOrIri{
54+
Literal::fromStringRepresentation(std::move(internal))};
5455
}
5556
}
5657
template <typename H>

src/parser/TripleComponent.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ std::string TripleComponent::toRdfLiteral() const {
7272
} else if (isString()) {
7373
return getString();
7474
} else if (isLiteral()) {
75-
return std::string{getLiteral().toStringRepresentation()};
75+
return getLiteral().toStringRepresentation();
7676
} else if (isIri()) {
77-
return std::string{getIri().toStringRepresentation()};
77+
return getIri().toStringRepresentation();
7878
} else {
7979
auto [value, type] =
8080
ExportQueryExecutionTrees::idToStringAndTypeForEncodedValue(

src/parser/TripleComponent.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ class TripleComponent {
191191
AD_CONTRACT_CHECK(!isString());
192192
if (isLiteral() || isIri()) {
193193
VocabIndex idx;
194-
const std::string_view content = [&]() {
194+
const std::string& content = [&]() -> const std::string& {
195195
if (isLiteral()) {
196196
return getLiteral().toStringRepresentation();
197197
} else {
@@ -224,16 +224,14 @@ class TripleComponent {
224224
// If `toValueId` could not convert to `Id`, we have a string, which we
225225
// look up in (and potentially add to) our local vocabulary.
226226
AD_CORRECTNESS_CHECK(isString() || isLiteral() || isIri());
227-
// TODO<joka921> Make the internal representation of the `Literal` and
228-
// `Iri` class movable.
229-
std::string newWord = [&]() -> std::string {
227+
std::string& newWord = [&]() -> std::string& {
230228
if (isString()) {
231229
return getString();
232230
} else {
233231
if (isLiteral()) {
234-
return std::string{getLiteral().toStringRepresentation()};
232+
return getLiteral().toStringRepresentation();
235233
} else {
236-
return std::string{getIri().toStringRepresentation()};
234+
return getIri().toStringRepresentation();
237235
}
238236
}
239237
}();

test/util/IndexTestHelpers.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ std::function<Id(const std::string&)> makeGetId(const Index& index) {
267267
return triple_component::LiteralOrIri::iriref(el);
268268
} else {
269269
AD_CONTRACT_CHECK(el.starts_with('\"'));
270-
return triple_component::LiteralOrIri::fromInternalRepresentation(el);
270+
return triple_component::LiteralOrIri::fromStringRepresentation(el);
271271
}
272272
}();
273273
auto id = index.getId(literalOrIri);

0 commit comments

Comments
 (0)