Browse Source

SONAR-20164 replace in-memory pagination with sql pagination

tags/10.2.0.77647
Pierre 9 months ago
parent
commit
17a83503db

+ 8
- 7
server/sonar-db-dao/src/it/java/org/sonar/db/webhook/WebhookDeliveryDaoIT.java View File

import org.sonar.db.DbClient; import org.sonar.db.DbClient;
import org.sonar.db.DbSession; import org.sonar.db.DbSession;
import org.sonar.db.DbTester; import org.sonar.db.DbTester;
import org.sonar.db.Pagination;


import static com.google.common.collect.ImmutableList.of; import static com.google.common.collect.ImmutableList.of;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
public void selectOrderedByComponentUuid_returns_empty_if_no_records() { public void selectOrderedByComponentUuid_returns_empty_if_no_records() {
underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D1", "WEBHOOK_UUID_1", "PROJECT_1", "TASK_1")); 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(); assertThat(deliveries).isEmpty();
} }
underTest.insert(dbSession, dto2); underTest.insert(dbSession, dto2);
underTest.insert(dbSession, dto1); 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"); assertThat(deliveries).extracting(WebhookDeliveryLiteDto::getUuid).containsExactly("D2", "D1");
} }
public void selectOrderedByCeTaskUuid_returns_empty_if_no_records() { public void selectOrderedByCeTaskUuid_returns_empty_if_no_records() {
underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D1", "WEBHOOK_UUID_1", "PROJECT_1", "TASK_1")); 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(); assertThat(deliveries).isEmpty();
} }
underTest.insert(dbSession, dto2); underTest.insert(dbSession, dto2);
underTest.insert(dbSession, dto1); 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"); assertThat(deliveries).extracting(WebhookDeliveryLiteDto::getUuid).containsExactly("D2", "D1");
} }


underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D1", "WEBHOOK_UUID_1", "PROJECT_1", "TASK_1")); 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(); assertThat(deliveries).isEmpty();
} }
underTest.insert(dbSession, dto2); underTest.insert(dbSession, dto2);
underTest.insert(dbSession, dto1); 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"); assertThat(deliveries).extracting(WebhookDeliveryLiteDto::getUuid).containsExactly("D2", "D1");
} }
underTest.insert(dbSession, WebhookDeliveryTesting.newDto("D5", webhookDto.getUuid(), "PROJECT_1", "TASK_2").setCreatedAt(NOW - 1_000L)); 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)); 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"); 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

import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import org.apache.ibatis.session.RowBounds;
import org.sonar.db.Dao; import org.sonar.db.Dao;
import org.sonar.db.DbSession; import org.sonar.db.DbSession;
import org.sonar.db.Pagination;


