Search in sources :

Example 1 with Descriptor

use of org.infinispan.protostream.descriptors.Descriptor in project kogito-apps by kiegroup.

the class ProtoIndexParser method create.

@Override
public EntityIndexDescriptor create(Descriptor annotatedDescriptor, AnnotationElement.Annotation annotation) {
    String name = annotatedDescriptor.getFullName();
    List<IndexDescriptor> indexes = new ArrayList<>(annotatedDescriptor.getFields().size());
    List<AttributeDescriptor> fields = new ArrayList<>(annotatedDescriptor.getFields().size());
    for (FieldDescriptor fd : annotatedDescriptor.getFields()) {
        AnnotationElement.Annotation fieldAnnotation = fd.getAnnotations().get(FIELD_ANNOTATION);
        if (fieldAnnotation != null) {
            String fieldName = Optional.ofNullable((String) fieldAnnotation.getAttributeValue(FIELD_NAME_ATTRIBUTE).getValue()).filter(a -> !a.isEmpty()).orElseGet(fd::getName);
            boolean isIndexed = INDEX_YES.equals(fieldAnnotation.getAttributeValue(FIELD_INDEX_ATTRIBUTE).getValue());
            fields.add(createAttributeDescriptor(fd, fieldName));
            if (isIndexed) {
                indexes.add(new IndexDescriptor(fieldName, List.of(fieldName)));
            }
        }
    }
    return new EntityIndexDescriptor(name, indexes, fields);
}
Also used : FileDescriptor(org.infinispan.protostream.descriptors.FileDescriptor) AnnotationMetadataCreator(org.infinispan.protostream.AnnotationMetadataCreator) Set(java.util.Set) Descriptor(org.infinispan.protostream.descriptors.Descriptor) AttributeDescriptor(org.kie.kogito.persistence.api.schema.AttributeDescriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) ArrayList(java.util.ArrayList) Objects(java.util.Objects) List(java.util.List) EntityIndexDescriptor(org.kie.kogito.persistence.api.schema.EntityIndexDescriptor) Map(java.util.Map) Optional(java.util.Optional) Configuration(org.infinispan.protostream.config.Configuration) IndexDescriptor(org.kie.kogito.persistence.api.schema.IndexDescriptor) AnnotationElement(org.infinispan.protostream.descriptors.AnnotationElement) Collectors.toSet(java.util.stream.Collectors.toSet) ArrayList(java.util.ArrayList) AttributeDescriptor(org.kie.kogito.persistence.api.schema.AttributeDescriptor) EntityIndexDescriptor(org.kie.kogito.persistence.api.schema.EntityIndexDescriptor) IndexDescriptor(org.kie.kogito.persistence.api.schema.IndexDescriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) AnnotationElement(org.infinispan.protostream.descriptors.AnnotationElement) EntityIndexDescriptor(org.kie.kogito.persistence.api.schema.EntityIndexDescriptor)

Example 2 with Descriptor

use of org.infinispan.protostream.descriptors.Descriptor in project infinispan by infinispan.

the class ProtobufPropertyHelper method mapPropertyNamePathToFieldIdPath.

@Override
public List<?> mapPropertyNamePathToFieldIdPath(Descriptor messageDescriptor, String[] propertyPath) {
    List<Integer> translatedPath = new ArrayList<>(propertyPath.length);
    Descriptor md = messageDescriptor;
    for (String prop : propertyPath) {
        FieldDescriptor fd = md.findFieldByName(prop);
        translatedPath.add(fd.getNumber());
        if (fd.getJavaType() == JavaType.MESSAGE) {
            md = fd.getMessageType();
        } else {
            // iteration is expected to stop here
            md = null;
        }
    }
    return translatedPath;
}
Also used : ArrayList(java.util.ArrayList) EnumValueDescriptor(org.infinispan.protostream.descriptors.EnumValueDescriptor) EnumDescriptor(org.infinispan.protostream.descriptors.EnumDescriptor) Descriptor(org.infinispan.protostream.descriptors.Descriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor)

Example 3 with Descriptor

use of org.infinispan.protostream.descriptors.Descriptor in project infinispan by infinispan.

the class ProtobufPropertyHelper method getField.

/**
 * @param entityType
 * @param propertyPath
 * @return the field descriptor or null if not found
 */
private FieldDescriptor getField(Descriptor entityType, String[] propertyPath) {
    Descriptor messageDescriptor = entityType;
    int i = 0;
    for (String p : propertyPath) {
        FieldDescriptor field = messageDescriptor.findFieldByName(p);
        if (field == null || ++i == propertyPath.length) {
            return field;
        }
        if (field.getJavaType() == JavaType.MESSAGE) {
            messageDescriptor = field.getMessageType();
        } else {
            break;
        }
    }
    return null;
}
Also used : EnumValueDescriptor(org.infinispan.protostream.descriptors.EnumValueDescriptor) EnumDescriptor(org.infinispan.protostream.descriptors.EnumDescriptor) Descriptor(org.infinispan.protostream.descriptors.Descriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor)

Example 4 with Descriptor

use of org.infinispan.protostream.descriptors.Descriptor in project infinispan by infinispan.

the class ProtobufPropertyHelper method isRepeatedProperty.

