Skip to content

Commit

Permalink
ISPN-6436 Overriding the name of an indexed property is not supported…
Browse files Browse the repository at this point in the history
… with DSL query
  • Loading branch information
anistor committed Apr 19, 2016
1 parent 457077b commit b432823
Show file tree
Hide file tree
Showing 32 changed files with 651 additions and 563 deletions.
Expand Up @@ -33,7 +33,11 @@ public ObjectFilter withParameters(Map<String, Object> namedParameters) {
if (namedParameters == null) {
throw new IllegalArgumentException("namedParameters argument cannot be null");
}
//todo validate params
for (String paramName : getParameterNames()) {
if (namedParameters.get(paramName) == null) {
throw new IllegalArgumentException("Query parameter '" + paramName + "' was not set");
}
}
return new AcceptObjectFilter<>(namedParameters, matcher, metadataAdapter, parsingResult);
}

Expand Down
Expand Up @@ -72,7 +72,7 @@ public FilterSubscriptionImpl<TypeMetadata, AttributeMetadata, AttributeId> addF
if (projection != null && projection.length != 0) {
translatedProjections = new ArrayList<>(projection.length);
for (String projectionPath : projection) {
translatedProjections.add(metadataAdapter.translatePropertyPath(StringHelper.split(projectionPath)));
translatedProjections.add(metadataAdapter.mapPropertyNamePathToFieldIdPath(StringHelper.split(projectionPath)));
}
}

Expand All @@ -90,7 +90,7 @@ public FilterSubscriptionImpl<TypeMetadata, AttributeMetadata, AttributeId> addF
// translate sort field paths
translatedSortFields = new ArrayList<>(sortFields.length);
for (SortField sortField : sortFields) {
translatedSortFields.add(metadataAdapter.translatePropertyPath(sortField.getPath().getPath()));
translatedSortFields.add(metadataAdapter.mapPropertyNamePathToFieldIdPath(sortField.getPath().getPath()));
}
}

Expand Down
Expand Up @@ -16,10 +16,10 @@ public interface MetadataAdapter<TypeMetadata, AttributeMetadata, AttributeId ex
TypeMetadata getTypeMetadata();

/**
* Transforms a String property path into an internal representation of the path which might not be String based
* Transforms a String property name path into an internal representation of the path which might not be String based
* (AttributeId is an Integer in the Protobuf case).
*/
List<AttributeId> translatePropertyPath(String[] path);
List<AttributeId> mapPropertyNamePathToFieldIdPath(String[] path);

AttributeMetadata makeChildAttributeMetadata(AttributeMetadata parentAttributeMetadata, AttributeId attribute);

