Search in sources :

Example 1 with Obj

use of org.finos.legend.pure.runtime.java.compiled.serialization.model.Obj in project legend-pure by finos.

the class TestDistributedBinaryGraphSerialization method getExpectedObjsFromRuntime.

private ListIterable<Obj> getExpectedObjsFromRuntime(String metadataName) {
    MutableSet<CoreInstance> ignoredClassifiers = PrimitiveUtilities.getPrimitiveTypes(repository).toSet();
    ArrayAdapter.adapt(M3Paths.EnumStub, M3Paths.ImportStub, M3Paths.PropertyStub, M3Paths.RouteNodePropertyStub).collect(processorSupport::package_getByUserPath, ignoredClassifiers);
    IdBuilder idBuilder = IdBuilder.newIdBuilder(DistributedMetadataHelper.getMetadataIdPrefix(metadataName), processorSupport);
    GraphSerializer.ClassifierCaches classifierCaches = new GraphSerializer.ClassifierCaches(processorSupport);
    return GraphNodeIterable.fromModelRepository(repository).reject(i -> ignoredClassifiers.contains(i.getClassifier())).collect(i -> GraphSerializer.buildObj(i, idBuilder, classifierCaches, processorSupport), Lists.mutable.empty());
}
Also used : GraphNodeIterable(org.finos.legend.pure.m4.tools.GraphNodeIterable) ArrayAdapter(org.eclipse.collections.impl.list.fixed.ArrayAdapter) BeforeClass(org.junit.BeforeClass) AbstractPureTestWithCoreCompiled(org.finos.legend.pure.m3.AbstractPureTestWithCoreCompiled) Lists(org.eclipse.collections.api.factory.Lists) IdBuilder(org.finos.legend.pure.runtime.java.compiled.generation.processors.IdBuilder) Test(org.junit.Test) IOException(java.io.IOException) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) MutableList(org.eclipse.collections.api.list.MutableList) Obj(org.finos.legend.pure.runtime.java.compiled.serialization.model.Obj) Function(java.util.function.Function) M3Paths(org.finos.legend.pure.m3.navigation.M3Paths) MutableSet(org.eclipse.collections.api.set.MutableSet) ListMultimap(org.eclipse.collections.api.multimap.list.ListMultimap) GraphSerializer(org.finos.legend.pure.runtime.java.compiled.serialization.GraphSerializer) PrimitiveUtilities(org.finos.legend.pure.m3.navigation.PrimitiveUtilities) ListIterable(org.eclipse.collections.api.list.ListIterable) Assert(org.junit.Assert) GraphSerializer(org.finos.legend.pure.runtime.java.compiled.serialization.GraphSerializer) IdBuilder(org.finos.legend.pure.runtime.java.compiled.generation.processors.IdBuilder) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance)

Example 2 with Obj

use of org.finos.legend.pure.runtime.java.compiled.serialization.model.Obj in project legend-pure by finos.

the class GraphSerializer method buildObj.

public static Obj buildObj(CoreInstance instance, IdBuilder idBuilder, ClassifierCaches classifierCaches, ProcessorSupport processorSupport) {
    SourceInformation sourceInformation = instance.getSourceInformation();
    String identifier = idBuilder.buildId(instance);
    String classifierString = classifierCaches.getClassifierId(instance.getClassifier());
    ListIterable<PropertyValue> propertyValues = collectProperties(instance, idBuilder, classifierCaches, processorSupport);
    return classifierCaches.isEnum(instance) ? new Enum(sourceInformation, identifier, classifierString, instance.getName(), propertyValues) : new Obj(sourceInformation, identifier, classifierString, instance.getName(), propertyValues);
}
Also used : Enum(org.finos.legend.pure.runtime.java.compiled.serialization.model.Enum) Obj(org.finos.legend.pure.runtime.java.compiled.serialization.model.Obj) PropertyValue(org.finos.legend.pure.runtime.java.compiled.serialization.model.PropertyValue) SourceInformation(org.finos.legend.pure.m4.coreinstance.SourceInformation)

Example 3 with Obj

use of org.finos.legend.pure.runtime.java.compiled.serialization.model.Obj in project legend-pure by finos.

the class DistributedBinaryGraphDeserializer method getInstances.

