]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-20164 replace in-memory pagination with sql pagination
authorPierre <pierre.guillot@sonarsource.com>
Mon, 28 Aug 2023 15:18:13 +0000 (17:18 +0200)
committersonartech <sonartech@sonarsource.com>
Thu, 31 Aug 2023 20:02:56 +0000 (20:02 +0000)
server/sonar-db-dao/src/it/java/org/sonar/db/webhook/WebhookDeliveryDaoIT.java
server/sonar-db-dao/src/main/java/org/sonar/db/webhook/WebhookDeliveryDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/webhook/WebhookDeliveryMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/webhook/WebhookDeliveryMapper.xml
server/sonar-webserver-webapi/src/main/java/org/sonar/server/webhook/ws/WebhookDeliveriesAction.java

index e57c45310f9af6af56c35a2517c31ecaea469f19..5f0d6214b6274f6e8b7c3224b68a8b674c252c28 100644 (file)
@@ -28,6 +28,7 @@ import org.sonar.api.utils.System2;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
+import org.sonar.db.Pagination;
 
 import static com.google.common.collect.ImmutableList.of;
 import static org.assertj.core.api.Assertions.assertThat;
@@ -56,7 +57,7 @@ public class WebhookDeliveryDaoIT {
   public void selectOrderedByComponentUuid_returns_empty_if_no_records() {
     underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D1", "WEBHOOK_UUID_1", "PROJECT_1", "TASK_1"));
 
-    List<WebhookDeliveryLiteDto> deliveries = underTest.selectOrderedByProjectUuid(dbSession, "ANOTHER_PROJECT", 0, 10);
+    List<WebhookDeliveryLiteDto> deliveries = underTest.selectOrderedByProjectUuid(dbSession, "ANOTHER_PROJECT", Pagination.all());
 
     assertThat(deliveries).isEmpty();
   }
@@ -70,7 +71,7 @@ public class WebhookDeliveryDaoIT {
     underTest.insert(dbSession, dto2);
     underTest.insert(dbSession, dto1);
 
-    List<WebhookDeliveryLiteDto> deliveries = underTest.selectOrderedByProjectUuid(dbSession, "PROJECT_1", 0, 10);
+    List<WebhookDeliveryLiteDto> deliveries = underTest.selectOrderedByProjectUuid(dbSession, "PROJECT_1", Pagination.all());
 
     assertThat(deliveries).extracting(WebhookDeliveryLiteDto::getUuid).containsExactly("D2", "D1");
   }
@@ -79,7 +80,7 @@ public class WebhookDeliveryDaoIT {
   public void selectOrderedByCeTaskUuid_returns_empty_if_no_records() {
     underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D1", "WEBHOOK_UUID_1", "PROJECT_1", "TASK_1"));
 
-    List<WebhookDeliveryLiteDto> deliveries = underTest.selectOrderedByCeTaskUuid(dbSession, "ANOTHER_TASK", 0, 10);
+    List<WebhookDeliveryLiteDto> deliveries = underTest.selectOrderedByCeTaskUuid(dbSession, "ANOTHER_TASK", Pagination.all());
 
     assertThat(deliveries).isEmpty();
   }
@@ -93,7 +94,7 @@ public class WebhookDeliveryDaoIT {
     underTest.insert(dbSession, dto2);
     underTest.insert(dbSession, dto1);
 
-    List<WebhookDeliveryLiteDto> deliveries = underTest.selectOrderedByCeTaskUuid(dbSession, "TASK_1", 0, 10);
+    List<WebhookDeliveryLiteDto> deliveries = underTest.selectOrderedByCeTaskUuid(dbSession, "TASK_1", Pagination.all());
 
     assertThat(deliveries).extracting(WebhookDeliveryLiteDto::getUuid).containsExactly("D2", "D1");
   }
@@ -103,7 +104,7 @@ public class WebhookDeliveryDaoIT {
 
     underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D1", "WEBHOOK_UUID_1", "PROJECT_1", "TASK_1"));
 
-    List<WebhookDeliveryLiteDto> deliveries = underTest.selectByWebhookUuid(dbSession, "a-webhook-uuid", 0, 10);
+    List<WebhookDeliveryLiteDto> deliveries = underTest.selectByWebhookUuid(dbSession, "a-webhook-uuid", Pagination.all());
 
     assertThat(deliveries).isEmpty();
   }
@@ -118,7 +119,7 @@ public class WebhookDeliveryDaoIT {
     underTest.insert(dbSession, dto2);
     underTest.insert(dbSession, dto1);
 
-    List<WebhookDeliveryLiteDto> deliveries = underTest.selectByWebhookUuid(dbSession, webhookDto.getUuid(), 0, 10);
+    List<WebhookDeliveryLiteDto> deliveries = underTest.selectByWebhookUuid(dbSession, webhookDto.getUuid(), Pagination.all());
 
     assertThat(deliveries).extracting(WebhookDeliveryLiteDto::getUuid).containsExactly("D2", "D1");
   }
@@ -133,7 +134,7 @@ public class WebhookDeliveryDaoIT {
     underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D5", webhookDto.getUuid(), "PROJECT_1", "TASK_2").setCreatedAt(NOW - 1_000L));
     underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D6", webhookDto.getUuid(), "PROJECT_1", "TASK_2").setCreatedAt(NOW));
 
-    List<WebhookDeliveryLiteDto> deliveries = underTest.selectByWebhookUuid(dbSession, webhookDto.getUuid(), 2, 2);
+    List<WebhookDeliveryLiteDto> deliveries = underTest.selectByWebhookUuid(dbSession, webhookDto.getUuid(), Pagination.forPage(2).andSize(2));
 
     assertThat(deliveries).extracting(WebhookDeliveryLiteDto::getUuid).containsExactly("D4", "D3");
   }
index d71ab8370c8529dba548da923c2826860705c3f0..565ad0deb7e4f612892cba329a73b06c7c5f0e0c 100644 (file)
@@ -22,9 +22,9 @@ package org.sonar.db.webhook;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
-import org.apache.ibatis.session.RowBounds;
 import org.sonar.db.Dao;
 import org.sonar.db.DbSession;
+import org.sonar.db.Pagination;
 
 import static java.util.function.Function.identity;
 import static java.util.stream.Collectors.toMap;
@@ -42,8 +42,8 @@ public class WebhookDeliveryDao implements Dao {
   /**
    * All the deliveries for the specified webhook. Results are ordered by descending date.
    */
-  public List<WebhookDeliveryLiteDto> selectByWebhookUuid(DbSession dbSession, String webhookUuid, int offset, int limit) {
-    return mapper(dbSession).selectByWebhookUuid(webhookUuid, new RowBounds(offset, limit));
+  public List<WebhookDeliveryLiteDto> selectByWebhookUuid(DbSession dbSession, String webhookUuid, Pagination pagination) {
+    return mapper(dbSession).selectByWebhookUuid(webhookUuid, pagination);
   }
 
   public int countDeliveriesByProjectUuid(DbSession dbSession, String projectUuid) {
@@ -53,8 +53,8 @@ public class WebhookDeliveryDao implements Dao {
   /**
    * All the deliveries for the specified project. Results are ordered by descending date.
    */
-  public List<WebhookDeliveryLiteDto> selectOrderedByProjectUuid(DbSession dbSession, String projectUuid, int offset, int limit) {
-    return mapper(dbSession).selectOrderedByProjectUuid(projectUuid, new RowBounds(offset, limit));
+  public List<WebhookDeliveryLiteDto> selectOrderedByProjectUuid(DbSession dbSession, String projectUuid, Pagination pagination) {
+    return mapper(dbSession).selectOrderedByProjectUuid(projectUuid, pagination);
   }
 
   public int countDeliveriesByCeTaskUuid(DbSession dbSession, String ceTaskId) {
@@ -64,8 +64,8 @@ public class WebhookDeliveryDao implements Dao {
   /**
    * All the deliveries for the specified CE task. Results are ordered by descending date.
    */
-  public List<WebhookDeliveryLiteDto> selectOrderedByCeTaskUuid(DbSession dbSession, String ceTaskUuid, int offset, int limit) {
-    return mapper(dbSession).selectOrderedByCeTaskUuid(ceTaskUuid, new RowBounds(offset, limit));
+  public List<WebhookDeliveryLiteDto> selectOrderedByCeTaskUuid(DbSession dbSession, String ceTaskUuid, Pagination pagination) {
+    return mapper(dbSession).selectOrderedByCeTaskUuid(ceTaskUuid, pagination);
   }
 
   public void insert(DbSession dbSession, WebhookDeliveryDto dto) {
@@ -78,7 +78,7 @@ public class WebhookDeliveryDao implements Dao {
 
   public Map<String, WebhookDeliveryLiteDto> selectLatestDeliveries(DbSession dbSession, List<WebhookDto> webhooks) {
     return webhooks.stream()
-      .flatMap(webhook -> selectByWebhookUuid(dbSession, webhook.getUuid(),0,1).stream())
+      .flatMap(webhook -> selectByWebhookUuid(dbSession, webhook.getUuid(), Pagination.forPage(1).andSize(1)).stream())
       .collect(toMap(WebhookDeliveryLiteDto::getWebhookUuid, identity()));
   }
 
index 66f4dead9831e0171393613405b8f58ec3958877..7a1764f78d144db019d330dfd9b4bb719357d662 100644 (file)
@@ -22,7 +22,7 @@ package org.sonar.db.webhook;
 import java.util.List;
 import javax.annotation.CheckForNull;
 import org.apache.ibatis.annotations.Param;
-import org.apache.ibatis.session.RowBounds;
+import org.sonar.db.Pagination;
 
 public interface WebhookDeliveryMapper {
 
@@ -31,15 +31,15 @@ public interface WebhookDeliveryMapper {
 
   int countByWebhookUuid(@Param("webhookUuid") String webhookUuid);
 
-  List<WebhookDeliveryLiteDto> selectByWebhookUuid(@Param("webhookUuid") String webhookUuid, RowBounds rowBounds);
+  List<WebhookDeliveryLiteDto> selectByWebhookUuid(@Param("webhookUuid") String webhookUuid, @Param("pagination") Pagination pagination);
 
   int countByProjectUuid(@Param("projectUuid") String projectUuid);
 
-  List<WebhookDeliveryLiteDto> selectOrderedByProjectUuid(@Param("projectUuid") String projectUuid, RowBounds rowBounds);
+  List<WebhookDeliveryLiteDto> selectOrderedByProjectUuid(@Param("projectUuid") String projectUuid, @Param("pagination") Pagination pagination);
 
   int countByCeTaskUuid(@Param("ceTaskUuid") String ceTaskId);
 
-  List<WebhookDeliveryLiteDto> selectOrderedByCeTaskUuid(@Param("ceTaskUuid") String ceTaskUuid, RowBounds rowBounds);
+  List<WebhookDeliveryLiteDto> selectOrderedByCeTaskUuid(@Param("ceTaskUuid") String ceTaskUuid, @Param("pagination") Pagination pagination);
 
   void insert(WebhookDeliveryDto dto);
 
index cbb17b6a747263b22c53cb21ecfc0bb40ee65d54..62796241a06ba8830c994d9e696f1134eef56284 100644 (file)
     where webhook_uuid = #{webhookUuid,jdbcType=VARCHAR}
   </select>
 
-  <select id="selectByWebhookUuid" parameterType="String" resultType="org.sonar.db.webhook.WebhookDeliveryLiteDto">
+  <select id="selectByWebhookUuid" resultType="org.sonar.db.webhook.WebhookDeliveryLiteDto">
     select <include refid="sqlLiteColumns" />
     from webhook_deliveries
     where webhook_uuid = #{webhookUuid,jdbcType=VARCHAR}
     order by created_at desc
+    offset (#{pagination.startRowNumber,jdbcType=INTEGER}-1) rows fetch next #{pagination.pageSize,jdbcType=INTEGER} rows only
   </select>
 
   <select id="countByProjectUuid" parameterType="String" resultType="int">
     where project_uuid = #{projectUuid,jdbcType=VARCHAR}
   </select>
 
-  <select id="selectOrderedByProjectUuid" parameterType="String" resultType="org.sonar.db.webhook.WebhookDeliveryLiteDto">
+  <select id="selectOrderedByProjectUuid" resultType="org.sonar.db.webhook.WebhookDeliveryLiteDto">
     select <include refid="sqlLiteColumns" />
     from webhook_deliveries
     where project_uuid = #{projectUuid,jdbcType=VARCHAR}
     order by created_at desc
+    offset (#{pagination.startRowNumber,jdbcType=INTEGER}-1) rows fetch next #{pagination.pageSize,jdbcType=INTEGER} rows only
   </select>
 
-  <select id="selectOrderedByCeTaskUuid" parameterType="String" resultType="org.sonar.db.webhook.WebhookDeliveryLiteDto">
+  <select id="selectOrderedByCeTaskUuid" resultType="org.sonar.db.webhook.WebhookDeliveryLiteDto">
     select <include refid="sqlLiteColumns" />
     from webhook_deliveries
     where ce_task_uuid = #{ceTaskUuid,jdbcType=VARCHAR}
     order by created_at desc
+    offset (#{pagination.startRowNumber,jdbcType=INTEGER}-1) rows fetch next #{pagination.pageSize,jdbcType=INTEGER} rows only
   </select>
 
   <select id="countByCeTaskUuid" parameterType="String" resultType="int">
index 07d8ea74669e6fd9ecb1c3c6725c54f30fc62932..c10a2a654b72c6a19dfd64a074392e8ed6407c55 100644 (file)
@@ -34,6 +34,7 @@ import org.sonar.api.web.UserRole;
 import org.sonar.core.util.Uuids;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
+import org.sonar.db.Pagination;
 import org.sonar.db.project.ProjectDto;
 import org.sonar.db.webhook.WebhookDeliveryLiteDto;
 import org.sonar.server.component.ComponentFinder;
@@ -46,7 +47,6 @@ import static com.google.common.base.Preconditions.checkArgument;
 import static org.apache.commons.lang.StringUtils.isNotBlank;
 import static org.sonar.api.server.ws.WebService.Param.PAGE;
 import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE;
-import static org.sonar.api.utils.Paging.offset;
 import static org.sonar.core.util.Uuids.UUID_EXAMPLE_02;
 import static org.sonar.server.es.SearchOptions.MAX_PAGE_SIZE;
 import static org.sonar.server.webhook.ws.WebhookWsSupport.copyDtoToProtobuf;
@@ -121,17 +121,17 @@ public class WebhookDeliveriesAction implements WebhooksWsAction {
     try (DbSession dbSession = dbClient.openSession(false)) {
       if (isNotBlank(webhookUuid)) {
         totalElements = dbClient.webhookDeliveryDao().countDeliveriesByWebhookUuid(dbSession, webhookUuid);
-        deliveries = dbClient.webhookDeliveryDao().selectByWebhookUuid(dbSession, webhookUuid, offset(page, pageSize), pageSize);
+        deliveries = dbClient.webhookDeliveryDao().selectByWebhookUuid(dbSession, webhookUuid, Pagination.forPage(page).andSize(pageSize));
         projectUuidMap = getProjectsDto(dbSession, deliveries);
       } else if (projectKey != null) {
         ProjectDto project = componentFinder.getProjectByKey(dbSession, projectKey);
         projectUuidMap = new HashMap<>();
         projectUuidMap.put(project.getUuid(), project);
         totalElements = dbClient.webhookDeliveryDao().countDeliveriesByProjectUuid(dbSession, project.getUuid());
-        deliveries = dbClient.webhookDeliveryDao().selectOrderedByProjectUuid(dbSession, project.getUuid(), offset(page, pageSize), pageSize);
+        deliveries = dbClient.webhookDeliveryDao().selectOrderedByProjectUuid(dbSession, project.getUuid(), Pagination.forPage(page).andSize(pageSize));
       } else {
         totalElements = dbClient.webhookDeliveryDao().countDeliveriesByCeTaskUuid(dbSession, ceTaskId);
-        deliveries = dbClient.webhookDeliveryDao().selectOrderedByCeTaskUuid(dbSession, ceTaskId, offset(page, pageSize), pageSize);
+        deliveries = dbClient.webhookDeliveryDao().selectOrderedByCeTaskUuid(dbSession, ceTaskId, Pagination.forPage(page).andSize(pageSize));
         projectUuidMap = getProjectsDto(dbSession, deliveries);
       }
     }