Browse Source

SONAR-20164 replace in-memory pagination with sql pagination

tags/10.2.0.77647
Pierre 8 months ago
parent
commit
17a83503db

+ 8
- 7
server/sonar-db-dao/src/it/java/org/sonar/db/webhook/WebhookDeliveryDaoIT.java View 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");
}

+ 8
- 8
server/sonar-db-dao/src/main/java/org/sonar/db/webhook/WebhookDeliveryDao.java View 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()));
}


+ 4
- 4
server/sonar-db-dao/src/main/java/org/sonar/db/webhook/WebhookDeliveryMapper.java View 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);


+ 6
- 3
server/sonar-db-dao/src/main/resources/org/sonar/db/webhook/WebhookDeliveryMapper.xml View File

@@ -33,11 +33,12 @@
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">
@@ -47,18 +48,20 @@
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">

+ 4
- 4
server/sonar-webserver-webapi/src/main/java/org/sonar/server/webhook/ws/WebhookDeliveriesAction.java View 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);
}
}

Loading…
Cancel
Save