]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.graph.db/src/org/simantics/graph/db/TransferableGraphs.java
Added more TransferableGraphs.writeTransferableGraph methods
[simantics/platform.git] / bundles / org.simantics.graph.db / src / org / simantics / graph / db / TransferableGraphs.java
index edcbc51d86499597619ee29b504de0bd087f2d19..0841e1455569ed08e0f184c750c5125e9fe55aa3 100644 (file)
@@ -21,6 +21,7 @@ import java.util.Collection;
 import java.util.HashSet;
 import java.util.TreeMap;
 import java.util.UUID;
+import java.util.function.BiFunction;
 
 import org.simantics.databoard.Accessors;
 import org.simantics.databoard.Bindings;
@@ -61,12 +62,14 @@ import org.simantics.graph.representation.External;
 import org.simantics.graph.representation.Identity;
 import org.simantics.graph.representation.TransferableGraph1;
 import org.simantics.graph.representation.Value;
-import org.simantics.utils.datastructures.BinaryFunction;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import gnu.trove.list.array.TIntArrayList;
 import gnu.trove.map.hash.TObjectIntHashMap;
 
 public class TransferableGraphs {
+    final static Logger LOGGER = LoggerFactory.getLogger(TransferableGraphs.class); 
 
        public static long[] importGraph(Session session, Object tg, IImportAdvisor advisor) throws DatabaseException, TransferableGraphException {
                if (tg instanceof TransferableGraph1) 
@@ -151,7 +154,7 @@ public class TransferableGraphs {
                                session.getService(SerialisationSupport.class));
        }
 
-       public static void importGraph1(Session session, final TransferableGraph1 tg, IImportAdvisor advisor, final BinaryFunction<Boolean, WriteOnlyGraph, TransferableGraphImportProcess> callback) throws DatabaseException, TransferableGraphException {
+       public static void importGraph1(Session session, final TransferableGraph1 tg, IImportAdvisor advisor, final BiFunction<WriteOnlyGraph, TransferableGraphImportProcess, Boolean> callback) throws DatabaseException, TransferableGraphException {
                final TransferableGraphImportProcess process = new TransferableGraphImportProcess(tg, 
                                advisor == null ? new ImportAdvisor() : advisor);
                session.syncRequest(new ReadRequest() {
@@ -165,24 +168,24 @@ public class TransferableGraphs {
                        public void perform(WriteOnlyGraph graph) throws DatabaseException {
                                process.write(graph);
                                if(callback != null)
-                                       callback.call(graph, process);
+                                       callback.apply(graph, process);
                        }
                });
        }
 
-    public static void importGraph1(Session session, final TransferableGraphSource tg, IImportAdvisor advisor) throws Exception {
-        importGraph1(session, tg, advisor, null);
+    public static ImportResult importGraph1(Session session, final TransferableGraphSource tg, IImportAdvisor advisor) throws Exception {
+        return importGraph1(session, tg, advisor, null);
     }
 
-    public static void importGraph1(Session session, final TransferableGraphSource tg, IImportAdvisor advisor, TGStatusMonitor monitor) throws DatabaseException {
-        importGraph1(session, null, tg, advisor, monitor);
+    public static ImportResult importGraph1(Session session, final TransferableGraphSource tg, IImportAdvisor advisor, TGStatusMonitor monitor) throws DatabaseException {
+        return importGraph1(session, null, tg, advisor, monitor);
     }
 
-    public static void importGraph1(Session session, VirtualGraph vg, final TransferableGraphSource tg, IImportAdvisor advisor_, TGStatusMonitor monitor) throws DatabaseException {
+    public static ImportResult importGraph1(Session session, VirtualGraph vg, final TransferableGraphSource tg, IImportAdvisor advisor_, TGStatusMonitor monitor) throws DatabaseException {
         
         final IImportAdvisor2 advisor = (advisor_ instanceof IImportAdvisor2) ? ((IImportAdvisor2)advisor_) : new WrapperAdvisor(advisor_);
 
-               final StreamingTransferableGraphImportProcess process = new StreamingTransferableGraphImportProcess(session, vg, tg, advisor);
+               final StreamingTransferableGraphImportProcess process = new StreamingTransferableGraphImportProcess(session, vg, tg, advisor, monitor);
                session.syncRequest(new ReadRequest() {
                        @Override
                        public void run(ReadGraph graph) throws DatabaseException {
@@ -207,6 +210,8 @@ public class TransferableGraphs {
                                }
                        }
                });
+
+               return new ImportResult(process.missingExternals);
        }
 
        public static void importGraph1WithMonitor(Session session, final TransferableGraph1 tg, IImportAdvisor advisor_, TGStatusMonitor monitor) throws DatabaseException {
@@ -232,7 +237,7 @@ public class TransferableGraphs {
                });
        }
 
-       public static void importGraph1WithChanges(Session session, final TransferableGraph1 tg, IImportAdvisor advisor, final BinaryFunction<Boolean, WriteGraph, TransferableGraphImportProcess> callback) throws DatabaseException, TransferableGraphException {
+       public static void importGraph1WithChanges(Session session, final TransferableGraph1 tg, IImportAdvisor advisor, final BiFunction<WriteGraph, TransferableGraphImportProcess, Boolean> callback) throws DatabaseException, TransferableGraphException {
                final TransferableGraphImportProcess process = new TransferableGraphImportProcess(tg, 
                                advisor == null ? new ImportAdvisor() : advisor);
                session.syncRequest(new ReadRequest() {
@@ -249,7 +254,7 @@ public class TransferableGraphs {
                 comments.add("Imported transferable graph with " + tg.resourceCount + " resources");
                 graph.addMetadata(comments);
                                if(callback != null)
-                                       callback.call(graph, process);
+                                       callback.apply(graph, process);
                        }
                });
        }
@@ -435,15 +440,23 @@ public class TransferableGraphs {
        }
 
        public static void writeTransferableGraph(RequestProcessor processor, String format, int version, TreeMap<String, Variant> metadata, TransferableGraphSource source, File target, TGStatusMonitor monitor) throws Exception {
-               final Serializer datatypeSerializer = Bindings.getSerializerUnchecked(Datatype.class);
                try (RandomAccessBinary out = new BinaryFile(target, 128*1024)) {
-                       DataContainer container = new DataContainer(format, version, metadata, null);
-                       DataContainers.writeHeader(out, container);
-                       datatypeSerializer.serialize((DataOutput) out, Datatypes.getDatatypeUnchecked(TransferableGraph1.class));
-                       writeTransferableGraph(processor, source, out, monitor);
+                       DataContainers.writeHeader(out, new DataContainer(format, version, metadata, null));
+                       writeTransferableGraphVariant(processor, source, out, monitor);
+               }
+       }
+
+       public static void writeTransferableGraph(RequestProcessor processor, TransferableGraphSource source, File target, TGStatusMonitor monitor) throws Exception {
+               try (RandomAccessBinary out = new BinaryFile(target, 128*1024)) {
+                       writeTransferableGraphVariant(processor, source, out, monitor);
                }
        }
 
+       public static void writeTransferableGraphVariant(RequestProcessor processor, TransferableGraphSource source, RandomAccessBinary out, TGStatusMonitor monitor) throws Exception {
+               Bindings.getSerializerUnchecked(Datatype.class).serialize(out, Datatypes.getDatatypeUnchecked(TransferableGraph1.class));
+               writeTransferableGraph(processor, source, out, monitor);
+       }
+
        private static TGStatusMonitor safeMonitor(TGStatusMonitor mon) {
                return mon == null ? TGStatusMonitor.NULL_MONITOR : mon;
        }
@@ -579,7 +592,7 @@ public class TransferableGraphs {
                });
 
                long end = System.nanoTime();
-               System.err.println("Wrote transferable graph in " + 1e-9*(end-start) + " seconds.");
+               LOGGER.info("Wrote transferable graph in {} seconds.", 1e-9*(end-start));
        }
 
        public static TransferableGraph1 create(ReadGraph graph, TransferableGraphSource source) throws DatabaseException {