public ListIterable<Obj> getInstances(String classifierId, Iterable<String> instanceIds) {
    if (!hasClassifier(classifierId)) {
        throw new RuntimeException("Unknown classifier id: '" + classifierId + "'");
    }
    ClassifierIndex classifierIndex = getClassifierIndex(classifierId);
    MutableMap<String, MutableList<SourceCoordinates>> sourceCoordinatesByFile = Maps.mutable.empty();
    int size = 0;
    for (String instanceId : instanceIds) {
        SourceCoordinates sourceCoordinates = classifierIndex.getSourceCoordinates(instanceId);
        if (sourceCoordinates == null) {
            throw new RuntimeException("Unknown instance: classifier='" + classifierId + "', id='" + instanceId + "'");
        }
        sourceCoordinatesByFile.getIfAbsentPut(sourceCoordinates.getFilePath(), Lists.mutable::empty).add(sourceCoordinates);
        size++;
    }
    if (size == 0) {
        return Lists.immutable.empty();
    }
    MutableList<Obj> objs = Lists.mutable.withInitialCapacity(size);
    sourceCoordinatesByFile.forEachKeyValue((filePath, fileSourceCoordinates) -> {
        fileSourceCoordinates.sortThis(SourceCoordinates::compareByOffset);
        try (Reader reader = this.fileReader.getReader(filePath)) {
            int offset = 0;
            for (SourceCoordinates sourceCoordinates : fileSourceCoordinates) {
                objs.add(sourceCoordinates.getObj(reader, offset, this.stringIndex, classifierIndex));
                offset = sourceCoordinates.getOffsetAfterReading();
            }
        }
    });
    return objs;
}
Also used : MutableList(org.eclipse.collections.api.list.MutableList) Lists(org.eclipse.collections.api.factory.Lists) Obj(org.finos.legend.pure.runtime.java.compiled.serialization.model.Obj) Reader(org.finos.legend.pure.m4.serialization.Reader)

Example 4 with Obj

use of org.finos.legend.pure.runtime.java.compiled.serialization.model.Obj in project legend-pure by finos.

the class DistributedMetadataTool method instance.

private void instance(String classifier, String id) {
    if (this.deserializer.hasInstance(classifier, id)) {
        Obj instance = this.deserializer.getInstance(classifier, id);
        new ObjPrinter(instance).printObj();
    } else {
        this.out.println("Instance not found");
    }
}
Also used : Obj(org.finos.legend.pure.runtime.java.compiled.serialization.model.Obj)

Example 5 with Obj

use of org.finos.legend.pure.runtime.java.compiled.serialization.model.Obj in project legend-pure by finos.

the class DistributedBinaryGraphSerializer method serialize.

