X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.graph%2Fsrc%2Forg%2Fsimantics%2Fgraph%2Frepresentation%2FTransferableGraphFileReader.java;fp=bundles%2Forg.simantics.graph%2Fsrc%2Forg%2Fsimantics%2Fgraph%2Frepresentation%2FTransferableGraphFileReader.java;h=407ec62cc327866533fa2c2d3bace6fb008b04fd;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphFileReader.java b/bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphFileReader.java new file mode 100644 index 000000000..407ec62cc --- /dev/null +++ b/bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphFileReader.java @@ -0,0 +1,296 @@ +package org.simantics.graph.representation; + +import java.io.DataInput; +import java.io.DataInputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.nio.channels.ReadableByteChannel; +import java.util.ArrayList; +import java.util.List; + +import org.simantics.databoard.Bindings; +import org.simantics.databoard.Datatypes; +import org.simantics.databoard.binding.error.RuntimeDatatypeConstructionException; +import org.simantics.databoard.binding.mutable.Variant; +import org.simantics.databoard.container.DataContainers; +import org.simantics.databoard.serialization.RuntimeSerializerConstructionException; +import org.simantics.databoard.serialization.Serializer; +import org.simantics.databoard.type.Datatype; + + +final public class TransferableGraphFileReader extends ByteFileReader { + + InputStream in = new InputStream() { + + @Override + public int read() throws IOException { + return getByte(); + } + + @Override + public int read(byte[] b) throws IOException { + // FIXME not correctly implemented + System.arraycopy(safeBytes(b.length), 0, b, 0, b.length); + return b.length; + } + + @Override + public int read(byte[] b, int off, int len) throws IOException { + // FIXME not correctly implemented + System.arraycopy(safeBytes(len), 0, b, off, len); + return len; + } + + }; + + + final static class InputChannel implements ReadableByteChannel { + + final private InputStream stream; + + public InputChannel(InputStream stream) { + this.stream = stream; + } + + @Override + public boolean isOpen() { + return true; + } + + @Override + public void close() throws IOException { + } + + @Override + public int read(ByteBuffer dst) throws IOException { + int pos = dst.position(); + int limit = dst.limit(); + int i=stream.read(dst.array(), pos, limit-pos); + //System.err.println("Read " + i + " (expected " + dst.array().length + ")"); + return i; + } + + } + + private static boolean init = true; + + final private static int SIZE = 1<<18; + final private static int HEADER = headerSize(); + final private int header; + + public TransferableGraphFileReader(File file) throws IOException { + super(file, SIZE); + if(init) { + init=false; + TransferableGraphFileReader r = new TransferableGraphFileReader(file, 0); + for(int i=0;i<40000;i++) r.readTG(); + } + this.header = HEADER; + } + + public TransferableGraphFileReader(InputStream stream) throws IOException { + super(null, new InputChannel(stream), SIZE); + if(init) { + init=false; + TransferableGraphFileReader r = new TransferableGraphFileReader(stream, 0); + for(int i=0;i<40000;i++) r.readTG(); + } + this.header = 0; + } + + public TransferableGraphFileReader(ReadableByteChannel channel) throws IOException { + super(null, channel, SIZE); + if(init) { + init=false; + TransferableGraphFileReader r = new TransferableGraphFileReader(channel, 0); + for(int i=0;i<40000;i++) r.readTG(); + } + this.header = 0; + } + + public TransferableGraphFileReader(ReadableByteChannel channel, int size) throws IOException { + super(null, channel, SIZE); + this.header = 0; + } + + public TransferableGraphFileReader(InputStream stream, int size) throws IOException { + super(null, new InputChannel(stream), size); + this.header = 0; + } + + public TransferableGraphFileReader(File file, int size) throws IOException { + super(file, size); + this.header = HEADER; + } + + private static int headerSize() { + try { + return Bindings.getSerializerUnchecked(Datatype.class).serialize(Datatypes.getDatatypeUnchecked(TransferableGraph1.class)).length; + } catch (RuntimeSerializerConstructionException e) { + throw new Error("Failed to determine TransferableGraph1 header size. ", e); + } catch (RuntimeDatatypeConstructionException e) { + throw new Error("Failed to determine TransferableGraph1 header size. ", e); + } catch (IOException e) { + throw new Error("Failed to determine TransferableGraph1 header size. ", e); + } + } + + public TransferableGraph1 readTG() throws IOException { + + if(getSize() == 0) return null; + +// long start = System.nanoTime(); + + final byte[] bytes = getBytes(); + +// byteIndex = header; + + DataInputStream dis = new DataInputStream(in); + + // Header + DataContainers.readHeader(dis); + + // Content variant data type + Bindings.getSerializerUnchecked(Datatype.class).deserialize((DataInput)dis); + + int resourceCount = safeInt(); + + List idcontext = new ArrayList(); + dis = new DataInputStream(in); + Extensions extensions = (Extensions)Bindings.getSerializerUnchecked(Extensions.class).deserialize((DataInput)dis, idcontext); + + int identities = safeInt(); + Identity[] ids = new Identity[identities]; + +// System.err.println("rc: " + resourceCount); +// System.err.println("ids: " + identities); + +// long duration = System.nanoTime() - start; +// System.err.println("start in " + 1e-9*duration + "s."); +// start = System.nanoTime(); + + for(int i=0;i> 2; + int allowed = Math.min(stmLength-stmIndex, avail); + for(int index = byteIndex, i=0;i(); + dis = new DataInputStream(in); + + for(int i=0;i