Skip to content

Commit

Permalink
Merge pull request #39326 from karesti/protostream-5-final
Browse files Browse the repository at this point in the history
Updates Infinispan to Protostream 5.0.1.Final
  • Loading branch information
gsmet authored Mar 13, 2024
2 parents 72f748b + 03130c7 commit b924348
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 211 deletions.
2 changes: 1 addition & 1 deletion bom/application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
<hamcrest.version>2.2</hamcrest.version><!-- The version needs to be compatible with both REST Assured and Awaitility -->
<junit.jupiter.version>5.10.2</junit.jupiter.version>
<infinispan.version>15.0.0.CR1</infinispan.version>
<infinispan.protostream.version>5.0.0.CR2</infinispan.protostream.version>
<infinispan.protostream.version>5.0.1.Final</infinispan.protostream.version>
<caffeine.version>3.1.5</caffeine.version>
<netty.version>4.1.107.Final</netty.version>
<brotli4j.version>1.14.0</brotli4j.version>
Expand Down
83 changes: 35 additions & 48 deletions docs/src/main/asciidoc/infinispan-client-reference.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -373,57 +373,43 @@ Here is an example of how the preceding classes should be changed:
.Author.java
[source,java]
----
@ProtoFactory
public Author(String name, String surname) {
this.name = Objects.requireNonNull(name);
this.surname = Objects.requireNonNull(surname);
}
@ProtoField(number = 1)
public String getName() {
return name;
}
@Proto //<1>
public record Author(String name, String surname) { //<2>
}
----
<1> Since Protostream 5.0, a single annotation is needed to generate a default mapping
<2> Since Protostream 5.0, records are supported

@ProtoField(number = 2)
public String getSurname() {
return surname;
}
.Type.java
[source,java]
----
@Proto
public enum Type { //<1>
FANTASY,
PROGRAMMING
}
----
<1> Enums are supported

.Book.java
[source,java]
----
@ProtoFactory
public Book(String title, String description, int publicationYear, Set<Author> authors) {
this.title = Objects.requireNonNull(title);
this.description = Objects.requireNonNull(description);
this.publicationYear = publicationYear;
this.authors = Objects.requireNonNull(authors);
}
@ProtoField(number = 1)
public String getTitle() {
return title;
}
@ProtoField(number = 2)
public String getDescription() {
return description;
}
@ProtoField(number = 3, defaultValue = "-1")
public int getPublicationYear() {
return publicationYear;
}
@ProtoField(number = 4)
public Set<Author> getAuthors() {
return authors;
}
@Proto
@Indexed // <1>
public record Book(@Text String title, //<2>
@Keyword(projectable = true, sortable = true, normalizer = "lowercase", indexNullAs = "unnamed", norms = false) //<3>
String description,
int publicationYear,
Set<Author> authors, // <4>
Type bookType,
BigDecimal price) { // <5>
}
----

If your classes have only mutable fields, then the `ProtoFactory` annotation
is not required, assuming your class has a no arg constructor.
<1> Indicates that the entity will be indexed. Necessary to perform distributed full-text query operations.
<2> Indicates the `title` should be indexed as text
<3> Indicates the `description` field field should be indexed as a Keyword.
<4> Collections are supported
<5> Protostream provides default Protobuf mappers for commonly used types as `BigDecimal`, included in the `org.infinispan.protostream.types` package.

Then all that is required is a very simple `GeneratedSchema` interface with an annotation
on it to specify configuration settings
Expand All @@ -432,23 +418,24 @@ on it to specify configuration settings
[source,java]
----
import org.infinispan.protostream.GeneratedSchema;
import org.infinispan.protostream.annotations.AutoProtoSchemaBuilder;
import org.infinispan.protostream.annotations.ProtoSchema;
import org.infinispan.protostream.types.java.math.BigDecimalAdapter;
@AutoProtoSchemaBuilder(includeClasses = { Book.class, Author.class, BigDecimalAdapter.class }, schemaPackageName = "book_sample")
@ProtoSchema(includeClasses = { Book.class, Author.class, BigDecimalAdapter.class }, schemaPackageName = "book_sample")
interface BookStoreSchema extends GeneratedSchema {
}
----

