Search in sources :

Example 6 with FieldDescriptor

use of org.infinispan.protostream.descriptors.FieldDescriptor 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 7 with FieldDescriptor

use of org.infinispan.protostream.descriptors.FieldDescriptor 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)

Example 8 with FieldDescriptor

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

the class ProtobufFieldIndexingMetadata method getFlag.

private boolean getFlag(String[] propertyPath, BiFunction<IndexingMetadata, String, Boolean> metadataFun) {
    Descriptor md = messageDescriptor;
    int i = 0;
    for (String p : propertyPath) {
        i++;
        FieldDescriptor field = md.findFieldByName(p);
        if (field == null) {
            break;
        }
        IndexingMetadata indexingMetadata = md.getProcessedAnnotation(IndexingMetadata.INDEXED_ANNOTATION);
        boolean res = indexingMetadata == null ? false : metadataFun.apply(indexingMetadata, field.getName());
        if (!res) {
            break;
        }
        if (field.getJavaType() == JavaType.MESSAGE) {
            md = field.getMessageType();
        } else {
            return i == propertyPath.length;
        }
    }
    return false;
}
Also used : Descriptor(org.infinispan.protostream.descriptors.Descriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) IndexingMetadata(org.infinispan.query.remote.impl.indexing.IndexingMetadata) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor)

Example 9 with FieldDescriptor

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

the class ProtobufFieldIndexingMetadata method getNullMarker.

@Override
public Object getNullMarker(String[] propertyPath) {
    Descriptor md = messageDescriptor;
    int i = 0;
    for (String p : propertyPath) {
        i++;
        FieldDescriptor field = md.findFieldByName(p);
        if (field == null) {
            break;
        }
        if (i == propertyPath.length) {
            IndexingMetadata indexingMetadata = md.getProcessedAnnotation(IndexingMetadata.INDEXED_ANNOTATION);
            return indexingMetadata == null ? null : indexingMetadata.getNullMarker(field.getName());
        }
        if (field.getJavaType() != JavaType.MESSAGE) {
            break;
        }
        md = field.getMessageType();
    }
    return null;
}
Also used : Descriptor(org.infinispan.protostream.descriptors.Descriptor) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor) IndexingMetadata(org.infinispan.query.remote.impl.indexing.IndexingMetadata) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor)

Example 10 with FieldDescriptor

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

the class IndexingMetadataCreator method create.

