package com.tinkerpop.gremlin.structure.io;

import com.tinkerpop.gremlin.structure.Graph;
import com.tinkerpop.gremlin.structure.io.kryo.KryoReader;
import com.tinkerpop.gremlin.structure.io.kryo.KryoWriter;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

/**
 * {@link GraphMigrator} takes the data in one graph and pipes it to another graph.  Uses the {@link KryoReader}
 * and {@link KryoWriter} to migrate the graph data as it is the only IO implementation that is non-lossy.
 *
 * @author Alex Averbuch (alex.averbuch@gmail.com)
 * @author Stephen Mallette (http://stephen.genoprime.com)
 */
public final class GraphMigrator {

    private static final KryoReader defaultKryoReader = new KryoReader.Builder().build();
    private static final KryoWriter defaultKryoWriter = new KryoWriter.Builder().build();

    public static void migrateGraph(final Graph fromGraph, final Graph toGraph) throws IOException {
        // todo: incremental loading?
        migrateGraph(fromGraph, toGraph, defaultKryoReader, defaultKryoWriter);
    }

    /**
     * Pipe the data from one graph to another graph.  It is important that the reader and writer utilize the
     * same format.
     *
     * @param fromGraph the graph to take data from
     * @param toGraph   the graph to take data to
     * @param reader reads from the graph written by the writer
     * @param writer writes the graph to be read by the reader
     * @throws java.io.IOException        thrown if there is an error in steam between the two graphs
     */
    public static void migrateGraph(final Graph fromGraph, final Graph toGraph,
                                    final KryoReader reader, final KryoWriter writer) throws IOException {

        final PipedInputStream inPipe = new PipedInputStream() {
            // Default is 1024
            protected static final int PIPE_SIZE = 1024;
        };

        final PipedOutputStream outPipe = new PipedOutputStream(inPipe) {
            public void close() throws IOException {
                while (inPipe.available() > 0) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                super.close();
            }
        };

        new Thread(() -> {
            try {
                writer.writeGraph(outPipe, fromGraph);
                outPipe.flush();
                outPipe.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();

        reader.readGraph(inPipe, toGraph);
    }

    // todo: could do a "mergeGraph" maybe if IO ends up supporting incremental loading
}

@@ -1751,24 +1751,132 @@
 xception %7B%0A%0A
+        // todo: if this is the standard way to %22migrate%22 a graph, then rethink exception handling and such%0A
         fina

Add todo comment for later.