[TIP]
====
Protostream provides default Protobuf mappers for commonly used types as `BigDecimal`, included in the `org.infinispan.protostream.types` package.
You can use the `basePackages` property to scan a full package containing your classes.
You can override the default marshalling by using the `@Protofield` annotation.
====

So in this case we will automatically generate the marshaller and schemas for the included classes and
place them in the schema package automatically. The package does not have to be provided, but if you use Infinispan search capabilities, you must know the generated package.

NOTE: In Quarkus the `schemaFileName` and `schemaFilePath` attributes should NOT be set on the `AutoProtoSchemaBuilder` annotation. Setting either attributes causes native runtime errors.
NOTE: In Quarkus the `schemaFileName` and `schemaFilePath` attributes should NOT be set on the `ProtoSchema` annotation. Setting either attributes causes native runtime errors.

=== Custom serialization

Expand Down
25 changes: 8 additions & 17 deletions docs/src/main/asciidoc/infinispan-client.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,12 @@ Create the `src/main/java/org/acme/infinispan/client/Greeting.java` file, with t
----
package org.acme.infinispan.client;
import org.infinispan.protostream.annotations.ProtoFactory;
import org.infinispan.protostream.annotations.ProtoField;
import org.infinispan.protostream.annotations.Proto;
public class Greeting { // <1>
@ProtoField(number = 1) // <2>
public String name;
@ProtoField(number = 2) // <3>
public String message;
}
@Proto //<1>
public record Greeting(String name, String message) {} //<2>
----
<1> If your classes have only mutable fields, then the `ProtoFactory` annotation is not required
is not required, assuming your class has a no arg constructor
<2> `@ProtoField` annotation to add the name field as string in the generated Protobuf schema
<3> `@ProtoField` annotation to add the message field as string in the generated Protobuf schema
<1> You only need an annotation to tag the record to be marshalled by Protostream

Note that we are not going to use Java serialization. https://github.com/infinispan/protostream[Protostream] is
a serialization library based on Protobuf data format part of Infinispan. Using an annotation based API, we
Expand All @@ -104,13 +95,13 @@ Create the `src/main/java/org/acme/infinispan/client/GreetingSchema.java` file,
package org.acme.infinispan.client;
import org.infinispan.protostream.GeneratedSchema;
import org.infinispan.protostream.annotations.AutoProtoSchemaBuilder;
import org.infinispan.protostream.annotations.ProtoSchema;
@AutoProtoSchemaBuilder(includeClasses = Greeting.class) // <1>
@ProtoSchema(includeClasses = Greeting.class) // <1>
public interface GreetingSchema extends GeneratedSchema { // <2>
}
----
<1> Includes the `Greeting` pojo with the `@AutoProtoSchemaBuilder` annotation
<1> Includes the `Greeting` pojo with the `@ProtoSchema` annotation
<2> Extends `GeneratedSchema` Protostream API interface

