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);
}
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);
}
}
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);
}
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);
}
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);
}
}
Aggregations