From 97748a475a40b014059fc2ceda98371493173d12 Mon Sep 17 00:00:00 2001 From: imbajin Date: Tue, 1 Nov 2022 18:05:04 +0800 Subject: [PATCH] adapt the latest version & clean code also fix the sec alert --- .../baidu/hugegraph/api/job/AlgorithmAPI.java | 18 ++++----- .../job/algorithm/AbstractAlgorithm.java | 11 ++---- .../hugegraph/job/algorithm/Algorithm.java | 8 ++-- .../job/algorithm/AlgorithmPool.java | 4 +- .../hugegraph/job/algorithm/BfsTraverser.java | 4 +- .../hugegraph/job/algorithm/Consumers.java | 5 ++- .../job/algorithm/SubgraphStatAlgorithm.java | 18 ++++----- .../algorithm/cent/AbstractCentAlgorithm.java | 11 ++---- .../cent/BetweennessCentralityAlgorithm.java | 2 +- .../cent/ClosenessCentralityAlgorithm.java | 2 +- .../cent/ClosenessCentralityAlgorithmV2.java | 2 +- .../cent/DegreeCentralityAlgorithm.java | 4 +- .../cent/EigenvectorCentralityAlgorithm.java | 2 +- .../cent/StressCentralityAlgorithm.java | 2 +- ....java => ClusterCoefficientAlgorithm.java} | 13 +++---- .../job/algorithm/comm/LouvainTraverser.java | 37 ++++++++----------- .../job/algorithm/comm/LpaAlgorithm.java | 2 +- .../job/computer/AbstractComputer.java | 5 +-- .../baidu/hugegraph/job/schema/SchemaJob.java | 4 +- .../job/system/DeleteExpiredIndexJob.java | 2 +- .../hugegraph/job/system/JobCounters.java | 11 +++--- 21 files changed, 75 insertions(+), 92 deletions(-) rename hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/comm/{ClusterCoeffcientAlgorithm.java => ClusterCoefficientAlgorithm.java} (84%) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/job/AlgorithmAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/job/AlgorithmAPI.java index c965e02a56..b0e0d06925 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/job/AlgorithmAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/job/AlgorithmAPI.java @@ -21,15 +21,6 @@ import java.util.Map; -import javax.inject.Singleton; -import javax.ws.rs.Consumes; -import javax.ws.rs.NotFoundException; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.core.Context; - import org.slf4j.Logger; import com.baidu.hugegraph.HugeGraph; @@ -46,6 +37,15 @@ import com.codahale.metrics.annotation.Timed; import com.google.common.collect.ImmutableMap; +import jakarta.inject.Singleton; +import jakarta.ws.rs.Consumes; +import jakarta.ws.rs.NotFoundException; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.PathParam; +import jakarta.ws.rs.Produces; +import jakarta.ws.rs.core.Context; + @Path("graphs/{graph}/jobs/algorithm") @Singleton public class AlgorithmAPI extends API { diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/AbstractAlgorithm.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/AbstractAlgorithm.java index 943debb4b8..dbe3f7e2b8 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/AbstractAlgorithm.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/AbstractAlgorithm.java @@ -57,8 +57,7 @@ import com.baidu.hugegraph.util.E; import com.baidu.hugegraph.util.JsonUtil; import com.baidu.hugegraph.util.ParameterUtil; - -import jersey.repackaged.com.google.common.base.Objects; +import com.google.common.base.Objects; @SuppressWarnings("deprecation") // StringEscapeUtils public abstract class AbstractAlgorithm implements Algorithm { @@ -382,9 +381,7 @@ protected Iterator vertices(Object label, long limit) { ConditionQuery query = new ConditionQuery(HugeType.VERTEX); query.capacity(Query.NO_CAPACITY); query.limit(limit); - if (label != null) { - query.eq(HugeKeys.LABEL, this.getVertexLabelId(label)); - } + query.eq(HugeKeys.LABEL, this.getVertexLabelId(label)); return this.graph().vertices(query); } @@ -544,8 +541,8 @@ public JsonMap() { this(4 * (int) Bytes.KB); } - public JsonMap(int initCapaticy) { - this.json = new StringBuilder(initCapaticy); + public JsonMap(int initCapacity) { + this.json = new StringBuilder(initCapacity); } public void startObject() { diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/Algorithm.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/Algorithm.java index b1cb531443..856e38dbcb 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/Algorithm.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/Algorithm.java @@ -25,11 +25,11 @@ public interface Algorithm { - public String name(); + String name(); - public String category(); + String category(); - public Object call(UserJob job, Map parameters); + Object call(UserJob job, Map parameters); - public void checkParameters(Map parameters); + void checkParameters(Map parameters); } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/AlgorithmPool.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/AlgorithmPool.java index 02ac4c24ea..7031318acb 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/AlgorithmPool.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/AlgorithmPool.java @@ -30,7 +30,7 @@ import com.baidu.hugegraph.job.algorithm.cent.EigenvectorCentralityAlgorithm; import com.baidu.hugegraph.job.algorithm.cent.StressCentralityAlgorithm; import com.baidu.hugegraph.job.algorithm.cent.StressCentralityAlgorithmV2; -import com.baidu.hugegraph.job.algorithm.comm.ClusterCoeffcientAlgorithm; +import com.baidu.hugegraph.job.algorithm.comm.ClusterCoefficientAlgorithm; import com.baidu.hugegraph.job.algorithm.comm.KCoreAlgorithm; import com.baidu.hugegraph.job.algorithm.comm.LouvainAlgorithm; import com.baidu.hugegraph.job.algorithm.comm.LpaAlgorithm; @@ -56,7 +56,7 @@ public class AlgorithmPool { INSTANCE.register(new EigenvectorCentralityAlgorithm()); INSTANCE.register(new TriangleCountAlgorithm()); - INSTANCE.register(new ClusterCoeffcientAlgorithm()); + INSTANCE.register(new ClusterCoefficientAlgorithm()); INSTANCE.register(new LpaAlgorithm()); INSTANCE.register(new LouvainAlgorithm()); INSTANCE.register(new WeakConnectedComponent()); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/BfsTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/BfsTraverser.java index 3b0920855f..a85cef0220 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/BfsTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/BfsTraverser.java @@ -36,7 +36,7 @@ public abstract class BfsTraverser extends AbstractAlgorithm.AlgoTraverser implements AutoCloseable { - private Stack traversedVertices = new Stack<>(); + private final Stack traversedVertices = new Stack<>(); public BfsTraverser(UserJob job) { super(job); @@ -113,7 +113,7 @@ public static class Node { private Id[] parents; private int pathCount; - private int distance; + private final int distance; public Node(Node parentNode) { this(0, parentNode.distance + 1); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/Consumers.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/Consumers.java index 1c68413fc0..9a60e3031a 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/Consumers.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/Consumers.java @@ -92,7 +92,7 @@ private Void runAndDone() { this.run(); this.done(); } catch (Throwable e) { - // Only the first exception of one thread can be stored + // Only the first exception to one thread can be stored this.exception = e; if (!(e instanceof StopExecution)) { LOG.error("Error when running task", e); @@ -110,7 +110,8 @@ private void run() { this.consume(); } assert this.ending; - while (this.consume()); + while (this.consume()) { + } LOG.debug("Worker finished"); } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/SubgraphStatAlgorithm.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/SubgraphStatAlgorithm.java index d91748e41e..814277b2a1 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/SubgraphStatAlgorithm.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/SubgraphStatAlgorithm.java @@ -22,7 +22,7 @@ import java.util.Iterator; import java.util.Map; -import org.apache.commons.configuration.PropertiesConfiguration; +import org.apache.commons.configuration2.PropertiesConfiguration; import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; import org.apache.tinkerpop.gremlin.structure.Vertex; import org.slf4j.Logger; @@ -90,7 +90,6 @@ private HugeGraph createTempGraph(UserJob job) { PropertiesConfiguration config = new PropertiesConfiguration(); config.setProperty(CoreOptions.BACKEND.name(), "memory"); config.setProperty(CoreOptions.STORE.name(), name); - config.setDelimiterParsingDisabled(true); /* * NOTE: this temp graph don't need to init backend because no task info * required, also not set started because no task to be scheduled. @@ -129,12 +128,11 @@ protected static boolean copySchema(Map parameters) { private static class Traverser extends AlgoTraverser { - private static Map PARAMS = ImmutableMap.of( - "depth", 10L, - "degree", -1L, - "sample", -1L, - "top", -1L /* sorted */, - "workers", 0); + private static final Map PARAMS = ImmutableMap.of("depth", 10L, + "degree", -1L, + "sample", -1L, + "top", -1L /* sorted */, + "workers", 0); public Traverser(UserJob job) { super(job); @@ -166,8 +164,8 @@ public Object subgraphStat(UserJob job) { results.put("page_ranks", pageRanks(job)); - algo = pool.get("cluster_coeffcient"); - results.put("cluster_coeffcient", algo.call(job, parameters)); + algo = pool.get("cluster_coefficient"); + results.put("cluster_coefficient", algo.call(job, parameters)); algo = pool.get("rings"); parameters = ImmutableMap.builder() diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/cent/AbstractCentAlgorithm.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/cent/AbstractCentAlgorithm.java index 066234873b..6a7ba396a4 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/cent/AbstractCentAlgorithm.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/cent/AbstractCentAlgorithm.java @@ -86,8 +86,7 @@ protected GraphTraversal constructSource( t = t.filter(it -> { this.updateProgress(++this.progress); - return sourceCLabel == null ? true : - match(it.get(), sourceCLabel); + return sourceCLabel == null || match(it.get(), sourceCLabel); }); if (sourceSample > 0L) { @@ -164,9 +163,7 @@ protected GraphTraversal filterNonShortestPath( triples.put(key, len); } else { assert len == shortest; - if (keepOneShortestPath) { - return false; - } + return !keepOneShortestPath; } return true; }); @@ -182,7 +179,7 @@ protected GraphTraversal substractPath( @SuppressWarnings("unchecked") Iterator items = (Iterator) path.iterator(); - return new MapperIterator<>(items, v -> v.id()); + return new MapperIterator<>(items, HugeVertex::id); } int len = path.size(); if (len < 3) { @@ -195,7 +192,7 @@ protected GraphTraversal substractPath( @SuppressWarnings("unchecked") Iterator items = (Iterator) path.iterator(); - return new MapperIterator<>(items, v -> v.id()); + return new MapperIterator<>(items, HugeVertex::id); }); } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/cent/BetweennessCentralityAlgorithm.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/cent/BetweennessCentralityAlgorithm.java index 46f4d4a405..25e1451cf4 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/cent/BetweennessCentralityAlgorithm.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/cent/BetweennessCentralityAlgorithm.java @@ -95,7 +95,7 @@ public Object betweennessCentrality(Directions direction, tg = this.computeBetweenness(tg); GraphTraversal tLimit = topN(tg, topN); - return this.execute(tLimit, () -> tLimit.next()); + return this.execute(tLimit, tLimit::next); } protected GraphTraversal groupPathByEndpoints( diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/cent/ClosenessCentralityAlgorithm.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/cent/ClosenessCentralityAlgorithm.java index 6a95794a0a..81979dc015 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/cent/ClosenessCentralityAlgorithm.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/cent/ClosenessCentralityAlgorithm.java @@ -102,7 +102,7 @@ public Object closenessCentrality(Directions direction, .math("_-1").sack(Operator.div).sack().sum()); GraphTraversal tLimit = topN(tg, topN); - return this.execute(tLimit, () -> tLimit.next()); + return this.execute(tLimit, tLimit::next); } } } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/cent/ClosenessCentralityAlgorithmV2.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/cent/ClosenessCentralityAlgorithmV2.java index 1651c8943d..55dc93ad99 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/cent/ClosenessCentralityAlgorithmV2.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/cent/ClosenessCentralityAlgorithmV2.java @@ -61,7 +61,7 @@ public Object call(UserJob job, Map parameters) { private static class Traverser extends BfsTraverser { - private Map globalCloseness; + private final Map globalCloseness; private float startVertexCloseness; diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/cent/DegreeCentralityAlgorithm.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/cent/DegreeCentralityAlgorithm.java index 6592c119d2..6032b56e2e 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/cent/DegreeCentralityAlgorithm.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/cent/DegreeCentralityAlgorithm.java @@ -19,7 +19,7 @@ package com.baidu.hugegraph.job.algorithm.cent; -import java.util.Arrays; +import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -155,7 +155,7 @@ protected Object degreeCentralityForBothDir(String label, long topN) { } private long degree(Id source, String label) { - List labels = label == null ? null : Arrays.asList(label); + List labels = label == null ? null : Collections.singletonList(label); EdgeStep step = new EdgeStep(this.graph(), Directions.BOTH, labels, null, NO_LIMIT, 0); return this.edgesCount(source, step); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/cent/EigenvectorCentralityAlgorithm.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/cent/EigenvectorCentralityAlgorithm.java index 15748ec726..d396f3cf39 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/cent/EigenvectorCentralityAlgorithm.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/cent/EigenvectorCentralityAlgorithm.java @@ -96,7 +96,7 @@ public Object eigenvectorCentrality(Directions direction, GraphTraversal tCap = t.cap("m"); GraphTraversal tLimit = topN(tCap, topN); - return this.execute(tLimit, () -> tLimit.next()); + return this.execute(tLimit, tLimit::next); } } } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/cent/StressCentralityAlgorithm.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/cent/StressCentralityAlgorithm.java index 87f1471d4b..6f41892553 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/cent/StressCentralityAlgorithm.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/cent/StressCentralityAlgorithm.java @@ -100,7 +100,7 @@ public Object stressCentrality(Directions direction, .groupCount(); GraphTraversal tLimit = topN(tg, topN); - return this.execute(tLimit, () -> tLimit.next()); + return this.execute(tLimit, tLimit::next); } } } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/comm/ClusterCoeffcientAlgorithm.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/comm/ClusterCoefficientAlgorithm.java similarity index 84% rename from hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/comm/ClusterCoeffcientAlgorithm.java rename to hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/comm/ClusterCoefficientAlgorithm.java index 2a0cf1a42e..b7a3895a31 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/comm/ClusterCoeffcientAlgorithm.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/comm/ClusterCoefficientAlgorithm.java @@ -26,9 +26,9 @@ import com.baidu.hugegraph.util.E; import com.baidu.hugegraph.util.InsertionOrderUtil; -public class ClusterCoeffcientAlgorithm extends AbstractCommAlgorithm { +public class ClusterCoefficientAlgorithm extends AbstractCommAlgorithm { - public static final String ALGO_NAME = "cluster_coeffcient"; + public static final String ALGO_NAME = "cluster_coefficient"; @Override public String name() { @@ -46,8 +46,7 @@ public void checkParameters(Map parameters) { public Object call(UserJob job, Map parameters) { int workers = workersWhenBoth(parameters); try (Traverser traverser = new Traverser(job, workers)) { - return traverser.clusterCoeffcient(direction(parameters), - degree(parameters)); + return traverser.clusterCoefficient(direction(parameters), degree(parameters)); } } @@ -67,18 +66,18 @@ public Traverser(UserJob job, int workers) { super(job, ALGO_NAME, workers); } - public Object clusterCoeffcient(Directions direction, long degree) { + public Object clusterCoefficient(Directions direction, long degree) { Map results = this.triangles(direction, degree); results = InsertionOrderUtil.newMap(results); long triangles = results.remove(KEY_TRIANGLES); long triads = results.remove(KEY_TRIADS); assert triangles <= triads; - double coeffcient = triads == 0L ? 0d : 1d * triangles / triads; + double coefficient = triads == 0L ? 0d : 1d * triangles / triads; @SuppressWarnings({ "unchecked", "rawtypes" }) Map converted = (Map) results; - converted.put("cluster_coeffcient", coeffcient); + converted.put("cluster_coefficient", coefficient); return results; } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/comm/LouvainTraverser.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/comm/LouvainTraverser.java index 4359d46b80..d6f647baf5 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/comm/LouvainTraverser.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/comm/LouvainTraverser.java @@ -20,11 +20,12 @@ package com.baidu.hugegraph.job.algorithm.comm; import java.io.BufferedOutputStream; -import java.io.FileOutputStream; import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -238,10 +239,7 @@ private boolean needSkipVertex(int pass, Vertex v) { } } // skip the vertex with unmatched clabel - if (this.sourceCLabel != null && !match(v, this.sourceCLabel)) { - return true; - } - return false; + return this.sourceCLabel != null && !match(v, this.sourceCLabel); } private Iterator sourceVertices(int pass) { @@ -321,9 +319,7 @@ private Community wrapCommunity(Vertex v, List nbs) { return comm; } - private Collection> nbCommunities( - int pass, - List edges) { + private Collection> nbCommunities(int pass, List edges) { // comms is a map of cid:[community,weight] Map> comms = new HashMap<>(); for (Edge edge : edges) { @@ -512,7 +508,7 @@ private void mergeCommunity(int pass, Community c, Set cvertices) { if (cvertices.contains(otherV.id())) { // inner edges of this community, will be calc twice // due to both e-in and e-out are in vertices, - kin += weightOfEdge(edge); + kin += (int) weightOfEdge(edge); continue; } assert this.cache.vertex2Community(otherV.id()) != null; @@ -579,11 +575,10 @@ public Object louvain(int maxTimes, int stableTimes, double precision) { int times = maxTimes; int movedTimes = 0; double movedPercent = 0d; - double lastMovedPercent = 0d; + double lastMovedPercent; for (int i = 0; i < maxTimes; i++) { boolean finished = true; - movedPercent = 0d; lastMovedPercent = 1d; int tinyChanges = 0; while ((movedPercent = this.moveCommunities(i)) > 0d) { @@ -654,7 +649,7 @@ private double modularity(String label) { public Collection showCommunity(String community) { final String C_PASS0 = labelOfPassN(0); - Collection comms = Arrays.asList(community); + Collection comms = Collections.singletonList(community); boolean reachPass0 = false; while (comms.size() > 0 && !reachPass0) { Iterator subComms = this.vertices(comms.iterator()); @@ -679,7 +674,7 @@ public long exportCommunity(int pass, boolean vertexFirst) { String label = labelOfPassN(pass); GraphTraversal t = this.g.V().hasLabel(label); this.execute(t, () -> { - try (OutputStream os = new FileOutputStream(exportFile); + try (OutputStream os = Files.newOutputStream(Paths.get(exportFile)); BufferedOutputStream bos = new BufferedOutputStream(os)) { while (t.hasNext()) { String comm = t.next().id().toString(); @@ -712,7 +707,7 @@ public long clearPass(int pass) { List els = this.cpassEdgeLabels(); if (els.size() > 0) { String first = els.remove(0); - te = te.hasLabel(first, els.toArray(new String[els.size()])); + te = te.hasLabel(first, els.toArray(new String[0])); this.drop(te); } // drop schema @@ -736,7 +731,7 @@ public long clearPass(int pass) { List vls = this.cpassVertexLabels(); if (vls.size() > 0) { String first = vls.remove(0); - tv = tv.hasLabel(first, vls.toArray(new String[vls.size()])); + tv = tv.hasLabel(first, vls.toArray(new String[0])); this.drop(tv); } // drop schema @@ -895,7 +890,7 @@ public Id genId2(int pass, Id cid) { // gen id for merge-community vertex String id = cid.toString(); if (pass == 0) { - // conncat pass with cid + // concat pass with cid id = pass + "~" + id; } else { // replace last pass with current pass @@ -915,11 +910,9 @@ public Collection>> communities(){ if (c.empty()) { continue; } - Pair> pair = comms.get(c.cid); - if (pair == null) { - pair = Pair.of(c, new HashSet<>()); - comms.put(c.cid, pair); - } + Pair> pair = comms.computeIfAbsent(c.cid, k -> { + return Pair.of(c, new HashSet<>()); + }); // collect members joined to the community [current pass] pair.getRight().add(e.getKey()); } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/comm/LpaAlgorithm.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/comm/LpaAlgorithm.java index f25201bea9..59d53245f8 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/comm/LpaAlgorithm.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/algorithm/comm/LpaAlgorithm.java @@ -184,7 +184,7 @@ private String voteCommunityOfVertex(Vertex vertex, String edgeLabel, Iterator neighbors = this.adjacentVertices(source, dir, labelId, degree); - // whether or not include vertex itself, greatly affects the result. + // whether include vertex itself, greatly affects the result. // get a larger number of small communities if include itself //neighbors.inject(v); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/computer/AbstractComputer.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/computer/AbstractComputer.java index ca08ec9a38..3ef49c751d 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/computer/AbstractComputer.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/computer/AbstractComputer.java @@ -174,7 +174,7 @@ private Map readSubConfig(String sub) { "'%s' must be contained in config '%s'", sub); ImmutableNode root = null; - NodeHandler nodeHandler = null; + NodeHandler nodeHandler; Map results = new HashMap<>(nodes.size()); for (HierarchicalConfiguration node : nodes) { NodeModel nodeModel = node.getNodeModel(); @@ -191,8 +191,7 @@ private Map readSubConfig(String sub) { private String[] constructShellCommands(Map configs) { String hadoopHome = System.getenv(HADOOP_HOME); String commandPrefix = String.format(MAIN_COMMAND, hadoopHome); - List command = new ArrayList<>(); - command.addAll(Arrays.asList(commandPrefix.split(SPACE))); + List command = new ArrayList<>(Arrays.asList(commandPrefix.split(SPACE))); command.add(this.name()); for (Map.Entry entry : configs.entrySet()) { command.add(MINUS_C); diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/schema/SchemaJob.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/schema/SchemaJob.java index afa6c30693..4841ac4843 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/schema/SchemaJob.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/schema/SchemaJob.java @@ -106,8 +106,8 @@ protected static void removeSchema(SchemaTransaction tx, /** * Use reflection to call SchemaTransaction.updateSchema(), * which is protected - * @param tx The update operation actual executer - * @param schema the schema to be update + * @param tx The update operation actual execute + * @param schema the schema to be updated */ protected static void updateSchema(SchemaTransaction tx, SchemaElement schema) { diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/system/DeleteExpiredIndexJob.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/system/DeleteExpiredIndexJob.java index ede9a03bb3..d7889a3be5 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/system/DeleteExpiredIndexJob.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/system/DeleteExpiredIndexJob.java @@ -34,7 +34,7 @@ public class DeleteExpiredIndexJob extends DeleteExpiredJob { private static final String JOB_TYPE = "delete_expired_index"; - private Set indexes; + private final Set indexes; public DeleteExpiredIndexJob(Set indexes) { E.checkArgument(indexes != null && !indexes.isEmpty(), diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/system/JobCounters.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/system/JobCounters.java index afbe890903..9f4bb1cae3 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/job/system/JobCounters.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/job/system/JobCounters.java @@ -30,8 +30,7 @@ public class JobCounters { - private ConcurrentHashMap jobCounters = - new ConcurrentHashMap<>(); + private final ConcurrentHashMap jobCounters = new ConcurrentHashMap<>(); public JobCounter jobCounter(HugeGraph g) { int batch = g.option(CoreOptions.TASK_TTL_DELETE_BATCH); @@ -44,10 +43,10 @@ public JobCounter jobCounter(HugeGraph g) { public static class JobCounter { - private AtomicInteger jobs; + private final AtomicInteger jobs; private Set elements; private Set indexes; - private int batchSize; + private final int batchSize; public JobCounter(int batchSize) { this.jobs = new AtomicInteger(0); @@ -94,7 +93,7 @@ public boolean addAndTriggerDelete(Object object) { /** * Try to add element in collection waiting to be deleted * @param element - * @return true if should create a new delete job, false otherwise + * @return true if we should create a new delete job, false otherwise */ public boolean addElementAndTriggerDelete(HugeElement element) { if (this.elements.size() >= this.batchSize) { @@ -107,7 +106,7 @@ public boolean addElementAndTriggerDelete(HugeElement element) { /** * Try to add edge in collection waiting to be deleted * @param index - * @return true if should create a new delete job, false otherwise + * @return true if we should create a new delete job, false otherwise */ public boolean addIndexAndTriggerDelete(HugeIndex index) { if (this.indexes.size() >= this.batchSize) {