.forEach(lm -> { if (supportUpsert) { dao.upsert(dbSession, lm); } else { dao.insertOrUpdate(dbSession, lm); dao.deleteByComponentUuidExcludingMetricIds(dbSession, component.getUuid(), metricIds); dbSession.commit();
@CheckForNull private String findDataFromComponent(DbSession dbSession, ComponentDto component) { return dbClient.liveMeasureDao().selectMeasure(dbSession, component.uuid(), CoreMetrics.DUPLICATIONS_DATA_KEY) .map(LiveMeasureDto::getDataAsString) .orElse(null); } }
public Optional<LiveMeasureDto> selectMeasure(DbSession dbSession, String componentUuid, String metricKey) { List<LiveMeasureDto> measures = selectByComponentUuidsAndMetricKeys(dbSession, singletonList(componentUuid), singletonList(metricKey)); // couple of columns [component_uuid, metric_id] is unique. List can't have more than 1 item. if (measures.size() == 1) { return Optional.of(measures.get(0)); } return Optional.empty(); }
@Test public void selectByComponentUuidsAndMetricIds_returns_empty_list_if_component_does_not_match() { LiveMeasureDto measure = newLiveMeasure(); underTest.insert(db.getSession(), measure); List<LiveMeasureDto> selected = underTest.selectByComponentUuidsAndMetricIds(db.getSession(), singletonList("_missing_"), singletonList(measure.getMetricId())); assertThat(selected).isEmpty(); }
@Test public void selectByComponentUuidsAndMetricKeys_returns_empty_list_if_component_does_not_match() { LiveMeasureDto measure = newLiveMeasure().setMetricId(metric.getId()); underTest.insert(db.getSession(), measure); List<LiveMeasureDto> selected = underTest.selectByComponentUuidsAndMetricKeys(db.getSession(), singletonList("_missing_"), singletonList(metric.getKey())); assertThat(selected).isEmpty(); }
.collect(uniqueIndex(MetricDto::getId)); List<String> componentUuids = components.stream().map(ComponentDto::uuid).collect(toArrayList(components.size())); List<LiveMeasureDto> dbMeasures = dbClient.liveMeasureDao().selectByComponentUuidsAndMetricIds(dbSession, componentUuids, metricsPerId.keySet()); .forEach(m -> dbClient.liveMeasureDao().insertOrUpdate(dbSession, m)); projectIndexer.commitAndIndex(dbSession, singleton(project), ProjectIndexer.Cause.MEASURE_CHANGE);
@Test public void insert_data() { byte[] data = "text_value".getBytes(StandardCharsets.UTF_8); MetricDto metric = db.measures().insertMetric(); ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); LiveMeasureDto measure = newLiveMeasure(file, metric).setData(data); 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"); }
@Test public void insertOrUpdate() { // insert LiveMeasureDto dto = newLiveMeasure(); underTest.insertOrUpdate(db.getSession(), dto); verifyPersisted(dto); verifyTableSize(1); // update dto.setValue(dto.getValue() + 1); dto.setVariation(dto.getVariation() + 10); dto.setData(dto.getDataAsString() + "_new"); underTest.insertOrUpdate(db.getSession(), dto); verifyPersisted(dto); verifyTableSize(1); }
@Test public void deleteByComponentUuidExcludingMetricIds_with_empty_metrics() { LiveMeasureDto measure1 = newLiveMeasure().setComponentUuid("C1").setMetricId(1); LiveMeasureDto measure2 = newLiveMeasure().setComponentUuid("C1").setMetricId(2); LiveMeasureDto measureOnOtherComponent = newLiveMeasure().setComponentUuid("C2").setMetricId(2); underTest.insertOrUpdate(db.getSession(), measure1); underTest.insertOrUpdate(db.getSession(), measure2); underTest.insertOrUpdate(db.getSession(), measureOnOtherComponent); int count = underTest.deleteByComponentUuidExcludingMetricIds(db.getSession(), "C1", Collections.emptyList()); assertThat(count).isEqualTo(2); verifyTableSize(1); verifyPersisted(measureOnOtherComponent); }
private List<LiveMeasureDto> searchMeasures() { return dbClient.liveMeasureDao().selectByComponentUuidsAndMetricIds(dbSession, projects.stream().map(ComponentDto::uuid).collect(MoreCollectors.toArrayList(projects.size())), metrics.stream().map(MetricDto::getId).collect(MoreCollectors.toArrayList(metrics.size()))); }
private long computeNcloc() { try (DbSession dbSession = dbClient.openSession(false)) { SumNclocDbQuery query = SumNclocDbQuery.builder() .setOnlyPrivateProjects(false) .setOrganizationUuid(defaultOrganizationProvider.get().getUuid()) .build(); return dbClient.liveMeasureDao().sumNclocOfBiggestLongLivingBranch(dbSession, query); } } }
@Test public void selectTreeByQuery() { List<LiveMeasureDto> results = new ArrayList<>(); MetricDto metric = db.measures().insertMetric(); ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); underTest.insert(db.getSession(), newLiveMeasure(file, metric).setValue(3.14)); underTest.selectTreeByQuery(db.getSession(), project, MeasureTreeQuery.builder() .setMetricIds(singleton(metric.getId())) .setStrategy(MeasureTreeQuery.Strategy.LEAVES).build(), context -> results.add(context.getResultObject())); assertThat(results).hasSize(1); LiveMeasureDto result = results.get(0); assertThat(result.getComponentUuid()).isEqualTo(file.uuid()); assertThat(result.getMetricId()).isEqualTo(metric.getId()); assertThat(result.getValue()).isEqualTo(3.14); }
@Test public void upsert_does_not_update_row_if_values_are_not_changed() { if (!db.getDbClient().getDatabase().getDialect().supportsUpsert()) { return; } LiveMeasureDto dto = newLiveMeasure(); underTest.upsert(db.getSession(), dto); // update int count = underTest.upsert(db.getSession(), dto); assertThat(count).isEqualTo(0); verifyPersisted(dto); verifyTableSize(1); }
private Table<String, MetricDto, ComponentTreeData.Measure> searchMeasuresByComponentUuidAndMetric(DbSession dbSession, ComponentDto baseComponent, ComponentTreeQuery componentTreeQuery, List<ComponentDto> components, List<MetricDto> metrics) { Map<Integer, MetricDto> metricsById = Maps.uniqueIndex(metrics, MetricDto::getId); MeasureTreeQuery measureQuery = MeasureTreeQuery.builder() .setStrategy(MeasureTreeQuery.Strategy.valueOf(componentTreeQuery.getStrategy().name())) .setNameOrKeyQuery(componentTreeQuery.getNameOrKeyQuery()) .setQualifiers(componentTreeQuery.getQualifiers()) .setMetricIds(new ArrayList<>(metricsById.keySet())) .build(); Table<String, MetricDto, ComponentTreeData.Measure> measuresByComponentUuidAndMetric = HashBasedTable.create(components.size(), metrics.size()); dbClient.liveMeasureDao().selectTreeByQuery(dbSession, baseComponent, measureQuery, result -> { LiveMeasureDto measureDto = result.getResultObject(); measuresByComponentUuidAndMetric.put( measureDto.getComponentUuid(), metricsById.get(measureDto.getMetricId()), ComponentTreeData.Measure.createFromMeasureDto(measureDto)); }); addBestValuesToMeasures(measuresByComponentUuidAndMetric, components, metrics); return measuresByComponentUuidAndMetric; }
@Test public void selectByComponentUuidAndMetricKey() { LiveMeasureDto measure = newLiveMeasure().setMetricId(metric.getId()); underTest.insert(db.getSession(), measure); Optional<LiveMeasureDto> selected = underTest.selectByComponentUuidAndMetricKey(db.getSession(), measure.getComponentUuid(), metric.getKey()); assertThat(selected).isNotEmpty(); assertThat(selected.get()).isEqualToComparingFieldByField(measure); }
@SafeVarargs public final LiveMeasureDto insertLiveMeasure(ComponentDto component, MetricDto metric, Consumer<LiveMeasureDto>... consumers) { LiveMeasureDto dto = newLiveMeasure(component, metric); Arrays.stream(consumers).forEach(c -> c.accept(dto)); dbClient.liveMeasureDao().insert(dbSession, dto); dbSession.commit(); return dto; }
.collect(uniqueIndex(MetricDto::getId)); List<String> componentUuids = components.stream().map(ComponentDto::uuid).collect(toArrayList(components.size())); List<LiveMeasureDto> dbMeasures = dbClient.liveMeasureDao().selectByComponentUuidsAndMetricIds(dbSession, componentUuids, metricsPerId.keySet()); matrix.getChanged().forEach(m -> dbClient.liveMeasureDao().insertOrUpdate(dbSession, m, null)); projectIndexer.commitAndIndex(dbSession, singleton(project), ProjectIndexer.Cause.MEASURE_CHANGE);
@Test public void selectMeasure() { MetricDto metric = db.measures().insertMetric(); LiveMeasureDto stored = newLiveMeasure().setMetricId(metric.getId()); underTest.insert(db.getSession(), stored); // metric exists but not component assertThat(underTest.selectMeasure(db.getSession(), "_missing_", metric.getKey())).isEmpty(); // component exists but not metric assertThat(underTest.selectMeasure(db.getSession(), stored.getComponentUuid(), "_missing_")).isEmpty(); // component and metric don't match assertThat(underTest.selectMeasure(db.getSession(), "_missing_", "_missing_")).isEmpty(); // matches assertThat(underTest.selectMeasure(db.getSession(), stored.getComponentUuid(), metric.getKey()).get()) .isEqualToComparingFieldByField(stored); }
private LiveMeasureDto insertMeasure(String componentUuid, String projectUuid, Metric metric) { LiveMeasureDto measure = newLiveMeasure() .setComponentUuid(componentUuid) .setProjectUuid(projectUuid) .setMetricId(metricRepository.getByKey(metric.getKey()).getId()); dbClient.liveMeasureDao().insertOrUpdate(db.getSession(), measure); return measure; }
@Test public void deleteByComponentUuidExcludingMetricIds() { 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); int count = underTest.deleteByComponentUuidExcludingMetricIds(db.getSession(), "C1", Arrays.asList(1, 2)); verifyTableSize(3); verifyPersisted(measure1); verifyPersisted(measure2); verifyPersisted(measureOtherComponent); assertThat(count).isEqualTo(1); }