Search in sources :

Example 1 with ExternalReferenceSerializer

use of org.finos.legend.pure.m3.serialization.runtime.binary.reference.ExternalReferenceSerializer in project legend-pure by finos.

the class BinaryModelSourceSerializer method findSerializer.

private ExternalReferenceSerializer findSerializer(CoreInstance instance) {
    // Find serializer by instance type
    CoreInstance type = instance.getClassifier();
    ListIterable<CoreInstance> generalizations = Type.getGeneralizationResolutionOrder(type, this.processorSupport);
    for (CoreInstance genl : generalizations) {
        String genlPath = PackageableElement.getUserPathForPackageableElement(genl);
        ExternalReferenceSerializer serializer = this.serializerLibrary.getSerializer(genlPath);
        if (serializer != null) {
            return serializer;
        }
    }
    return null;
}
Also used : CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) ExternalReferenceSerializer(org.finos.legend.pure.m3.serialization.runtime.binary.reference.ExternalReferenceSerializer)

Example 2 with ExternalReferenceSerializer

use of org.finos.legend.pure.m3.serialization.runtime.binary.reference.ExternalReferenceSerializer in project legend-pure by finos.

the class BinaryModelSourceDeserializer method readOtherExternalReference.

private Reference readOtherExternalReference(int i, Reference[] externalReferences, byte[][] externalReferenceBytes) {
    Reference externalReference = externalReferences[i];
    if (externalReference == null) {
        Reader reader = BinaryReaders.newBinaryReader(externalReferenceBytes[i]);
        String serializerType = readStringById(reader);
        ExternalReferenceSerializer serializer = this.serializerLibrary.getSerializer(serializerType);
        if (serializer == null) {
            throw new RuntimeException("Cannot find serializer for type: " + serializerType);
        }
        externalReference = serializer.deserialize(new BinaryExternalReferenceDeserializationHelper(reader, externalReferences, externalReferenceBytes));
        externalReferences[i] = externalReference;
    }
    return externalReference;
}
Also used : AbstractReference(org.finos.legend.pure.m3.serialization.runtime.binary.reference.AbstractReference) Reference(org.finos.legend.pure.m3.serialization.runtime.binary.reference.Reference) ExternalReferenceSerializer(org.finos.legend.pure.m3.serialization.runtime.binary.reference.ExternalReferenceSerializer) Reader(org.finos.legend.pure.m4.serialization.Reader)

Example 3 with ExternalReferenceSerializer

use of org.finos.legend.pure.m3.serialization.runtime.binary.reference.ExternalReferenceSerializer in project legend-pure by finos.

the class BinaryModelSourceSerializer method serializeOtherExternalReference.

private void serializeOtherExternalReference(CoreInstance referenceInstance, Writer writer) {
    writeOtherExternalReference(referenceInstance, writer);
    if (!this.externalOtherReferenceSerializations.containsKey(referenceInstance)) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        Writer instanceWriter = BinaryWriters.newBinaryWriter(stream);
        ExternalReferenceSerializer serializer = findSerializer(referenceInstance);
        if (serializer == null) {
            // Couldn't find a serializer
            throwUnsupportedExternalReferenceException(referenceInstance);
        }
        int serializerId = getStringReferenceId(serializer.getTypePath());
        instanceWriter.writeInt(serializerId);
        try {
            serializer.serialize(referenceInstance, new BinaryExternalReferenceSerializationHelper(instanceWriter));
        } catch (Exception e) {
            // Exception occurred during serialization
            StringBuilder message = new StringBuilder("Error serializaing ");
            message.append(referenceInstance);
            message.append(" with serializer for type ");
            message.append(serializer.getTypePath());
            SourceInformation sourceInfo = referenceInstance.getSourceInformation();
            if (sourceInfo != null) {
                sourceInfo.appendMessage(message.append(" (source information: ")).append(')');
            }
            throw new RuntimeException(message.toString(), e);
        }
        this.externalOtherReferenceSerializations.put(referenceInstance, stream.toByteArray());
    }
}
Also used : ExternalReferenceSerializer(org.finos.legend.pure.m3.serialization.runtime.binary.reference.ExternalReferenceSerializer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Writer(org.finos.legend.pure.m4.serialization.Writer) SourceInformation(org.finos.legend.pure.m4.coreinstance.SourceInformation)

Aggregations

ExternalReferenceSerializer (org.finos.legend.pure.m3.serialization.runtime.binary.reference.ExternalReferenceSerializer)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 AbstractReference (org.finos.legend.pure.m3.serialization.runtime.binary.reference.AbstractReference)1 Reference (org.finos.legend.pure.m3.serialization.runtime.binary.reference.Reference)1 CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)1 SourceInformation (org.finos.legend.pure.m4.coreinstance.SourceInformation)1 Reader (org.finos.legend.pure.m4.serialization.Reader)1 Writer (org.finos.legend.pure.m4.serialization.Writer)1