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

feat(api): optimize adjacent-edges query #2408

Merged
merged 13 commits into from
Feb 26, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,10 @@ public Object value() {
return this.value;
}

public void setValue(Object value) {
this.value = value;
}

public void serialKey(Object key) {
this.serialKey = key;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,23 @@ public ConditionQuery copyAndResetUnshared() {
return query;
}

public Condition.Relation copyRelation(Object key) {
Condition.Relation copyRes = null;
for (int i = 0; i < this.conditions.size(); i++) {
Condition c = this.conditions.get(i);
if (c.isRelation()) {
Condition.Relation r = (Condition.Relation) c;
if (r.key().equals(key)) {
copyRes = r.copy();
this.conditions.set(i, copyRes);
break;
}
}
}
E.checkArgument(copyRes != null, "Copying Condition.Relation failed. key:%s", key);
return copyRes;
}

@Override
public boolean test(HugeElement element) {
if (!this.ids().isEmpty() && !super.test(element)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1457,22 +1457,32 @@
EdgeLabel edgeLabel = this.graph().edgeLabel(label);

if (query.containsRelation(HugeKeys.OWNER_VERTEX, Condition.RelationType.IN)) {
// For IN queries, filter out non-adjacent vertices.
// For IN query, filter schema non-adjacent vertices.
ArrayList<Id> vertexIdList = query.condition(HugeKeys.OWNER_VERTEX);
List<Id> filterVertexList = vertexIdList.stream().filter(vertexId -> {
Vertex vertex = this.graph().vertex(vertexId);
VertexLabel vertexLabel = graph().vertexLabel(vertex.label());
return edgeLabel.linkWithLabel(vertexLabel.id(), dir.type());
}).collect(Collectors.toList());
vertexIdList.clear();
vertexIdList.addAll(filterVertexList);
if (CollectionUtils.isEmpty(filterVertexList)) {
// Return empty query to skip storage query
return new Query(query.resultType());

Check warning on line 1469 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/tx/GraphTransaction.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/tx/GraphTransaction.java#L1469

Added line #L1469 was not covered by tests
} else if (vertexIdList.size() != filterVertexList.size()) {
// Modify on the copied relation to avoid affecting other query
Condition.Relation relation = query.copyRelation(HugeKeys.OWNER_VERTEX);
relation.setValue(filterVertexList);
}
} else if (query.containsRelation(HugeKeys.OWNER_VERTEX, Condition.RelationType.EQ)) {
// For EQ queries, return emptyQuery() if it is a non-adjacent vertex.
// For EQ query, just skip query if adjacent schema is unavailable.
Id vertexId = query.condition(HugeKeys.OWNER_VERTEX);
Vertex vertex = this.graph().vertex(vertexId);
VertexLabel vertexLabel = graph().vertexLabel(vertex.label());
if (!edgeLabel.linkWithLabel(vertexLabel.id(), dir.type())) {
return new Query(query.resultType());
Iterator<Vertex> iter = this.queryVertices(vertexId);
Vertex vertex = QueryResults.one(iter);
if (vertex != null) {
VertexLabel vertexLabel = graph().vertexLabel(vertex.label());
if (!edgeLabel.linkWithLabel(vertexLabel.id(), dir.type())) {
// Return empty query to skip storage query
return new Query(query.resultType());
}
}
}

Expand All @@ -1482,9 +1492,8 @@
query = query.copy();
// Serialize sort-values
List<Id> keys = this.graph().edgeLabel(label).sortKeys();
List<Condition> conditions =
GraphIndexTransaction.constructShardConditions(
query, keys, HugeKeys.SORT_VALUES);
List<Condition> conditions = GraphIndexTransaction.constructShardConditions(
query, keys, HugeKeys.SORT_VALUES);
query.query(conditions);
/*
* Reset all userprop since transferred to sort-keys, ignore other
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@
return this.sourceLabel.equals(id) || this.targetLabel.equals(id);
}

public boolean linkWithLabel(Id id, HugeType hugeType) {
if (hugeType.equals(HugeType.EDGE_IN)) {
public boolean linkWithLabel(Id id, HugeType type) {
if (type.equals(HugeType.EDGE_IN)) {
return this.targetLabel.equals(id);
} else if (hugeType.equals(HugeType.EDGE_OUT)) {
} else if (type.equals(HugeType.EDGE_OUT)) {
return this.sourceLabel.equals(id);
}
return false;

Check warning on line 107 in hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/schema/EdgeLabel.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/schema/EdgeLabel.java#L107

Added line #L107 was not covered by tests
}

public boolean checkLinkEqual(Id sourceLabel, Id targetLabel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@
* (when switch from non-auth mode to auth mode)
*/
graph.initSystemInfo();
LOG.info("Skip init-store due to the backend store of '{}' " +
"had been initialized", graph.name());
"had been initialized", graph.name());

Check warning on line 108 in hugegraph-server/hugegraph-dist/src/main/java/org/apache/hugegraph/cmd/InitStore.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-server/hugegraph-dist/src/main/java/org/apache/hugegraph/cmd/InitStore.java#L107-L108

Added lines #L107 - L108 were not covered by tests
} else {
initBackend(graph);
}
Expand Down
Loading