Skip to content

Commit

Permalink
add post-processing to findByIdsUsingQuery(), update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
agrgr committed Sep 9, 2024
1 parent fcedbff commit 6ed0804
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,7 @@ <T, S> List<?> findByIdsUsingQuery(Collection<?> ids, Class<T> entityClass, Clas
/**
* Find if there are existing records by ids and a query using the given entityClass.
* <p>
* The records will be mapped to the given targetClass.
* The records will not be mapped to the given entityClass. The results are not processed (no pagination).
*
* @param ids The ids of the documents to find. Must not be {@literal null}.
* @param entityClass The class to extract set name from. Must not be {@literal null}.
Expand All @@ -1096,16 +1096,15 @@ <T, S> List<?> findByIdsUsingQuery(Collection<?> ids, Class<T> entityClass, Clas
/**
* Find if there are existing records by ids and a query using the given entityClass within the set.
* <p>
* The records will be mapped to the given targetClass.
* The records will not be mapped to a Java class. The results are not processed (no pagination).
*
* @param ids The ids of the documents to find. Must not be {@literal null}.
* @param entityClass The class to extract set name from. Must not be {@literal null}.
* @param setName Set name to use. Must not be {@literal null}.
* @param query The {@link Query} to filter results. Optional argument (null if no filtering required).
* @param ids The ids of the documents to find. Must not be {@literal null}.
* @param setName Set name to use. Must not be {@literal null}.
* @param query The {@link Query} to filter results. Optional argument (null if no filtering required).
* @return The matching records mapped to targetClass's type if provided (otherwise to entityClass's type), or an
* empty list if no documents found.
*/
<T> boolean existsByIdsUsingQuery(Collection<?> ids, Class<T> entityClass, String setName, @Nullable Query query);
boolean existsByIdsUsingQuery(Collection<?> ids, String setName, @Nullable Query query);

/**
* Return the amount of records in the set determined by the given entityClass.
Expand Down Expand Up @@ -1144,7 +1143,7 @@ <T, S> List<?> findByIdsUsingQuery(Collection<?> ids, Class<T> entityClass, Clas
/**
* Count existing records by ids and a query using the given entityClass.
* <p>
* The records will be mapped to the given targetClass.
* The records will not be mapped to the given entityClass. The results are not processed (no pagination).
*
* @param ids The ids of the documents to find. Must not be {@literal null}.
* @param entityClass The class to extract set name from. Must not be {@literal null}.
Expand All @@ -1157,16 +1156,15 @@ <T, S> List<?> findByIdsUsingQuery(Collection<?> ids, Class<T> entityClass, Clas
/**
* Count existing records by ids and a query using the given entityClass within the set.
* <p>
* The records will be mapped to the given targetClass.
* The records will not be mapped to a Java class. The results are not processed (no pagination).
*
* @param ids The ids of the documents to find. Must not be {@literal null}.
* @param entityClass The class to extract set name from. Must not be {@literal null}.
* @param setName Set name to use. Must not be {@literal null}.
* @param query The {@link Query} to filter results. Optional argument (null if no filtering required).
* @param ids The ids of the documents to find. Must not be {@literal null}.
* @param setName Set name to use. Must not be {@literal null}.
* @param query The {@link Query} to filter results. Optional argument (null if no filtering required).
* @return The matching records mapped to targetClass's type if provided (otherwise to entityClass's type), or an
* empty list if no documents found.
*/
<T> long countByIdsUsingQuery(Collection<?> ids, Class<T> entityClass, String setName, @Nullable Query query);
long countByIdsUsingQuery(Collection<?> ids, String setName, @Nullable Query query);

/**
* Execute query, apply statement's aggregation function, and return result iterator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1027,10 +1027,11 @@ public <T, S> List<?> findByIdsUsingQuery(Collection<?> ids, Class<T> entityClas
target = entityClass;
}

return IntStream.range(0, keys.length)
Stream<?> results = IntStream.range(0, keys.length)
.filter(index -> aeroRecords[index] != null)
.mapToObj(index -> mapToEntity(keys[index], target, aeroRecords[index]))
.collect(Collectors.toList());
.mapToObj(index -> mapToEntity(keys[index], target, aeroRecords[index]));

return applyPostProcessingOnResults(results, query).collect(Collectors.toList());
} catch (AerospikeException e) {
throw translateError(e);
}
Expand Down Expand Up @@ -1209,11 +1210,11 @@ public boolean exists(Query query, String setName) {

@Override
public <T> boolean existsByIdsUsingQuery(Collection<?> ids, Class<T> entityClass, Query query) {
return existsByIdsUsingQuery(ids, entityClass, getSetName(entityClass), query);
return existsByIdsUsingQuery(ids, getSetName(entityClass), query);
}

@Override
public <T> boolean existsByIdsUsingQuery(Collection<?> ids, Class<T> entityClass, String setName, Query query) {
public boolean existsByIdsUsingQuery(Collection<?> ids, String setName, Query query) {
long findQueryResults = findByIdsUsingQueryWithoutMapping(ids, setName, query)
.filter(Objects::nonNull)
.count();
Expand Down Expand Up @@ -1282,11 +1283,11 @@ private Stream<KeyRecord> findKeyRecordsUsingQuery(String setName, Query query)

@Override
public <T> long countByIdsUsingQuery(Collection<?> ids, Class<T> entityClass, Query query) {
return countByIdsUsingQuery(ids, entityClass, getSetName(entityClass), query);
return countByIdsUsingQuery(ids, getSetName(entityClass), query);
}

@Override
public <T> long countByIdsUsingQuery(Collection<?> ids, Class<T> entityClass, String setName, Query query) {
public long countByIdsUsingQuery(Collection<?> ids, String setName, Query query) {
return findByIdsUsingQueryWithoutMapping(ids, setName, query)
.filter(Objects::nonNull)
.count();
Expand Down Expand Up @@ -1481,15 +1482,17 @@ private <T> Stream<T> findWithPostProcessing(String setName, Class<T> targetClas
}

private <T> Stream<T> applyPostProcessingOnResults(Stream<T> results, Query query) {
if (query.getSort() != null && query.getSort().isSorted()) {
Comparator<T> comparator = getComparator(query);
results = results.sorted(comparator);
}
if (query.hasOffset()) {
results = results.skip(query.getOffset());
}
if (query.hasRows()) {
results = results.limit(query.getRows());
if (query != null) {
if (query.getSort() != null && query.getSort().isSorted()) {
Comparator<T> comparator = getComparator(query);
results = results.sorted(comparator);
}
if (query.hasOffset()) {
results = results.skip(query.getOffset());
}
if (query.hasRows()) {
results = results.limit(query.getRows());
}
}

return results;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,8 @@ public interface ReactiveAerospikeOperations {
* @return The matching records mapped to targetClass's type if provided (otherwise to entityClass's type), or an
* empty list if no documents found.
*/
<T> Mono<Void> deleteByIdsUsingQuery(Collection<?> ids, Class<T> entityClass, String setName, @Nullable Query query);
<T> Mono<Void> deleteByIdsUsingQuery(Collection<?> ids, Class<T> entityClass, String setName,
@Nullable Query query);

/**
* Reactively delete multiple records in one batch request. The policies are analogous to {@link #delete(Object)}.
Expand Down Expand Up @@ -1054,16 +1055,16 @@ <T, S> Flux<?> findByIdsUsingQuery(Collection<?> ids, Class<T> entityClass, Clas
/**
* Reactively check using a query if any matching records exist within the given set.
*
* @param query The query to check if any matching records exist. Must not be {@literal null}.
* @param setName Set name to use. Must not be {@literal null}.
* @param query The query to check if any matching records exist. Must not be {@literal null}.
* @param setName Set name to use. Must not be {@literal null}.
* @return A Mono of whether matching records exist.
*/
Mono<Boolean> exists(Query query, String setName);

/**
* Find if there are existing records by ids and a query using the given entityClass.
* <p>
* The records will be mapped to the given targetClass.
* The records will not be mapped to the given entityClass. The results are not processed (no pagination).
*
* @param ids The ids of the documents to find. Must not be {@literal null}.
* @param entityClass The class to extract set name from. Must not be {@literal null}.
Expand All @@ -1076,16 +1077,15 @@ <T, S> Flux<?> findByIdsUsingQuery(Collection<?> ids, Class<T> entityClass, Clas
/**
* Find if there are existing records by ids and a query using the given entityClass within the set.
* <p>
* The records will be mapped to the given targetClass.
* The records will not be mapped to a Java class. The results are not processed (no pagination).
*
* @param ids The ids of the documents to find. Must not be {@literal null}.
* @param entityClass The class to extract set name from. Must not be {@literal null}.
* @param setName Set name to use. Must not be {@literal null}.
* @param query The {@link Query} to filter results. Optional argument (null if no filtering required).
* @param ids The ids of the documents to find. Must not be {@literal null}.
* @param setName Set name to use. Must not be {@literal null}.
* @param query The {@link Query} to filter results. Optional argument (null if no filtering required).
* @return The matching records mapped to targetClass's type if provided (otherwise to entityClass's type), or an
* empty list if no documents found.
*/
<T> Mono<Boolean> existsByIdsUsingQuery(Collection<?> ids, Class<T> entityClass, String setName, @Nullable Query query);
Mono<Boolean> existsByIdsUsingQuery(Collection<?> ids, String setName, @Nullable Query query);

/**
* Reactively return the amount of records in the set determined by the given entityClass.
Expand Down Expand Up @@ -1124,7 +1124,7 @@ <T, S> Flux<?> findByIdsUsingQuery(Collection<?> ids, Class<T> entityClass, Clas
/**
* Count existing records by ids and a query using the given entityClass.
* <p>
* The records will be mapped to the given targetClass.
* The records will not be mapped to the given entityClass. The results are not processed (no pagination).
*
* @param ids The ids of the documents to find. Must not be {@literal null}.
* @param entityClass The class to extract set name from. Must not be {@literal null}.
Expand All @@ -1137,11 +1137,11 @@ <T, S> Flux<?> findByIdsUsingQuery(Collection<?> ids, Class<T> entityClass, Clas
/**
* Count existing records by ids and a query using the given entityClass within the set.
* <p>
* The records will be mapped to the given targetClass.
* The records will not be mapped to a Java class. The results are not processed (no pagination).
*
* @param ids The ids of the documents to find. Must not be {@literal null}.
* @param setName Set name to use. Must not be {@literal null}.
* @param query The {@link Query} to filter results. Optional argument (null if no filtering required).
* @param ids The ids of the documents to find. Must not be {@literal null}.
* @param setName Set name to use. Must not be {@literal null}.
* @param query The {@link Query} to filter results. Optional argument (null if no filtering required).
* @return The matching records mapped to targetClass's type if provided (otherwise to entityClass's type), or an
* empty list if no documents found.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -975,11 +975,13 @@ public <T, S> Flux<?> findByIdsUsingQuery(Collection<?> ids, Class<T> entityClas
target = entityClass;
}

return Flux.fromIterable(ids)
Flux<?> results = Flux.fromIterable(ids)
.map(id -> getKey(id, setName))
.flatMap(key -> getFromClient(policy, key, targetClass))
.filter(keyRecord -> nonNull(keyRecord.record))
.map(keyRecord -> mapToEntity(keyRecord.key, target, keyRecord.record));

return applyPostProcessingOnResults(results, query);
}

private Flux<?> findByIdsUsingQueryWithoutMapping(Collection<?> ids, String setName, Query query) {
Expand Down Expand Up @@ -1142,12 +1144,11 @@ public Mono<Boolean> exists(Query query, String setName) {

@Override
public <T> Mono<Boolean> existsByIdsUsingQuery(Collection<?> ids, Class<T> entityClass, Query query) {
return existsByIdsUsingQuery(ids, entityClass, getSetName(entityClass), query);
return existsByIdsUsingQuery(ids, getSetName(entityClass), query);
}

@Override
public <T> Mono<Boolean> existsByIdsUsingQuery(Collection<?> ids, Class<T> entityClass, String setName,
Query query) {
public Mono<Boolean> existsByIdsUsingQuery(Collection<?> ids, String setName, Query query) {
return findByIdsUsingQueryWithoutMapping(ids, setName, query)
.filter(Objects::nonNull)
.hasElements();
Expand Down Expand Up @@ -1454,16 +1455,18 @@ private void verifyUnsortedWithOffset(Sort sort, long offset) {
}

private <T> Flux<T> applyPostProcessingOnResults(Flux<T> results, Query query) {
if (query.getSort() != null && query.getSort().isSorted()) {
Comparator<T> comparator = getComparator(query);
results = results.sort(comparator);
}
if (query != null) {
if (query.getSort() != null && query.getSort().isSorted()) {
Comparator<T> comparator = getComparator(query);
results = results.sort(comparator);
}

if (query.hasOffset()) {
results = results.skip(query.getOffset());
}
if (query.hasRows()) {
results = results.take(query.getRows());
if (query.hasOffset()) {
results = results.skip(query.getOffset());
}
if (query.hasRows()) {
results = results.take(query.getRows());
}
}
return results;
}
Expand Down

0 comments on commit 6ed0804

Please sign in to comment.