Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Several small (non-functional) improvements of the Server class. #514

Merged
merged 7 commits into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 3 additions & 10 deletions src/SparqlEngineMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,22 +212,15 @@ void processQuery(QueryExecutionContext& qec, const string& query) {
timer.stop();
LOG(INFO) << "Time to create Execution Tree: " << timer.msecs() << "ms\n";
LOG(INFO) << "Execution Tree: " << qet.asString() << "ms\n";
size_t limit = MAX_NOF_ROWS_IN_RESULT;
size_t offset = 0;
if (pq._limit.size() > 0) {
limit = static_cast<size_t>(atol(pq._limit.c_str()));
}
if (pq._offset.size() > 0) {
offset = static_cast<size_t>(atol(pq._offset.c_str()));
}
size_t limit = pq._limit.value_or(MAX_NOF_ROWS_IN_RESULT);
size_t offset = pq._offset.value_or(0);
qet.writeResultToStream(cout, pq._selectClause._selectedVariables, limit,
offset);
t.stop();
std::cout << "\nDone. Time: " << t.usecs() / 1000.0 << " ms\n";
size_t numMatches = qet.getResult()->size();
std::cout << "\nNumber of matches (no limit): " << numMatches << "\n";
size_t effectiveLimit =
atoi(pq._limit.c_str()) > 0 ? atoi(pq._limit.c_str()) : numMatches;
size_t effectiveLimit = pq._limit.value_or(numMatches);
std::cout << "\nNumber of matches (limit): "
<< std::min(numMatches, effectiveLimit) << "\n";
}
2 changes: 1 addition & 1 deletion src/engine/RuntimeInformation.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
#pragma once

#include <iostream>
#include <nlohmann/json.hpp>
#include <string>
#include <vector>

#include "../util/HashMap.h"
#include "../util/StringUtils.h"
#include "../util/Timer.h"
#include "../util/json.h"

