From 2df8c2fb76ac35b0af77d67f4e65e4bcc5d4a0eb Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Thu, 21 Nov 2024 09:01:51 +0100 Subject: [PATCH] Use `ReactivePageableExecutionUtils` from Spring Data Commons. Closes #1946 --- .../ReactivePageableExecutionUtils.java | 69 ------------------- .../support/SimpleR2dbcRepository.java | 1 + 2 files changed, 1 insertion(+), 69 deletions(-) delete mode 100644 spring-data-r2dbc/src/main/java/org/springframework/data/r2dbc/repository/support/ReactivePageableExecutionUtils.java diff --git a/spring-data-r2dbc/src/main/java/org/springframework/data/r2dbc/repository/support/ReactivePageableExecutionUtils.java b/spring-data-r2dbc/src/main/java/org/springframework/data/r2dbc/repository/support/ReactivePageableExecutionUtils.java deleted file mode 100644 index 8151247e8a8..00000000000 --- a/spring-data-r2dbc/src/main/java/org/springframework/data/r2dbc/repository/support/ReactivePageableExecutionUtils.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright 2021-2024 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.data.r2dbc.repository.support; - -import reactor.core.publisher.Mono; - -import java.util.List; - -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageImpl; -import org.springframework.data.domain.Pageable; -import org.springframework.util.Assert; - -/** - * Support for query execution using {@link Pageable}. Using {@link ReactivePageableExecutionUtils} assumes that data - * queries are cheaper than {@code COUNT} queries and so some cases can take advantage of optimizations. - * - * @author Mark Paluch - * @since 1.4 - */ -abstract class ReactivePageableExecutionUtils { - - private ReactivePageableExecutionUtils() {} - - /** - * Constructs a {@link Page} based on the given {@code content}, {@link Pageable} and {@link Mono} applying - * optimizations. The construction of {@link Page} omits a count query if the total can be determined based on the - * result size and {@link Pageable}. - * - * @param content must not be {@literal null}. - * @param pageable must not be {@literal null}. - * @param totalSupplier must not be {@literal null}. - * @return the {@link Page}. - */ - public static Mono> getPage(List content, Pageable pageable, Mono totalSupplier) { - - Assert.notNull(content, "Content must not be null"); - Assert.notNull(pageable, "Pageable must not be null"); - Assert.notNull(totalSupplier, "TotalSupplier must not be null"); - - if (pageable.isUnpaged() || pageable.getOffset() == 0) { - - if (pageable.isUnpaged() || pageable.getPageSize() > content.size()) { - return Mono.just(new PageImpl<>(content, pageable, content.size())); - } - - return totalSupplier.map(total -> new PageImpl<>(content, pageable, total)); - } - - if (!content.isEmpty() && pageable.getPageSize() > content.size()) { - return Mono.just(new PageImpl<>(content, pageable, pageable.getOffset() + content.size())); - } - - return totalSupplier.map(total -> new PageImpl<>(content, pageable, total)); - } -} diff --git a/spring-data-r2dbc/src/main/java/org/springframework/data/r2dbc/repository/support/SimpleR2dbcRepository.java b/spring-data-r2dbc/src/main/java/org/springframework/data/r2dbc/repository/support/SimpleR2dbcRepository.java index 9b15b669086..becad9256a3 100644 --- a/spring-data-r2dbc/src/main/java/org/springframework/data/r2dbc/repository/support/SimpleR2dbcRepository.java +++ b/spring-data-r2dbc/src/main/java/org/springframework/data/r2dbc/repository/support/SimpleR2dbcRepository.java @@ -44,6 +44,7 @@ import org.springframework.data.relational.repository.query.RelationalExampleMapper; import org.springframework.data.repository.query.FluentQuery; import org.springframework.data.repository.reactive.ReactiveSortingRepository; +import org.springframework.data.support.ReactivePageableExecutionUtils; import org.springframework.data.util.Lazy; import org.springframework.data.util.Streamable; import org.springframework.r2dbc.core.DatabaseClient;