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