The Protobuf Schema that will be generated and used both on client and Infinispan Server side, will have
Expand All @@ -121,7 +112,7 @@ the following content:
// File name: GreetingSchema.proto
// Generated from : org.acme.infinispan.client.GreetingSchema
syntax = "proto2";
syntax = "proto3";
message Greeting {
Expand Down
30 changes: 15 additions & 15 deletions extensions/infinispan-client/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-elytron-security-common</artifactId>
</dependency>
<!-- Will allow projects to run annotation processor -->
<dependency>
<groupId>org.infinispan.protostream</groupId>
<artifactId>protostream-processor</artifactId>
<exclusions>
<exclusion>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.infinispan.protostream</groupId>
<artifactId>protostream-types</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly.security</groupId>
<artifactId>wildfly-elytron-sasl-plain</artifactId>
Expand Down Expand Up @@ -111,21 +126,6 @@
<groupId>org.infinispan</groupId>
<artifactId>infinispan-query-dsl</artifactId>
</dependency>
<!-- Will allow projects to run annotation processor -->
<dependency>
<groupId>org.infinispan.protostream</groupId>
<artifactId>protostream-processor</artifactId>
<exclusions>
<exclusion>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.infinispan.protostream</groupId>
<artifactId>protostream-types</artifactId>
</dependency>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,7 @@
package io.quarkus.it.infinispan.client;

import java.util.Objects;
import org.infinispan.protostream.annotations.Proto;

import org.infinispan.protostream.annotations.ProtoFactory;
import org.infinispan.protostream.annotations.ProtoField;

/**
* @author William Burns
*/
public class Author {
private final String name;
private final String surname;

@ProtoFactory
public Author(String name, String surname) {
this.name = Objects.requireNonNull(name);
this.surname = Objects.requireNonNull(surname);
}

@ProtoField(number = 1)
public String getName() {
return name;
}

@ProtoField(number = 2)
public String getSurname() {
return surname;
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Author author = (Author) o;
return name.equals(author.name) &&
surname.equals(author.surname);
}

@Override
public int hashCode() {
return Objects.hash(name, surname);
}
@Proto
public record Author(String name, String surname) {
}
Original file line number Diff line number Diff line change
@@ -1,86 +1,16 @@
package io.quarkus.it.infinispan.client;

import java.math.BigDecimal;
import java.util.Objects;
import java.util.Set;

import org.infinispan.api.annotations.indexing.Indexed;
import org.infinispan.api.annotations.indexing.Keyword;
import org.infinispan.api.annotations.indexing.Text;
import org.infinispan.protostream.annotations.ProtoFactory;
import org.infinispan.protostream.annotations.ProtoField;
import org.infinispan.protostream.annotations.Proto;

/**
* @author William Burns
*/
@Proto
@Indexed
public class Book {
private final String title;
private final String description;
private final int publicationYear;
private final Set<Author> authors;
private final Type bookType;
private final BigDecimal price;

@ProtoFactory
public Book(String title, String description, int publicationYear, Set<Author> authors, Type bookType, BigDecimal price) {
this.title = Objects.requireNonNull(title);
this.description = Objects.requireNonNull(description);
this.publicationYear = publicationYear;
this.authors = Objects.requireNonNull(authors);
this.bookType = bookType;
this.price = price;
}

@ProtoField(number = 1)
@Text
public String getTitle() {
return title;
}

@ProtoField(number = 2)
@Keyword(projectable = true, sortable = true, normalizer = "lowercase", indexNullAs = "unnamed", norms = false)
public String getDescription() {
return description;
}

@ProtoField(number = 3, defaultValue = "-1")
public int getPublicationYear() {
return publicationYear;
}

@ProtoField(number = 4)
public Set<Author> getAuthors() {
return authors;
}

@ProtoField(number = 5)
public Type getBookType() {
return bookType;
}

@ProtoField(number = 6)
public BigDecimal getPrice() {
return price;
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Book book = (Book) o;
return publicationYear == book.publicationYear &&
title.equals(book.title) &&
description.equals(book.description) &&
authors.equals(book.authors) &&
bookType.equals(book.bookType) &&
price.equals(book.price);
}

@Override
public int hashCode() {
return Objects.hash(title, description, publicationYear, authors, bookType, price);
}
public record Book(@Text String title,
@Keyword(projectable = true, sortable = true, normalizer = "lowercase", indexNullAs = "unnamed", norms = false) String description,
int publicationYear, Set<Author> authors, Type bookType, BigDecimal price) {
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package io.quarkus.it.infinispan.client;

import org.infinispan.protostream.GeneratedSchema;
import org.infinispan.protostream.annotations.AutoProtoSchemaBuilder;
import org.infinispan.protostream.annotations.ProtoSchema;
import org.infinispan.protostream.types.java.math.BigDecimalAdapter;

@AutoProtoSchemaBuilder(includeClasses = { Book.class, Type.class, Author.class,
@ProtoSchema(includeClasses = { Book.class, Type.class, Author.class,
BigDecimalAdapter.class }, schemaPackageName = "book_sample")
interface BookStoreSchema extends GeneratedSchema {
}
Loading

0 comments on commit b924348

Please sign in to comment.