Skip to content

Commit

Permalink
fix: adjust the code style and naming convention
Browse files Browse the repository at this point in the history
  • Loading branch information
DanGuge committed Jul 31, 2023
1 parent c7825a6 commit d468e18
Show file tree
Hide file tree
Showing 21 changed files with 114 additions and 119 deletions.
75 changes: 39 additions & 36 deletions hugegraph-api/src/main/java/org/apache/hugegraph/api/API.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@
package org.apache.hugegraph.api;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.function.Consumer;

import org.apache.commons.lang.mutable.MutableLong;
import org.apache.hugegraph.HugeException;
import org.apache.hugegraph.HugeGraph;
import org.apache.hugegraph.core.GraphManager;
import org.apache.hugegraph.define.Checkable;
import org.apache.hugegraph.metrics.MetricsUtil;
import org.apache.hugegraph.util.E;
import org.apache.hugegraph.util.InsertionOrderUtil;
import org.apache.hugegraph.util.JsonUtil;
import org.apache.hugegraph.util.Log;
import org.slf4j.Logger;
Expand All @@ -44,35 +44,30 @@

public class API {

protected static final Logger LOG = Log.logger(API.class);

public static final String CHARSET = "UTF-8";

public static final String TEXT_PLAIN = MediaType.TEXT_PLAIN;
public static final String APPLICATION_JSON = MediaType.APPLICATION_JSON;
public static final String APPLICATION_JSON_WITH_CHARSET =
APPLICATION_JSON + ";charset=" + CHARSET;
APPLICATION_JSON + ";charset=" + CHARSET;
public static final String JSON = MediaType.APPLICATION_JSON_TYPE
.getSubtype();

.getSubtype();
public static final String ACTION_APPEND = "append";
public static final String ACTION_ELIMINATE = "eliminate";
public static final String ACTION_CLEAR = "clear";

protected static final Logger LOG = Log.logger(API.class);
private static final Meter SUCCEED_METER =
MetricsUtil.registerMeter(API.class, "commit-succeed");
MetricsUtil.registerMeter(API.class, "commit-succeed");
private static final Meter ILLEGAL_ARG_ERROR_METER =
MetricsUtil.registerMeter(API.class, "illegal-arg");
MetricsUtil.registerMeter(API.class, "illegal-arg");
private static final Meter EXPECTED_ERROR_METER =
MetricsUtil.registerMeter(API.class, "expected-error");
MetricsUtil.registerMeter(API.class, "expected-error");
private static final Meter UNKNOWN_ERROR_METER =
MetricsUtil.registerMeter(API.class, "unknown-error");
MetricsUtil.registerMeter(API.class, "unknown-error");

public static HugeGraph graph(GraphManager manager, String graph) {
HugeGraph g = manager.graph(graph);
if (g == null) {
throw new NotFoundException(String.format(
"Graph '%s' does not exist", graph));
throw new NotFoundException(String.format("Graph '%s' does not exist", graph));
}
return g;
}
Expand Down Expand Up @@ -142,8 +137,7 @@ protected static void checkUpdatingBody(Checkable body) {
body.checkUpdate();
}

protected static void checkCreatingBody(
Collection<? extends Checkable> bodies) {
protected static void checkCreatingBody(Collection<? extends Checkable> bodies) {
E.checkArgumentNotNull(bodies, "The request body can't be empty");
for (Checkable body : bodies) {
E.checkArgument(body != null,
Expand All @@ -152,8 +146,7 @@ protected static void checkCreatingBody(
}
}

protected static void checkUpdatingBody(
Collection<? extends Checkable> bodies) {
protected static void checkUpdatingBody(Collection<? extends Checkable> bodies) {
E.checkArgumentNotNull(bodies, "The request body can't be empty");
for (Checkable body : bodies) {
E.checkArgumentNotNull(body,
Expand Down Expand Up @@ -188,41 +181,51 @@ public static boolean checkAndParseAction(String action) {
} else if (action.equals(ACTION_ELIMINATE)) {
return false;
} else {
throw new NotSupportedException(
String.format("Not support action '%s'", action));
throw new NotSupportedException(String.format("Not support action '%s'", action));
}
}

public static class ApiMeasure {
public static final String EDGE_ITER = "edge_iters";
public static final String VERTICE_ITER = "vertice_iters";
public static class ApiMeasurer {

public static final String EDGE_ITER = "edge_iterations";
public static final String VERTICE_ITER = "vertice_iterations";
public static final String COST = "cost";
protected long timeStart = System.currentTimeMillis();
protected HashMap<String, Object> mapResult = new LinkedHashMap<>();
private final long timeStart;
private final Map<String, Object> measures;

public ApiMeasurer() {
this.timeStart = System.currentTimeMillis();
this.measures = InsertionOrderUtil.newMap();
}

public Map<String, Object> getResult() {
mapResult.put(COST, System.currentTimeMillis() - timeStart);
return mapResult;
public Map<String, Object> measures() {
measures.put(COST, System.currentTimeMillis() - timeStart);
return measures;
}

public void put(String key, String value) {
this.mapResult.put(key, value);
this.measures.put(key, value);
}

public void put(String key, long value) {
this.mapResult.put(key, value);
this.measures.put(key, value);
}

public void put(String key, int value) {
this.mapResult.put(key, value);
this.measures.put(key, value);
}

protected void addCount(String key, long value) {
long cur = 0;
if (this.mapResult.containsKey(key)) {
cur = (long) this.mapResult.get(key);
Object current = measures.get(key);
if (current == null) {
measures.put(key, new MutableLong(value));
} else if (current instanceof MutableLong) {
MutableLong currentMutableLong = (MutableLong) current;
currentMutableLong.add(value);
} else if (current instanceof Long) {
Long currentLong = (Long) current;
measures.put(key, new MutableLong(currentLong + value));
}
this.mapResult.put(key, cur + value);
}

public void addIterCount(long verticeIters, long edgeIters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public String get(@Context GraphManager manager,
graph, source, target, direction, edgeLabel, depth,
maxDegree, skipDegree, capacity, withVertex, withEdge);

ApiMeasure measure = new ApiMeasure();
ApiMeasurer measure = new ApiMeasurer();

Id sourceId = VertexAPI.checkAndParseVertexId(source);
Id targetId = VertexAPI.checkAndParseVertexId(target);
Expand All @@ -96,9 +96,9 @@ public String get(@Context GraphManager manager,
ShortestPathTraverser traverser = new ShortestPathTraverser(g);
List<String> edgeLabels = edgeLabel == null ? ImmutableList.of() :
ImmutableList.of(edgeLabel);
HugeTraverser.PathSet paths = traverser.allShortestPaths(
sourceId, targetId, dir, edgeLabels,
depth, maxDegree, skipDegree, capacity);
HugeTraverser.PathSet paths = traverser.allShortestPaths(sourceId, targetId, dir,
edgeLabels, depth, maxDegree,
skipDegree, capacity);

measure.addIterCount(traverser.vertexIterCounter.get(),
traverser.edgeIterCounter.get());
Expand All @@ -123,7 +123,7 @@ public String get(@Context GraphManager manager,
iterEdge = HugeTraverser.EdgeRecord.getEdgeIds(edges).iterator();
}

return manager.serializer(g, measure.getResult())
return manager.serializer(g, measure.measures())
.writePaths("paths", paths, false,
iterVertex, iterEdge);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public String get(@Context GraphManager manager,
graph, source, target, direction, edgeLabel,
depth, maxDegree, capacity, limit);

ApiMeasure measure = new ApiMeasure();
ApiMeasurer measure = new ApiMeasurer();
Id sourceId = VertexAPI.checkAndParseVertexId(source);
Id targetId = VertexAPI.checkAndParseVertexId(target);
Directions dir = Directions.convert(EdgeAPI.parseDirection(direction));
Expand All @@ -87,7 +87,7 @@ public String get(@Context GraphManager manager,
limit);
measure.addIterCount(traverser.vertexIterCounter.get(),
traverser.edgeIterCounter.get());
return manager.serializer(g, measure.getResult())
return manager.serializer(g, measure.measures())
.writePaths("crosspoints", paths, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,10 @@ public class CustomizedCrosspointsAPI extends API {
private static List<CustomizedCrosspointsTraverser.PathPattern> pathPatterns(
HugeGraph graph, CrosspointsRequest request) {
int stepSize = request.pathPatterns.size();
List<CustomizedCrosspointsTraverser.PathPattern> pathPatterns;
pathPatterns = new ArrayList<>(stepSize);
List<CustomizedCrosspointsTraverser.PathPattern> pathPatterns = new ArrayList<>(stepSize);
for (PathPattern pattern : request.pathPatterns) {
CustomizedCrosspointsTraverser.PathPattern pathPattern;
pathPattern = new CustomizedCrosspointsTraverser.PathPattern();
CustomizedCrosspointsTraverser.PathPattern pathPattern =
new CustomizedCrosspointsTraverser.PathPattern();
for (Step step : pattern.steps) {
pathPattern.add(step.jsonToStep(graph));
}
Expand Down Expand Up @@ -100,7 +99,7 @@ public String post(@Context GraphManager manager,
graph, request.sources, request.pathPatterns, request.withPath,
request.withVertex, request.capacity, request.limit, request.withEdge);

ApiMeasure measure = new ApiMeasure();
ApiMeasurer measure = new ApiMeasurer();

HugeGraph g = graph(manager, graph);
Iterator<Vertex> sources = request.sources.vertices(g);
Expand Down Expand Up @@ -142,7 +141,7 @@ public String post(@Context GraphManager manager,
}
}

return manager.serializer(g, measure.getResult())
return manager.serializer(g, measure.measures())
.writeCrosspoints(paths, iterVertex,
iterEdge, request.withPath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public String post(@Context GraphManager manager,
request.sortBy, request.capacity, request.limit,
request.withVertex, request.withEdge);

ApiMeasure measure = new ApiMeasure();
ApiMeasurer measure = new ApiMeasurer();

HugeGraph g = graph(manager, graph);
Iterator<Vertex> sources = request.sources.vertices(g);
Expand Down Expand Up @@ -136,7 +136,7 @@ public String post(@Context GraphManager manager,
iterEdge = HugeTraverser.EdgeRecord.getEdgeIds(edges).iterator();
}

return manager.serializer(g, measure.getResult())
return manager.serializer(g, measure.measures())
.writePaths("paths", paths, false,
iterVertex, iterEdge);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public String post(@Context GraphManager manager,
request.minNeighbors, request.alpha, request.minSimilars,
request.groupProperty, request.minGroups);

ApiMeasure measure = new ApiMeasure();
ApiMeasurer measure = new ApiMeasurer();
HugeGraph g = graph(manager, graph);
Iterator<Vertex> sources = request.sources.vertices(g);
E.checkArgument(sources != null && sources.hasNext(),
Expand Down Expand Up @@ -125,7 +125,7 @@ public String post(@Context GraphManager manager,
iterVertex = vertexIds.iterator();
}

return manager.serializer(g, measure.getResult())
return manager.serializer(g, measure.measures())
.writeSimilars(result, iterVertex);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public String get(@Context GraphManager manager,
"with direction {}, edge label {} and max degree '{}'",
graph, vertex, other, direction, edgeLabel, maxDegree);

ApiMeasure measure = new ApiMeasure();
ApiMeasurer measure = new ApiMeasurer();

Id sourceId = VertexAPI.checkAndParseVertexId(vertex);
Id targetId = VertexAPI.checkAndParseVertexId(other);
Expand All @@ -90,7 +90,7 @@ public String get(@Context GraphManager manager,
traverser.edgeIterCounter.get());
}

return manager.serializer(g, measure.getResult())
return manager.serializer(g, measure.measures())
.writeMap(ImmutableMap.of("jaccard_similarity", similarity));
}

Expand All @@ -114,7 +114,7 @@ public String post(@Context GraphManager manager,
graph, request.vertex, request.step,
request.top, request.capacity);

ApiMeasure measure = new ApiMeasure();
ApiMeasurer measure = new ApiMeasurer();

HugeGraph g = graph(manager, graph);
Id sourceId = HugeVertex.getIdValue(request.vertex);
Expand All @@ -129,7 +129,7 @@ public String post(@Context GraphManager manager,
measure.addIterCount(traverser.vertexIterCounter.get(),
traverser.edgeIterCounter.get());
}
return manager.serializer(g, measure.getResult())
return manager.serializer(g, measure.measures())
.writeMap(ImmutableMap.of("jaccard_similarity", results));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public String get(@Context GraphManager manager,
graph, sourceV, direction, edgeLabel, depth,
maxDegree, limit);

ApiMeasure measure = new ApiMeasure();
ApiMeasurer measure = new ApiMeasurer();

Id source = VertexAPI.checkAndParseVertexId(sourceV);
Directions dir = Directions.convert(EdgeAPI.parseDirection(direction));
Expand All @@ -104,10 +104,10 @@ public String get(@Context GraphManager manager,
traverser.edgeIterCounter.get());
}
if (countOnly) {
return manager.serializer(g, measure.getResult()).writeMap(
ImmutableMap.of("vertices_size", ids.size()));
return manager.serializer(g, measure.measures())
.writeMap(ImmutableMap.of("vertices_size", ids.size()));
}
return manager.serializer(g, measure.getResult()).writeList("vertices", ids);
return manager.serializer(g, measure.measures()).writeList("vertices", ids);
}

@POST
Expand All @@ -134,7 +134,7 @@ public String post(@Context GraphManager manager,
request.countOnly, request.withVertex, request.withPath,
request.withEdge);

ApiMeasure measure = new ApiMeasure();
ApiMeasurer measure = new ApiMeasurer();

HugeGraph g = graph(manager, graph);
Id sourceId = HugeVertex.getIdValue(request.source);
Expand Down Expand Up @@ -163,7 +163,7 @@ public String post(@Context GraphManager manager,
}

if (request.countOnly) {
return manager.serializer(g, measure.getResult())
return manager.serializer(g, measure.measures())
.writeNodesWithPath("kneighbor", neighbors, size, paths,
QueryResults.emptyIterator(),
QueryResults.emptyIterator());
Expand Down Expand Up @@ -193,7 +193,7 @@ public String post(@Context GraphManager manager,
}
}

return manager.serializer(g, measure.getResult())
return manager.serializer(g, measure.measures())
.writeNodesWithPath("kneighbor", neighbors,
size, paths, iterVertex, iterEdge);
}
Expand Down
Loading

0 comments on commit d468e18

Please sign in to comment.