Use `singleOrEmpty()` instead of `buffer()`

This commit avoids fetching and buffering results in temporary `List` buffers.

See gh-31282
This commit is contained in:
Ronny Perinke 2023-09-20 17:23:59 +02:00 committed by Stéphane Nicoll
parent 2e2a62a23c
commit cbfb99fef2
1 changed files with 5 additions and 12 deletions

View File

@ -59,18 +59,11 @@ class DefaultFetchSpec<T> implements FetchSpec<T> {
@Override
public Mono<T> one() {
return all().buffer(2)
.flatMap(list -> {
if (list.isEmpty()) {
return Mono.empty();
}
if (list.size() > 1) {
return Mono.error(new IncorrectResultSizeDataAccessException(
String.format("Query [%s] returned non unique result.", this.resultFunction.getSql()),
1));
}
return Mono.just(list.get(0));
}).next();
return all().singleOrEmpty()
.onErrorMap(IndexOutOfBoundsException.class, ex -> {
String message = String.format("Query [%s] returned non unique result.", resultFunction.getSql());
return new IncorrectResultSizeDataAccessException(message, 1);
});
}
@Override