Pārlūkot izejas kodu

SONAR-11917 Improved copy project measures step in portfolio recalculation

Changed implementation of copy project measures step in portfolio recalculation to use scrolling select.
tags/7.8
Michal Duda pirms 5 gadiem
vecāks
revīzija
a4352cc55d

+ 9
- 0
server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureDao.java Parādīt failu

@@ -56,6 +56,15 @@ public class LiveMeasureDao implements Dao {
componentUuids -> mapper(dbSession).selectByComponentUuidsAndMetricIds(componentUuids, metricIds));
}

public void scrollSelectByComponentUuidAndMetricKeys(DbSession dbSession, String componentUuid, Collection<String> metricIds,
ResultHandler<LiveMeasureDto> handler) {
if (metricIds.isEmpty()) {
return;
}

mapper(dbSession).scrollSelectByComponentUuidAndMetricKeys(componentUuid, metricIds, handler);
}

public List<LiveMeasureDto> selectByComponentUuidsAndMetricKeys(DbSession dbSession, Collection<String> largeComponentUuids, Collection<String> metricKeys) {
if (largeComponentUuids.isEmpty() || metricKeys.isEmpty()) {
return Collections.emptyList();

+ 6
- 1
server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureMapper.java Parādīt failu

@@ -37,6 +37,11 @@ public interface LiveMeasureMapper {
@Param("componentUuids") Collection<String> componentUuids,
@Param("metricKeys") Collection<String> metricKeys);

void scrollSelectByComponentUuidAndMetricKeys(
@Param("componentUuid") String componentUuid,
@Param("metricKeys") Collection<String> metricKeys,
ResultHandler<LiveMeasureDto> handler);

LiveMeasureDto selectByComponentUuidAndMetricKey(
@Param("componentUuid") String componentUuid,
@Param("metricKey") String metricKey);
@@ -63,7 +68,7 @@ public interface LiveMeasureMapper {
int update(
@Param("dto") LiveMeasureDto dto,
@Param("now") long now);
int upsert(
@Param("dtos") List<LiveMeasureDto> dtos,
@Param("now") long now);

+ 9
- 0
server/sonar-db-dao/src/main/resources/org/sonar/db/measure/LiveMeasureMapper.xml Parādīt failu

@@ -169,6 +169,15 @@
</if>
</sql>

<select id="scrollSelectByComponentUuidAndMetricKeys" resultType="org.sonar.db.measure.LiveMeasureDto" fetchSize="${_scrollFetchSize}"
resultSetType="FORWARD_ONLY">
select <include refid="columns"/> from live_measures lm
inner join metrics m on m.id = lm.metric_id
where
m.name in <foreach item="metricKey" collection="metricKeys" open="(" separator="," close=")">#{metricKey, jdbcType=VARCHAR}</foreach>
and lm.component_uuid = #{componentUuid, jdbcType=VARCHAR}
</select>

<select id="selectTreeByQuery" parameterType="map" resultType="org.sonar.db.measure.LiveMeasureDto" fetchSize="${_scrollFetchSize}" resultSetType="FORWARD_ONLY">
select <include refid="columns"/> from live_measures lm
inner join projects p on p.uuid = lm.component_uuid

+ 36
- 2
server/sonar-db-dao/src/test/java/org/sonar/db/measure/LiveMeasureDaoTest.java Parādīt failu

@@ -30,6 +30,7 @@ import org.apache.commons.lang.RandomStringUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.internal.util.collections.Sets;
import org.sonar.api.utils.System2;
import org.sonar.db.DbTester;
import org.sonar.db.component.BranchType;
@@ -208,6 +209,40 @@ public class LiveMeasureDaoTest {
assertThat(result.getValue()).isEqualTo(3.14);
}

@Test
public void scrollSelectByComponentUuidAndMetricKeys_for_non_empty_metric_set() {
List<LiveMeasureDto> results = new ArrayList<>();
MetricDto metric = db.measures().insertMetric();
MetricDto metric2 = db.measures().insertMetric();
ComponentDto project = db.components().insertPrivateProject();
ComponentDto project2 = db.components().insertPrivateProject();
underTest.insert(db.getSession(), newLiveMeasure(project, metric).setValue(3.14));
underTest.insert(db.getSession(), newLiveMeasure(project, metric2).setValue(4.54));
underTest.insert(db.getSession(), newLiveMeasure(project2, metric).setValue(99.99));
underTest.scrollSelectByComponentUuidAndMetricKeys(db.getSession(), project.uuid(), Sets.newSet(metric.getKey(), metric2.getKey()),
context -> results.add(context.getResultObject()));

assertThat(results).hasSize(2);
LiveMeasureDto result = results.stream().filter(lm -> lm.getMetricId() == metric.getId()).findFirst().get();
assertThat(result.getComponentUuid()).isEqualTo(project.uuid());
assertThat(result.getMetricId()).isEqualTo(metric.getId());
assertThat(result.getValue()).isEqualTo(3.14);
LiveMeasureDto result2 = results.stream().filter(lm -> lm.getMetricId() == metric2.getId()).findFirst().get();
assertThat(result2.getComponentUuid()).isEqualTo(project.uuid());
assertThat(result2.getMetricId()).isEqualTo(metric2.getId());
assertThat(result2.getValue()).isEqualTo(4.54);
}

@Test
public void scrollSelectByComponentUuidAndMetricKeys_for_empty_metric_set() {
List<LiveMeasureDto> results = new ArrayList<>();
ComponentDto project = db.components().insertPrivateProject();
underTest.scrollSelectByComponentUuidAndMetricKeys(db.getSession(), project.uuid(), Sets.newSet(),
context -> results.add(context.getResultObject()));

assertThat(results).isEmpty();
}

@Test
public void selectTreeByQuery_with_empty_results() {
List<LiveMeasureDto> results = new ArrayList<>();
@@ -311,7 +346,6 @@ public class LiveMeasureDaoTest {

underTest.insert(db.getSession(), measure);


LiveMeasureDto result = underTest.selectMeasure(db.getSession(), file.uuid(), metric.getKey()).orElseThrow(() -> new IllegalArgumentException("Measure not found"));
assertThat(new String(result.getData(), StandardCharsets.UTF_8)).isEqualTo("text_value");
assertThat(result.getDataAsString()).isEqualTo("text_value");
@@ -451,7 +485,7 @@ public class LiveMeasureDaoTest {
underTest.upsert(db.getSession(), asList(dto));

// update
dto.setData((String)null);
dto.setData((String) null);
int count = underTest.upsert(db.getSession(), asList(dto));
assertThat(count).isEqualTo(1);
verifyPersisted(dto);

Notiek ielāde…
Atcelt
Saglabāt