Skip to content
Snippets Groups Projects
Unverified Commit 12a39e4c authored by Yee's avatar Yee Committed by GitHub
Browse files

Make static empty result initialize lazily (#291)


* Make static empty result initialize lazily

* Rename static functions

Co-authored-by: default avatardutor <440396+dutor@users.noreply.github.com>
parent 8b9cf0f5
No related branches found
No related tags found
No related merge requests found
......@@ -64,29 +64,28 @@ const Result& ExecutionContext::getResult(const std::string& name) const {
if (it != valueMap_.end() && !it->second.empty()) {
return it->second.back();
} else {
return Result::kEmptyResult;
return Result::EmptyResult();
}
}
const Result& ExecutionContext::getVersionedResult(const std::string& name,
int64_t version) const {
const Result& ExecutionContext::getVersionedResult(const std::string& name, int64_t version) const {
auto& result = getHistory(name);
auto size = result.size();
if (static_cast<size_t>(std::abs(version)) >= size) {
return Result::kEmptyResult;
return Result::EmptyResult();
} else {
return result[(size + version - 1) % size ];
return result[(size + version - 1) % size];
}
}
const std::vector<Result>& ExecutionContext::getHistory(
const std::string& name) const {
const std::vector<Result>& ExecutionContext::getHistory(const std::string& name) const {
auto it = valueMap_.find(name);
if (it != valueMap_.end()) {
return it->second;
} else {
return Result::kEmptyResultList;
return Result::EmptyResultList();
}
}
} // namespace graph
} // namespace nebula
......@@ -9,9 +9,32 @@
namespace nebula {
namespace graph {
const Result Result::kEmptyResult = ResultBuilder().iter(Iterator::Kind::kDefault).finish();
const Result& Result::EmptyResult() {
static Result kEmptyResult = ResultBuilder().iter(Iterator::Kind::kDefault).finish();
return kEmptyResult;
}
const std::vector<Result> Result::kEmptyResultList;
const std::vector<Result>& Result::EmptyResultList() {
static std::vector<Result> kEmptyResultList;
return kEmptyResultList;
}
ResultBuilder& ResultBuilder::iter(Iterator::Kind kind) {
DCHECK(kind == Iterator::Kind::kDefault || core_.value)
<< "Must set value when creating non-default iterator";
switch (kind) {
case Iterator::Kind::kDefault:
return iter(std::make_unique<DefaultIter>(core_.value));
case Iterator::Kind::kSequential:
return iter(std::make_unique<SequentialIter>(core_.value));
case Iterator::Kind::kGetNeighbors:
return iter(std::make_unique<GetNeighborsIter>(core_.value));
case Iterator::Kind::kProp:
return iter(std::make_unique<PropIter>(core_.value));
default:
LOG(FATAL) << "Invalid Iterator kind" << static_cast<uint8_t>(kind);
}
}
} // namespace graph
} // namespace nebula
......@@ -21,15 +21,15 @@ class ResultBuilder;
// An executor will produce a result.
class Result final {
public:
static const Result kEmptyResult;
static const std::vector<Result> kEmptyResultList;
enum class State : uint8_t {
kUnExecuted,
kPartialSuccess,
kSuccess,
};
static const Result& EmptyResult();
static const std::vector<Result>& EmptyResultList();
std::shared_ptr<Value> valuePtr() const {
return core_.value;
}
......@@ -98,22 +98,7 @@ public:
return *this;
}
ResultBuilder& iter(Iterator::Kind kind) {
DCHECK(kind == Iterator::Kind::kDefault || core_.value)
<< "Must set value when creating non-default iterator";
switch (kind) {
case Iterator::Kind::kDefault:
return iter(std::make_unique<DefaultIter>(core_.value));
case Iterator::Kind::kSequential:
return iter(std::make_unique<SequentialIter>(core_.value));
case Iterator::Kind::kGetNeighbors:
return iter(std::make_unique<GetNeighborsIter>(core_.value));
case Iterator::Kind::kProp:
return iter(std::make_unique<PropIter>(core_.value));
default:
LOG(FATAL) << "Invalid Iterator kind" << static_cast<uint8_t>(kind);
}
}
ResultBuilder& iter(Iterator::Kind kind);
ResultBuilder& state(Result::State state) {
core_.state = state;
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment