Skip to content

Commit

Permalink
improve and perf paths api
Browse files Browse the repository at this point in the history
  • Loading branch information
zhoney committed Apr 7, 2021
1 parent 10a44f7 commit cdc64f4
Show file tree
Hide file tree
Showing 29 changed files with 199 additions and 170 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,6 @@ public String get(@Context GraphManager manager,
dir, edgeLabel, depth,
degree, capacity, limit);
return manager.serializer(g).writePaths("crosspoints",
paths.paths(),true);
paths.paths(), true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_DEGREE;
import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_ELEMENTS_LIMIT;
import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_PATHS_LIMIT;

import java.util.ArrayList;
import java.util.HashSet;
Expand Down Expand Up @@ -171,7 +170,7 @@ private static class Request {
@JsonProperty("max_depth")
public int maxDepth;
@JsonProperty("limit")
public long limit = Long.valueOf(DEFAULT_PATHS_LIMIT);
public long limit = Long.valueOf(DEFAULT_ELEMENTS_LIMIT);
@JsonProperty("count_only")
public boolean countOnly = false;
@JsonProperty("with_vertex")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_CAPACITY;
import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_DEGREE;
import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_ELEMENTS_LIMIT;
import static com.baidu.hugegraph.traversal.algorithm.HugeTraverser.DEFAULT_PATHS_LIMIT;

import java.util.ArrayList;
import java.util.HashSet;
Expand Down Expand Up @@ -187,7 +186,7 @@ private static class Request {
@JsonProperty("capacity")
public long capacity = Long.valueOf(DEFAULT_CAPACITY);
@JsonProperty("limit")
public long limit = Long.valueOf(DEFAULT_PATHS_LIMIT);
public long limit = Long.valueOf(DEFAULT_ELEMENTS_LIMIT);
@JsonProperty("with_vertex")
public boolean withVertex = false;
@JsonProperty("with_path")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import com.baidu.hugegraph.backend.id.Id;
import com.baidu.hugegraph.backend.query.QueryResults;
import com.baidu.hugegraph.core.GraphManager;
import com.baidu.hugegraph.perf.PerfUtil;
import com.baidu.hugegraph.server.RestServer;
import com.baidu.hugegraph.traversal.algorithm.CollectionPathsTraverser;
import com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep;
Expand Down Expand Up @@ -87,6 +88,10 @@ public String get(@Context GraphManager manager,
graph, source, target, direction, edgeLabel, depth,
degree, capacity, limit);

PerfUtil.instance().clear();
PerfUtil.instance().start("paths-get");
try {

Id sourceId = VertexAPI.checkAndParseVertexId(source);
Id targetId = VertexAPI.checkAndParseVertexId(target);
Directions dir = Directions.convert(EdgeAPI.parseDirection(direction));
Expand All @@ -99,6 +104,11 @@ public String get(@Context GraphManager manager,
limit);
return manager.serializer(g).writePaths("paths", paths.paths(),
false);

} finally {
PerfUtil.instance().end("paths-get");
LOG.info("option = {}", PerfUtil.instance().toECharts());
}
}

@POST
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
import com.baidu.hugegraph.api.graph.VertexAPI;
import com.baidu.hugegraph.backend.id.Id;
import com.baidu.hugegraph.core.GraphManager;
import com.baidu.hugegraph.perf.PerfUtil;
import com.baidu.hugegraph.perf.PerfUtil.Watched;
import com.baidu.hugegraph.server.RestServer;
import com.baidu.hugegraph.traversal.algorithm.HugeTraverser;
import com.baidu.hugegraph.traversal.algorithm.ShortestPathTraverser;
Expand Down Expand Up @@ -73,27 +71,20 @@ public String get(@Context GraphManager manager,
"max degree '{}', skipped degree '{}' and capacity '{}'",
graph, source, target, direction, edgeLabel, depth,
degree, skipDegree, capacity);
//PerfUtil.instance().clear();
//PerfUtil.instance().start("sp-get");
//try {
Id sourceId = VertexAPI.checkAndParseVertexId(source);
Id targetId = VertexAPI.checkAndParseVertexId(target);
Directions dir = Directions.convert(EdgeAPI.parseDirection(direction));
Id sourceId = VertexAPI.checkAndParseVertexId(source);
Id targetId = VertexAPI.checkAndParseVertexId(target);
Directions dir = Directions.convert(EdgeAPI.parseDirection(direction));

HugeGraph g = graph(manager, graph);
HugeGraph g = graph(manager, graph);

ShortestPathTraverser traverser = new ShortestPathTraverser(g);
ShortestPathTraverser traverser = new ShortestPathTraverser(g);

List<String> edgeLabels = edgeLabel == null ? ImmutableList.of() :
ImmutableList.of(edgeLabel);
HugeTraverser.Path path = traverser.shortestPath(sourceId, targetId,
dir, edgeLabels, depth,
degree, skipDegree,
capacity);
return manager.serializer(g).writeList("path", path.vertices());
//} finally {
// PerfUtil.instance().end("sp-get");
// LOG.info("option = {}", PerfUtil.instance().toECharts());
//}
List<String> edgeLabels = edgeLabel == null ? ImmutableList.of() :
ImmutableList.of(edgeLabel);
HugeTraverser.Path path = traverser.shortestPath(sourceId, targetId,
dir, edgeLabels, depth,
degree, skipDegree,
capacity);
return manager.serializer(g).writeList("path", path.vertices());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,6 @@ public long asLong() {
return 0L;
}

@Override
public int asInt() {
// TODO: improve
return 0;
}

@Override
public byte[] asBytes() {
return StringEncoding.encode(this.query);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import com.baidu.hugegraph.exception.NotSupportException;
import com.baidu.hugegraph.iterator.ExtendableIterator;
import com.baidu.hugegraph.iterator.ListIterator;
import com.baidu.hugegraph.perf.PerfUtil;
import com.baidu.hugegraph.perf.PerfUtil.Watched;
import com.baidu.hugegraph.schema.IndexLabel;
import com.baidu.hugegraph.structure.HugeEdge;
import com.baidu.hugegraph.structure.HugeVertex;
Expand Down Expand Up @@ -265,7 +265,7 @@ private Iterator<HugeVertex> queryVerticesByIds(IdQuery query) {
}

@Override
@PerfUtil.Watched
@Watched
protected final Iterator<HugeEdge> queryEdgesFromBackend(Query query) {
RamTable ramtable = this.params().ramtable();
if (ramtable != null && ramtable.matched(query)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,6 @@ public long asLong() {
throw new UnsupportedOperationException();
}

@Override
public int asInt() {
throw new UnsupportedOperationException();
}

@Override
public byte[] asBytes() {
return StringEncoding.encode(this.asString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ public interface Id extends Comparable<Id> {

public long asLong();

public int asInt();

public byte[] asBytes();

public int length();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,6 @@ public long asLong() {
return Long.parseLong(this.id);
}

@Override
public int asInt() {
return Integer.parseInt(this.id);
}

@Override
public byte[] asBytes() {
return StringEncoding.encode(this.id);
Expand Down Expand Up @@ -238,11 +233,6 @@ public long asLong() {
return this.id;
}

@Override
public int asInt() {
return (int) this.id;
}

@Override
public byte[] asBytes() {
return NumericUtil.longToBytes(this.id);
Expand Down Expand Up @@ -338,11 +328,6 @@ public long asLong() {
throw new UnsupportedOperationException();
}

@Override
public int asInt() {
throw new UnsupportedOperationException();
}

@Override
public byte[] asBytes() {
BytesBuffer buffer = BytesBuffer.allocate(16);
Expand Down Expand Up @@ -425,11 +410,6 @@ public long asLong() {
throw new UnsupportedOperationException();
}

@Override
public int asInt() {
throw new UnsupportedOperationException();
}

@Override
public byte[] asBytes() {
throw new UnsupportedOperationException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import com.baidu.hugegraph.backend.id.SplicingIdGenerator;
import com.baidu.hugegraph.backend.query.Condition.Relation;
import com.baidu.hugegraph.backend.query.Condition.RelationType;
import com.baidu.hugegraph.perf.PerfUtil;
import com.baidu.hugegraph.perf.PerfUtil.Watched;
import com.baidu.hugegraph.structure.HugeElement;
import com.baidu.hugegraph.type.HugeType;
import com.baidu.hugegraph.type.define.HugeKeys;
Expand Down Expand Up @@ -142,7 +142,7 @@ public List<Condition.Relation> relations() {
return relations;
}

@PerfUtil.Watched
@Watched
public <T> T condition(Object key) {
List<Object> values = new ArrayList<>();
for (Condition c : this.conditions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,6 @@ public long asLong() {
throw new UnsupportedOperationException();
}

@Override
public int asInt() {
throw new UnsupportedOperationException();
}

@Override
public int compareTo(Id other) {
return Bytes.compare(this.bytes, other.asBytes());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,7 @@ protected void parseEdge(BackendColumn col, HugeVertex vertex,

// Construct edge
HugeEdge edge = HugeEdge.constructEdge(vertex, direction, edgeLabel,
sortValues, otherVertexId,
false);
sortValues, otherVertexId);

// Parse edge-id + edge-properties
buffer = BytesBuffer.wrap(col.value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ protected HugeEdge parseEdge(TableBackendEntry.Row row,

// Construct edge
HugeEdge edge = HugeEdge.constructEdge(vertex, direction, edgeLabel,
sortValues, otherId, false);
sortValues, otherId);

// Parse edge properties
this.parseProperties(edge, row);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ private void parseEdge(String colName, String colValue,
Id otherVertexId = readEntryId(colParts[3]);
// Construct edge
HugeEdge edge = HugeEdge.constructEdge(vertex, direction, edgeLabel,
sortValues, otherVertexId, false);
sortValues, otherVertexId);

String[] valParts = colValue.split(VALUE_SPLITOR);
// Parse edge expired time
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
import com.baidu.hugegraph.backend.query.Condition;
import com.baidu.hugegraph.backend.query.ConditionQuery;
import com.baidu.hugegraph.backend.query.Query;
import com.baidu.hugegraph.perf.PerfUtil;
import com.baidu.hugegraph.perf.PerfUtil.Watched;
import com.baidu.hugegraph.schema.EdgeLabel;
import com.baidu.hugegraph.schema.VertexLabel;
import com.baidu.hugegraph.structure.HugeEdge;
Expand Down Expand Up @@ -255,7 +255,7 @@ public long edgesSize() {
return this.edges.size() - 1L;
}

@PerfUtil.Watched
@Watched
public boolean matched(Query query) {
if (this.edgesSize() == 0L || this.loading) {
return false;
Expand Down Expand Up @@ -295,7 +295,7 @@ public boolean matched(Query query) {
return matchedConds == cq.conditions().size();
}

@PerfUtil.Watched
@Watched
public Iterator<HugeEdge> query(Query query) {
assert this.matched(query);
assert this.edgesSize() > 0;
Expand All @@ -314,7 +314,7 @@ public Iterator<HugeEdge> query(Query query) {
return this.query(owner.asLong(), dir, (int) label.asLong());
}

@PerfUtil.Watched
@Watched
public Iterator<HugeEdge> query(long owner, Directions dir, int label) {
if (this.loading) {
// don't query when loading
Expand Down Expand Up @@ -455,7 +455,7 @@ private HugeEdge fetch() {

HugeEdge edge = HugeEdge.constructEdge(this.owner, direction,
edgeLabel, sortValues,
otherVertexId, false);
otherVertexId);
edge.propNotLoaded();
return edge;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,28 +497,16 @@ public static HugeEdge constructEdge(HugeVertex ownerVertex,
EdgeLabel edgeLabel,
String sortValues,
Id otherVertexId) {
return constructEdge(ownerVertex, isOutEdge, edgeLabel,
sortValues, otherVertexId, true);

}

@Watched
public static HugeEdge constructEdge(HugeVertex ownerVertex,
boolean isOutEdge,
EdgeLabel edgeLabel,
String sortValues,
Id otherVertexId,
boolean needDedup) {
HugeGraph graph = ownerVertex.graph();
VertexLabel srcLabel = graph.vertexLabelOrNone(edgeLabel.sourceLabel());
VertexLabel tgtLabel = graph.vertexLabelOrNone(edgeLabel.targetLabel());

VertexLabel otherVertexLabel;
if (isOutEdge) {
// ownerVertex.correctVertexLabel(srcLabel);
ownerVertex.correctVertexLabel(srcLabel);
otherVertexLabel = tgtLabel;
} else {
// ownerVertex.correctVertexLabel(tgtLabel);
ownerVertex.correctVertexLabel(tgtLabel);
otherVertexLabel = srcLabel;
}
HugeVertex otherVertex = new HugeVertex(graph, otherVertexId,
Expand All @@ -533,11 +521,11 @@ public static HugeEdge constructEdge(HugeVertex ownerVertex,
edge.assignId();

if (isOutEdge) {
ownerVertex.addOutEdge(edge, needDedup);
otherVertex.addInEdge(edge.switchOwner(), needDedup);
ownerVertex.addOutEdge(edge);
otherVertex.addInEdge(edge.switchOwner());
} else {
ownerVertex.addInEdge(edge, needDedup);
otherVertex.addOutEdge(edge.switchOwner(), needDedup);
ownerVertex.addInEdge(edge);
otherVertex.addOutEdge(edge.switchOwner());
}

return edge;
Expand Down
Loading

0 comments on commit cdc64f4

Please sign in to comment.