-
Notifications
You must be signed in to change notification settings - Fork 70
Protobuf Validation & Conversion #691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jfallows
merged 6 commits into
aklivity:feature/schema-registry
from
ankitk-me:protobuf
Jan 10, 2024
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ee6ae5f
Protobuf Validation & Conversion
ankitk-me e324ae8
Schema text to descriptor changes & Validation update
ankitk-me 682bb44
Descriptor tree & other validation changes
ankitk-me ef84156
json convertor changes and unit tests
ankitk-me a842df3
Flow optimisation, module-info changes & bug fixes
ankitk-me c10639f
Addressing review comments
ankitk-me File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
json convertor changes and unit tests
- Loading branch information
commit ef84156927d35d22f0201759e29ba4e57f71af49
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -33,6 +33,7 @@ bindings: | |
| options: | ||
| value: | ||
| type: protobuf | ||
| format: json | ||
| catalog: | ||
| catalog0: | ||
| - subject: test0 | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -21,16 +21,23 @@ | |
|
|
||
| import org.agrona.BitUtil; | ||
| import org.agrona.DirectBuffer; | ||
| import org.agrona.collections.Int2IntHashMap; | ||
| import org.agrona.collections.Int2ObjectCache; | ||
| import org.agrona.collections.Object2ObjectHashMap; | ||
| import org.agrona.concurrent.UnsafeBuffer; | ||
| import org.agrona.io.DirectBufferInputStream; | ||
| import org.antlr.v4.runtime.BailErrorStrategy; | ||
| import org.antlr.v4.runtime.CharStream; | ||
| import org.antlr.v4.runtime.CharStreams; | ||
| import org.antlr.v4.runtime.CommonTokenStream; | ||
| import org.antlr.v4.runtime.tree.ParseTreeWalker; | ||
|
|
||
| import com.google.protobuf.Descriptors; | ||
| import com.google.protobuf.Descriptors.DescriptorValidationException; | ||
| import com.google.protobuf.Descriptors.FileDescriptor; | ||
| import com.google.protobuf.DynamicMessage; | ||
| import com.google.protobuf.InvalidProtocolBufferException; | ||
| import com.google.protobuf.util.JsonFormat; | ||
|
|
||
| import io.aklivity.zilla.runtime.engine.catalog.CatalogHandler; | ||
| import io.aklivity.zilla.runtime.engine.config.CatalogedConfig; | ||
|
|
@@ -42,15 +49,20 @@ | |
| public class ProtobufValidator | ||
| { | ||
| protected static final byte ZERO_INDEX = 0x0; | ||
| protected static final String FORMAT_JSON = "json"; | ||
|
|
||
| protected final SchemaConfig catalog; | ||
| protected final CatalogHandler handler; | ||
| protected final String subject; | ||
| protected final String format; | ||
| protected final List<Integer> indexes; | ||
| protected final DirectBufferInputStream in; | ||
| protected final DirectBuffer valueRO; | ||
|
|
||
| private final Int2ObjectCache<FileDescriptor> descriptors; | ||
| private final Object2ObjectHashMap<String, DynamicMessage.Builder> builders; | ||
| private final FileDescriptor[] dependencies; | ||
| private final Int2IntHashMap paddings; | ||
|
|
||
| protected ProtobufValidator( | ||
| ProtobufValidatorConfig config, | ||
|
|
@@ -62,10 +74,14 @@ protected ProtobufValidator( | |
| this.subject = catalog != null && catalog.subject != null | ||
| ? catalog.subject | ||
| : config.subject; | ||
| this.format = config.format; | ||
| this.descriptors = new Int2ObjectCache<>(1, 1024, i -> {}); | ||
| this.builders = new Object2ObjectHashMap<>(); | ||
| this.in = new DirectBufferInputStream(); | ||
| this.dependencies = new FileDescriptor[0]; | ||
| this.indexes = new LinkedList<>(); | ||
| this.paddings = new Int2IntHashMap(-1); | ||
| this.valueRO = new UnsafeBuffer(); | ||
| } | ||
|
|
||
| protected FileDescriptor supplyDescriptor( | ||
|
|
@@ -116,6 +132,40 @@ protected int decodeIndexes( | |
| return progress; | ||
| } | ||
|
|
||
| protected int supplyIndexPadding( | ||
| int schemaId) | ||
| { | ||
| return paddings.computeIfAbsent(schemaId, id -> calculateIndexPadding(supplyDescriptor(id))); | ||
| } | ||
|
|
||
| protected int supplyJsonFormatPadding( | ||
| int schemaId) | ||
| { | ||
| return paddings.computeIfAbsent(schemaId, id -> calculateJsonFormatPadding(supplyDescriptor(id))); | ||
| } | ||
|
|
||
| protected DynamicMessage.Builder supplyDynamicMessageBuilder( | ||
| Descriptors.Descriptor descriptor) | ||
| { | ||
| DynamicMessage.Builder builder; | ||
| if (builders.containsKey(catalog.record)) | ||
| { | ||
| builder = builders.get(catalog.record); | ||
| } | ||
| else | ||
| { | ||
| builder = createDynamicMessageBuilder(descriptor); | ||
| builders.put(catalog.record, builder); | ||
| } | ||
| return builder; | ||
| } | ||
|
|
||
| private DynamicMessage.Builder createDynamicMessageBuilder( | ||
| Descriptors.Descriptor descriptor) | ||
| { | ||
| return DynamicMessage.newBuilder(descriptor); | ||
| } | ||
|
|
||
| private int decodeIndex( | ||
| byte encodedByte) | ||
| { | ||
|
|
@@ -130,6 +180,41 @@ private int decodeIndex( | |
| return (result >>> 1) ^ -(result & 1); | ||
| } | ||
|
|
||
| private int calculateIndexPadding( | ||
| FileDescriptor descriptor) | ||
| { | ||
| int padding = 0; | ||
| if (descriptor != null) | ||
| { | ||
| DescriptorTree tree = new DescriptorTree(descriptor).findByName(catalog.record); | ||
| if (tree != null) | ||
| { | ||
| padding = tree.indexes.size() + 1; | ||
| } | ||
| } | ||
| return padding; | ||
| } | ||
|
|
||
| private int calculateJsonFormatPadding( | ||
| FileDescriptor descriptor) | ||
| { | ||
| int padding = 0; | ||
|
|
||
| if (descriptor != null) | ||
| { | ||
| try | ||
| { | ||
| padding = 2 + JsonFormat.printer().print(descriptor.toProto()).length(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps we should reuse this |
||
| } | ||
| catch (InvalidProtocolBufferException e) | ||
| { | ||
| e.printStackTrace(); | ||
| } | ||
|
|
||
| } | ||
| return padding; | ||
| } | ||
|
|
||
| private FileDescriptor createDescriptors( | ||
| int schemaId) | ||
| { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.