-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve cacheability of filters #871
base: development
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,20 +38,23 @@ | |
import java.text.DateFormat; | ||
import java.text.ParseException; | ||
import java.util.Collection; | ||
import java.util.Comparator; | ||
import java.util.Date; | ||
import java.util.stream.Collectors; | ||
|
||
import static ubic.gemma.persistence.util.PropertyMappingUtils.formProperty; | ||
|
||
/** | ||
* Holds the necessary information to filter an entity with a property, operator and right-hand side value. | ||
* | ||
* <p> | ||
* Clauses and sub-clauses are always sorted by objectAlias, propertyName, operator and requiredValue. A clause is | ||
* sorted by its first element, if any. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not correct, a clause is sorted lexicographically and a sub-clause as mentioned. |
||
* @author tesarst | ||
* @author poirigui | ||
*/ | ||
@Value | ||
@EqualsAndHashCode(of = { "objectAlias", "operator", "requiredValue" }) | ||
public class Filter implements PropertyMapping { | ||
@EqualsAndHashCode(of = { "objectAlias", "propertyName", "operator", "requiredValue" }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be included immediatly in the dev branch and next patch release. |
||
public class Filter implements PropertyMapping, Comparable<Filter> { | ||
|
||
/** | ||
* This is only the date part of the ISO 8601 standard. | ||
|
@@ -63,6 +66,25 @@ public class Filter implements PropertyMapping { | |
*/ | ||
private static final ConfigurableConversionService conversionService = new GenericConversionService(); | ||
|
||
private static final Comparator<Filter> comparator = Comparator | ||
.comparing( Filter::getObjectAlias, Comparator.nullsFirst( Comparator.naturalOrder() ) ) | ||
.thenComparing( Filter::getPropertyName ) | ||
.thenComparing( Filter::getOperator ) | ||
.thenComparing( Filter::getComparableRequiredValue, Comparator.nullsLast( Comparator.naturalOrder() ) ); | ||
|
||
@Nullable | ||
private Comparable<Object> getComparableRequiredValue() { | ||
Object requiredValue = this.requiredValue; | ||
if ( operator.requiredType != null && Collection.class.isAssignableFrom( operator.requiredType ) ) { | ||
// unpack the first element of the collection | ||
if ( requiredValue != null ) { | ||
requiredValue = ( ( Collection<?> ) requiredValue ).iterator().next(); | ||
} | ||
} | ||
//noinspection unchecked | ||
return ( Comparable<Object> ) requiredValue; | ||
} | ||
|
||
private static <T> void addConverter( Class<T> targetClass, Converter<String, T> converter, Converter<T, String> reverseConverter ) { | ||
conversionService.addConverter( String.class, targetClass, converter ); | ||
conversionService.addConverter( targetClass, String.class, reverseConverter ); | ||
|
@@ -253,6 +275,11 @@ private Filter( @Nullable String objectAlias, String propertyName, Class<?> prop | |
this.checkTypeCorrect(); | ||
} | ||
|
||
@Override | ||
public int compareTo( Filter filter ) { | ||
return comparator.compare( this, filter ); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return toString( false ); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might not be worth to do this for every single filter.
Maybe having a list of properties that are expensive to filter by would be interesting.