// Recognized annotations:
// @Indexed ('index' optional string attribute; defaults to empty string)
// @Analyzer (definition = "<definition name>")
// @Field (name optional string, index = true/false, defaults to true, analyze = true/false, defaults to true, store = true/false, defaults to false, analyzer = @Analyzer(definition = "<definition name>"))
// @SortableField
@Override
public IndexingMetadata create(Descriptor descriptor, AnnotationElement.Annotation annotation) {
    String indexName = (String) annotation.getAttributeValue(IndexingMetadata.INDEXED_INDEX_ATTRIBUTE).getValue();
    if (indexName.isEmpty()) {
        indexName = null;
    }
    String entityAnalyzer = null;
    AnnotationElement.Annotation entityAnalyzerAnnotation = descriptor.getAnnotations().get(IndexingMetadata.ANALYZER_ANNOTATION);
    if (entityAnalyzerAnnotation != null) {
        String v = (String) entityAnalyzerAnnotation.getAttributeValue(IndexingMetadata.ANALYZER_DEFINITION_ATTRIBUTE).getValue();
        if (!v.isEmpty()) {
            entityAnalyzer = v;
        }
    }
    Map<String, FieldMapping> fields = new HashMap<>(descriptor.getFields().size());
    for (FieldDescriptor fd : descriptor.getFields()) {
        String fieldLevelAnalyzer = null;
        AnnotationElement.Annotation fieldAnalyzerAnnotation = fd.getAnnotations().get(IndexingMetadata.ANALYZER_ANNOTATION);
        if (fieldAnalyzerAnnotation != null) {
            String v = (String) fieldAnalyzerAnnotation.getAttributeValue(IndexingMetadata.ANALYZER_DEFINITION_ATTRIBUTE).getValue();
            if (!v.isEmpty()) {
                fieldLevelAnalyzer = v;
            }
        }
        boolean isSortable = false;
        AnnotationElement.Annotation sortableFieldAnnotation = fd.getAnnotations().get(IndexingMetadata.SORTABLE_FIELD_ANNOTATION);
        if (sortableFieldAnnotation != null) {
            isSortable = true;
        }
        AnnotationElement.Annotation fieldAnnotation = fd.getAnnotations().get(IndexingMetadata.FIELD_ANNOTATION);
        if (fieldAnnotation != null) {
            String fieldName = fd.getName();
            String v = (String) fieldAnnotation.getAttributeValue(IndexingMetadata.FIELD_NAME_ATTRIBUTE).getValue();
            if (!v.isEmpty()) {
                fieldName = v;
            }
            AnnotationElement.Value indexAttribute = fieldAnnotation.getAttributeValue(IndexingMetadata.FIELD_INDEX_ATTRIBUTE);
            boolean isIndexed = IndexingMetadata.INDEX_YES.equals(indexAttribute.getValue());
            AnnotationElement.Value analyzeAttribute = fieldAnnotation.getAttributeValue(IndexingMetadata.FIELD_ANALYZE_ATTRIBUTE);
            boolean isAnalyzed = IndexingMetadata.ANALYZE_YES.equals(analyzeAttribute.getValue());
            AnnotationElement.Value storeAttribute = fieldAnnotation.getAttributeValue(IndexingMetadata.FIELD_STORE_ATTRIBUTE);
            boolean isStored = IndexingMetadata.STORE_YES.equals(storeAttribute.getValue());
            AnnotationElement.Value indexNullAsAttribute = fieldAnnotation.getAttributeValue(IndexingMetadata.FIELD_INDEX_NULL_AS_ATTRIBUTE);
            String indexNullAs = (String) indexNullAsAttribute.getValue();
            if (IndexingMetadata.DO_NOT_INDEX_NULL.equals(indexNullAs)) {
                indexNullAs = null;
            }
            AnnotationElement.Annotation fieldLevelAnalyzerAnnotationAttribute = (AnnotationElement.Annotation) fieldAnnotation.getAttributeValue(IndexingMetadata.FIELD_ANALYZER_ATTRIBUTE).getValue();
            String fieldLevelAnalyzerAttribute = (String) fieldLevelAnalyzerAnnotationAttribute.getAttributeValue(IndexingMetadata.ANALYZER_DEFINITION_ATTRIBUTE).getValue();
            if (fieldLevelAnalyzerAttribute.isEmpty()) {
                fieldLevelAnalyzerAttribute = null;
            } else {
                // TODO [anistor] field level analyzer attribute overrides the one specified by an eventual field level Analyzer annotation. Need to check if this is consistent with hibernate search
                fieldLevelAnalyzer = fieldLevelAnalyzerAttribute;
            }
            // field level analyzer should not be specified unless the field is analyzed
            if (!isAnalyzed && (fieldLevelAnalyzer != null || fieldLevelAnalyzerAttribute != null)) {
                throw new IllegalStateException("Cannot specify an analyzer for field " + fd.getFullName() + " unless the field is analyzed.");
            }
            FieldMapping fieldMapping = new FieldMapping(fieldName, isIndexed, isAnalyzed, isStored, isSortable, fieldLevelAnalyzer, indexNullAs, fd);
            fields.put(fieldName, fieldMapping);
            if (log.isDebugEnabled()) {
                log.debugf("fieldName=%s fieldMapping=%s", fieldName, fieldMapping);
            }
        }
    }
    IndexingMetadata indexingMetadata = new IndexingMetadata(true, indexName, entityAnalyzer, fields);
    if (log.isDebugEnabled()) {
        log.debugf("Descriptor name=%s indexingMetadata=%s", descriptor.getFullName(), indexingMetadata);
    }
    return indexingMetadata;
}
Also used : AnnotationElement(org.infinispan.protostream.descriptors.AnnotationElement) HashMap(java.util.HashMap) FieldDescriptor(org.infinispan.protostream.descriptors.FieldDescriptor)

Aggregations

FieldDescriptor (org.infinispan.protostream.descriptors.FieldDescriptor)51 Descriptor (org.infinispan.protostream.descriptors.Descriptor)16 EnumDescriptor (org.infinispan.protostream.descriptors.EnumDescriptor)12 TagWriter (org.infinispan.protostream.TagWriter)9 IOException (java.io.IOException)7 GenericDescriptor (org.infinispan.protostream.descriptors.GenericDescriptor)7 EnumValueDescriptor (org.infinispan.protostream.descriptors.EnumValueDescriptor)6 FileDescriptor (org.infinispan.protostream.descriptors.FileDescriptor)6 Map (java.util.Map)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 FileDescriptorSource (org.infinispan.protostream.FileDescriptorSource)4 Configuration (org.infinispan.protostream.config.Configuration)4 ExtendDescriptor (org.infinispan.protostream.descriptors.ExtendDescriptor)4 Test (org.junit.Test)4 HashMap (java.util.HashMap)3 Objects (java.util.Objects)3 AnnotationElement (org.infinispan.protostream.descriptors.AnnotationElement)3 Set (java.util.Set)2 Function (java.util.function.Function)2