Skip to content

Commit

Permalink
add support for findByQualifiers() for reactive flow, updates for not…
Browse files Browse the repository at this point in the history
… setting statement filter if EXCLUDE_FILTER == true and for reusing Qualifiers, cleanup
  • Loading branch information
agrgr committed Oct 11, 2023
1 parent b6cbd15 commit 4f8ef8b
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 236 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -558,12 +558,12 @@ <T> void createIndex(Class<T> entityClass, String indexName, String binName,
boolean indexExists(String indexName);

/**
* Find all documents in the given entityClass's set using provided qualifiers.
* Find all documents in the given entityClass's set using provided {@link Qualifier}s.
*
* @param entityClass The class to extract the Aerospike set from. Must not be {@literal null}.
* @param filter Secondary index filter.
* @param qualifiers Qualifiers to build filter expressions from.
* @return Stream of entities
* @param qualifiers Qualifiers to build filter expressions from. Must not be {@literal null}.
* @return Stream of entities.
*/
<T> Stream<?> findAllUsingQuery(Class<T> entityClass, Filter filter, Qualifier... qualifiers);
<T> Stream<T> findAllUsingQuery(Class<T> entityClass, Filter filter, Qualifier... qualifiers);
}
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ <T> Mono<Void> createIndex(Class<T> entityClass, String indexName, String binNam
*
* @param entityClass The class to extract the Aerospike set from. Must not be {@literal null}.
* @param filter Secondary index filter.
* @param qualifiers Qualifiers to build filter expressions from.
* @param qualifiers Qualifiers to build filter expressions from. Must not be {@literal null}.
* @return Flux of entities.
*/
<T> Flux<T> findAllUsingQuery(Class<T> entityClass, Filter filter, Qualifier... qualifiers);
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,9 @@ public Set<Entry<String, Object>> entrySet() {
public String toString() {
if (!StringUtils.hasLength(getField()) && StringUtils.hasLength(getMetadataField().toString())) {
return String.format("%s:%s:%s:%s", getField(), getOperation(), getValue1(), getValue2());
} else {
return String.format("(metadata)%s:%s:%s:%s", getMetadataField().toString(),
getOperation(), getValue1(), getValue2());
}
return String.format("(metadata)%s:%s:%s:%s", getMetadataField().toString(),
getOperation(), getValue1(), getValue2());
}

public static class QualifierRegexpBuilder {
Expand Down Expand Up @@ -383,21 +382,21 @@ public Map<String, Object> buildMap() {
}
}

public static void validateQualifier(Qualifier qualifier) {
public static void validate(Qualifier qualifier) {
// metadata query
if (qualifier.getMetadataField() != null && qualifier.getField() == null) {
FilterOperation operation = qualifier.getOperation();
switch (operation) {
case EQ, NOTEQ, LT, LTEQ, GT, GTEQ ->
Assert.isTrue(qualifier.getValue1AsObj() != null && qualifier.getValue1AsObj() instanceof Long,
operation.name() + ": value1 is expected to be set as Long");
case EQ, NOTEQ, LT, LTEQ, GT, GTEQ -> Assert.isTrue(qualifier.getValue1AsObj() instanceof Long,
operation.name() + ": value1 is expected to be set as Long");
case BETWEEN -> {
Assert.isTrue(qualifier.getValue1AsObj() != null && qualifier.getValue1AsObj() instanceof Long,
Assert.isTrue(qualifier.getValue1AsObj() instanceof Long,
"BETWEEN: value1 is expected to be set as Long");
Assert.isTrue(qualifier.getValue2AsObj() != null && qualifier.getValue2AsObj() instanceof Long,
Assert.isTrue(qualifier.getValue2AsObj() instanceof Long,
"BETWEEN: value2 is expected to be set as Long");
}
case NOT_IN, IN -> Assert.isTrue(qualifier.getValue1AsObj() instanceof Collection
&& (!((Collection<Object>) qualifier.getValue1AsObj()).isEmpty())
&& ((Collection<Object>) qualifier.getValue1AsObj()).toArray()[0] instanceof Long,
operation.name() + ": value1 is expected to be a non-empty Collection<Long>");
default -> throw new IllegalArgumentException("Operation " + operation + " cannot be applied to " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,6 @@ && isIndexedBin(stmt, innerQualifier)) {
if (filter != null) {
stmt.setFilter(filter);
qualifier.setQueryAsFilter(true);
/* If this was the only qualifier, we do not need to do anymore work, just return
* the query iterator.
*/
if (qualifiers.length == 1) {
return;
}
break; // the first processed filter becomes statement filter
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public interface AerospikeRepository<T, ID> extends PagingAndSortingRepository<T
* Each qualifier itself may contain internal qualifiers and combine them using either {@link FilterOperation#AND}
* or {@link FilterOperation#OR}.
*
* @param qualifiers One or more qualifiers representing expressions.
* @param qualifiers One or more qualifiers representing expressions. Must not be {@literal null}.
* @return Iterable of entities.
*/
Iterable<T> findByQualifiers(Qualifier... qualifiers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public interface ReactiveAerospikeRepository<T, ID> extends ReactiveCrudReposito
* Each qualifier itself may contain internal qualifiers and combine them using either {@link FilterOperation#AND}
* or {@link FilterOperation#OR}.
*
* @param qualifiers One or more qualifiers representing expressions.
* @param qualifiers One or more qualifiers representing expressions. Must not be {@literal null}.
* @return Flux of entities.
*/
Flux<T> findByQualifiers(Qualifier... qualifiers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@
*/
package org.springframework.data.aerospike.repository.query;

import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import org.springframework.beans.BeanUtils;
import org.springframework.data.aerospike.query.FilterOperation;
import org.springframework.data.aerospike.query.Qualifier;
import org.springframework.data.repository.query.ParameterAccessor;
import org.springframework.data.repository.query.ParametersParameterAccessor;
Expand All @@ -33,11 +29,8 @@
import org.springframework.util.ClassUtils;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
* @author Peter Milne
Expand Down Expand Up @@ -176,54 +169,4 @@ protected Object runIdQuery(Class<?> sourceClass, Class<?> targetClass, Object i

abstract Object findByIds(Iterable<?> iterable, Class<?> sourceClass, Class<?> targetClass,
Qualifier... qualifiers);

protected static List<MetadataQueryParams> getMetadataQueryParameters(@NonNull Object[] parameters) {
List<MetadataQueryParams> paramsList = new ArrayList<>();
for (int i = 0; i < parameters.length; i++) {
if (parameters[i] instanceof CriteriaDefinition.AerospikeMetadata && parameters.length > i + 2) {
MetadataQueryParams metadataQueryParams = new MetadataQueryParams();
getMetadataFieldParameter(parameters[i]).ifPresent(metadataQueryParams::setField);
getOperationParameter(parameters[i + 1]).ifPresent(metadataQueryParams::setOperation);
getValuesParameter(parameters[i + 2]).ifPresent(metadataQueryParams::setValues);
paramsList.add(metadataQueryParams);
i += 2;
}
}

return paramsList;
}

protected static Optional<CriteriaDefinition.AerospikeMetadata> getMetadataFieldParameter(@NonNull Object parameter) {
if (parameter instanceof CriteriaDefinition.AerospikeMetadata) {
return Optional.of((CriteriaDefinition.AerospikeMetadata) parameter);
}
return Optional.empty();
}

protected static Optional<FilterOperation> getOperationParameter(@NonNull Object parameter) {
if (parameter instanceof FilterOperation) {
return Optional.of((FilterOperation) parameter);
}
return Optional.empty();
}

protected static Optional<List<Long>> getValuesParameter(@NonNull Object parameter) {
if (parameter instanceof long[]) {
return Optional.of(Arrays.stream((long[]) parameter).boxed().toList());
} else if (parameter instanceof Long[]) {
return Optional.of(List.of((Long) parameter));
} else if (parameter instanceof Long) {
return Optional.of(List.of((Long) parameter));
}
return Optional.empty();
}

@Setter
@Getter
public static class MetadataQueryParams {

CriteriaDefinition.AerospikeMetadata field;
FilterOperation operation;
List<Long> values;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,12 @@ public boolean indexExists(String indexName) {

@Override
public Iterable<T> findByQualifiers(Qualifier... qualifiers) {
Assert.notNull(qualifiers, "Qualifiers must not be null");

Arrays.stream(qualifiers).forEach(qualifier -> {
qualifier.setExcludeFilter(true);
Qualifier.validateQualifier(qualifier);
Qualifier.validate(qualifier);
});
return (Iterable<T>) operations.findAllUsingQuery(entityInformation.getJavaType(), null, qualifiers).toList();
return operations.findAllUsingQuery(entityInformation.getJavaType(), null, qualifiers).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ public void deleteIndex(Class<T> domainType, String indexName) {

@Override
public Flux<T> findByQualifiers(Qualifier... qualifiers) {
Arrays.stream(qualifiers).forEach(Qualifier::validateQualifier);
Assert.notNull(qualifiers, "Qualifiers must not be null");

if (qualifiers == null || qualifiers.length == 0) return Flux.empty();
Arrays.stream(qualifiers).forEach(Qualifier::validate);
return operations.findAllUsingQuery(entityInformation.getJavaType(), null, qualifiers);
}
}

0 comments on commit 4f8ef8b

Please sign in to comment.