import static java.util.function.Function.identity; import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toMap; import static java.util.stream.Collectors.toMap;
/** /**
* All the deliveries for the specified webhook. Results are ordered by descending date. * 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) { public int countDeliveriesByProjectUuid(DbSession dbSession, String projectUuid) {
/** /**
* All the deliveries for the specified project. Results are ordered by descending date. * 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) { public int countDeliveriesByCeTaskUuid(DbSession dbSession, String ceTaskId) {
/** /**
* All the deliveries for the specified CE task. Results are ordered by descending date. * 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) { public void insert(DbSession dbSession, WebhookDeliveryDto dto) {


public Map<String, WebhookDeliveryLiteDto> selectLatestDeliveries(DbSession dbSession, List<WebhookDto> webhooks) { public Map<String, WebhookDeliveryLiteDto> selectLatestDeliveries(DbSession dbSession, List<WebhookDto> webhooks) {
return webhooks.stream() 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())); .collect(toMap(WebhookDeliveryLiteDto::getWebhookUuid, identity()));
} }



+ 4
- 4
server/sonar-db-dao/src/main/java/org/sonar/db/webhook/WebhookDeliveryMapper.java View File

import java.util.List; import java.util.List;
import javax.annotation.CheckForNull; import javax.annotation.CheckForNull;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.RowBounds;
import org.sonar.db.Pagination;


public interface WebhookDeliveryMapper { public interface WebhookDeliveryMapper {




int countByWebhookUuid(@Param("webhookUuid") String webhookUuid); 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); 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); 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); void insert(WebhookDeliveryDto dto);



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

where webhook_uuid = #{webhookUuid,jdbcType=VARCHAR} where webhook_uuid = #{webhookUuid,jdbcType=VARCHAR}
</select> </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" /> select <include refid="sqlLiteColumns" />
from webhook_deliveries from webhook_deliveries
where webhook_uuid = #{webhookUuid,jdbcType=VARCHAR} where webhook_uuid = #{webhookUuid,jdbcType=VARCHAR}
order by created_at desc order by created_at desc
offset (#{pagination.startRowNumber,jdbcType=INTEGER}-1) rows fetch next #{pagination.pageSize,jdbcType=INTEGER} rows only
</select> </select>


<select id="countByProjectUuid" parameterType="String" resultType="int"> <select id="countByProjectUuid" parameterType="String" resultType="int">
where project_uuid = #{projectUuid,jdbcType=VARCHAR} where project_uuid = #{projectUuid,jdbcType=VARCHAR}
</select> </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" /> select <include refid="sqlLiteColumns" />
from webhook_deliveries from webhook_deliveries
where project_uuid = #{projectUuid,jdbcType=VARCHAR} where project_uuid = #{projectUuid,jdbcType=VARCHAR}
order by created_at desc order by created_at desc
offset (#{pagination.startRowNumber,jdbcType=INTEGER}-1) rows fetch next #{pagination.pageSize,jdbcType=INTEGER} rows only
</select> </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" /> select <include refid="sqlLiteColumns" />
from webhook_deliveries from webhook_deliveries
where ce_task_uuid = #{ceTaskUuid,jdbcType=VARCHAR} where ce_task_uuid = #{ceTaskUuid,jdbcType=VARCHAR}
order by created_at desc order by created_at desc
offset (#{pagination.startRowNumber,jdbcType=INTEGER}-1) rows fetch next #{pagination.pageSize,jdbcType=INTEGER} rows only
</select> </select>


<select id="countByCeTaskUuid" parameterType="String" resultType="int"> <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

import org.sonar.core.util.Uuids; import org.sonar.core.util.Uuids;
import org.sonar.db.DbClient; import org.sonar.db.DbClient;
import org.sonar.db.DbSession; import org.sonar.db.DbSession;
import org.sonar.db.Pagination;
import org.sonar.db.project.ProjectDto; import org.sonar.db.project.ProjectDto;
import org.sonar.db.webhook.WebhookDeliveryLiteDto; import org.sonar.db.webhook.WebhookDeliveryLiteDto;
import org.sonar.server.component.ComponentFinder; import org.sonar.server.component.ComponentFinder;
import static org.apache.commons.lang.StringUtils.isNotBlank; 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;
import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE; 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.core.util.Uuids.UUID_EXAMPLE_02;
import static org.sonar.server.es.SearchOptions.MAX_PAGE_SIZE; import static org.sonar.server.es.SearchOptions.MAX_PAGE_SIZE;
import static org.sonar.server.webhook.ws.WebhookWsSupport.copyDtoToProtobuf; import static org.sonar.server.webhook.ws.WebhookWsSupport.copyDtoToProtobuf;
try (DbSession dbSession = dbClient.openSession(false)) { try (DbSession dbSession = dbClient.openSession(false)) {
if (isNotBlank(webhookUuid)) { if (isNotBlank(webhookUuid)) {
totalElements = dbClient.webhookDeliveryDao().countDeliveriesByWebhookUuid(dbSession, 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); projectUuidMap = getProjectsDto(dbSession, deliveries);
} else if (projectKey != null) { } else if (projectKey != null) {
ProjectDto project = componentFinder.getProjectByKey(dbSession, projectKey); ProjectDto project = componentFinder.getProjectByKey(dbSession, projectKey);
projectUuidMap = new HashMap<>(); projectUuidMap = new HashMap<>();
projectUuidMap.put(project.getUuid(), project); projectUuidMap.put(project.getUuid(), project);
totalElements = dbClient.webhookDeliveryDao().countDeliveriesByProjectUuid(dbSession, project.getUuid()); 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 { } else {
totalElements = dbClient.webhookDeliveryDao().countDeliveriesByCeTaskUuid(dbSession, ceTaskId); 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); projectUuidMap = getProjectsDto(dbSession, deliveries);
} }
} }

Loading…
Cancel
Save