Skip to content

Commit

Permalink
align public "find" APIs to use Query instead of Qualifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
agrgr committed Nov 7, 2023
1 parent 479b1bb commit dd1e1bd
Show file tree
Hide file tree
Showing 13 changed files with 284 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -712,11 +712,11 @@ public interface AerospikeOperations {
* @param id The id of the document to find. Must not be {@literal null}.
* @param entityClass The class to extract the Aerospike set from. Must not be {@literal null}.
* @param targetClass The class to map the document to.
* @param qualifiers {@link Qualifier}s provided to build a filter Expression for the query. Optional argument.
* @param query {@link Query} provided to build a filter expression. Optional argument.
* @return The document from Aerospike, returned document will be mapped to targetClass's type.
*/
<T, S> Object findByIdUsingQualifiers(Object id, Class<T> entityClass, Class<S> targetClass,
Qualifier... qualifiers);
<T, S> Object findByIdUsingQuery(Object id, Class<T> entityClass, Class<S> targetClass,
Query query);

/**
* Find document by providing id with a given set name.
Expand All @@ -728,11 +728,11 @@ <T, S> Object findByIdUsingQualifiers(Object id, Class<T> entityClass, Class<S>
* {@literal null}.
* @param targetClass The class to map the document to.
* @param setName Set name to find the document from.
* @param qualifiers {@link Qualifier}s provided to build a filter Expression for the query. Optional argument.
* @param query {@link Query} provided to build a filter expression. Optional argument.
* @return The document from Aerospike, returned document will be mapped to targetClass's type.
*/
<T, S> Object findByIdUsingQualifiers(Object id, Class<T> entityClass, Class<S> targetClass, String setName,
Qualifier... qualifiers);
<T, S> Object findByIdUsingQuery(Object id, Class<T> entityClass, Class<S> targetClass, String setName,
Query query);

/**
* Find documents by providing multiple ids, set name will be determined by the given entityClass.
Expand All @@ -742,12 +742,11 @@ <T, S> Object findByIdUsingQualifiers(Object id, Class<T> entityClass, Class<S>
* @param ids The ids of the documents to find. Must not be {@literal null}.
* @param entityClass The class to extract the Aerospike set from. Must not be {@literal null}.
* @param targetClass The class to map the document to.
* @param qualifiers {@link Qualifier}s provided to build a filter Expression for the query. Optional argument.
* @param query {@link Query} provided to build a filter expression. Optional argument.
* @return The documents from Aerospike, returned documents will be mapped to targetClass's type, if no document
* exists, an empty list is returned.
*/
<T, S> List<?> findByIdsUsingQualifiers(Collection<?> ids, Class<T> entityClass, Class<S> targetClass,
Qualifier... qualifiers);
<T, S> List<?> findByIdsUsingQuery(Collection<?> ids, Class<T> entityClass, Class<S> targetClass, Query query);

/**
* Find documents by providing multiple ids with a given set name.
Expand All @@ -759,12 +758,12 @@ <T, S> List<?> findByIdsUsingQualifiers(Collection<?> ids, Class<T> entityClass,
* {@literal null}.
* @param targetClass The class to map the document to.
* @param setName Set name to find the document from.
* @param qualifiers {@link Qualifier}s provided to build a filter Expression for the query. Optional argument.
* @param query {@link Query} provided to build a filter expression. Optional argument.
* @return The documents from Aerospike, returned documents will be mapped to targetClass's type, if no document
* exists, an empty list is returned.
*/
<T, S> List<?> findByIdsUsingQualifiers(Collection<?> ids, Class<T> entityClass, Class<S> targetClass,
String setName, Qualifier... qualifiers);
<T, S> List<?> findByIdsUsingQuery(Collection<?> ids, Class<T> entityClass, Class<S> targetClass, String setName,
Query query);

/**
* Find documents in the given entityClass's set using a query and map them to the given class type.
Expand Down Expand Up @@ -797,49 +796,47 @@ <T, S> List<?> findByIdsUsingQualifiers(Collection<?> ids, Class<T> entityClass,
<T> Stream<T> find(Query query, Class<T> targetClass, String setName);

/**
* Find all documents in the given entityClass's set using provided {@link Qualifier}.
* Find all documents in the given entityClass's set using provided {@link Query}.
*
* @param query Query to build filter expression from. 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.
* @param qualifier Qualifier to build filter expressions from. 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.
* @return Stream of entities.
*/
<T> Stream<T> findUsingQualifier(Class<T> entityClass, @Nullable Filter filter, Qualifier qualifier);
<T> Stream<T> find(Query query, Class<T> entityClass, @Nullable Filter filter);

/**
* Find all documents in the given entityClass's set using provided {@link Qualifier}.
* Find all documents in the given entityClass's set using provided {@link Query}.
*
* @param query Query to build filter expression from. 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.
* @param qualifier Qualifier to build filter expressions from. 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.
* @return Stream of entities.
*/
<T, S> Stream<?> findUsingQualifier(Class<T> entityClass, Class<S> targetClass, @Nullable Filter filter,
Qualifier qualifier);
<T, S> Stream<?> find(Query query, Class<T> entityClass, Class<S> targetClass, @Nullable Filter filter);

/**
* Find all documents in the given set using provided {@link Qualifier}.
* Find all documents in the given set using provided {@link Query}.
*
* @param query Query to build filter expression from. 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.
* @param qualifier Qualifier to build filter expressions from. 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.
* @return Stream of entities.
*/
<T> Stream<T> findUsingQualifier(Class<T> targetClass, String setName, @Nullable Filter filter,
Qualifier qualifier);
<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 @@ -71,6 +71,7 @@
import static org.springframework.data.aerospike.core.CoreUtils.verifyUnsortedWithOffset;
import static org.springframework.data.aerospike.core.TemplateUtils.excludeIdQualifier;
import static org.springframework.data.aerospike.core.TemplateUtils.getIdValue;
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;
Expand Down Expand Up @@ -664,7 +665,7 @@ public <T, S> S findById(Object id, Class<T> entityClass, Class<S> targetClass)
public <T, S> S findById(Object id, Class<T> entityClass, Class<S> targetClass, String setName) {
Assert.notNull(id, "Id must not be null!");
Assert.notNull(entityClass, "Class must not be null!");
return (S) findByIdUsingQualifiers(id, entityClass, targetClass, setName);
return (S) findByIdUsingQuery(id, entityClass, targetClass, setName, null);
}

private Record getRecord(AerospikePersistentEntity<?> entity, Key key, Qualifier... qualifiers) {
Expand Down Expand Up @@ -779,7 +780,7 @@ public <T, S> List<S> findByIds(Iterable<?> ids, Class<T> entityClass, Class<S>
Assert.notNull(entityClass, "Entity class must not be null!");
Assert.notNull(setName, "Set name must not be null!");

return (List<S>) findByIdsUsingQualifiers(IterableConverter.toList(ids), entityClass, targetClass, setName);
return (List<S>) findByIdsUsingQuery(IterableConverter.toList(ids), entityClass, targetClass, setName, null);
}

@Override
Expand All @@ -801,44 +802,44 @@ private GroupedEntities findGroupedEntitiesByGroupedKeys(GroupedKeys groupedKeys
}

@Override
public <T, S> Object findByIdUsingQualifiers(Object id, Class<T> entityClass, Class<S> targetClass,
Qualifier... qualifiers) {
public <T, S> Object findByIdUsingQuery(Object id, Class<T> entityClass, Class<S> targetClass,
Query query) {
Assert.notNull(id, "Id must not be null!");
Assert.notNull(entityClass, "Class must not be null!");
return findByIdUsingQualifiers(id, entityClass, targetClass, getSetName(entityClass), qualifiers);
return findByIdUsingQuery(id, entityClass, targetClass, getSetName(entityClass), query);
}

@Override
public <T, S> Object findByIdUsingQualifiers(Object id, Class<T> entityClass, Class<S> targetClass, String setName,
Qualifier... qualifiers) {
public <T, S> Object findByIdUsingQuery(Object id, Class<T> entityClass, Class<S> targetClass, String setName,
Query query) {
Assert.notNull(id, "Id must not be null!");
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;
try {
AerospikePersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(entityClass);
Key key = getKey(id, setName);

if (targetClass != null && targetClass != entityClass) {
return getRecordMapToTargetClass(entity, key, targetClass, qualifiers);
return getRecordMapToTargetClass(entity, key, targetClass, criteria);
}
return mapToEntity(key, entityClass, getRecord(entity, key, qualifiers));
return mapToEntity(key, entityClass, getRecord(entity, key, criteria));
} catch (AerospikeException e) {
throw translateError(e);
}
}

@Override
public <T, S> List<?> findByIdsUsingQualifiers(Collection<?> ids, Class<T> entityClass, Class<S> targetClass,
Qualifier... qualifiers) {
Assert.notNull(entityClass, "Class must not be null!");
return findByIdsUsingQualifiers(ids, entityClass, targetClass, getSetName(entityClass), qualifiers);
public <T, S> List<?> findByIdsUsingQuery(Collection<?> ids, Class<T> entityClass, Class<S> targetClass,
Query query) {
return findByIdsUsingQuery(ids, entityClass, targetClass, getSetName(entityClass), query);
}

@Override
public <T, S> List<?> findByIdsUsingQualifiers(Collection<?> ids, Class<T> entityClass, Class<S> targetClass,
String setName, Qualifier... qualifiers) {
Assert.notNull(ids, "List of ids must not be null!");
public <T, S> List<?> findByIdsUsingQuery(Collection<?> ids, Class<T> entityClass, Class<S> targetClass,
String setName, Query query) {
Assert.notNull(ids, "Ids must not be null!");
Assert.notNull(entityClass, "Entity class must not be null!");
Assert.notNull(setName, "Set name must not be null!");

Expand All @@ -851,6 +852,8 @@ public <T, S> List<?> findByIdsUsingQualifiers(Collection<?> ids, Class<T> entit
.map(id -> getKey(id, setName))
.toArray(Key[]::new);

Qualifier[] qualifiers = queryCriteriaIsNotNull(query) && query.getCriteria() != null ?
new Qualifier[]{query.getCriteria().getCriteriaObject()} : null;
BatchPolicy policy = getBatchPolicyFilterExp(qualifiers);

Class<?> target;
Expand Down Expand Up @@ -893,22 +896,20 @@ public <T> Stream<T> find(Query query, Class<T> targetClass, String setName) {
}

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

@Override
public <T, S> Stream<?> findUsingQualifier(Class<T> entityClass, Class<S> targetClass, Filter filter,
Qualifier qualifier) {
return findRecordsUsingQualifiers(getSetName(entityClass), targetClass, filter, qualifier)
public <T, S> Stream<?> 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> findUsingQualifier(Class<T> targetClass, String setName, Filter filter,
Qualifier qualifier) {
return findRecordsUsingQualifiers(setName, targetClass, filter, qualifier)
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)
.map(keyRecord -> mapToEntity(keyRecord, targetClass));
}

Expand All @@ -932,7 +933,7 @@ public <T> Stream<T> findAll(Class<T> targetClass, String setName) {
Assert.notNull(targetClass, "Target class must not be null!");
Assert.notNull(setName, "Set name must not be null!");

return findUsingQualifier(targetClass, setName, null, null);
return find(null, targetClass, setName, null);
}

@Override
Expand All @@ -956,16 +957,15 @@ public <T> Stream<T> findAll(Sort sort, long offset, long limit, Class<T> target

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

private <T> Stream<T> findUsingQualifiersWithDistinctPredicate(String setName, Class<T> targetClass,
Predicate<KeyRecord> distinctPredicate,
Qualifier... qualifiers) {
return findRecordsUsingQualifiers(setName, targetClass, null, qualifiers)
Query query) {
return findRecordsUsingQualifiers(setName, targetClass, null, query.getCriteria().getCriteriaObject())
.filter(distinctPredicate)
.map(keyRecord -> mapToEntity(keyRecord, targetClass));
}
Expand Down Expand Up @@ -1258,7 +1258,8 @@ private <T> Stream<T> findUsingQualifierWithPostProcessing(String setName, Class
long offset, long limit, Filter filter,
Qualifier qualifier) {
verifyUnsortedWithOffset(sort, offset);
Stream<T> results = findUsingQualifier(targetClass, setName, filter, qualifier);
Query query = qualifier != null ? new Query(qualifier) : null;
Stream<T> results = find(query, targetClass, setName, filter);
return applyPostProcessingOnResults(results, sort, offset, limit);
}

Expand Down
Loading

0 comments on commit dd1e1bd

Please sign in to comment.