Expand Down
Expand Up @@ -89,7 +89,7 @@ final class ObjectFilterImpl<TypeMetadata, AttributeMetadata, AttributeId extend
if (projection != null && projection.length != 0) {
translatedProjections = new ArrayList<>(projection.length);
for (String projectionPath : projection) {
translatedProjections.add(metadataAdapter.translatePropertyPath(StringHelper.split(projectionPath)));
translatedProjections.add(metadataAdapter.mapPropertyNamePathToFieldIdPath(StringHelper.split(projectionPath)));
}
} else {
translatedProjections = null;
Expand All @@ -108,7 +108,7 @@ final class ObjectFilterImpl<TypeMetadata, AttributeMetadata, AttributeId extend
// translate sort field paths
translatedSortFields = new ArrayList<>(sortFields.length);
for (SortField sortField : sortFields) {
translatedSortFields.add(metadataAdapter.translatePropertyPath(sortField.getPath().getPath()));
translatedSortFields.add(metadataAdapter.mapPropertyNamePathToFieldIdPath(sortField.getPath().getPath()));
}
} else {
translatedSortFields = null;
Expand Down Expand Up @@ -185,7 +185,11 @@ public ObjectFilter withParameters(Map<String, Object> namedParameters) {
if (namedParameters == null) {
throw new IllegalArgumentException("namedParameters argument cannot be null");
}
//todo validate params
for (String paramName : getParameterNames()) {
if (namedParameters.get(paramName) == null) {
throw new IllegalArgumentException("Query parameter '" + paramName + "' was not set");
}
}
return new ObjectFilterImpl<>(this, namedParameters);
}

Expand Down
@@ -1,15 +1,14 @@
package org.infinispan.objectfilter.impl;

import org.infinispan.objectfilter.impl.hql.ObjectPropertyHelper;
import org.infinispan.objectfilter.impl.hql.ProtobufEntityNamesResolver;
import org.infinispan.objectfilter.impl.hql.ProtobufPropertyHelper;
import org.infinispan.objectfilter.impl.predicateindex.ProtobufMatcherEvalContext;
import org.infinispan.protostream.SerializationContext;
import org.infinispan.protostream.WrappedMessage;
import org.infinispan.protostream.descriptors.Descriptor;
import org.infinispan.protostream.descriptors.FieldDescriptor;
import org.infinispan.protostream.descriptors.JavaType;

import java.util.ArrayList;
import java.util.List;

/**
Expand Down Expand Up @@ -54,15 +53,17 @@ protected FilterRegistry<Descriptor, FieldDescriptor, Integer> getFilterRegistry

@Override
protected MetadataAdapter<Descriptor, FieldDescriptor, Integer> createMetadataAdapter(Descriptor messageDescriptor) {
return new MetadataAdapterImpl(messageDescriptor);
return new MetadataAdapterImpl(messageDescriptor, propertyHelper);
}

private static class MetadataAdapterImpl implements MetadataAdapter<Descriptor, FieldDescriptor, Integer> {

private final Descriptor messageDescriptor;
private final ObjectPropertyHelper<Descriptor> propertyHelper;

MetadataAdapterImpl(Descriptor messageDescriptor) {
MetadataAdapterImpl(Descriptor messageDescriptor, ObjectPropertyHelper<Descriptor> propertyHelper) {
this.messageDescriptor = messageDescriptor;
this.propertyHelper = propertyHelper;
}

@Override
Expand All @@ -76,19 +77,8 @@ public Descriptor getTypeMetadata() {
}

@Override
public List<Integer> translatePropertyPath(String[] path) {
List<Integer> propPath = new ArrayList<>(path.length);
Descriptor md = messageDescriptor;
for (String prop : path) {
FieldDescriptor fd = md.findFieldByName(prop);
propPath.add(fd.getNumber());
if (fd.getJavaType() == JavaType.MESSAGE) {
md = fd.getMessageType();
} else {
md = null; // iteration is expected to stop here
}
}
return propPath;
public List<Integer> mapPropertyNamePathToFieldIdPath(String[] path) {
return (List<Integer>) propertyHelper.mapPropertyNamePathToFieldIdPath(messageDescriptor, path);
}

@Override
Expand Down
Expand Up @@ -8,7 +8,6 @@
import org.infinispan.objectfilter.impl.util.ReflectionHelper;

import java.beans.IntrospectionException;
import java.util.Arrays;
import java.util.List;

/**
Expand Down Expand Up @@ -56,15 +55,18 @@ protected FilterRegistry<Class<?>, ReflectionHelper.PropertyAccessor, String> ge

@Override
protected MetadataAdapter<Class<?>, ReflectionHelper.PropertyAccessor, String> createMetadataAdapter(Class<?> clazz) {
return new MetadataAdapterImpl(clazz);
return new MetadataAdapterImpl(clazz, propertyHelper);
}

private static class MetadataAdapterImpl implements MetadataAdapter<Class<?>, ReflectionHelper.PropertyAccessor, String> {

private final Class<?> clazz;

MetadataAdapterImpl(Class<?> clazz) {
private final ObjectPropertyHelper<Class<?>> propertyHelper;

MetadataAdapterImpl(Class<?> clazz, ObjectPropertyHelper<Class<?>> propertyHelper) {
this.clazz = clazz;
this.propertyHelper = propertyHelper;
}

@Override
Expand All @@ -78,8 +80,8 @@ public Class<?> getTypeMetadata() {
}

@Override
public List<String> translatePropertyPath(String[] path) {
return Arrays.asList(path);
public List<String> mapPropertyNamePathToFieldIdPath(String[] path) {
return (List<String>) propertyHelper.mapPropertyNamePathToFieldIdPath(clazz, path);
}

@Override
Expand Down
Expand Up @@ -23,7 +23,11 @@ public ObjectFilter withParameters(Map<String, Object> namedParameters) {
if (namedParameters == null) {
throw new IllegalArgumentException("namedParameters argument cannot be null");
}
//todo validate params
for (String paramName : getParameterNames()) {
if (namedParameters.get(paramName) == null) {
throw new IllegalArgumentException("Query parameter '" + paramName + "' was not set");
}
}
return new RejectObjectFilter<>(namedParameters, parsingResult);
}

Expand Down
Expand Up @@ -70,7 +70,7 @@ public RowPropertyHelper.RowMetadata getTypeMetadata() {
}

@Override
public List<Integer> translatePropertyPath(String[] path) {
public List<Integer> mapPropertyNamePathToFieldIdPath(String[] path) {
if (path.length > 1) {
throw new IllegalStateException("Nested attributes are not supported");
}
Expand Down
Expand Up @@ -20,7 +20,7 @@
* @author anistor@redhat.com
* @since 7.0
*/
public class FilterQueryResolverDelegate implements QueryResolverDelegate {
final class FilterQueryResolverDelegate implements QueryResolverDelegate {

private static final Log log = Logger.getMessageLogger(Log.class, FilterQueryResolverDelegate.class.getName());

Expand All @@ -40,7 +40,7 @@ protected enum Status {

protected Status status;

protected FilterQueryResolverDelegate(ObjectPropertyHelper propertyHelper) {
FilterQueryResolverDelegate(ObjectPropertyHelper propertyHelper) {
this.propertyHelper = propertyHelper;
}

Expand Down
Expand Up @@ -6,7 +6,7 @@
* @author anistor@redhat.com
* @since 7.0
*/
public interface FilterTypeDescriptor extends TypeDescriptor {
interface FilterTypeDescriptor extends TypeDescriptor {

/**
* Returns the Java type of the represented entity.
Expand Down
Expand Up @@ -3,6 +3,7 @@
import org.hibernate.hql.ast.spi.EntityNamesResolver;
import org.hibernate.hql.ast.spi.PropertyHelper;
import org.infinispan.objectfilter.impl.logging.Log;
import org.infinispan.objectfilter.impl.syntax.BooleShannonExpansion;
import org.infinispan.objectfilter.impl.util.DateHelper;
import org.infinispan.objectfilter.impl.util.StringHelper;
import org.jboss.logging.Logger;
Expand Down Expand Up @@ -164,6 +165,10 @@ public Object convertToPropertyType(String entityType, List<String> propertyPath
*/
public abstract boolean isRepeatedProperty(String entityType, String[] propertyPath);

public BooleShannonExpansion.IndexedFieldProvider getIndexedFieldProvider(TypeMetadata typeMetadata) {
return BooleShannonExpansion.IndexedFieldProvider.NO_INDEXING;
}

/**
* This is an alternative to {@link EntityNamesResolver#getClassFromName}, because the metadata may not always be a
* {@link Class}.
Expand All @@ -173,6 +178,8 @@ public Object convertToPropertyType(String entityType, List<String> propertyPath
*/
public abstract TypeMetadata getEntityMetadata(String targetTypeName);

public abstract List<?> mapPropertyNamePathToFieldIdPath(TypeMetadata type, String[] propertyPath);

/**
* Converts the given property value into the type expected by the query backend.
*
Expand Down
Expand Up @@ -10,6 +10,7 @@
import org.infinispan.protostream.descriptors.JavaType;
import org.jboss.logging.Logger;

import java.util.ArrayList;
import java.util.List;

/**
Expand All @@ -32,6 +33,22 @@ public Descriptor getEntityMetadata(String targetTypeName) {
return serializationContext.getMessageDescriptor(targetTypeName);
}

@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 {
md = null; // iteration is expected to stop here
}
}
return translatedPath;
}

@Override
public Class<?> getPrimitivePropertyType(String entityType, String[] propertyPath) {
FieldDescriptor field = getField(entityType, propertyPath);
Expand Down Expand Up @@ -162,7 +179,7 @@ public boolean isRepeatedProperty(String entityType, String[] propertyPath) {
public Object convertToPropertyType(String entityType, List<String> propertyPath, String value) {
FieldDescriptor field = getField(entityType, propertyPath.toArray(new String[propertyPath.size()]));

//todo [anistor] this is just for remote query because booleans and enums are handled as integers for historical reasons.
//todo [anistor] this is just for remote query because enums are handled as integers for historical reasons.
if (field.getJavaType() == JavaType.BOOLEAN) {
try {
return Integer.parseInt(value) != 0;
Expand Down
Expand Up @@ -4,12 +4,14 @@
import org.infinispan.objectfilter.impl.util.ReflectionHelper;

import java.beans.IntrospectionException;
import java.util.Arrays;
import java.util.List;

/**
* @author anistor@redhat.com
* @since 7.0
*/
public final class ReflectionPropertyHelper extends ObjectPropertyHelper<Class<?>> {
public class ReflectionPropertyHelper extends ObjectPropertyHelper<Class<?>> {

public ReflectionPropertyHelper(EntityNamesResolver entityNamesResolver) {
super(entityNamesResolver);
Expand All @@ -20,21 +22,21 @@ public Class<?> getEntityMetadata(String targetTypeName) {
return entityNamesResolver.getClassFromName(targetTypeName);
}

@Override
public List<?> mapPropertyNamePathToFieldIdPath(Class<?> type, String[] propertyPath) {
return Arrays.asList(propertyPath);
}

@Override
public Class<?> getPrimitivePropertyType(String entityType, String[] propertyPath) {
Class<?> type = entityNamesResolver.getClassFromName(entityType);
if (type == null) {
throw new IllegalStateException("Unknown entity name " + entityType);
}
Class<?> entityClass = getEntityClass(entityType);

try {
Class<?> propType = getPropertyAccessor(type, propertyPath).getPropertyType();
Class<?> propType = getPropertyAccessor(entityClass, propertyPath).getPropertyType();
if (propType.isEnum()) {
return propType;
}
if (primitives.containsKey(propType)) {
return primitives.get(propType);
}
return primitives.get(propType);
} catch (IntrospectionException e) {
// ignored
}
Expand All @@ -43,13 +45,10 @@ public Class<?> getPrimitivePropertyType(String entityType, String[] propertyPat

@Override
public boolean hasEmbeddedProperty(String entityType, String[] propertyPath) {
Class<?> entity = entityNamesResolver.getClassFromName(entityType);
if (entity == null) {
throw new IllegalStateException("Unknown entity name " + entityType);
}
Class<?> entityClass = getEntityClass(entityType);

try {
Class<?> propType = getPropertyAccessor(entity, propertyPath).getPropertyType();
Class<?> propType = getPropertyAccessor(entityClass, propertyPath).getPropertyType();
return propType != null && !propType.isEnum() && !primitives.containsKey(propType);
} catch (IntrospectionException e) {
return false;
Expand All @@ -58,12 +57,10 @@ public boolean hasEmbeddedProperty(String entityType, String[] propertyPath) {

@Override
public boolean isRepeatedProperty(String entityType, String[] propertyPath) {
Class<?> entity = entityNamesResolver.getClassFromName(entityType);
if (entity == null) {
throw new IllegalStateException("Unknown entity name " + entityType);
}
Class<?> entityClass = getEntityClass(entityType);

try {
ReflectionHelper.PropertyAccessor a = ReflectionHelper.getAccessor(entity, propertyPath[0]);
ReflectionHelper.PropertyAccessor a = ReflectionHelper.getAccessor(entityClass, propertyPath[0]);
if (a.isMultiple()) {
return true;
}
Expand All @@ -81,19 +78,24 @@ public boolean isRepeatedProperty(String entityType, String[] propertyPath) {

@Override
public boolean hasProperty(String entityType, String[] propertyPath) {
Class<?> entity = entityNamesResolver.getClassFromName(entityType);
if (entity == null) {
throw new IllegalStateException("Unknown entity name " + entityType);
}
Class<?> entityClass = getEntityClass(entityType);

try {
Class<?> propType = getPropertyAccessor(entity, propertyPath).getPropertyType();
Class<?> propType = getPropertyAccessor(entityClass, propertyPath).getPropertyType();
return propType != null;
} catch (IntrospectionException e) {
return false;
}
}

private Class<?> getEntityClass(String entityType) {
Class<?> entityClass = getEntityMetadata(entityType);
if (entityClass == null) {
throw new IllegalStateException("Unknown entity name " + entityType);
}
return entityClass;
}

private ReflectionHelper.PropertyAccessor getPropertyAccessor(Class<?> entityClass, String[] propertyPath) throws IntrospectionException {
ReflectionHelper.PropertyAccessor accessor = ReflectionHelper.getAccessor(entityClass, propertyPath[0]);
for (int i = 1; i < propertyPath.length; i++) {
Expand Down

0 comments on commit b432823

Please sign in to comment.