import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
-import java.util.stream.Stream;
import javax.annotation.Nonnull;
import org.sonar.ce.task.projectanalysis.component.Component;
import org.sonar.ce.task.projectanalysis.component.CrawlerDepthLimit;
@Override
public void execute(ComputationStep.Context context) {
boolean supportUpsert = dbClient.getDatabase().getDialect().supportsUpsert();
- try (DbSession dbSession = dbClient.openSession(false)) {
+ try (DbSession dbSession = dbClient.openSession(true)) {
Component root = treeRootHolder.getRoot();
MeasureVisitor visitor = new MeasureVisitor(dbSession, supportUpsert);
new DepthTraversalTypeAwareCrawler(visitor).visit(root);
-
+ dbSession.commit();
context.getStatistics()
.add("insertsOrUpdates", visitor.insertsOrUpdates);
}
Metric metric = metricRepository.getByKey(metricKey);
Predicate<Measure> notBestValueOptimized = BestValueOptimization.from(metric, component).negate();
Measure m = measuresByMetricKey.getValue();
- Stream.of(m)
- .filter(NonEmptyMeasure.INSTANCE)
- .filter(notBestValueOptimized)
- .map(measure -> measureToMeasureDto.toLiveMeasureDto(measure, metric, component))
- .forEach(lm -> {
- dtos.add(lm);
- metricIds.add(metric.getId());
- });
+ if (!NonEmptyMeasure.INSTANCE.test(m) || !notBestValueOptimized.test(m)) {
+ continue;
+ }
+
+ LiveMeasureDto lm = measureToMeasureDto.toLiveMeasureDto(m, metric, component);
+ dtos.add(lm);
+ metricIds.add(metric.getId());
}
+
if (supportUpsert) {
- dbClient.liveMeasureDao().upsert(dbSession, dtos);
- } else {
for (LiveMeasureDto dto : dtos) {
- dbClient.liveMeasureDao().insertOrUpdate(dbSession, dto);
+ dbClient.liveMeasureDao().upsert(dbSession, dto);
}
+ // The measures that no longer exist on the component must be deleted, for example
+ // when the coverage on a file goes to the "best value" 100%.
+ // The measures on deleted components are deleted by the step PurgeDatastoresStep
+ dbClient.liveMeasureDao().deleteByComponentUuidExcludingMetricIds(dbSession, component.getUuid(), metricIds);
+ } else {
+ dbClient.liveMeasureDao().deleteByComponent(dbSession, component.getUuid());
+ dtos.forEach(dto -> dbClient.liveMeasureDao().insert(dbSession, dto));
}
- insertsOrUpdates += dtos.size();
- // The measures that no longer exist on the component must be deleted, for example
- // when the coverage on a file goes to the "best value" 100%.
- // The measures on deleted components are deleted by the step PurgeDatastoresStep
- dbClient.liveMeasureDao().deleteByComponentUuidExcludingMetricIds(dbSession, component.getUuid(), metricIds);
dbSession.commit();
+ insertsOrUpdates += dtos.size();
}
}
*/
package org.sonar.db.measure;
-import com.google.common.collect.Iterables;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
mapper(dbSession).insert(dto, Uuids.create(), system2.now());
}
+ public void update(DbSession dbSession, LiveMeasureDto dto) {
+ mapper(dbSession).update(dto, system2.now());
+ }
+
public void insertOrUpdate(DbSession dbSession, LiveMeasureDto dto) {
LiveMeasureMapper mapper = mapper(dbSession);
long now = system2.now();
}
}
+ public void deleteByComponent(DbSession dbSession, String componentUuid) {
+ mapper(dbSession).deleteByComponent(componentUuid);
+ }
+
/**
- * Similar to {@link #insertOrUpdate(DbSession, LiveMeasureDto)}, except that:
- * <ul>
- * <li>it is batch session friendly (single same statement for both updates and inserts)</li>
- * <li>it triggers a single SQL request</li>
- * </ul>
- * <p>
+ * Similar to {@link #insertOrUpdate(DbSession, LiveMeasureDto)}, except that it triggers a single SQL request
* <strong>This method should not be called unless {@link Dialect#supportsUpsert()} is true</strong>
*/
- public int upsert(DbSession dbSession, Iterable<LiveMeasureDto> dtos) {
- for (LiveMeasureDto dto : dtos) {
- dto.setUuidForUpsert(Uuids.create());
- }
- int updated = 0;
- for (List<LiveMeasureDto> chunk : Iterables.partition(dtos, 100)) {
- updated += mapper(dbSession).upsert(chunk, system2.now());
- }
- return updated;
+ public int upsert(DbSession dbSession, LiveMeasureDto dto) {
+ dto.setUuidForUpsert(Uuids.create());
+ return mapper(dbSession).upsert(dto, system2.now());
}
- public int deleteByComponentUuidExcludingMetricIds(DbSession dbSession, String componentUuid, List<Integer> excludedMetricIds) {
- return mapper(dbSession).deleteByComponentUuidExcludingMetricIds(componentUuid, excludedMetricIds);
+ public void deleteByComponentUuidExcludingMetricIds(DbSession dbSession, String componentUuid, List<Integer> excludedMetricIds) {
+ mapper(dbSession).deleteByComponentUuidExcludingMetricIds(componentUuid, excludedMetricIds);
}
private static LiveMeasureMapper mapper(DbSession dbSession) {
underTest.insertOrUpdate(db.getSession(), measure3);
underTest.insertOrUpdate(db.getSession(), measureOtherComponent);
- int count = underTest.deleteByComponentUuidExcludingMetricIds(db.getSession(), "C1", Arrays.asList(1, 2));
+ underTest.deleteByComponentUuidExcludingMetricIds(db.getSession(), "C1", Arrays.asList(1, 2));
verifyTableSize(3);
verifyPersisted(measure1);
verifyPersisted(measure2);
verifyPersisted(measureOtherComponent);
- assertThat(count).isEqualTo(1);
+ }
+
+ @Test
+ public void deleteByComponentUuid() {
+ LiveMeasureDto measure1 = newLiveMeasure().setComponentUuid("C1").setMetricId(1);
+ LiveMeasureDto measure2 = newLiveMeasure().setComponentUuid("C1").setMetricId(2);
+ LiveMeasureDto measure3 = newLiveMeasure().setComponentUuid("C1").setMetricId(3);
+ LiveMeasureDto measureOtherComponent = newLiveMeasure().setComponentUuid("C2").setMetricId(3);
+ underTest.insertOrUpdate(db.getSession(), measure1);
+ underTest.insertOrUpdate(db.getSession(), measure2);
+ underTest.insertOrUpdate(db.getSession(), measure3);
+ underTest.insertOrUpdate(db.getSession(), measureOtherComponent);
+
+ underTest.deleteByComponent(db.getSession(), "C1");
+
+ verifyTableSize(1);
+ verifyPersisted(measureOtherComponent);
}
@Test
underTest.insertOrUpdate(db.getSession(), measure2);
underTest.insertOrUpdate(db.getSession(), measureOnOtherComponent);
- int count = underTest.deleteByComponentUuidExcludingMetricIds(db.getSession(), "C1", Collections.emptyList());
+ underTest.deleteByComponentUuidExcludingMetricIds(db.getSession(), "C1", Collections.emptyList());
- assertThat(count).isEqualTo(2);
verifyTableSize(1);
verifyPersisted(measureOnOtherComponent);
}
// insert
LiveMeasureDto dto = newLiveMeasure();
- int count = underTest.upsert(db.getSession(), asList(dto));
+ int count = underTest.upsert(db.getSession(), dto);
verifyPersisted(dto);
verifyTableSize(1);
assertThat(count).isEqualTo(1);
dto.setValue(dto.getValue() + 1);
dto.setVariation(dto.getVariation() + 10);
dto.setData(dto.getDataAsString() + "_new");
- count = underTest.upsert(db.getSession(), asList(dto));
+ count = underTest.upsert(db.getSession(), dto);
assertThat(count).isEqualTo(1);
verifyPersisted(dto);
verifyTableSize(1);
}
LiveMeasureDto dto = newLiveMeasure();
- underTest.upsert(db.getSession(), asList(dto));
+ underTest.upsert(db.getSession(), dto);
// update
- int count = underTest.upsert(db.getSession(), asList(dto));
+ int count = underTest.upsert(db.getSession(), dto);
assertThat(count).isEqualTo(0);
verifyPersisted(dto);
verifyTableSize(1);
}
LiveMeasureDto dto = newLiveMeasure().setData(RandomStringUtils.random(10_000));
- underTest.upsert(db.getSession(), asList(dto));
+ underTest.upsert(db.getSession(), dto);
// update
dto.setData(RandomStringUtils.random(dto.getDataAsString().length() + 10));
- int count = underTest.upsert(db.getSession(), asList(dto));
+ int count = underTest.upsert(db.getSession(), dto);
assertThat(count).isEqualTo(1);
verifyPersisted(dto);
verifyTableSize(1);
return;
}
LiveMeasureDto dto = newLiveMeasure().setData(RandomStringUtils.random(10_000));
- underTest.upsert(db.getSession(), asList(dto));
+ underTest.upsert(db.getSession(), dto);
// update
- int count = underTest.upsert(db.getSession(), asList(dto));
+ int count = underTest.upsert(db.getSession(), dto);
assertThat(count).isEqualTo(0);
verifyPersisted(dto);
verifyTableSize(1);
}
LiveMeasureDto dto = newLiveMeasure().setData(RandomStringUtils.random(10_000));
- underTest.upsert(db.getSession(), asList(dto));
+ underTest.upsert(db.getSession(), dto);
// update
dto.setData((String) null);
- int count = underTest.upsert(db.getSession(), asList(dto));
+ int count = underTest.upsert(db.getSession(), dto);
assertThat(count).isEqualTo(1);
verifyPersisted(dto);
verifyTableSize(1);
return;
}
LiveMeasureDto dto = newLiveMeasure().setVariation(40.0);
- underTest.upsert(db.getSession(), asList(dto));
+ underTest.upsert(db.getSession(), dto);
// update
dto.setVariation(50.0);
- int count = underTest.upsert(db.getSession(), asList(dto));
+ int count = underTest.upsert(db.getSession(), dto);
assertThat(count).isEqualTo(1);
verifyPersisted(dto);
verifyTableSize(1);
return;
}
LiveMeasureDto dto = newLiveMeasure().setVariation(40.0);
- underTest.upsert(db.getSession(), asList(dto));
+ underTest.upsert(db.getSession(), dto);
// update
dto.setVariation(null);
- int count = underTest.upsert(db.getSession(), asList(dto));
+ int count = underTest.upsert(db.getSession(), dto);
assertThat(count).isEqualTo(1);
verifyPersisted(dto);
verifyTableSize(1);
return;
}
LiveMeasureDto dto = newLiveMeasure().setVariation(null);
- underTest.upsert(db.getSession(), asList(dto));
+ underTest.upsert(db.getSession(), dto);
// update
dto.setVariation(40.0);
- int count = underTest.upsert(db.getSession(), asList(dto));
+ int count = underTest.upsert(db.getSession(), dto);
assertThat(count).isEqualTo(1);
verifyPersisted(dto);
verifyTableSize(1);
return;
}
LiveMeasureDto dto = newLiveMeasure().setValue(40.0);
- underTest.upsert(db.getSession(), asList(dto));
+ underTest.upsert(db.getSession(), dto);
// update
dto.setValue(50.0);
- int count = underTest.upsert(db.getSession(), asList(dto));
+ int count = underTest.upsert(db.getSession(), dto);
assertThat(count).isEqualTo(1);
verifyPersisted(dto);
verifyTableSize(1);
return;
}
LiveMeasureDto dto = newLiveMeasure().setValue(40.0);
- underTest.upsert(db.getSession(), asList(dto));
+ underTest.upsert(db.getSession(), dto);
// update
dto.setValue(null);
- int count = underTest.upsert(db.getSession(), asList(dto));
+ int count = underTest.upsert(db.getSession(), dto);
assertThat(count).isEqualTo(1);
verifyPersisted(dto);
verifyTableSize(1);
return;
}
LiveMeasureDto dto = newLiveMeasure().setValue(null);
- underTest.upsert(db.getSession(), asList(dto));
+ underTest.upsert(db.getSession(), dto);
// update
dto.setValue(40.0);
- int count = underTest.upsert(db.getSession(), asList(dto));
+ int count = underTest.upsert(db.getSession(), dto);
assertThat(count).isEqualTo(1);
verifyPersisted(dto);
verifyTableSize(1);
// insert 30
List<LiveMeasureDto> inserted = new ArrayList<>();
IntStream.range(0, 30).forEach(i -> inserted.add(newLiveMeasure()));
- int result = underTest.upsert(db.getSession(), inserted);
+ for (LiveMeasureDto dto : inserted) {
+ underTest.upsert(db.getSession(), dto);
+ }
verifyTableSize(30);
- assertThat(result).isEqualTo(30);
// update 10 with new values, update 5 without any change and insert new 50
List<LiveMeasureDto> upserted = new ArrayList<>();
});
upserted.addAll(inserted.subList(10, 15));
IntStream.range(0, 50).forEach(i -> upserted.add(newLiveMeasure()));
- result = underTest.upsert(db.getSession(), upserted);
+ for (LiveMeasureDto dto : upserted) {
+ underTest.upsert(db.getSession(), dto);
+ }
+ db.getSession().commit();
verifyTableSize(80);
- assertThat(result).isEqualTo(60);
}
private void verifyTableSize(int expectedSize) {