}
@Override
public void readGraph(final InputStream inputStream, final Graph graphToWriteTo) throws IOException {
this.counter.set(0);
final Input input = new Input(inputStream);
this.headerReader.read(kryo, input);
final BatchGraph graph;
try {
// will throw an exception if not constructed properly
graph = BatchGraph.build(graphToWriteTo)
.vertexIdKey(vertexIdKey)
.edgeIdKey(edgeIdKey)
.bufferSize(batchSize).create();
} catch (Exception ex) {
throw new IOException("Could not instantiate BatchGraph wrapper", ex);
}
try (final Output output = new Output(new FileOutputStream(tempFile))) {
final boolean supportedMemory = input.readBoolean();
if (supportedMemory) {
// if the graph that serialized the data supported sideEffects then the sideEffects needs to be read
// to advance the reader forward. if the graph being read into doesn't support the sideEffects
// then we just setting the data to sideEffects.
final Map<String, Object> memMap = (Map<String, Object>) kryo.readObject(input, HashMap.class);
if (graphToWriteTo.features().graph().variables().supportsVariables()) {
final Graph.Variables variables = graphToWriteTo.variables();
memMap.forEach(variables::set);
}
}
final boolean hasSomeVertices = input.readBoolean();
if (hasSomeVertices) {
while (!input.eof()) {
final List<Object> vertexArgs = new ArrayList<>();
final DetachedVertex current = (DetachedVertex) kryo.readClassAndObject(input);
appendToArgList(vertexArgs, T.id, current.id());
appendToArgList(vertexArgs, T.label, current.label());
final Vertex v = graph.addVertex(vertexArgs.toArray());
current.iterators().propertyIterator().forEachRemaining(p -> createVertexProperty(graphToWriteTo, v, p, false));
current.iterators().hiddenPropertyIterator().forEachRemaining(p -> createVertexProperty(graphToWriteTo, v, p, true));
// the gio file should have been written with a direction specified
final boolean hasDirectionSpecified = input.readBoolean();
final Direction directionInStream = kryo.readObject(input, Direction.class);
final Direction directionOfEdgeBatch = kryo.readObject(input, Direction.class);
// graph serialization requires that a direction be specified in the stream and that the
// direction of the edges be OUT
if (!hasDirectionSpecified || directionInStream != Direction.OUT || directionOfEdgeBatch != Direction.OUT)
throw new IllegalStateException(String.format("Stream must specify edge direction and that direction must be %s", Direction.OUT));
// if there are edges then read them to end and write to temp, otherwise read what should be
// the vertex terminator
if (!input.readBoolean())
kryo.readClassAndObject(input);
else
readToEndOfEdgesAndWriteToTemp(input, output);
}
}
} catch (Exception ex) {
throw new IOException(ex);
}
// done writing to temp
// start reading in the edges now from the temp file
try (final Input edgeInput = new Input(new FileInputStream(tempFile))) {
readFromTempEdges(edgeInput, graph);
graph.tx().commit();
} catch (Exception ex) {
ex.printStackTrace();
throw new IOException(ex);