*******************************************************************************/\r
package org.simantics.graph.db;\r
\r
-import java.io.BufferedOutputStream;\r
import java.io.DataInput;\r
import java.io.DataOutput;\r
-import java.io.DataOutputStream;\r
import java.io.File;\r
-import java.io.FileOutputStream;\r
import java.io.IOException;\r
import java.io.InputStream;\r
import java.util.ArrayList;\r
import org.simantics.databoard.serialization.SerializationException;\r
import org.simantics.databoard.serialization.Serializer;\r
import org.simantics.databoard.type.Datatype;\r
+import org.simantics.databoard.util.binary.BinaryFile;\r
+import org.simantics.databoard.util.binary.RandomAccessBinary;\r
import org.simantics.db.ReadGraph;\r
import org.simantics.db.RequestProcessor;\r
import org.simantics.db.Resource;\r
\r
public static void writeTransferableGraph(RequestProcessor processor, String format, int version, TreeMap<String, Variant> metadata, TransferableGraphSource source, File target, TGStatusMonitor monitor) throws Exception {\r
final Serializer datatypeSerializer = Bindings.getSerializerUnchecked(Datatype.class);\r
- try (DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(target), 128*1024))) {\r
+ try (RandomAccessBinary out = new BinaryFile(target, 128*1024)) {\r
DataContainer container = new DataContainer(format, version, metadata, null);\r
DataContainers.writeHeader(out, container);\r
datatypeSerializer.serialize((DataOutput) out, Datatypes.getDatatypeUnchecked(TransferableGraph1.class));\r
\r
private static final int LITERAL_VALUE_IO_BUFFER_SIZE = 128 * 1024;\r
\r
- private static void writeTransferableGraph(RequestProcessor processor, final TransferableGraphSource source, final DataOutput out, TGStatusMonitor monitor) throws Exception {\r
+ private static void writeTransferableGraph(RequestProcessor processor, final TransferableGraphSource source, final RandomAccessBinary out, TGStatusMonitor monitor) throws Exception {\r
long start = System.nanoTime();\r
\r
final Serializer datatypeSerializer = Bindings.getSerializerUnchecked(Datatype.class);\r
final Serializer identitySerializer = Bindings.getSerializerUnchecked(Identity.class);\r
final Serializer extensionSerializer = Bindings.getSerializerUnchecked(Extensions.class);\r
\r
- int totalCount = source.getIdentityCount() + source.getStatementCount()/4 + source.getValueCount();\r
- TGStatusMonitor.Updater progress = new TGStatusMonitor.Updater(safeMonitor(monitor), totalCount);\r
-\r
- out.writeInt(source.getResourceCount());\r
+ int resourceCount = source.getResourceCount();\r
+ //System.err.println("resourceCount: " + resourceCount);\r
+ out.writeInt(resourceCount);\r
extensionSerializer.serialize(out, new Extensions(source.getExtensions()));\r
\r
// System.err.println("resource count: " + source.getResourceCount());\r
if (monitor.isCanceled())\r
throw new CancelTransactionException();\r
\r
- out.writeInt(source.getIdentityCount());\r
+ int identityCount = source.getIdentityCount();\r
+ TGStatusMonitor.Updater identityProgress = new TGStatusMonitor.Updater(safeMonitor(monitor), 0, 33, identityCount);\r
+ out.writeInt(identityCount);\r
+ //System.err.println("identities: " + identityCount);\r
source.forIdentities(graph, value -> {\r
//System.err.println("id: " + value);\r
identitySerializer.serialize(out, value);\r
- progress.worked(1);\r
+ identityProgress.worked(1);\r
});\r
\r
if (monitor.isCanceled())\r
throw new CancelTransactionException();\r
\r
- out.writeInt(source.getStatementCount());\r
- //System.err.println("stms: " + source.getStatementCount());\r
- //int[] counter = {0};\r
+ long statementCountPos = out.position();\r
+ int originalStatementCount = source.getStatementCount();\r
+ TGStatusMonitor.Updater statementProgress = new TGStatusMonitor.Updater(safeMonitor(monitor), 34, 66, originalStatementCount);\r
+ out.writeInt(originalStatementCount);\r
+ //System.err.println("original statementCount: " + originalStatementCount);\r
+ int[] statementCounter = { 0 };\r
source.forStatements(graph, r -> {\r
for (int i = 0; i < 4; ++i)\r
out.writeInt(r[i]);\r
- //System.err.println("stm " + (counter[0]++) + ": " + r[0] + " " + r[1] + " " + r[2] + " " + r[3]);\r
- progress.worked(1);\r
+ statementCounter[0]++;\r
+ //System.err.println("stm " + (statementCounter[0]) + ": " + r[0] + " " + r[1] + " " + r[2] + " " + r[3]);\r
+ statementProgress.worked(1);\r
});\r
+ //System.err.println("wrote " + statementCounter[0] + " statements, " + (statementCounter[0]*4)+ " integers");\r
+\r
+ // Rewrite statement count after knowing exactly how many\r
+ // statements were written. It is possible that some\r
+ // statements get filtered out at this stage and the\r
+ // original statement count does not reflect that.\r
+ long afterStatementsPos = out.position();\r
+ out.position(statementCountPos);\r
+ out.writeInt(statementCounter[0]*4);\r
+ out.position(afterStatementsPos);\r
\r
if (monitor.isCanceled())\r
throw new CancelTransactionException();\r
\r
- out.writeInt(source.getValueCount());\r
- //System.err.println("vals: " + source.getValueCount());\r
+ int valueCount = source.getValueCount();\r
+ TGStatusMonitor.Updater valueProgress = new TGStatusMonitor.Updater(safeMonitor(monitor), 67, 100, valueCount);\r
+ out.writeInt(valueCount);\r
+// System.err.println("valueCount: " + valueCount);\r
CopyingInputStream cis = new CopyingInputStream();\r
cis.out = out;\r
source.forValues2(graph, new TransferableGraphSourceValueProcedure() {\r
long copied = copy(buffer, input, out, length);\r
assert copied == length;\r
//System.err.println("value " + (num++) + ": raw variant, " + length + " bytes, copied " + copied + " bytes");\r
- progress.worked(1);\r
+ valueProgress.worked(1);\r
}\r
\r
@Override\r
cis.in = input;\r
serializer.skip(cis);\r
cis.in = null;\r
- progress.worked(1);\r
+ valueProgress.worked(1);\r
}\r
});\r
\r