Search in sources :

Example 1 with ImmutableSerializationContext

use of org.infinispan.protostream.ImmutableSerializationContext in project infinispan by infinispan.

the class AbstractSchemaJdbcStore method createTableOperations.

@Override
protected TableOperations<K, V> createTableOperations(InitializationContext ctx, C config) throws SQLException {
    AdvancedCache<K, V> advancedCache = ctx.getCache().getAdvancedCache();
    // We use a type as the protostream -> json conversion leaves it as a String instead of byte[]
    MediaType jsonStringType = MediaType.fromString(MediaType.APPLICATION_JSON_TYPE + ";type=String");
    // This seems like a bug that `withRequestMediaType` isn't injected...
    DataConversion keyDataConversion = advancedCache.getKeyDataConversion().withRequestMediaType(jsonStringType);
    DataConversion valueDataConversion = advancedCache.getValueDataConversion().withRequestMediaType(jsonStringType);
    ComponentRegistry componentRegistry = advancedCache.getComponentRegistry();
    componentRegistry.wireDependencies(keyDataConversion, true);
    componentRegistry.wireDependencies(valueDataConversion, true);
    Parameter[] parameters = generateParameterInformation(config, connectionFactory);
    assert parameters.length != 0;
    Parameter[] primaryParameters = determinePrimaryParameters(config, parameters);
    assert primaryParameters.length != 0;
    assert Arrays.stream(primaryParameters).allMatch(Parameter::isPrimaryIdentifier);
    // We have to use the user serialization context as it will have the schemas they registered
    ImmutableSerializationContext serializationContext = componentRegistry.getComponent(SerializationContextRegistry.class).getUserCtx();
    ProtoSchemaOptions<K, V, C> options = verifySchemaAndCreateOptions(serializationContext, config.getSchemaJdbcConfiguration(), parameters, primaryParameters, keyDataConversion, valueDataConversion, ctx.getMarshallableEntryFactory());
    return actualCreateTableOperations(options);
}
Also used : DataConversion(org.infinispan.encoding.DataConversion) SerializationContextRegistry(org.infinispan.marshall.protostream.impl.SerializationContextRegistry) ComponentRegistry(org.infinispan.factories.ComponentRegistry) ImmutableSerializationContext(org.infinispan.protostream.ImmutableSerializationContext) MediaType(org.infinispan.commons.dataconversion.MediaType)

Example 2 with ImmutableSerializationContext

use of org.infinispan.protostream.ImmutableSerializationContext in project infinispan by infinispan.

the class AbstractSchemaJdbcStore method verifyParametersPresentForMessage.

void verifyParametersPresentForMessage(ImmutableSerializationContext ctx, String fullTypeName, Map<String, Parameter> parameterMap, boolean key) {
    GenericDescriptor genericDescriptor;
    try {
        genericDescriptor = ctx.getDescriptorByName(fullTypeName);
    } catch (IllegalArgumentException t) {
        throw log.schemaNotFound(fullTypeName);
    }
    Set<String> seenNames = new HashSet<>();
    if (genericDescriptor instanceof Descriptor) {
        recursiveUpdateParameters((Descriptor) genericDescriptor, parameterMap, null, seenNames, key);
    } else if (genericDescriptor instanceof EnumDescriptor) {
        if (!key && config.getSchemaJdbcConfiguration().embeddedKey()) {
            throw log.keyCannotEmbedWithEnum(fullTypeName);
        }
        String name = genericDescriptor.getName();
        // treat an enum as just a string
        Parameter enumParam = parameterMap.get(name.toUpperCase());
        if (enumParam != null) {
            assert enumParam.getType() == ProtostreamFieldType.STRING;
            updateUnwrap(enumParam, key, json -> json.at("_value"));
            enumParam.jsonUpdateConsumer = (json, o) -> {
                json.set("_type", fullTypeName);
                json.set("_value", o);
            };
        }
    } else {
        throw new UnsupportedOperationException("Unsupported descriptor found " + genericDescriptor);
    }
}
Also used : Type(org.infinispan.protostream.descriptors.Type) Arrays(java.util.Arrays) ImmutableSerializationContext(org.infinispan.protostream.ImmutableSerializationContext) TableOperations(org.infinispan.persistence.jdbc.common.TableOperations) AbstractSchemaJdbcConfiguration(org.infinispan.persistence.sql.configuration.AbstractSchemaJdbcConfiguration) MarshallableEntryFactory(org.infinispan.persistence.spi.MarshallableEntryFactory) HashMap(java.util.HashMap) ComponentRegistry(org.infinispan.factories.ComponentRegistry) Descriptor(org.infinispan.protostream.descriptors.Descriptor) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) EnumDescriptor(org.infinispan.protostream.descriptors.EnumDescriptor) SQLException(java.sql.SQLException) MediaType(org.infinispan.commons.dataconversion.MediaType) AdvancedCache(org.infinispan.AdvancedCache) ResultSet(java.sql.ResultSet) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) MarshallableEntry(org.infinispan.persistence.spi.MarshallableEntry) SerializationContextRegistry(org.infinispan.marshall.protostream.impl.SerializationContextRegistry) BaseJdbcStore(org.infinispan.persistence.jdbc.common.impl.BaseJdbcStore) BaseTableOperations(org.infinispan.persistence.jdbc.common.sql.BaseTableOperations) Predicate(java.util.function.Predicate) CacheConfigurationException(org.infinispan.commons.CacheConfigurationException) Timestamp(java.sql.Timestamp) Set(java.util.Set) Json(org.infinispan.commons.dataconversion.internal.Json) PreparedStatement(java.sql.PreparedStatement) Collectors(java.util.stream.Collectors) SchemaJdbcConfiguration(org.infinispan.persistence.sql.configuration.SchemaJdbcConfiguration) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) Objects(java.util.Objects) GenericDescriptor(org.infinispan.protostream.descriptors.GenericDescriptor) Base64(java.util.Base64) List(java.util.List) ConnectionFactory(org.infinispan.persistence.jdbc.common.connectionfactory.ConnectionFactory) InitializationContext(org.infinispan.persistence.spi.InitializationContext) DataConversion(org.infinispan.encoding.DataConversion) Types(java.sql.Types) GenericDescriptor(org.infinispan.protostream.descriptors.GenericDescriptor) Descriptor(org.infinispan.protostream.descriptors.Descriptor) EnumDescriptor(org.infinispan.protostream.descriptors.EnumDescriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) GenericDescriptor(org.infinispan.protostream.descriptors.GenericDescriptor) EnumDescriptor(org.infinispan.protostream.descriptors.EnumDescriptor) HashSet(java.util.HashSet)

