use of com.baidu.hugegraph.computer.core.graph.vertex.Vertex in project hugegraph-computer by hugegraph.
the class StreamGraphOutputInputTest method testWriteReadEdgesWithSinglePerLabelFrequency.
@Test
public void testWriteReadEdgesWithSinglePerLabelFrequency() throws Exception {
UnitTestBase.updateOptions(ComputerOptions.INPUT_EDGE_FREQ, "SINGLE_PER_LABEL");
ComputerContext context = ComputerContext.instance();
GraphFactory graphFactory = context.graphFactory();
Id longId = BytesId.of(100L);
LongValue longValue = new LongValue(999L);
Vertex vertex = graphFactory().createVertex(longId, longValue);
vertex.addEdge(graphFactory.createEdge(BytesId.of(2L)));
vertex.addEdge(graphFactory.createEdge("knows", BytesId.of(3L)));
vertex.addEdge(graphFactory.createEdge("watch", BytesId.of(3L)));
vertex.addEdge(graphFactory.createEdge("watch", "1111", BytesId.of(4L)));
vertex.addEdge(graphFactory.createEdge("watch", "2222", BytesId.of(4L)));
byte[] bytes;
try (BytesOutput bao = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE)) {
StreamGraphOutput output = newStreamGraphOutput(bao);
output.writeEdges(vertex);
bytes = bao.toByteArray();
bytes = reweaveBytes(bytes);
}
try (BytesInput bai = IOFactory.createBytesInput(bytes)) {
StreamGraphInput input = newStreamGraphInput(bai);
assertEdgesEqual(vertex, input.readEdges(), EdgeFrequency.SINGLE_PER_LABEL);
}
}
use of com.baidu.hugegraph.computer.core.graph.vertex.Vertex in project hugegraph-computer by hugegraph.
the class StreamGraphOutputInputTest method testWriteReadEdgesWithMultipleFrequency.
@Test
public void testWriteReadEdgesWithMultipleFrequency() throws Exception {
UnitTestBase.updateOptions(ComputerOptions.INPUT_EDGE_FREQ, "MULTIPLE");
ComputerContext context = ComputerContext.instance();
GraphFactory graphFactory = context.graphFactory();
Id longId = BytesId.of(100L);
LongValue longValue = new LongValue(999L);
Vertex vertex = graphFactory().createVertex(longId, longValue);
vertex.addEdge(graphFactory.createEdge(BytesId.of(2L)));
vertex.addEdge(graphFactory.createEdge("knows", BytesId.of(3L)));
vertex.addEdge(graphFactory.createEdge("watch", BytesId.of(3L)));
vertex.addEdge(graphFactory.createEdge("watch", "1111", BytesId.of(4L)));
vertex.addEdge(graphFactory.createEdge("watch", "2222", BytesId.of(4L)));
byte[] bytes;
try (BytesOutput bao = IOFactory.createBytesOutput(Constants.SMALL_BUF_SIZE)) {
StreamGraphOutput output = newStreamGraphOutput(bao);
output.writeEdges(vertex);
bytes = bao.toByteArray();
bytes = reweaveBytes(bytes);
}
try (BytesInput bai = IOFactory.createBytesInput(bytes)) {
StreamGraphInput input = newStreamGraphInput(bai);
assertEdgesEqual(vertex, input.readEdges(), EdgeFrequency.MULTIPLE);
}
}
use of com.baidu.hugegraph.computer.core.graph.vertex.Vertex in project hugegraph-computer by hugegraph.
the class CsvStructGraphOutputTest method testWriteReadVertexOnlyIdAndValue.
@Test
public void testWriteReadVertexOnlyIdAndValue() throws IOException {
UnitTestBase.updateOptions(ComputerOptions.OUTPUT_WITH_ADJACENT_EDGES, "false", ComputerOptions.OUTPUT_WITH_VERTEX_PROPERTIES, "false", ComputerOptions.OUTPUT_WITH_EDGE_PROPERTIES, "false");
ComputerContext context = context();
GraphFactory factory = context.graphFactory();
Id longId = BytesId.of(100L);
Value value = BytesId.of(999L);
Vertex vertex = factory.createVertex(longId, value);
String fileName = "output.csv";
File file = new File(fileName);
try {
BufferedFileOutput dos = new BufferedFileOutput(file);
StructGraphOutput output = (StructGraphOutput) IOFactory.createGraphOutput(context, OutputFormat.CSV, dos);
output.writeVertex(vertex);
dos.close();
@SuppressWarnings("deprecation") String text = FileUtils.readFileToString(file);
Assert.assertEquals("100,999" + System.lineSeparator(), text);
} finally {
FileUtils.deleteQuietly(file);
}
}
use of com.baidu.hugegraph.computer.core.graph.vertex.Vertex in project hugegraph-computer by hugegraph.
the class StreamGraphInput method readEdges.
@Override
public Vertex readEdges() throws IOException {
Vertex vertex = this.graphFactory.createVertex();
KvEntryReader reader = this.in.readEntry(in -> {
// Read id
vertex.id(readId(in));
});
if (this.frequency == EdgeFrequency.SINGLE) {
while (reader.hasRemaining()) {
Edge edge = this.graphFactory.createEdge();
// Only use targetId as subKey, use properties as subValue
reader.readSubKv(in -> {
edge.targetId(readId(in));
}, in -> {
edge.properties(readProperties(in));
});
vertex.addEdge(edge);
}
} else if (this.frequency == EdgeFrequency.SINGLE_PER_LABEL) {
while (reader.hasRemaining()) {
Edge edge = this.graphFactory.createEdge();
// Use label + targetId as subKey, use properties as subValue
reader.readSubKv(in -> {
edge.label(readLabel(in));
edge.targetId(readId(in));
}, in -> {
edge.properties(readProperties(in));
});
vertex.addEdge(edge);
}
} else {
assert this.frequency == EdgeFrequency.MULTIPLE;
while (reader.hasRemaining()) {
Edge edge = this.graphFactory.createEdge();
/*
* Use label + sortValues + targetId as subKey,
* use properties as subValue
*/
reader.readSubKv(in -> {
edge.label(readLabel(in));
edge.name(readLabel(in));
edge.targetId(readId(in));
}, in -> {
edge.properties(this.readProperties(in));
});
vertex.addEdge(edge);
}
}
return vertex;
}
use of com.baidu.hugegraph.computer.core.graph.vertex.Vertex in project hugegraph-computer by hugegraph.
the class StreamGraphInput method readVertex.
@Override
public Vertex readVertex() throws IOException {
Vertex vertex = this.graphFactory.createVertex();
this.in.readEntry(in -> {
vertex.id(readId(in));
}, in -> {
vertex.label(readLabel(in));
vertex.properties(readProperties(in));
});
return vertex;
}
Aggregations