Skip to content

Commit

Permalink
Merge branch 'master' into optimize_traverse
Browse files Browse the repository at this point in the history
  • Loading branch information
nevermore3 authored Dec 7, 2022
2 parents bcab031 + 3762db7 commit f075335
Show file tree
Hide file tree
Showing 58 changed files with 2,032 additions and 211 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
lint:
name: lint
if: ${{ contains(github.event.pull_request.labels.*.name, 'ready-for-testing') && github.event.pull_request.merged != true }}
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
with:
Expand All @@ -31,7 +31,7 @@ jobs:
uses: apache/skywalking-eyes/header@main
- name: Ensure clang-format-10 is available
run: |
command -v clang-format-10 > /dev/null || (apt-get update && apt-get install -y clang-format-10)
command -v clang-format-10 > /dev/null || (sudo apt update && sudo apt install -y clang-format-10)
- name: Cpplint
run: |
ln -snf $PWD/.linters/cpp/hooks/pre-commit.sh $PWD/.linters/cpp/pre-commit.sh
Expand Down
12 changes: 12 additions & 0 deletions src/common/datatypes/Edge.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct Edge {
std::string name;
EdgeRanking ranking;
std::unordered_map<std::string, Value> props;
std::atomic<size_t> refcnt{1};

Edge() {}
Edge(Edge&& v) noexcept
Expand All @@ -44,6 +45,13 @@ struct Edge {
ranking(std::move(r)),
props(std::move(p)) {}

size_t ref() {
return ++refcnt;
}
size_t unref() {
return --refcnt;
}

void clear();

void __clear() {
Expand All @@ -57,6 +65,10 @@ struct Edge {

bool operator==(const Edge& rhs) const;

bool operator!=(const Edge& rhs) const {
return !(*this == rhs);
}

void format() {
if (type < 0) {
reverse();
Expand Down
2 changes: 1 addition & 1 deletion src/common/datatypes/Path.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ struct Step {
if (dst != rhs.dst) {
return dst < rhs.dst;
}
if (type != rhs.dst) {
if (type != rhs.type) {
return type < rhs.type;
}
if (ranking != rhs.ranking) {
Expand Down
82 changes: 52 additions & 30 deletions src/common/datatypes/Value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,9 +463,9 @@ void Value::setVertex(Vertex&& v) {
setV(std::move(v));
}

void Value::setVertex(std::unique_ptr<Vertex>&& v) {
void Value::setVertex(Vertex* v) {
clear();
setV(std::move(v));
setV(v);
}

void Value::setEdge(const Edge& v) {
Expand All @@ -478,9 +478,9 @@ void Value::setEdge(Edge&& v) {
setE(std::move(v));
}

void Value::setEdge(std::unique_ptr<Edge>&& v) {
void Value::setEdge(Edge* v) {
clear();
setE(std::move(v));
setE(v);
}

void Value::setPath(const Path& v) {
Expand Down Expand Up @@ -620,7 +620,7 @@ const Vertex& Value::getVertex() const {

const Vertex* Value::getVertexPtr() const {
CHECK_EQ(type_, Type::VERTEX);
return value_.vVal.get();
return value_.vVal;
}

const Edge& Value::getEdge() const {
Expand All @@ -630,7 +630,7 @@ const Edge& Value::getEdge() const {

const Edge* Value::getEdgePtr() const {
CHECK_EQ(type_, Type::EDGE);
return value_.eVal.get();
return value_.eVal;
}

const Path& Value::getPath() const {
Expand Down Expand Up @@ -945,11 +945,23 @@ void Value::clearSlow() {
break;
}
case Type::VERTEX: {
destruct(value_.vVal);
if (value_.vVal) {
if (value_.vVal->unref() == 0) {
delete value_.vVal;
}
value_.vVal = nullptr;
type_ = Type::__EMPTY__;
}
break;
}
case Type::EDGE: {
destruct(value_.eVal);
if (value_.eVal) {
if (value_.eVal->unref() == 0) {
delete value_.eVal;
}
value_.eVal = nullptr;
type_ = Type::__EMPTY__;
}
break;
}
case Type::PATH: {
Expand Down Expand Up @@ -1026,12 +1038,18 @@ Value& Value::operator=(Value&& rhs) noexcept {
break;
}
case Type::VERTEX: {
setV(std::move(rhs.value_.vVal));
break;
value_.vVal = rhs.value_.vVal;
type_ = Type::VERTEX;
rhs.value_.vVal = nullptr;
rhs.type_ = Type::__EMPTY__;
return *this;
}
case Type::EDGE: {
setE(std::move(rhs.value_.eVal));
break;
value_.eVal = rhs.value_.eVal;
type_ = Type::EDGE;
rhs.value_.eVal = nullptr;
rhs.type_ = Type::__EMPTY__;
return *this;
}
case Type::PATH: {
setP(std::move(rhs.value_.pVal));
Expand Down Expand Up @@ -1246,44 +1264,36 @@ void Value::setDT(DateTime&& v) {
new (std::addressof(value_.dtVal)) DateTime(std::move(v));
}

void Value::setV(const std::unique_ptr<Vertex>& v) {
type_ = Type::VERTEX;
new (std::addressof(value_.vVal)) std::unique_ptr<Vertex>(new Vertex(*v));
}

void Value::setV(std::unique_ptr<Vertex>&& v) {
void Value::setV(Vertex* v) {
type_ = Type::VERTEX;
new (std::addressof(value_.vVal)) std::unique_ptr<Vertex>(std::move(v));
value_.vVal = v;
value_.vVal->ref();
}

void Value::setV(const Vertex& v) {
type_ = Type::VERTEX;
new (std::addressof(value_.vVal)) std::unique_ptr<Vertex>(new Vertex(v));
new (std::addressof(value_.vVal)) Vertex*(new Vertex(v));
}

void Value::setV(Vertex&& v) {
type_ = Type::VERTEX;
new (std::addressof(value_.vVal)) std::unique_ptr<Vertex>(new Vertex(std::move(v)));
new (std::addressof(value_.vVal)) Vertex*(new Vertex(std::move(v)));
}

void Value::setE(const std::unique_ptr<Edge>& v) {
void Value::setE(Edge* v) {
type_ = Type::EDGE;
new (std::addressof(value_.eVal)) std::unique_ptr<Edge>(new Edge(*v));
}

void Value::setE(std::unique_ptr<Edge>&& v) {
type_ = Type::EDGE;
new (std::addressof(value_.eVal)) std::unique_ptr<Edge>(std::move(v));
value_.eVal = v;
value_.eVal->ref();
}

void Value::setE(const Edge& v) {
type_ = Type::EDGE;
new (std::addressof(value_.eVal)) std::unique_ptr<Edge>(new Edge(v));
new (std::addressof(value_.eVal)) Edge*(new Edge(v));
}

void Value::setE(Edge&& v) {
type_ = Type::EDGE;
new (std::addressof(value_.eVal)) std::unique_ptr<Edge>(new Edge(std::move(v)));
new (std::addressof(value_.eVal)) Edge*(new Edge(std::move(v)));
}

void Value::setP(const std::unique_ptr<Path>& v) {
Expand Down Expand Up @@ -1902,9 +1912,15 @@ Value Value::equal(const Value& v) const {
return getDateTime() == v.getDateTime();
}
case Value::Type::VERTEX: {
if (value_.vVal == v.value_.vVal) {
return true;
}
return getVertex() == v.getVertex();
}
case Value::Type::EDGE: {
if (value_.eVal == v.value_.eVal) {
return true;
}
return getEdge() == v.getEdge();
}
case Value::Type::PATH: {
Expand Down Expand Up @@ -2737,9 +2753,15 @@ bool Value::equals(const Value& rhs) const {
return getDateTime() == rhs.getDateTime();
}
case Value::Type::VERTEX: {
if (value_.vVal == rhs.value_.vVal) {
return true;
}
return getVertex() == rhs.getVertex();
}
case Value::Type::EDGE: {
if (value_.eVal == rhs.value_.eVal) {
return true;
}
return getEdge() == rhs.getEdge();
}
case Value::Type::PATH: {
Expand Down
14 changes: 6 additions & 8 deletions src/common/datatypes/Value.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,10 @@ struct Value {
void setDateTime(DateTime&& v);
void setVertex(const Vertex& v);
void setVertex(Vertex&& v);
void setVertex(std::unique_ptr<Vertex>&& v);
void setVertex(Vertex* v);
void setEdge(const Edge& v);
void setEdge(Edge&& v);
void setEdge(std::unique_ptr<Edge>&& v);
void setEdge(Edge* v);
void setPath(const Path& v);
void setPath(Path&& v);
void setPath(std::unique_ptr<Path>&& v);
Expand Down Expand Up @@ -391,8 +391,8 @@ struct Value {
Date dVal;
Time tVal;
DateTime dtVal;
std::unique_ptr<Vertex> vVal;
std::unique_ptr<Edge> eVal;
Vertex* vVal;
Edge* eVal;
std::unique_ptr<Path> pVal;
std::unique_ptr<List> lVal;
std::unique_ptr<Map> mVal;
Expand Down Expand Up @@ -437,13 +437,11 @@ struct Value {
void setDT(const DateTime& v);
void setDT(DateTime&& v);
// Vertex value
void setV(const std::unique_ptr<Vertex>& v);
void setV(std::unique_ptr<Vertex>&& v);
void setV(Vertex* v);
void setV(const Vertex& v);
void setV(Vertex&& v);
// Edge value
void setE(const std::unique_ptr<Edge>& v);
void setE(std::unique_ptr<Edge>&& v);
void setE(Edge* v);
void setE(const Edge& v);
void setE(Edge&& v);
// Path value
Expand Down
16 changes: 8 additions & 8 deletions src/common/datatypes/ValueOps-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -368,21 +368,21 @@ void Cpp2Ops<nebula::Value>::read(Protocol* proto, nebula::Value* obj) {
}
case 9: {
if (readState.fieldType == apache::thrift::protocol::T_STRUCT) {
obj->setVertex(nebula::Vertex());
auto ptr = std::make_unique<nebula::Vertex>();
Cpp2Ops<nebula::Vertex>::read(proto, ptr.get());
obj->setVertex(std::move(ptr));
auto* ptr = new nebula::Vertex();
Cpp2Ops<nebula::Vertex>::read(proto, ptr);
obj->setVertex(ptr);
ptr->unref();
} else {
proto->skip(readState.fieldType);
}
break;
}
case 10: {
if (readState.fieldType == apache::thrift::protocol::T_STRUCT) {
obj->setEdge(nebula::Edge());
auto ptr = std::make_unique<nebula::Edge>();
Cpp2Ops<nebula::Edge>::read(proto, ptr.get());
obj->setEdge(std::move(ptr));
auto* ptr = new nebula::Edge();
Cpp2Ops<nebula::Edge>::read(proto, ptr);
obj->setEdge(ptr);
ptr->unref();
} else {
proto->skip(readState.fieldType);
}
Expand Down
12 changes: 12 additions & 0 deletions src/common/datatypes/Vertex.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,20 @@ struct Tag {
struct Vertex {
Value vid;
std::vector<Tag> tags;
std::atomic<size_t> refcnt{1};

Vertex() = default;
Vertex(const Vertex& v) : vid(v.vid), tags(v.tags) {}
Vertex(Vertex&& v) noexcept : vid(std::move(v.vid)), tags(std::move(v.tags)) {}
Vertex(Value id, std::vector<Tag> t) : vid(std::move(id)), tags(std::move(t)) {}

size_t ref() {
return ++refcnt;
}
size_t unref() {
return --refcnt;
}

void clear() {
vid.clear();
tags.clear();
Expand All @@ -89,6 +97,10 @@ struct Vertex {
return vid == rhs.vid && tags == rhs.tags;
}

bool operator!=(const Vertex& rhs) const {
return vid != rhs.vid || tags != rhs.tags;
}

bool operator<(const Vertex& rhs) const;

bool contains(const Value& key) const;
Expand Down
12 changes: 8 additions & 4 deletions src/common/expression/PredicateExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ std::unordered_map<std::string, PredicateExpression::Type> PredicateExpression::
const Value& PredicateExpression::evalExists(ExpressionContext& ctx) {
DCHECK(collection_->kind() == Expression::Kind::kAttribute ||
collection_->kind() == Expression::Kind::kSubscript ||
collection_->kind() == Expression::Kind::kLabelTagProperty);

if (collection_->kind() == Expression::Kind::kLabelTagProperty) {
result_ = !collection_->eval(ctx).isNull();
collection_->kind() == Expression::Kind::kLabelTagProperty ||
collection_->kind() == Expression::Kind::kTagProperty)
<< "actual kind: " << collection_->kind() << ", toString: " << toString();

if (collection_->kind() == Expression::Kind::kLabelTagProperty ||
collection_->kind() == Expression::Kind::kTagProperty) {
auto v = collection_->eval(ctx);
result_ = (!v.isNull()) && (!v.empty());
return result_;
}

Expand Down
5 changes: 5 additions & 0 deletions src/common/function/FunctionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2004,6 +2004,11 @@ FunctionManager::FunctionManager() {
};
}
{
// `none_direct_dst` always return the dstId of an edge key
// without considering the direction of the edge type.
// The encoding of the edge key is:
// type(1) + partId(3) + srcId(*) + edgeType(4) + edgeRank(8) + dstId(*) + placeHolder(1)
// More information of encoding could be found in `NebulaKeyUtils.h`
auto &attr = functions_["none_direct_dst"];
attr.minArity_ = 1;
attr.maxArity_ = 1;
Expand Down
Loading

0 comments on commit f075335

Please sign in to comment.