Github user spmallette commented on a diff in the pull request: https://github.com/apache/tinkerpop/pull/893#discussion_r206269042 --- Diff: gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/sideEffect/IoStep.java --- @@ -0,0 +1,254 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect; + +import org.apache.tinkerpop.gremlin.process.traversal.IO; +import org.apache.tinkerpop.gremlin.process.traversal.Traversal; +import org.apache.tinkerpop.gremlin.process.traversal.Traverser; +import org.apache.tinkerpop.gremlin.process.traversal.step.ReadWriting; +import org.apache.tinkerpop.gremlin.process.traversal.step.util.AbstractStep; +import org.apache.tinkerpop.gremlin.process.traversal.step.util.Parameters; +import org.apache.tinkerpop.gremlin.process.traversal.traverser.util.EmptyTraverser; +import org.apache.tinkerpop.gremlin.process.traversal.util.FastNoSuchElementException; +import org.apache.tinkerpop.gremlin.structure.Graph; +import org.apache.tinkerpop.gremlin.structure.io.GraphReader; +import org.apache.tinkerpop.gremlin.structure.io.GraphWriter; +import org.apache.tinkerpop.gremlin.structure.io.IoRegistry; +import org.apache.tinkerpop.gremlin.structure.io.graphml.GraphMLReader; +import org.apache.tinkerpop.gremlin.structure.io.graphml.GraphMLWriter; +import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONMapper; +import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONReader; +import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONWriter; +import org.apache.tinkerpop.gremlin.structure.io.gryo.GryoMapper; +import org.apache.tinkerpop.gremlin.structure.io.gryo.GryoReader; +import org.apache.tinkerpop.gremlin.structure.io.gryo.GryoWriter; +import org.apache.tinkerpop.gremlin.structure.util.StringFactory; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.lang.reflect.Method; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +/** + * Handles read and write operations into the {@link Graph}. + * + * @author Stephen Mallette (http://stephen.genoprime.com) + */ +public class IoStep<S> extends AbstractStep<S,S> implements ReadWriting { + + private Parameters parameters = new Parameters(); + private boolean first = true; + private String file; + private Mode mode = Mode.UNSET; + + public IoStep(final Traversal.Admin traversal, final String file) { + super(traversal); + + if (null == file || file.isEmpty()) + throw new IllegalArgumentException("file cannot be null or empty"); + + this.file = file; + } + + @Override + public void setMode(final Mode mode) { + this.mode = mode; + } + + @Override + public Mode getMode() { + return mode; + } + + @Override + public String getFile() { + return file; + } + + @Override + public Parameters getParameters() { + return this.parameters; + } + + @Override + public void configure(final Object... keyValues) { + this.parameters.set(null, keyValues); + } + + @Override + protected Traverser.Admin<S> processNextStart() { + if (mode == Mode.UNSET) throw new IllegalStateException("IO mode was not set to read() or write()"); + if (!this.first) throw FastNoSuchElementException.instance(); + + this.first = false; + final File file = new File(this.file); + + if (mode == Mode.READING) { + if (!file.exists()) throw new IllegalStateException(this.file + " does not exist"); + return read(file); + } else if (mode == Mode.WRITING) { + return write(file); + } else { + throw new IllegalStateException("Invalid ReadWriting.Mode configured in IoStep: " + mode.name()); + } + } + + private Traverser.Admin<S> write(final File file) { + try (final OutputStream stream = new FileOutputStream(file)) { + final Graph graph = (Graph) this.traversal.getGraph().get(); + constructWriter().writeGraph(stream, graph); + + return EmptyTraverser.instance(); + } catch (IOException ioe) { + throw new IllegalStateException(String.format("Could not write file %s from graph", this.file), ioe); + } + } + + private Traverser.Admin<S> read(final File file) { + try (final InputStream stream = new FileInputStream(file)) { --- End diff -- hmm - i started looking at this some more. i've already got a gang of work on this. if we decide we want to take another step to be able to read from a URL maybe we add that later on a separate ticket.
---