Skip to content
Permalink
Browse files
fix some warnings (#111)
* fix some warnings
* improve algorithm test
  • Loading branch information
javeme committed Oct 9, 2021
1 parent 063387d commit 393679249591e594ec150334f7c78f9b2c73c2a8
Showing 13 changed files with 96 additions and 117 deletions.
@@ -123,12 +123,12 @@ private class SuperEdges implements Edges {
@Override
public int size() {
if (this.size == 0) {
this.computeSize();
this.calculateSize();
}
return this.size;
}

private void computeSize() {
private void calculateSize() {
long currentPosition = EdgesInput.this.input.position();
try {
EdgesInput.this.input.seek(this.startPosition);
@@ -178,26 +178,24 @@ private class EdgesIterator implements Iterator<Edge> {
public boolean hasNext() {
if (currentEdgesIter.hasNext()) {
return true;
} else {
long currentPosition = input.position();
try {
if (input.available() > 0) {
idPointer.read(input);
if (idPointer.compareTo(vid) == 0) {
valuePointer.read(EdgesInput.this.input);
currentEdgesIter = readEdges(
valuePointer.input())
.iterator();
} else {
input.seek(currentPosition);
}
}
long currentPosition = input.position();
try {
if (input.available() > 0) {
idPointer.read(input);
if (idPointer.compareTo(vid) == 0) {
valuePointer.read(input);
currentEdgesIter = readEdges(valuePointer.input())
.iterator();
} else {
input.seek(currentPosition);
}
} catch (IOException e) {
throw new ComputerException(
"Error occurred when read edges from edges " +
"input '%s' at position %s", e,
edgeFile.getAbsoluteFile(), currentPosition);
}
} catch (IOException e) {
throw new ComputerException(
"Error occurred when read edges from edges " +
"input '%s' at position %s", e,
edgeFile.getAbsoluteFile(), currentPosition);
}
return currentEdgesIter.hasNext();
}
@@ -230,7 +228,7 @@ private Edges readEdges(RandomAccessInput in) {
for (int i = 0; i < count; i++) {
Edge edge = this.graphFactory.createEdge();
// Use label + targetId as subKey, use props as subValue
edge.label(in.readUTF());
edge.label(StreamGraphInput.readLabel(in));
edge.targetId(StreamGraphInput.readId(in));
// Read subValue
Properties props = this.graphFactory.createProperties();
@@ -246,8 +244,8 @@ private Edges readEdges(RandomAccessInput in) {
* Use label + sortValues + targetId as subKey,
* use properties as subValue
*/
edge.label(in.readUTF());
edge.name(in.readUTF());
edge.label(StreamGraphInput.readLabel(in));
edge.name(StreamGraphInput.readLabel(in));
edge.targetId(StreamGraphInput.readId(in));
// Read subValue
Properties props = this.graphFactory.createProperties();
@@ -58,8 +58,8 @@ public Vertex readVertex() throws IOException {
this.in.readEntry(in -> {
vertex.id(readId(in));
}, in -> {
vertex.label(this.readLabel(in));
vertex.properties(this.readProperties(in));
vertex.label(readLabel(in));
vertex.properties(readProperties(in));
});
return vertex;
}
@@ -79,7 +79,7 @@ public Vertex readEdges() throws IOException {
edge.targetId(readId(in));
}, in -> {
edge.label(readLabel(in));
edge.properties(this.readProperties(in));
edge.properties(readProperties(in));
});
vertex.addEdge(edge);
}
@@ -88,10 +88,10 @@ public Vertex readEdges() throws IOException {
Edge edge = this.graphFactory.createEdge();
// Use label + targetId as subKey, use properties as subValue
reader.readSubKv(in -> {
edge.label(in.readUTF());
edge.label(readLabel(in));
edge.targetId(readId(in));
}, in -> {
edge.properties(this.readProperties(in));
edge.properties(readProperties(in));
});
vertex.addEdge(edge);
}
@@ -104,8 +104,8 @@ public Vertex readEdges() throws IOException {
* use properties as subValue
*/
reader.readSubKv(in -> {
edge.label(in.readUTF());
edge.name(in.readUTF());
edge.label(readLabel(in));
edge.name(readLabel(in));
edge.targetId(readId(in));
}, in -> {
edge.properties(this.readProperties(in));
@@ -143,12 +143,6 @@ public Value<?> readValue(RandomAccessInput in) throws IOException {
return value;
}

public static Id readId(RandomAccessInput in) throws IOException {
Id id = new BytesId();
id.read(in);
return id;
}

private Properties readProperties(RandomAccessInput in) throws IOException {
Properties properties = this.graphFactory.createProperties();
int size = in.readInt();
@@ -160,6 +154,12 @@ private Properties readProperties(RandomAccessInput in) throws IOException {
return properties;
}

public static Id readId(RandomAccessInput in) throws IOException {
Id id = new BytesId();
id.read(in);
return id;
}

public static String readLabel(RandomAccessInput in) throws IOException {
return in.readUTF();
}
@@ -76,7 +76,7 @@ public void writeEdges(Vertex vertex) throws IOException {
for (Edge edge : vertex.edges()) {
// Use label + targetId as subKey, use properties as subValue
writer.writeSubKv(out -> {
out.writeUTF(edge.label());
this.writeLabel(out, edge.label());
this.writeId(out, edge.targetId());
}, out -> {
this.writeProperties(out, edge.properties());
@@ -90,8 +90,8 @@ public void writeEdges(Vertex vertex) throws IOException {
* use properties as subValue
*/
writer.writeSubKv(out -> {
out.writeUTF(edge.label());
out.writeUTF(edge.name());
this.writeLabel(out, edge.label());
this.writeLabel(out, edge.name());
this.writeId(out, edge.targetId());
}, out -> {
this.writeProperties(out, edge.properties());
@@ -26,6 +26,7 @@
import com.baidu.hugegraph.computer.core.util.StringEncoding;
import com.baidu.hugegraph.testutil.Whitebox;

@SuppressWarnings("deprecation") // StringEscapeUtils
public class StructRandomAccessOutput implements RandomAccessOutput {

private final RandomAccessOutput output;
@@ -173,7 +173,9 @@ private void mergeOutputFilesIfNeeded() {
* TODO Restore genOutputFileNames(sqrt(outputFiles.size()))
* after add Sorter#iterator() of subkv
*/
List<String> newOutputs = this.genOutputFileNames(1);
int mergeFileNum = this.mergeFileNum;
mergeFileNum = 1;
List<String> newOutputs = this.genOutputFileNames(mergeFileNum);
this.sortManager.mergeInputs(this.outputFiles, newOutputs,
this.withSubKv, this.outerSortFlusher());
this.outputFiles = newOutputs;
@@ -81,6 +81,7 @@ public SortManager(ComputerContext context) {
ComputerOptions.INPUT_MAX_EDGES_IN_ONE_VERTEX);
}

@Override
public abstract String name();

protected abstract String threadPrefix();
@@ -225,8 +226,8 @@ private Combiner<Pointer> createEdgeCombiner() {

private Combiner<Pointer> createMessageCombiner() {
Config config = this.context.config();
Combiner<?> valueCombiner = config.createObject(
ComputerOptions.WORKER_COMBINER_CLASS, false);
Combiner<Value<?>> valueCombiner = config.createObject(
ComputerOptions.WORKER_COMBINER_CLASS, false);

if (valueCombiner == null) {
return null;
@@ -235,7 +236,7 @@ private Combiner<Pointer> createMessageCombiner() {
Value<?> v1 = config.createObject(
ComputerOptions.ALGORITHM_MESSAGE_CLASS);
Value<?> v2 = v1.copy();
return new PointerCombiner(v1, v2, valueCombiner);
return new PointerCombiner<>(v1, v2, valueCombiner);
}

private Combiner<Pointer> createPropertiesCombiner(
@@ -38,11 +38,11 @@
public class DegreeCentralityTest extends AlgorithmTestBase {

@Test
public void testServiceWith1Worker() throws InterruptedException {
public void testRunAlgorithm() throws InterruptedException {
runAlgorithm(DegreeCentralityTestParams.class.getName(),
DegreeCentrality.OPTION_WEIGHT_PROPERTY,
"rate");
DegreeCentrality.OPTION_WEIGHT_PROPERTY, "rate");
DegreeCentralityTestOutput.assertResult();

runAlgorithm(DegreeCentralityTestParams.class.getName());
DegreeCentralityTestOutput.assertResult();
}
@@ -26,7 +26,7 @@
public class PageRankTest extends AlgorithmTestBase {

@Test
public void testServiceWith1Worker() throws InterruptedException {
public void testRunAlgorithm() throws InterruptedException {
runAlgorithm(PageRankParams.class.getName());
}
}
@@ -38,7 +38,7 @@
import jersey.repackaged.com.google.common.collect.ImmutableMap;

public class TriangleCountTest extends AlgorithmTestBase {

private static final String VERTX_LABEL = "tc_user";
private static final String EDGE_LABEL = "tc_know";
private static final String PROPERTY_KEY = "tc_weight";
@@ -96,6 +96,13 @@ public static void teardown() {
clearAll();
}

@Test
public void testRunAlgorithm() throws InterruptedException {
runAlgorithm(TriangleCountParams.class.getName(),
ComputerOptions.OUTPUT_CLASS.name(),
TriangleCountOutputTest.class.getName());
}

@Test
public void testTriangleCountValue() {
TriangleCountValue value = new TriangleCountValue();
@@ -112,13 +119,6 @@ public void testTriangleCountValue() {
Assert.assertContains("10", value.toString());
}

@Test
public void testTriangleCount() throws InterruptedException {
runAlgorithm(TriangleCountParams.class.getName(),
ComputerOptions.OUTPUT_CLASS.name(),
TriangleCountOutputTest.class.getName());
}

public static class TriangleCountOutputTest extends TriangleCountOutput {

@Override
@@ -27,9 +27,9 @@
public class WccTest extends AlgorithmTestBase {

@Test
public void testServiceWith1Worker() throws InterruptedException {
public void testRunAlgorithm() throws InterruptedException {
runAlgorithm(WccParams.class.getName(),
ComputerOptions.JOB_ID.name(), "local_002",
ComputerOptions.JOB_ID.name(), "local_wcc",
ComputerOptions.JOB_WORKERS_COUNT.name(), "1",
ComputerOptions.BSP_REGISTER_TIMEOUT.name(), "100000",
ComputerOptions.BSP_LOG_INTERVAL.name(), "30000",
@@ -19,6 +19,7 @@

package com.baidu.hugegraph.computer.algorithm.path.rings;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@@ -28,11 +29,14 @@

import com.baidu.hugegraph.computer.algorithm.AlgorithmTestBase;
import com.baidu.hugegraph.computer.core.config.ComputerOptions;
import com.baidu.hugegraph.computer.core.graph.value.IdList;
import com.baidu.hugegraph.computer.core.graph.value.IdListList;
import com.baidu.hugegraph.driver.GraphManager;
import com.baidu.hugegraph.driver.HugeClient;
import com.baidu.hugegraph.driver.SchemaManager;
import com.baidu.hugegraph.structure.constant.T;
import com.baidu.hugegraph.structure.graph.Vertex;
import com.baidu.hugegraph.testutil.Assert;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

@@ -88,7 +92,7 @@ public static void clear() {
}

@Test
public void test() throws InterruptedException {
public void testRunAlgorithm() throws InterruptedException {
runAlgorithm(RingsDetectionTestParams.class.getName());
}

@@ -101,4 +105,34 @@ public void setAlgorithmParameters(Map<String, String> params) {
super.setAlgorithmParameters(params);
}
}

public static class RingsDetectionTestOutput extends RingsDetectionOutput {

public static Map<String, Set<String>> EXPECT_RINGS;

@Override
public void write(
com.baidu.hugegraph.computer.core.graph.vertex.Vertex vertex) {
super.write(vertex);
this.assertResult(vertex);
}

private void assertResult(
com.baidu.hugegraph.computer.core.graph.vertex.Vertex vertex) {
IdListList rings = vertex.value();
Set<String> expect =
EXPECT_RINGS.getOrDefault(vertex.id().toString(),
new HashSet<>());

Assert.assertEquals(expect.size(), rings.size());
for (int i = 0; i < rings.size(); i++) {
IdList ring = rings.get(0);
StringBuilder ringValue = new StringBuilder();
for (int j = 0; j < ring.size(); j++) {
ringValue.append(ring.get(j).toString());
}
Assert.assertTrue(expect.contains(ringValue.toString()));
}
}
}
}

0 comments on commit 3936792

Please sign in to comment.