Example 3 with ImmutableSerializationContext

use of org.infinispan.protostream.ImmutableSerializationContext in project infinispan by infinispan.

the class ProtobufMetadataCachePreserveStateAcrossRestartsTest method verifySchemas.

private void verifySchemas(CacheContainer manager) {
    assertFiles(manager, "A.proto", "B.proto", "C.proto", "D.proto", "E.proto", "F.proto");
    assertFilesWithErrors(manager, "E.proto", "F.proto");
    SerializationContextRegistry scr = extractGlobalComponent(manager, SerializationContextRegistry.class);
    ImmutableSerializationContext serializationContext = scr.getUserCtx();
    Descriptor mDescriptor = serializationContext.getMessageDescriptor("D.M");
    assertNotNull(mDescriptor);
    List<String> fields = mDescriptor.getFields().stream().map(AnnotatedDescriptorImpl::getName).collect(Collectors.toList());
    assertEquals(singletonList("s"), fields);
}
Also used : SerializationContextRegistry(org.infinispan.marshall.protostream.impl.SerializationContextRegistry) ImmutableSerializationContext(org.infinispan.protostream.ImmutableSerializationContext) Descriptor(org.infinispan.protostream.descriptors.Descriptor)

Example 4 with ImmutableSerializationContext

use of org.infinispan.protostream.ImmutableSerializationContext in project protostream by infinispan.

the class UnknownFieldSetImplTest method createMarshalledObject.

private byte[] createMarshalledObject() throws IOException {
    ImmutableSerializationContext ctx = createContext();
    User user = new User();
    user.setId(1);
    user.setName("John");
    user.setSurname("Batman");
    user.setGender(User.Gender.MALE);
    user.setAccountIds(new HashSet<>(Arrays.asList(1, 3)));
    user.setAddresses(Collections.singletonList(new Address("Old Street", "XYZ42", -12)));
    return ProtobufUtil.toByteArray(ctx, user);
}
Also used : User(org.infinispan.protostream.domain.User) Address(org.infinispan.protostream.domain.Address) ImmutableSerializationContext(org.infinispan.protostream.ImmutableSerializationContext)

Example 5 with ImmutableSerializationContext

use of org.infinispan.protostream.ImmutableSerializationContext in project wildfly by wildfly.

the class DefaultProtoStreamWriter method writeObjectNoTag.

@Override
public void writeObjectNoTag(Object value) throws IOException {
    ImmutableSerializationContext context = this.getSerializationContext();
    ProtoStreamMarshaller<Object> marshaller = this.findMarshaller(value.getClass());
    OptionalInt size = this.size(marshaller, value);
    try (ByteBufferOutputStream output = new ByteBufferOutputStream(size)) {
        TagWriterImpl writer = size.isPresent() ? TagWriterImpl.newInstance(context, output, size.getAsInt()) : TagWriterImpl.newInstance(context, output);
        marshaller.writeTo(new DefaultProtoStreamWriter(writer), value);
        writer.flush();
        ByteBuffer buffer = output.getBuffer();
        int offset = buffer.arrayOffset();
        int length = buffer.limit() - offset;
        this.writeVarint32(length);
        this.writeRawBytes(buffer.array(), offset, length);
    }
}
Also used : ByteBufferOutputStream(org.wildfly.clustering.marshalling.spi.ByteBufferOutputStream) ImmutableSerializationContext(org.infinispan.protostream.ImmutableSerializationContext) OptionalInt(java.util.OptionalInt) TagWriterImpl(org.infinispan.protostream.impl.TagWriterImpl) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ImmutableSerializationContext (org.infinispan.protostream.ImmutableSerializationContext)8 SerializationContextRegistry (org.infinispan.marshall.protostream.impl.SerializationContextRegistry)3 MediaType (org.infinispan.commons.dataconversion.MediaType)2 DataConversion (org.infinispan.encoding.DataConversion)2 ComponentRegistry (org.infinispan.factories.ComponentRegistry)2 Descriptor (org.infinispan.protostream.descriptors.Descriptor)2 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Timestamp (java.sql.Timestamp)1 Types (java.sql.Types)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Base64 (java.util.Base64)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1