1
0
Fork 0

Add isQueryKilled callback to ShortestPathOptions and use it (#8739)

* Add isQueryKilled callback to ShortestPathOptions

* Use isQueryKilledCallback

in
 * ConstantWeightShortestPathFinder
 * AttributeWeightShortestPathFinder
 * ShortestPathExecutor
This commit is contained in:
Markus Pfeiffer 2019-04-12 09:39:01 +01:00 committed by Michael Hackstein
parent e5ce10cd60
commit b3224eed1d
10 changed files with 27 additions and 26 deletions

View File

@ -205,11 +205,7 @@ bool ShortestPathExecutor::fetchPath() {
TRI_ASSERT(start.isString()); TRI_ASSERT(start.isString());
TRI_ASSERT(end.isString()); TRI_ASSERT(end.isString());
_path->clear(); _path->clear();
} while (!_finder.shortestPath(start, end, *_path, [this]() { } while (!_finder.shortestPath(start, end, *_path));
if (_finder.options().query()->killed()) {
THROW_ARANGO_EXCEPTION(TRI_ERROR_QUERY_KILLED);
}
}));
_posInPath = 0; _posInPath = 0;
return true; return true;
} }

View File

@ -160,9 +160,9 @@ AttributeWeightShortestPathFinder::AttributeWeightShortestPathFinder(ShortestPat
AttributeWeightShortestPathFinder::~AttributeWeightShortestPathFinder() {} AttributeWeightShortestPathFinder::~AttributeWeightShortestPathFinder() {}
bool AttributeWeightShortestPathFinder::shortestPath( bool AttributeWeightShortestPathFinder::shortestPath(arangodb::velocypack::Slice const& st,
arangodb::velocypack::Slice const& st, arangodb::velocypack::Slice const& ta, arangodb::velocypack::Slice const& ta,
ShortestPathResult& result, std::function<void()> const& callback) { ShortestPathResult& result) {
// For the result: // For the result:
result.clear(); result.clear();
_highscoreSet = false; _highscoreSet = false;
@ -205,7 +205,7 @@ bool AttributeWeightShortestPathFinder::shortestPath(
if (++counter == 10) { if (++counter == 10) {
// check for abortion // check for abortion
callback(); options().isQueryKilledCallback();
counter = 0; counter = 0;
} }
} }

View File

@ -208,8 +208,7 @@ class AttributeWeightShortestPathFinder : public ShortestPathFinder {
// path // path
bool shortestPath(arangodb::velocypack::Slice const& start, bool shortestPath(arangodb::velocypack::Slice const& start,
arangodb::velocypack::Slice const& target, arangodb::velocypack::Slice const& target,
arangodb::graph::ShortestPathResult& result, arangodb::graph::ShortestPathResult& result) override;
std::function<void()> const& callback) override;
void inserter(std::unordered_map<arangodb::velocypack::StringRef, size_t>& candidates, void inserter(std::unordered_map<arangodb::velocypack::StringRef, size_t>& candidates,
std::vector<Step*>& result, arangodb::velocypack::StringRef const& s, std::vector<Step*>& result, arangodb::velocypack::StringRef const& s,

View File

@ -54,7 +54,7 @@ ConstantWeightShortestPathFinder::~ConstantWeightShortestPathFinder() {
bool ConstantWeightShortestPathFinder::shortestPath( bool ConstantWeightShortestPathFinder::shortestPath(
arangodb::velocypack::Slice const& s, arangodb::velocypack::Slice const& e, arangodb::velocypack::Slice const& s, arangodb::velocypack::Slice const& e,
arangodb::graph::ShortestPathResult& result, std::function<void()> const& callback) { arangodb::graph::ShortestPathResult& result) {
result.clear(); result.clear();
TRI_ASSERT(s.isString()); TRI_ASSERT(s.isString());
TRI_ASSERT(e.isString()); TRI_ASSERT(e.isString());
@ -81,7 +81,7 @@ bool ConstantWeightShortestPathFinder::shortestPath(
arangodb::velocypack::StringRef n; arangodb::velocypack::StringRef n;
while (!_leftClosure.empty() && !_rightClosure.empty()) { while (!_leftClosure.empty() && !_rightClosure.empty()) {
callback(); options().isQueryKilledCallback();
if (_leftClosure.size() < _rightClosure.size()) { if (_leftClosure.size() < _rightClosure.size()) {
if (expandClosure(_leftClosure, _leftFound, _rightFound, false, n)) { if (expandClosure(_leftClosure, _leftFound, _rightFound, false, n)) {

View File

@ -59,8 +59,7 @@ class ConstantWeightShortestPathFinder : public ShortestPathFinder {
bool shortestPath(arangodb::velocypack::Slice const& start, bool shortestPath(arangodb::velocypack::Slice const& start,
arangodb::velocypack::Slice const& end, arangodb::velocypack::Slice const& end,
arangodb::graph::ShortestPathResult& result, arangodb::graph::ShortestPathResult& result) override;
std::function<void()> const& callback) override;
private: private:
void expandVertex(bool backward, arangodb::velocypack::StringRef vertex); void expandVertex(bool backward, arangodb::velocypack::StringRef vertex);

View File

@ -42,8 +42,7 @@ class ShortestPathFinder {
virtual bool shortestPath(arangodb::velocypack::Slice const& start, virtual bool shortestPath(arangodb::velocypack::Slice const& start,
arangodb::velocypack::Slice const& target, arangodb::velocypack::Slice const& target,
arangodb::graph::ShortestPathResult& result, arangodb::graph::ShortestPathResult& result) = 0;
std::function<void()> const& callback) = 0;
void destroyEngines(); void destroyEngines();

View File

@ -203,7 +203,8 @@ EdgeCursor* ShortestPathOptions::nextReverseCursorCoordinator(arangodb::velocypa
return cursor.release(); return cursor.release();
} }
void ShortestPathOptions::fetchVerticesCoordinator(std::deque<arangodb::velocypack::StringRef> const& vertexIds) { void ShortestPathOptions::fetchVerticesCoordinator(
std::deque<arangodb::velocypack::StringRef> const& vertexIds) {
// TRI_ASSERT(arangodb::ServerState::instance()->isCoordinator()); // TRI_ASSERT(arangodb::ServerState::instance()->isCoordinator());
if (!arangodb::ServerState::instance()->isCoordinator()) { if (!arangodb::ServerState::instance()->isCoordinator()) {
return; return;
@ -229,3 +230,9 @@ void ShortestPathOptions::fetchVerticesCoordinator(std::deque<arangodb::velocypa
cache, ch->datalake(), *(leased.get())); cache, ch->datalake(), *(leased.get()));
} }
} }
void ShortestPathOptions::isQueryKilledCallback() const {
if (query()->killed()) {
THROW_ARANGO_EXCEPTION(TRI_ERROR_QUERY_KILLED);
}
}

View File

@ -97,6 +97,8 @@ struct ShortestPathOptions : public BaseOptions {
void fetchVerticesCoordinator(std::deque<arangodb::velocypack::StringRef> const& vertexIds); void fetchVerticesCoordinator(std::deque<arangodb::velocypack::StringRef> const& vertexIds);
void isQueryKilledCallback() const;
private: private:
EdgeCursor* nextCursorCoordinator(arangodb::velocypack::StringRef vid); EdgeCursor* nextCursorCoordinator(arangodb::velocypack::StringRef vid);
EdgeCursor* nextReverseCursorCoordinator(arangodb::velocypack::StringRef vid); EdgeCursor* nextReverseCursorCoordinator(arangodb::velocypack::StringRef vid);

View File

@ -121,8 +121,7 @@ class FakePathFinder : public ShortestPathFinder {
} }
bool shortestPath(VPackSlice const& source, VPackSlice const& target, bool shortestPath(VPackSlice const& source, VPackSlice const& target,
arangodb::graph::ShortestPathResult& result, arangodb::graph::ShortestPathResult& result) override {
std::function<void()> const& callback) override {
REQUIRE(source.isString()); REQUIRE(source.isString());
REQUIRE(target.isString()); REQUIRE(target.isString());
_calledWith.emplace_back(std::make_pair(source.copyString(), target.copyString())); _calledWith.emplace_back(std::make_pair(source.copyString(), target.copyString()));

View File

@ -350,7 +350,7 @@ TEST_CASE("ConstantWeightShortestPathFinder", "[graph]") {
auto end = velocypack::Parser::fromJson("\"v/0\""); auto end = velocypack::Parser::fromJson("\"v/0\"");
ShortestPathResult result; ShortestPathResult result;
REQUIRE(true == finder->shortestPath(start->slice(), end->slice(), result, []() {})); REQUIRE(true == finder->shortestPath(start->slice(), end->slice(), result));
} }
SECTION("no path exists") { SECTION("no path exists") {
@ -358,7 +358,7 @@ TEST_CASE("ConstantWeightShortestPathFinder", "[graph]") {
auto end = velocypack::Parser::fromJson("\"v/1\""); auto end = velocypack::Parser::fromJson("\"v/1\"");
ShortestPathResult result; ShortestPathResult result;
REQUIRE(false == finder->shortestPath(start->slice(), end->slice(), result, []() {})); REQUIRE(false == finder->shortestPath(start->slice(), end->slice(), result));
CHECK(result.length() == 0); CHECK(result.length() == 0);
} }
@ -367,7 +367,7 @@ TEST_CASE("ConstantWeightShortestPathFinder", "[graph]") {
auto end = velocypack::Parser::fromJson("\"v/2\""); auto end = velocypack::Parser::fromJson("\"v/2\"");
ShortestPathResult result; ShortestPathResult result;
auto rr = finder->shortestPath(start->slice(), end->slice(), result, []() {}); auto rr = finder->shortestPath(start->slice(), end->slice(), result);
REQUIRE(rr); REQUIRE(rr);
REQUIRE(checkPath(result, {"1", "2"}, {{}, {"v/1", "v/2"}})); REQUIRE(checkPath(result, {"1", "2"}, {{}, {"v/1", "v/2"}}));
} }
@ -377,7 +377,7 @@ TEST_CASE("ConstantWeightShortestPathFinder", "[graph]") {
auto end = velocypack::Parser::fromJson("\"v/4\""); auto end = velocypack::Parser::fromJson("\"v/4\"");
ShortestPathResult result; ShortestPathResult result;
auto rr = finder->shortestPath(start->slice(), end->slice(), result, []() {}); auto rr = finder->shortestPath(start->slice(), end->slice(), result);
REQUIRE(rr); REQUIRE(rr);
REQUIRE(checkPath(result, {"1", "2", "3", "4"}, REQUIRE(checkPath(result, {"1", "2", "3", "4"},
{{}, {"v/1", "v/2"}, {"v/2", "v/3"}, {"v/3", "v/4"}})); {{}, {"v/1", "v/2"}, {"v/2", "v/3"}, {"v/3", "v/4"}}));
@ -390,7 +390,7 @@ TEST_CASE("ConstantWeightShortestPathFinder", "[graph]") {
ShortestPathResult result; ShortestPathResult result;
{ {
auto rr = finder->shortestPath(start->slice(), end->slice(), result, []() {}); auto rr = finder->shortestPath(start->slice(), end->slice(), result);
REQUIRE(rr); REQUIRE(rr);
// One of the two has to be returned // One of the two has to be returned
@ -410,7 +410,7 @@ TEST_CASE("ConstantWeightShortestPathFinder", "[graph]") {
} }
{ {
auto rr = finder->shortestPath(end->slice(), start->slice(), result, []() {}); auto rr = finder->shortestPath(end->slice(), start->slice(), result);
REQUIRE(!rr); REQUIRE(!rr);
} }