public void serialize(FileWriter fileWriter) {
    MutableMap<String, MutableList<CoreInstance>> nodesByClassifierId = getNodesByClassifierId();
    // Build string cache
    DistributedStringCache stringCache = DistributedStringCache.fromNodes(nodesByClassifierId.valuesView().flatCollect(l -> l), this.idBuilder, this.processorSupport);
    BinaryObjSerializer serializer = new BinaryObjSerializerWithStringCacheAndImplicitIdentifiers(stringCache);
    // Write string cache
    stringCache.write(this.metadataName, fileWriter);
    // Write instances
    int partition = 0;
    int partitionTotalBytes = 0;
    ByteArrayOutputStream binByteStream = new ByteArrayOutputStream(MAX_BIN_FILE_BYTES);
    try (Writer binFileWriter = BinaryWriters.newBinaryWriter(binByteStream)) {
        ByteArrayOutputStream indexByteStream = new ByteArrayOutputStream();
        try (Writer indexWriter = BinaryWriters.newBinaryWriter(indexByteStream)) {
            for (String classifierId : nodesByClassifierId.keysView().toSortedList()) {
                ListIterable<CoreInstance> classifierObjs = nodesByClassifierId.get(classifierId).sortThisBy(this.idBuilder::buildId);
                // Initial index information
                // total obj count
                indexWriter.writeInt(classifierObjs.size());
                // initial partition
                indexWriter.writeInt(partition);
                // initial byte offset in partition
                indexWriter.writeInt(partitionTotalBytes);
                MutableList<ObjIndexInfo> partitionObjIndexInfos = Lists.mutable.empty();
                ByteArrayOutputStream objByteStream = new ByteArrayOutputStream();
                try (Writer objWriter = BinaryWriters.newBinaryWriter(objByteStream)) {
                    GraphSerializer.ClassifierCaches classifierCaches = new GraphSerializer.ClassifierCaches(this.processorSupport);
                    for (CoreInstance coreInstance : classifierObjs) {
                        // Obj serialization
                        Obj obj = GraphSerializer.buildObj(coreInstance, this.idBuilder, classifierCaches, this.processorSupport);
                        objByteStream.reset();
                        serializer.serializeObj(objWriter, obj);
                        int objByteCount = objByteStream.size();
                        if (partitionTotalBytes + objByteCount > MAX_BIN_FILE_BYTES) {
                            // Write current partition
                            try (Writer writer1 = fileWriter.getWriter(DistributedMetadataHelper.getMetadataPartitionBinFilePath(this.metadataName, partition))) {
                                writer1.writeBytes(binByteStream.toByteArray());
                                binByteStream.reset();
                            }
                            // Write partition portion of classifier index
                            indexWriter.writeInt(partitionObjIndexInfos.size());
                            partitionObjIndexInfos.forEach(info -> info.write(indexWriter, stringCache));
                            // New partition
                            partition++;
                            if (partition < 0) {
                                throw new RuntimeException("Too many partitions");
                            }
                            partitionTotalBytes = 0;
                            partitionObjIndexInfos.clear();
                        }
                        binFileWriter.writeBytes(objByteStream.toByteArray());
                        partitionTotalBytes += objByteCount;
                        partitionObjIndexInfos.add(new ObjIndexInfo(obj.getIdentifier(), objByteCount));
                    }
                }
                // Write final partition portion of classifier index
                if (partitionObjIndexInfos.notEmpty()) {
                    indexWriter.writeInt(partitionObjIndexInfos.size());
                    partitionObjIndexInfos.forEach(info -> info.write(indexWriter, stringCache));
                }
                // Write classifier index
                try (Writer writer1 = fileWriter.getWriter(DistributedMetadataHelper.getMetadataClassifierIndexFilePath(this.metadataName, classifierId))) {
                    writer1.writeBytes(indexByteStream.toByteArray());
                    indexByteStream.reset();
                }
            }
        }
    }
    // Write final partition
    if (binByteStream.size() > 0) {
        try (Writer writer = fileWriter.getWriter(DistributedMetadataHelper.getMetadataPartitionBinFilePath(this.metadataName, partition))) {
            writer.writeBytes(binByteStream.toByteArray());
        }
    }
}
Also used : BinaryWriters(org.finos.legend.pure.m4.serialization.binary.BinaryWriters) GraphNodeIterable(org.finos.legend.pure.m4.tools.GraphNodeIterable) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AnyStubHelper(org.finos.legend.pure.m3.coreinstance.helper.AnyStubHelper) PureRuntime(org.finos.legend.pure.m3.serialization.runtime.PureRuntime) Lists(org.eclipse.collections.api.factory.Lists) IdBuilder(org.finos.legend.pure.runtime.java.compiled.generation.processors.IdBuilder) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) MutableList(org.eclipse.collections.api.list.MutableList) Obj(org.finos.legend.pure.runtime.java.compiled.serialization.model.Obj) ProcessorSupport(org.finos.legend.pure.m3.navigation.ProcessorSupport) Maps(org.eclipse.collections.api.factory.Maps) Objects(java.util.Objects) MutableSet(org.eclipse.collections.api.set.MutableSet) MutableMap(org.eclipse.collections.api.map.MutableMap) GraphSerializer(org.finos.legend.pure.runtime.java.compiled.serialization.GraphSerializer) PrimitiveUtilities(org.finos.legend.pure.m3.navigation.PrimitiveUtilities) Writer(org.finos.legend.pure.m4.serialization.Writer) Map(java.util.Map) ListIterable(org.eclipse.collections.api.list.ListIterable) MetadataJavaPaths(org.finos.legend.pure.runtime.java.compiled.generation.processors.type.MetadataJavaPaths) Path(java.nio.file.Path) JarOutputStream(java.util.jar.JarOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GraphSerializer(org.finos.legend.pure.runtime.java.compiled.serialization.GraphSerializer) MutableList(org.eclipse.collections.api.list.MutableList) Obj(org.finos.legend.pure.runtime.java.compiled.serialization.model.Obj) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) Writer(org.finos.legend.pure.m4.serialization.Writer)

Aggregations

Obj (org.finos.legend.pure.runtime.java.compiled.serialization.model.Obj)8 Lists (org.eclipse.collections.api.factory.Lists)3 MutableList (org.eclipse.collections.api.list.MutableList)3 MutableSet (org.eclipse.collections.api.set.MutableSet)3 CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)3 ListIterable (org.eclipse.collections.api.list.ListIterable)2 PrimitiveUtilities (org.finos.legend.pure.m3.navigation.PrimitiveUtilities)2 SourceInformation (org.finos.legend.pure.m4.coreinstance.SourceInformation)2 GraphNodeIterable (org.finos.legend.pure.m4.tools.GraphNodeIterable)2 IdBuilder (org.finos.legend.pure.runtime.java.compiled.generation.processors.IdBuilder)2 GraphSerializer (org.finos.legend.pure.runtime.java.compiled.serialization.GraphSerializer)2 Enum (org.finos.legend.pure.runtime.java.compiled.serialization.model.Enum)2 PropertyValue (org.finos.legend.pure.runtime.java.compiled.serialization.model.PropertyValue)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Function (java.util.function.Function)1 JarOutputStream (java.util.jar.JarOutputStream)1