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;
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;
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() {
public void perform(WriteOnlyGraph graph) throws DatabaseException {
process.write(graph);
if(callback != null)
- callback.call(graph, process);
+ callback.apply(graph, process);
}
});
}
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 {
});
}
- 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() {
comments.add("Imported transferable graph with " + tg.resourceCount + " resources");
graph.addMetadata(comments);
if(callback != null)
- callback.call(graph, process);
+ callback.apply(graph, process);
}
});
}
}
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;
}
public static TransferableGraph1 create(ReadGraph graph, TransferableGraphSource source) throws DatabaseException {
- final TIntArrayList statements = new TIntArrayList();
- final ArrayList<Value> values = new ArrayList<>();
- final ArrayList<Identity> identities = new ArrayList<>();
-
try {
+ ArrayList<Identity> identities = new ArrayList<>(source.getIdentityCount());
+ source.forIdentities(graph, i -> identities.add(i));
+ TIntArrayList statements = new TIntArrayList(source.getStatementCount());
source.forStatements(graph, r -> statements.addAll(r));
+ ArrayList<Value> values = new ArrayList<>(source.getValueCount());
source.forValues(graph, v -> values.add(v));
- source.forIdentities(graph, i -> identities.add(i));
return new TransferableGraph1(source.getResourceCount(),
identities.toArray(new Identity[identities.size()]),