@Override
public boolean isRepeatedProperty(Descriptor entityType, String[] propertyPath) {
    Descriptor messageDescriptor = entityType;
    for (String p : propertyPath) {
        FieldDescriptor field = messageDescriptor.findFieldByName(p);
        if (field == null) {
            break;
        }
        if (field.isRepeated()) {
            return true;
        }
        if (field.getJavaType() != JavaType.MESSAGE) {
            break;
        }
        messageDescriptor = field.getMessageType();
    }
    return false;
}
Also used : EnumValueDescriptor(org.infinispan.protostream.descriptors.EnumValueDescriptor) EnumDescriptor(org.infinispan.protostream.descriptors.EnumDescriptor) Descriptor(org.infinispan.protostream.descriptors.Descriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor)

Example 5 with Descriptor

use of org.infinispan.protostream.descriptors.Descriptor in project infinispan by infinispan.

the class AbstractSchemaJdbcStore method recursiveUpdateParameters.

void recursiveUpdateParameters(Descriptor descriptor, Map<String, Parameter> parameterMap, String[] nestedMessageNames, Set<String> seenNames, boolean key) {
    for (FieldDescriptor fieldDescriptor : descriptor.getFields()) {
        String name = fieldDescriptor.getName();
        if (fieldDescriptor.isRepeated()) {
            throw log.repeatedFieldsNotSupported(name, fieldDescriptor.getTypeName());
        }
        Descriptor fieldMessageDescriptor = fieldDescriptor.getMessageType();
        if (fieldMessageDescriptor != null) {
            String[] newNestedMessageNames;
            if (nestedMessageNames == null) {
                newNestedMessageNames = new String[1];
                newNestedMessageNames[0] = name;
            } else {
                newNestedMessageNames = Arrays.copyOf(nestedMessageNames, nestedMessageNames.length + 1);
                newNestedMessageNames[nestedMessageNames.length] = name;
            }
            recursiveUpdateParameters(fieldMessageDescriptor, parameterMap, newNestedMessageNames, seenNames, key);
            continue;
        }
        if (!seenNames.add(name)) {
            throw log.duplicateFieldInSchema(name, fieldDescriptor.getTypeName());
        }
        Parameter parameter = parameterMap.get(name.toUpperCase());
        if (parameter == null) {
            if (fieldDescriptor.isRequired()) {
                throw log.requiredSchemaFieldNotPresent(name, fieldDescriptor.getTypeName());
            }
            continue;
        }
        if (parameter.primaryIdentifier && !key && !config.getSchemaJdbcConfiguration().embeddedKey()) {
            throw log.primaryKeyPresentButNotEmbedded(parameter.name, fieldDescriptor.getTypeName());
        }
        Function<Json, Json> retrievalFunction;
        BiConsumer<Json, Object> valueConsumer;
        // Oracle doesn't have a boolean type, so use a number of 0 or 1 instead
        if (parameter.type == ProtostreamFieldType.INT_32 && fieldDescriptor.getType() == Type.BOOL) {
            retrievalFunction = json -> Json.factory().number(json.at(name).asBoolean() ? 1 : 0);
            valueConsumer = (json, o) -> json.set(name, ((Integer) o) == 1);
        } else {
            retrievalFunction = json -> json.at(name);
            valueConsumer = (json, o) -> json.set(name, o);
        }
        if (nestedMessageNames == null) {
            updateUnwrap(parameter, key, retrievalFunction);
            parameter.jsonUpdateConsumer = valueConsumer;
        } else {
            updateUnwrap(parameter, key, json -> {
                for (String nestedName : nestedMessageNames) {
                    json = json.at(nestedName);
                    if (json == null)
                        return null;
                }
                return retrievalFunction.apply(json);
            });
            parameter.jsonUpdateConsumer = ((json, o) -> {
                Json nestedJSon = json;
                for (String nestedName : nestedMessageNames) {
                    nestedJSon = json.at(nestedName);
                    if (nestedJSon == null) {
                        nestedJSon = Json.object();
                        json.set(nestedName, nestedJSon);
                    }
                    json = nestedJSon;
                }
                valueConsumer.accept(nestedJSon, o);
            });
        }
    }
}
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) Descriptor(org.infinispan.protostream.descriptors.Descriptor) EnumDescriptor(org.infinispan.protostream.descriptors.EnumDescriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) GenericDescriptor(org.infinispan.protostream.descriptors.GenericDescriptor) Json(org.infinispan.commons.dataconversion.internal.Json) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor)

Aggregations

Descriptor (org.infinispan.protostream.descriptors.Descriptor)28 FieldDescriptor (org.infinispan.protostream.descriptors.FieldDescriptor)24 EnumDescriptor (org.infinispan.protostream.descriptors.EnumDescriptor)19 FileDescriptor (org.infinispan.protostream.descriptors.FileDescriptor)13 GenericDescriptor (org.infinispan.protostream.descriptors.GenericDescriptor)11 List (java.util.List)9 Test (org.junit.Test)8 Map (java.util.Map)7 FileDescriptorSource (org.infinispan.protostream.FileDescriptorSource)7 Configuration (org.infinispan.protostream.config.Configuration)7 EnumValueDescriptor (org.infinispan.protostream.descriptors.EnumValueDescriptor)7 ExtendDescriptor (org.infinispan.protostream.descriptors.ExtendDescriptor)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 AnnotationElement (org.infinispan.protostream.descriptors.AnnotationElement)5 IOException (java.io.IOException)4 Objects (java.util.Objects)3 Set (java.util.Set)3 Function (java.util.function.Function)3 Predicate (java.util.function.Predicate)3