forked from finos/legend-pure
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
667dc25
commit af68770
Showing
21 changed files
with
3,239 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
...va/org/finos/legend/pure/m3/serialization/compiler/element/ConcreteElementSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
// Copyright 2024 Goldman Sachs | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package org.finos.legend.pure.m3.serialization.compiler.element; | ||
|
||
import org.finos.legend.pure.m3.navigation.ProcessorSupport; | ||
import org.finos.legend.pure.m3.serialization.compiler.ExtensibleSerializer; | ||
import org.finos.legend.pure.m3.serialization.compiler.reference.ReferenceIdProvider; | ||
import org.finos.legend.pure.m3.serialization.compiler.reference.ReferenceIds; | ||
import org.finos.legend.pure.m3.serialization.compiler.strings.StringIndexer; | ||
import org.finos.legend.pure.m4.coreinstance.CoreInstance; | ||
import org.finos.legend.pure.m4.serialization.Reader; | ||
import org.finos.legend.pure.m4.serialization.Writer; | ||
|
||
import java.util.Arrays; | ||
import java.util.Objects; | ||
|
||
public class ConcreteElementSerializer extends ExtensibleSerializer<ConcreteElementSerializerExtension> | ||
{ | ||
private static final long LEGEND_ENTITY_SIGNATURE = Long.parseLong("LegendEntity", 36); | ||
|
||
private final ReferenceIds referenceIds; | ||
private final StringIndexer stringIndexer; | ||
private final ProcessorSupport processorSupport; | ||
|
||
private ConcreteElementSerializer(Iterable<? extends ConcreteElementSerializerExtension> extensions, int defaultVersion, StringIndexer stringIndexer, ReferenceIds referenceIds, ProcessorSupport processorSupport) | ||
{ | ||
super(extensions, defaultVersion); | ||
this.stringIndexer = stringIndexer; | ||
this.referenceIds = referenceIds; | ||
this.processorSupport = processorSupport; | ||
} | ||
|
||
public void serialize(Writer writer, CoreInstance element) | ||
{ | ||
serialize(writer, element, getDefaultExtension(), this.referenceIds.provider()); | ||
} | ||
|
||
public void serialize(Writer writer, CoreInstance element, int serializerVersion, int referenceIdVersion) | ||
{ | ||
ConcreteElementSerializerExtension serializerExtension = getExtension(serializerVersion); | ||
ReferenceIdProvider referenceIdProvider = this.referenceIds.provider(referenceIdVersion); | ||
serialize(writer, element, serializerExtension, referenceIdProvider); | ||
} | ||
|
||
private void serialize(Writer writer, CoreInstance element, ConcreteElementSerializerExtension serializerExtension, ReferenceIdProvider referenceIdProvider) | ||
{ | ||
writer.writeLong(LEGEND_ENTITY_SIGNATURE); | ||
writer.writeInt(serializerExtension.version()); | ||
writer.writeInt(referenceIdProvider.version()); | ||
serializerExtension.serialize(writer, element, new SerializationContext(this.stringIndexer, referenceIdProvider, this.processorSupport)); | ||
} | ||
|
||
public DeserializedConcreteElement deserialize(Reader reader) | ||
{ | ||
long signature = reader.readLong(); | ||
if (signature != LEGEND_ENTITY_SIGNATURE) | ||
{ | ||
throw new IllegalArgumentException("Invalid file format: not a Legend concrete element file"); | ||
} | ||
int version = reader.readInt(); | ||
ConcreteElementSerializerExtension extension = getExtension(version); | ||
int referenceIdVersion = reader.readInt(); | ||
return extension.deserialize(reader, new SerializationContext(this.stringIndexer, this.referenceIds.provider(referenceIdVersion), this.processorSupport)); | ||
} | ||
|
||
public static Builder builder(ProcessorSupport processorSupport) | ||
{ | ||
return new Builder(processorSupport); | ||
} | ||
|
||
public static class Builder extends AbstractBuilder<ConcreteElementSerializerExtension, ConcreteElementSerializer> | ||
{ | ||
private StringIndexer stringIndexer; | ||
private ReferenceIds referenceIds; | ||
private final ProcessorSupport processorSupport; | ||
|
||
private Builder(ProcessorSupport processorSupport) | ||
{ | ||
this.processorSupport = Objects.requireNonNull(processorSupport); | ||
} | ||
|
||
public Builder withExtension(ConcreteElementSerializerExtension extension) | ||
{ | ||
addExtension(extension); | ||
return this; | ||
} | ||
|
||
public Builder withExtensions(Iterable<? extends ConcreteElementSerializerExtension> extensions) | ||
{ | ||
addExtensions(extensions); | ||
return this; | ||
} | ||
|
||
public Builder withExtensions(ConcreteElementSerializerExtension... extensions) | ||
{ | ||
return withExtensions(Arrays.asList(extensions)); | ||
} | ||
|
||
public Builder withLoadedExtensions(ClassLoader classLoader) | ||
{ | ||
loadExtensions(classLoader); | ||
return this; | ||
} | ||
|
||
public Builder withLoadedExtensions() | ||
{ | ||
loadExtensions(); | ||
return this; | ||
} | ||
|
||
public Builder withDefaultVersion(int defaultVersion) | ||
{ | ||
setDefaultVersion(defaultVersion); | ||
return this; | ||
} | ||
|
||
public Builder withStringIndexer(StringIndexer stringIndexer) | ||
{ | ||
this.stringIndexer = stringIndexer; | ||
return this; | ||
} | ||
|
||
public Builder withReferenceIds(ReferenceIds referenceIds) | ||
{ | ||
this.referenceIds = referenceIds; | ||
return this; | ||
} | ||
|
||
@Override | ||
protected ConcreteElementSerializer build(Iterable<ConcreteElementSerializerExtension> extensions, int defaultVersion) | ||
{ | ||
return new ConcreteElementSerializer(extensions, defaultVersion, resolveStringIndexer(), resolveReferenceIds(), this.processorSupport); | ||
} | ||
|
||
private ReferenceIds resolveReferenceIds() | ||
{ | ||
// If reference ids has not been specified, create one | ||
return (this.referenceIds == null) ? | ||
ReferenceIds.builder(this.processorSupport).withAvailableExtensions().build() : | ||
this.referenceIds; | ||
} | ||
|
||
private StringIndexer resolveStringIndexer() | ||
{ | ||
// if string indexer has not been specified, use the default | ||
return (this.stringIndexer == null) ? StringIndexer.defaultStringIndexer() : this.stringIndexer; | ||
} | ||
|
||
@Override | ||
protected Class<ConcreteElementSerializerExtension> getExtensionClass() | ||
{ | ||
return ConcreteElementSerializerExtension.class; | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...nos/legend/pure/m3/serialization/compiler/element/ConcreteElementSerializerExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2024 Goldman Sachs | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package org.finos.legend.pure.m3.serialization.compiler.element; | ||
|
||
import org.finos.legend.pure.m3.serialization.compiler.SerializerExtension; | ||
import org.finos.legend.pure.m4.coreinstance.CoreInstance; | ||
import org.finos.legend.pure.m4.serialization.Reader; | ||
import org.finos.legend.pure.m4.serialization.Writer; | ||
|
||
public interface ConcreteElementSerializerExtension extends SerializerExtension | ||
{ | ||
void serialize(Writer writer, CoreInstance element, SerializationContext serializationContext); | ||
|
||
DeserializedConcreteElement deserialize(Reader reader, SerializationContext serializationContext); | ||
} |
24 changes: 24 additions & 0 deletions
24
.../org/finos/legend/pure/m3/serialization/compiler/element/DeserializedConcreteElement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2024 Goldman Sachs | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package org.finos.legend.pure.m3.serialization.compiler.element; | ||
|
||
public interface DeserializedConcreteElement extends DeserializedElement | ||
{ | ||
String getPath(); | ||
|
||
DeserializedElement getInternalElement(int id); | ||
|
||
int getReferenceIdVersion(); | ||
} |
28 changes: 28 additions & 0 deletions
28
...ain/java/org/finos/legend/pure/m3/serialization/compiler/element/DeserializedElement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2024 Goldman Sachs | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package org.finos.legend.pure.m3.serialization.compiler.element; | ||
|
||
import org.finos.legend.pure.m4.coreinstance.SourceInformation; | ||
|
||
public interface DeserializedElement | ||
{ | ||
String getName(); | ||
|
||
String getClassifierReferenceId(); | ||
|
||
SourceInformation getSourceInformation(); | ||
|
||
Iterable<? extends PropertyValues> getPropertyValues(); | ||
} |
26 changes: 26 additions & 0 deletions
26
...src/main/java/org/finos/legend/pure/m3/serialization/compiler/element/PropertyValues.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2024 Goldman Sachs | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package org.finos.legend.pure.m3.serialization.compiler.element; | ||
|
||
import org.eclipse.collections.api.list.ListIterable; | ||
|
||
public interface PropertyValues | ||
{ | ||
String getPropertyName(); | ||
|
||
ListIterable<String> getRealKey(); | ||
|
||
ListIterable<ValueOrReference> getValues(); | ||
} |
Oops, something went wrong.