Skip to content

Commit

Permalink
Merge pull request #39490 from karesti/protobuf-schema-api
Browse files Browse the repository at this point in the history
Updates to Schema programmatic API in Infinispan
  • Loading branch information
gsmet committed Mar 18, 2024
2 parents 19bf436 + e6ef81c commit 71c8263
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 31 deletions.
36 changes: 16 additions & 20 deletions docs/src/main/asciidoc/infinispan-client-reference.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ message Book {
required string description = 2;
required int32 publicationYear = 3; // no native Date type available in Protobuf
repeated Author authors = 4;
requited double price = 5; // no native BigDecimal type available in Protobuf
requited double price = 5; // no native BigDecimal type available in Protobuf but you can use the adapter
}
message Author {
Expand All @@ -470,29 +470,25 @@ message Author {
. In Code
+
Or you can define the proto schema directly in user code by defining a produced bean of type
`org.infinispan.protostream.FileDescriptorSource`.
`org.infinispan.protostream.schema.Schema`.
+
[source,java]
----
@Produces
FileDescriptorSource bookProtoDefinition() {
return FileDescriptorSource.fromString("library.proto", "package book_sample;\n" +
"\n" +
"message Book {\n" +
" required string title = 1;\n" +
" required string description = 2;\n" +
" required int32 publicationYear = 3; // no native Date type available in Protobuf\n" +
"\n" +
" repeated Author authors = 4;\n" +
"\n" +
" required double price = 5; // no native BigDecimal type available in Protobuf\n" +
"}\n" +
"\n" +
"message Author {\n" +
" required string name = 1;\n" +
" required string surname = 2;\n" +
"}");
}
Schema bookSchema() {
return new Schema.Builder("book.proto")
.packageName("book_sample")
.addMessage("Author")
.addField(Type.Scalar.STRING, "name", 1)
.addField(Type.Scalar.STRING, "surname", 2)
.addMessage("Book")
.addField(Type.Scalar.STRING, "title", 1)
.addField(Type.Scalar.STRING, "description", 2)
.addField(Type.Scalar.INT32, "publicationYear", 3)
.addRepeatedField(Type.create("Author"), "author", 4)
.addField(Type.Scalar.DOUBLE, "price", 5)
.build();
}
----
User Marshaller::
The last thing to do is to provide a `org.infinispan.protostream.MessageMarshaller` implementation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.infinispan.protostream.MessageMarshaller;
import org.infinispan.protostream.SerializationContextInitializer;
import org.infinispan.protostream.WrappedMessage;
import org.infinispan.protostream.schema.Schema;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.AnnotationValue;
Expand Down Expand Up @@ -447,7 +448,7 @@ private void addMaxEntries(String clientName, InfinispanClientBuildTimeConfig co
@BuildStep
UnremovableBeanBuildItem ensureBeanLookupAvailable() {
return UnremovableBeanBuildItem.beanTypes(BaseMarshaller.class, EnumMarshaller.class, MessageMarshaller.class,
FileDescriptorSource.class);
FileDescriptorSource.class, Schema.class);
}

@BuildStep
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.infinispan.protostream.FileDescriptorSource;
import org.infinispan.protostream.SerializationContext;
import org.infinispan.protostream.SerializationContextInitializer;
import org.infinispan.protostream.schema.Schema;
import org.infinispan.query.remote.client.ProtobufMetadataManagerConstants;

import io.quarkus.arc.Arc;
Expand Down Expand Up @@ -396,6 +397,19 @@ private static void handleProtoStreamMarshaller(ProtoStreamMarshaller marshaller
serializationContext.registerProtoFiles(fileDescriptorSource);
}

Set<Bean<Schema>> schemaBeans = (Set) beanManager.getBeans(Schema.class);
for (Bean<Schema> schemaBean : schemaBeans) {
CreationalContext<Schema> ctx = beanManager.createCreationalContext(schemaBean);
Schema schema = (Schema) beanManager.getReference(schemaBean, Schema.class,
ctx);
FileDescriptorSource fds = FileDescriptorSource.fromString(schema.getName(), schema.toString());
serializationContext.registerProtoFiles(fds);
// Register all the fds so they can be queried
for (Map.Entry<String, char[]> fdEntry : fds.getFileDescriptors().entrySet()) {
properties.put(PROTOBUF_FILE_PREFIX + fdEntry.getKey(), new String(fdEntry.getValue()));
}
}

Set<Bean<FileDescriptorSource>> protoFileBeans = (Set) beanManager.getBeans(FileDescriptorSource.class);
for (Bean<FileDescriptorSource> bean : protoFileBeans) {
CreationalContext<FileDescriptorSource> ctx = beanManager.createCreationalContext(bean);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Produces;

import org.infinispan.protostream.FileDescriptorSource;
import org.infinispan.protostream.MessageMarshaller;
import org.infinispan.protostream.schema.Schema;
import org.infinispan.protostream.schema.Type;

@ApplicationScoped
public class MarshallerConfiguration {
Expand All @@ -14,14 +15,14 @@ MessageMarshaller magazineMarshaller() {
}

@Produces
FileDescriptorSource bookProtoDefinition() {
return FileDescriptorSource.fromString("magazine.proto", "package magazine_sample;\n" +
"\n" +
"message Magazine {\n" +
" required string name = 1;\n" +
" required int32 publicationYear = 2;\n" +
" required int32 publicationMonth = 3;\n" +
" repeated string stories = 4;\n" +
"}");
Schema magazineSchema() {
return new Schema.Builder("magazine.proto")
.packageName("magazine_sample")
.addMessage("Magazine")
.addField(Type.Scalar.STRING, "name", 1)
.addField(Type.Scalar.INT32, "publicationYear", 2)
.addField(Type.Scalar.INT32, "publicationMonth", 3)
.addRepeatedField(Type.Scalar.STRING, "stories", 4)
.build();
}
}

0 comments on commit 71c8263

Please sign in to comment.