Skip to content

Commit

Permalink
remove public find methods that receive Filter parameter, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
agrgr committed Nov 8, 2023
1 parent 9a283aa commit 0f91c05
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 219 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@
import com.aerospike.client.query.ResultSet;
import org.springframework.data.aerospike.core.model.GroupedEntities;
import org.springframework.data.aerospike.core.model.GroupedKeys;
import org.springframework.data.aerospike.query.Qualifier;
import org.springframework.data.aerospike.repository.query.Query;
import org.springframework.data.domain.Sort;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.lang.Nullable;

import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -794,49 +792,6 @@ <T, S> List<?> findByIdsUsingQuery(Collection<?> ids, Class<T> entityClass, Clas
*/
<T> Stream<T> find(Query query, Class<T> targetClass, String setName);

/**
* Find all documents in the given entityClass's set using provided {@link Query}.
*
* @param query The {@link Query} to filter results. Constructed using a {@link Qualifier} that can contain
* other qualifiers. Must not be {@literal null}. If filter param is null and qualifier has
* {@link Qualifier#getExcludeFilter()} == false, secondary index filter is built based on the
* first processed qualifier.
* @param entityClass The class to extract the Aerospike set from and to map the entity to. Must not be
* {@literal null}.
* @param filter Secondary index filter.
* @return Stream of entities.
*/
<T> Stream<T> find(Query query, Class<T> entityClass, @Nullable Filter filter);

/**
* Find all documents in the given entityClass's set using provided {@link Query}.
*
* @param query The {@link Query} to filter results. Constructed using a {@link Qualifier} that can contain
* other qualifiers. Must not be {@literal null}. If filter param is null and qualifier has
* {@link Qualifier#getExcludeFilter()} == false, secondary index filter is built based on the
* first processed qualifier.
* @param entityClass The class to extract the Aerospike set from and to map the entity to. Must not be
* {@literal null}.
* @param targetClass The class to map the entity to. Must not be {@literal null}.
* @param filter Secondary index filter.
* @return Stream of entities.
*/
<T, S> Stream<S> find(Query query, Class<T> entityClass, Class<S> targetClass, @Nullable Filter filter);

/**
* Find all documents in the given set using provided {@link Query}.
*
* @param query The {@link Query} to filter results. Constructed using a {@link Qualifier} that can contain
* other qualifiers. Must not be {@literal null}. If filter param is null and qualifier has
* {@link Qualifier#getExcludeFilter()} == false, secondary index filter is built based on the
* first processed qualifier.
* @param targetClass The class to map the entity to. Must not be {@literal null}.
* @param setName Set name to find the documents in.
* @param filter Secondary index filter.
* @return Stream of entities.
*/
<T> Stream<T> find(Query query, Class<T> targetClass, String setName, @Nullable Filter filter);

/**
* Find all documents in the given entityClass's set and map them to the given class type.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
import static org.springframework.data.aerospike.core.TemplateUtils.queryCriteriaIsNotNull;
import static org.springframework.data.aerospike.query.QualifierUtils.getOneIdQualifier;
import static org.springframework.data.aerospike.query.QualifierUtils.validateQualifiers;
import static org.springframework.data.aerospike.utility.Utils.allArrayElementsAreNull;

/**
* Primary implementation of {@link AerospikeOperations}.
Expand Down Expand Up @@ -668,13 +667,13 @@ public <T, S> S findById(Object id, Class<T> entityClass, Class<S> targetClass,
return (S) findByIdUsingQuery(id, entityClass, targetClass, setName, null);
}

private Record getRecord(AerospikePersistentEntity<?> entity, Key key, Qualifier criteria) {
private Record getRecord(AerospikePersistentEntity<?> entity, Key key, Qualifier qualifier) {
Record aeroRecord;
if (entity.isTouchOnRead()) {
Assert.state(!entity.hasExpirationProperty(), "Touch on read is not supported for expiration property");
aeroRecord = getAndTouch(key, entity.getExpiration(), null);
aeroRecord = getAndTouch(key, entity.getExpiration(), null, null);
} else {
Policy policy = getPolicyFilterExp(criteria);
Policy policy = getPolicyFilterExp(qualifier);
aeroRecord = getAerospikeClient().get(policy, key);
}
return aeroRecord;
Expand All @@ -696,14 +695,14 @@ private Key[] getKeys(Collection<?> ids, String setName) {
}

private <S> Object getRecordMapToTargetClass(AerospikePersistentEntity<?> entity, Key key, Class<S> targetClass,
Qualifier criteria) {
Qualifier qualifier) {
Record aeroRecord;
String[] binNames = getBinNamesFromTargetClass(targetClass);
if (entity.isTouchOnRead()) {
Assert.state(!entity.hasExpirationProperty(), "Touch on read is not supported for expiration property");
aeroRecord = getAndTouch(key, entity.getExpiration(), binNames, criteria);
aeroRecord = getAndTouch(key, entity.getExpiration(), binNames, qualifier);
} else {
Policy policy = getPolicyFilterExp(criteria);
Policy policy = getPolicyFilterExp(qualifier);
aeroRecord = getAerospikeClient().get(policy, key, binNames);
}
return mapToEntity(key, targetClass, aeroRecord);
Expand All @@ -729,12 +728,12 @@ private Policy getPolicyFilterExp(Qualifier... qualifiers) {
return null;
}

private Record getAndTouch(Key key, int expiration, String[] binNames, Qualifier... qualifiers) {
private Record getAndTouch(Key key, int expiration, String[] binNames, Qualifier qualifier) {
WritePolicyBuilder writePolicyBuilder = WritePolicyBuilder.builder(client.getWritePolicyDefault())
.expiration(expiration);

if (qualifiers != null && qualifiers.length > 0) {
writePolicyBuilder.filterExp(queryEngine.getFilterExpressionsBuilder().build(qualifiers));
if (qualifier != null) {
writePolicyBuilder.filterExp(queryEngine.getFilterExpressionsBuilder().build(new Qualifier[]{qualifier}));
}
WritePolicy writePolicy = writePolicyBuilder.build();

Expand Down Expand Up @@ -816,15 +815,15 @@ public <T, S> Object findByIdUsingQuery(Object id, Class<T> entityClass, Class<S
Assert.notNull(entityClass, "Entity class must not be null!");
Assert.notNull(setName, "Set name must not be null!");

Qualifier criteria = queryCriteriaIsNotNull(query) ? query.getCriteria().getCriteriaObject() : null;
Qualifier qualifier = queryCriteriaIsNotNull(query) ? query.getCriteria().getCriteriaObject() : null;
try {
AerospikePersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(entityClass);
Key key = getKey(id, setName);

if (targetClass != null && targetClass != entityClass) {
return getRecordMapToTargetClass(entity, key, targetClass, criteria);
return getRecordMapToTargetClass(entity, key, targetClass, qualifier);
}
return mapToEntity(key, entityClass, getRecord(entity, key, criteria));
return mapToEntity(key, entityClass, getRecord(entity, key, qualifier));
} catch (AerospikeException e) {
throw translateError(e);
}
Expand Down Expand Up @@ -895,21 +894,9 @@ public <T> Stream<T> find(Query query, Class<T> targetClass, String setName) {
return findUsingQueryWithPostProcessing(setName, targetClass, query);
}

@Override
public <T> Stream<T> find(Query query, Class<T> entityClass, Filter filter) {
return find(query, entityClass, getSetName(entityClass), filter);
}

@Override
public <T, S> Stream<S> find(Query query, Class<T> entityClass, Class<S> targetClass, Filter filter) {
return findRecordsUsingQualifiers(getSetName(entityClass), targetClass, filter)
.map(keyRecord -> mapToEntity(keyRecord, targetClass));
}

@Override
public <T> Stream<T> find(Query query, Class<T> targetClass, String setName, Filter filter) {
Qualifier criteria = queryCriteriaIsNotNull(query) ? query.getCriteria().getCriteriaObject() : null;
return findRecordsUsingQualifiers(setName, targetClass, filter, criteria)
private <T> Stream<T> find(Query query, Class<T> targetClass, String setName, Filter filter) {
Qualifier qualifier = queryCriteriaIsNotNull(query) ? query.getCriteria().getCriteriaObject() : null;
return findRecordsUsingQualifier(setName, targetClass, filter, qualifier)
.map(keyRecord -> mapToEntity(keyRecord, targetClass));
}

Expand Down Expand Up @@ -951,21 +938,20 @@ public <T> Stream<T> findAll(Sort sort, long offset, long limit, Class<T> target
Assert.notNull(setName, "Set name must not be null!");
Assert.notNull(targetClass, "Target class must not be null!");

return findUsingQualifierWithPostProcessing(setName, targetClass, sort, offset, limit,
null, null);
return findUsingQualifierWithPostProcessing(setName, targetClass, sort, offset, limit, null);
}

private <T> Stream<T> findUsingQueryWithPostProcessing(String setName, Class<T> targetClass, Query query) {
verifyUnsortedWithOffset(query.getSort(), query.getOffset());
Stream<T> results = findUsingQualifiersWithDistinctPredicate(setName, targetClass,
Stream<T> results = findUsingQueryWithDistinctPredicate(setName, targetClass,
getDistinctPredicate(query), query);
return applyPostProcessingOnResults(results, query);
}

private <T> Stream<T> findUsingQualifiersWithDistinctPredicate(String setName, Class<T> targetClass,
Predicate<KeyRecord> distinctPredicate,
Query query) {
return findRecordsUsingQualifiers(setName, targetClass, null, query.getCriteria().getCriteriaObject())
private <T> Stream<T> findUsingQueryWithDistinctPredicate(String setName, Class<T> targetClass,
Predicate<KeyRecord> distinctPredicate,
Query query) {
return findRecordsUsingQualifier(setName, targetClass, null, query.getCriteria().getCriteriaObject())
.filter(distinctPredicate)
.map(keyRecord -> mapToEntity(keyRecord, targetClass));
}
Expand All @@ -988,8 +974,7 @@ public <T> Stream<T> findInRange(long offset, long limit, Sort sort,
Assert.notNull(targetClass, "Target class must not be null!");
Assert.notNull(setName, "Set name must not be null!");

return findUsingQualifierWithPostProcessing(setName, targetClass, sort, offset, limit,
null, null);
return findUsingQualifierWithPostProcessing(setName, targetClass, sort, offset, limit, null);
}

@Override
Expand Down Expand Up @@ -1072,7 +1057,7 @@ private Stream<KeyRecord> findRecordsUsingQuery(String setName, Query query) {
Assert.notNull(setName, "Set name must not be null!");

Qualifier qualifier = query.getCriteria().getCriteriaObject();
return findRecordsUsingQualifiers(setName, null, null, qualifier);
return findRecordsUsingQualifier(setName, null, null, qualifier);
}

@Override
Expand Down Expand Up @@ -1255,11 +1240,10 @@ private Record putAndGetHeader(AerospikeWriteData data, WritePolicy policy, bool

@SuppressWarnings("SameParameterValue")
private <T> Stream<T> findUsingQualifierWithPostProcessing(String setName, Class<T> targetClass, Sort sort,
long offset, long limit, Filter filter,
Qualifier qualifier) {
long offset, long limit, Qualifier qualifier) {
verifyUnsortedWithOffset(sort, offset);
Query query = qualifier != null ? new Query(qualifier) : null;
Stream<T> results = find(query, targetClass, setName, filter);
Stream<T> results = find(query, targetClass, setName, null);
return applyPostProcessingOnResults(results, sort, offset, limit);
}

Expand Down Expand Up @@ -1294,26 +1278,26 @@ private <T> Stream<T> applyPostProcessingOnResults(Stream<T> results, Sort sort,
return results;
}

private <T> Stream<KeyRecord> findRecordsUsingQualifiers(String setName, Class<T> targetClass, Filter filter,
Qualifier... qualifiers) {
if (qualifiers != null && qualifiers.length > 0 && !allArrayElementsAreNull(qualifiers)) {
validateQualifiers(qualifiers);
private <T> Stream<KeyRecord> findRecordsUsingQualifier(String setName, Class<T> targetClass, Filter filter,
Qualifier qualifier) {
if (qualifier != null) {
validateQualifiers(qualifier);

Qualifier idQualifier = getOneIdQualifier(qualifiers);
Qualifier idQualifier = getOneIdQualifier(qualifier);
if (idQualifier != null) {
// a special flow if there is id given
return findByIdsWithoutMapping(getIdValue(idQualifier), setName, targetClass,
excludeIdQualifier(qualifiers)).stream();
excludeIdQualifier(qualifier)).stream();
}
}

KeyRecordIterator recIterator;

if (targetClass != null) {
String[] binNames = getBinNamesFromTargetClass(targetClass);
recIterator = queryEngine.select(namespace, setName, binNames, filter, qualifiers);
recIterator = queryEngine.select(namespace, setName, binNames, filter, qualifier);
} else {
recIterator = queryEngine.select(namespace, setName, filter, qualifiers);
recIterator = queryEngine.select(namespace, setName, filter, qualifier);
}

return StreamUtils.createStreamFromIterator(recIterator)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@
import com.aerospike.client.AerospikeException;
import com.aerospike.client.cdt.CTX;
import com.aerospike.client.policy.WritePolicy;
import com.aerospike.client.query.Filter;
import com.aerospike.client.query.IndexCollectionType;
import com.aerospike.client.query.IndexType;
import com.aerospike.client.reactor.IAerospikeReactorClient;
import org.springframework.data.aerospike.core.model.GroupedEntities;
import org.springframework.data.aerospike.core.model.GroupedKeys;
import org.springframework.data.aerospike.query.Qualifier;
import org.springframework.data.aerospike.repository.query.Query;
import org.springframework.data.domain.Sort;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.lang.Nullable;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

Expand Down Expand Up @@ -765,49 +762,6 @@ <T, S> Flux<?> findByIdsUsingQuery(Collection<?> ids, Class<T> entityClass, Clas
*/
<T> Flux<T> find(Query query, Class<T> targetClass, String setName);

/**
* Find all documents in the given entityClass's set using provided {@link Query}.
*
* @param query The {@link Query} to filter results. Constructed using a {@link Qualifier} that can contain
* other qualifiers. Must not be {@literal null}. If filter param is null and qualifier has
* {@link Qualifier#getExcludeFilter()} == false, secondary index filter is built based on the
* first processed qualifier.
* @param entityClass The class to extract the Aerospike set from and to map the entity to. Must not be
* {@literal null}.
* @param filter Secondary index filter.
* @return Stream of entities.
*/
<T> Flux<T> find(Query query, Class<T> entityClass, @Nullable Filter filter);

/**
* Find all documents in the given entityClass's set using provided {@link Query}.
*
* @param query The {@link Query} to filter results. Constructed using a {@link Qualifier} that can contain
* other qualifiers. Must not be {@literal null}. If filter param is null and qualifier has
* {@link Qualifier#getExcludeFilter()} == false, secondary index filter is built based on the
* first processed qualifier.
* @param entityClass The class to extract the Aerospike set from and to map the entity to. Must not be
* {@literal null}.
* @param targetClass The class to map the entity to. Must not be {@literal null}.
* @param filter Secondary index filter.
* @return Stream of entities.
*/
<T, S> Flux<S> find(Query query, Class<T> entityClass, Class<S> targetClass, @Nullable Filter filter);

/**
* Find all documents in the given set using provided {@link Query}.
*
* @param query The {@link Query} to filter results. Constructed using a {@link Qualifier} that can contain
* other qualifiers. Must not be {@literal null}. If filter param is null and qualifier has
* {@link Qualifier#getExcludeFilter()} == false, secondary index filter is built based on the
* first processed qualifier.
* @param targetClass The class to map the entity to. Must not be {@literal null}.
* @param setName Set name to find the documents in.
* @param filter Secondary index filter.
* @return Stream of entities.
*/
<T> Flux<T> find(Query query, Class<T> targetClass, String setName, @Nullable Filter filter);

/**
* Reactively find all documents in the given entityClass's set and map them to the given class type.
*
Expand Down
Loading

0 comments on commit 0f91c05

Please sign in to comment.