class RuntimeInformation {
public:
Expand Down
93 changes: 39 additions & 54 deletions src/engine/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include <algorithm>
#include <cstring>
#include <nlohmann/json.hpp>
#include <sstream>
#include <string>
#include <thread>
Expand All @@ -18,6 +17,7 @@
#include "../util/HttpServer/UrlParser.h"
#include "../util/Log.h"
#include "../util/StringUtils.h"
#include "../util/json.h"
#include "QueryPlanner.h"

// _____________________________________________________________________________
Expand Down Expand Up @@ -118,10 +118,10 @@ boost::asio::awaitable<void> Server::process(
}

// _____________________________________________________________________________
string Server::composeResponseJson(const ParsedQuery& query,
const QueryExecutionTree& qet,
ad_utility::Timer& requestTimer,
size_t maxSend) const {
json Server::composeResponseJson(const ParsedQuery& query,
const QueryExecutionTree& qet,
ad_utility::Timer& requestTimer,
size_t maxSend) {
shared_ptr<const ResultTable> rt = qet.getResult();
requestTimer.stop();
off_t compResultUsecs = requestTimer.usecs();
Expand All @@ -139,51 +139,40 @@ string Server::composeResponseJson(const ParsedQuery& query,
qet.getRootOperation()->getRuntimeInfo());

{
size_t limit = MAX_NOF_ROWS_IN_RESULT;
size_t offset = 0;
if (query._limit.size() > 0) {
limit = static_cast<size_t>(atol(query._limit.c_str()));
}
if (query._offset.size() > 0) {
offset = static_cast<size_t>(atol(query._offset.c_str()));
}
size_t limit = query._limit.value_or(MAX_NOF_ROWS_IN_RESULT);
size_t offset = query._offset.value_or(0);
requestTimer.cont();
j["res"] = qet.writeResultAsJson(query._selectClause._selectedVariables,
std::min(limit, maxSend), offset);
requestTimer.stop();
}

requestTimer.stop();
j["time"]["total"] = std::to_string(requestTimer.usecs() / 1000.0) + "ms";
j["time"]["computeResult"] = std::to_string(compResultUsecs / 1000.0) + "ms";
j["time"]["total"] =
std::to_string(static_cast<double>(requestTimer.usecs()) / 1000.0) + "ms";
j["time"]["computeResult"] =
std::to_string(static_cast<double>(compResultUsecs) / 1000.0) + "ms";

return j.dump(4);
return j;
}

// _____________________________________________________________________________
string Server::composeResponseSepValues(const ParsedQuery& query,
const QueryExecutionTree& qet,
char sep) const {
char sep) {
std::ostringstream os;
size_t limit = std::numeric_limits<size_t>::max();
size_t offset = 0;
if (query._limit.size() > 0) {
limit = static_cast<size_t>(atol(query._limit.c_str()));
}
if (query._offset.size() > 0) {
offset = static_cast<size_t>(atol(query._offset.c_str()));
}
size_t limit = query._limit.value_or(MAX_NOF_ROWS_IN_RESULT);
size_t offset = query._offset.value_or(0);
qet.writeResultToStream(os, query._selectClause._selectedVariables, limit,
offset, sep);

return os.str();
}

// _____________________________________________________________________________
string Server::composeResponseJson(const string& query,
const std::exception& exception,
ad_utility::Timer& requestTimer) const {
std::ostringstream os;
json Server::composeResponseJson(const string& query,
const std::exception& exception,
ad_utility::Timer& requestTimer) {
requestTimer.stop();

json j;
Expand All @@ -193,32 +182,29 @@ string Server::composeResponseJson(const string& query,
j["time"]["total"] = requestTimer.msecs();
j["time"]["computeResult"] = requestTimer.msecs();
j["exception"] = exception.what();
return j.dump(4);
return j;
}

// _____________________________________________________________________________
string Server::composeStatsJson() const {
std::ostringstream os;
os << "{\n"
<< "\"kbindex\": \"" << _index.getKbName() << "\",\n"
<< "\"permutations\": \"" << (_index.hasAllPermutations() ? "6" : "2")
<< "\",\n";
json Server::composeStatsJson() const {
json result;
result["kbindex"] = _index.getKbName();
result["permutations"] = (_index.hasAllPermutations() ? 6 : 2);
if (_index.hasAllPermutations()) {
os << "\"nofsubjects\": \"" << _index.getNofSubjects() << "\",\n";
os << "\"nofpredicates\": \"" << _index.getNofPredicates() << "\",\n";
os << "\"nofobjects\": \"" << _index.getNofObjects() << "\",\n";
result["nofsubjects"] = _index.getNofSubjects();
result["nofpredicates"] = _index.getNofPredicates();
result["nofobjects"] = _index.getNofObjects();
}

auto [actualTriples, addedTriples] = _index.getNumTriplesActuallyAndAdded();
os << "\"noftriples\": \"" << _index.getNofTriples() << "\",\n"
<< "\"nofActualTriples\": \"" << actualTriples << "\",\n"
<< "\"nofAddedTriples\": \"" << addedTriples << "\",\n"
<< "\"textindex\": \"" << _index.getTextName() << "\",\n"
<< "\"nofrecords\": \"" << _index.getNofTextRecords() << "\",\n"
<< "\"nofwordpostings\": \"" << _index.getNofWordPostings() << "\",\n"
<< "\"nofentitypostings\": \"" << _index.getNofEntityPostings() << "\"\n"
<< "}\n";
return os.str();
result["noftriples"] = _index.getNofTriples();
result["nofActualTriples"] = actualTriples;
result["nofAddedTriples"] = addedTriples;
result["textindex"] = _index.getTextName();
result["nofrecords"] = _index.getNofTextRecords();
result["nofwordpostings"] = _index.getNofWordPostings();
result["nofentitypostings"] = _index.getNofEntityPostings();
return result;
}

// _______________________________________
Expand All @@ -241,14 +227,13 @@ boost::asio::awaitable<void> Server::processQuery(
const auto& query = params.at("query");
AD_CHECK(!query.empty());

auto sendJson =
[&request,
&send](std::string&& jsonString) -> boost::asio::awaitable<void> {
auto response = createJsonResponse(std::move(jsonString), request);
auto sendJson = [&request, &send](
const json& jsonString) -> boost::asio::awaitable<void> {
auto response = createJsonResponse(jsonString, request);
co_return co_await send(std::move(response));
};

std::optional<std::string> errorResponse;
std::optional<json> errorResponse;

try {
ad_utility::SharedConcurrentTimeoutTimer timeoutTimer =
Expand Down Expand Up @@ -314,6 +299,6 @@ boost::asio::awaitable<void> Server::processQuery(
errorResponse = composeResponseJson(query, e, requestTimer);
}
if (errorResponse.has_value()) {
co_return co_await sendJson(std::move(errorResponse.value()));
co_return co_await sendJson(errorResponse.value());
}
}
20 changes: 10 additions & 10 deletions src/engine/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,19 @@ class Server {
const ParamValueMap& params, ad_utility::Timer& requestTimer,
const ad_utility::httpUtils::HttpRequest auto& request, auto&& send);

string composeResponseJson(const ParsedQuery& query,
const QueryExecutionTree& qet,
ad_utility::Timer& requestTimer,
size_t sendMax = MAX_NOF_ROWS_IN_RESULT) const;

string composeResponseSepValues(const ParsedQuery& query,
static json composeResponseJson(const ParsedQuery& query,
const QueryExecutionTree& qet,
char sep) const;
ad_utility::Timer& requestTimer,
size_t sendMax = MAX_NOF_ROWS_IN_RESULT);

static string composeResponseSepValues(const ParsedQuery& query,
const QueryExecutionTree& qet,
char sep);

string composeResponseJson(const string& query, const std::exception& e,
ad_utility::Timer& requestTimer) const;
static json composeResponseJson(const string& query, const std::exception& e,
ad_utility::Timer& requestTimer);

string composeStatsJson() const;
json composeStatsJson() const;

json composeCacheStatsJson() const;
};
21 changes: 11 additions & 10 deletions src/index/Index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,8 @@ void Index::readConfiguration() {
f >> _configurationJson;
if (_configurationJson.find("external-literals") !=
_configurationJson.end()) {
_onDiskLiterals = _configurationJson["external-literals"];
_onDiskLiterals =
static_cast<bool>(_configurationJson["external-literals"]);
}

if (_configurationJson.find("prefixes") != _configurationJson.end()) {
Expand Down Expand Up @@ -1215,9 +1216,9 @@ void Index::readConfiguration() {
}

if (_configurationJson.count("locale")) {
std::string lang = _configurationJson["locale"]["language"];
std::string country = _configurationJson["locale"]["country"];
bool ignorePunctuation = _configurationJson["locale"]["ignore-punctuation"];
std::string lang{_configurationJson["locale"]["language"]};
std::string country{_configurationJson["locale"]["country"]};
bool ignorePunctuation{_configurationJson["locale"]["ignore-punctuation"]};
_vocab.setLocale(lang, country, ignorePunctuation);
_textVocab.setLocale(lang, country, ignorePunctuation);
} else {
Expand Down Expand Up @@ -1298,9 +1299,9 @@ void Index::initializeVocabularySettingsBuild() {
std::string country = LOCALE_DEFAULT_COUNTRY;
bool ignorePunctuation = LOCALE_DEFAULT_IGNORE_PUNCTUATION;
if (j.count("locale")) {
lang = j["locale"]["language"];
country = j["locale"]["country"];
ignorePunctuation = j["locale"]["ignore-punctuation"];
lang = std::string{j["locale"]["language"]};
country = std::string{j["locale"]["country"]};
ignorePunctuation = bool{j["locale"]["ignore-punctuation"]};
} else {
LOG(INFO) << "locale was not specified by the settings JSON, defaulting "
"to en US\n";
Expand Down Expand Up @@ -1332,7 +1333,7 @@ void Index::initializeVocabularySettingsBuild() {
}
if (j.count("ascii-prefixes-only")) {
if constexpr (std::is_same_v<std::decay_t<Parser>, TurtleParserDummy>) {
bool v = j["ascii-prefixes-only"];
bool v{j["ascii-prefixes-only"]};
if (v) {
LOG(WARN) << WARNING_ASCII_ONLY_PREFIXES;
_onlyAsciiTurtlePrefixes = true;
Expand All @@ -1348,7 +1349,7 @@ void Index::initializeVocabularySettingsBuild() {
}

if (j.count("num-triples-per-partial-vocab")) {
_numTriplesPerPartialVocab = j["num-triples-per-partial-vocab"];
_numTriplesPerPartialVocab = size_t{j["num-triples-per-partial-vocab"]};
LOG(INFO) << "Overriding setting num-triples-per-partial-vocab to "
<< _numTriplesPerPartialVocab
<< " This might influence performance / memory usage during "
Expand All @@ -1357,7 +1358,7 @@ void Index::initializeVocabularySettingsBuild() {
}

if (j.count("parser-batch-size")) {
_parserBatchSize = j["parser-batch-size"];
_parserBatchSize = size_t{j["parser-batch-size"]};
LOG(INFO) << "Overriding setting parser-batch-size to " << _parserBatchSize
<< " This might influence performance during index build."
<< std::endl;
Expand Down
2 changes: 1 addition & 1 deletion src/index/Index.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <array>
#include <fstream>
#include <memory>
#include <nlohmann/json.hpp>
#include <optional>
#include <string>
#include <stxxl/vector>
Expand All @@ -23,6 +22,7 @@
#include "../util/HashMap.h"
#include "../util/MmapVector.h"
#include "../util/Timer.h"
#include "../util/json.h"
#include "./CompressedRelation.h"
#include "./ConstantsIndexBuilding.h"
#include "./DocsDB.h"
Expand Down
10 changes: 7 additions & 3 deletions src/index/Vocabulary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

#include <fstream>
#include <iostream>
#include <nlohmann/json.hpp>

#include "../parser/RdfEscaping.h"
#include "../parser/Tokenizer.h"
#include "../util/BatchedPipeline.h"
#include "../util/File.h"
#include "../util/HashMap.h"
#include "../util/HashSet.h"
#include "../util/json.h"
#include "./ConstantsIndexBuilding.h"

using std::string;
Expand Down Expand Up @@ -320,7 +320,7 @@ template <class StringRange>
void Vocabulary<S, C>::initializeExternalizePrefixes(const StringRange& s) {
_externalizedPrefixes.clear();
for (const auto& el : s) {
_externalizedPrefixes.push_back(el);
_externalizedPrefixes.emplace_back(el);
}
}

Expand All @@ -329,7 +329,11 @@ template <class S, class C>
template <class StringRange>
void Vocabulary<S, C>::initializeInternalizedLangs(const StringRange& s) {
_internalizedLangs.clear();
_internalizedLangs.insert(_internalizedLangs.begin(), s.begin(), s.end());
// `StringRange` might be nlohmann::json, for which we have disabled
// implicit conversions, so `vector::insert` cannot be used.
for (const auto& el : s) {
_internalizedLangs.emplace_back(el);
}
}

// _____________________________________________________
Expand Down
8 changes: 6 additions & 2 deletions src/parser/ParsedQuery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,14 @@ string ParsedQuery::asString() const {
os << "\nWHERE: \n";
_rootGraphPattern.toString(os, 1);

os << "\nLIMIT: " << (_limit.size() > 0 ? _limit : "no limit specified");
os << "\nLIMIT: "
<< (_limit.has_value() ? std::to_string(_limit.value())
: "no limit specified");
os << "\nTEXTLIMIT: "
<< (_textLimit.size() > 0 ? _textLimit : "no limit specified");
os << "\nOFFSET: " << (_offset.size() > 0 ? _offset : "no offset specified");
os << "\nOFFSET: "
<< (_offset.has_value() ? std::to_string(_offset.value())
: "no offset specified");
os << "\nDISTINCT modifier is " << (_selectClause._distinct ? "" : "not ")
<< "present.";
os << "\nREDUCED modifier is " << (_selectClause._reduced ? "" : "not ")
Expand Down
4 changes: 2 additions & 2 deletions src/parser/ParsedQuery.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,9 @@ class ParsedQuery {
size_t _numGraphPatterns = 1;
vector<OrderKey> _orderBy;
vector<string> _groupByVariables;
string _limit;
std::optional<size_t> _limit = std::nullopt;
string _textLimit;
string _offset;
std::optional<size_t> _offset = std::nullopt;
string _originalString;
SelectClause _selectClause;

Expand Down