Skip to content

Commit

Permalink
Use singleOrEmpty() instead of buffer()
Browse files Browse the repository at this point in the history
This commit avoids fetching and buffering results in temporary `List` buffers.

See gh-31282
  • Loading branch information
sephiroth-j authored and snicoll committed Sep 26, 2023
1 parent 2e2a62a commit cbfb99f
Showing 1 changed file with 5 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cbfb99f

Please sign in to comment.