From 478383011bba15b19869380384aa569697706681 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Thu, 24 Apr 2014 18:05:36 +0200 Subject: [PATCH] Revert "SONAR-3437 Remove Measure from Hibernate model" This reverts commit cafd76d5317716817c0f15a402d820a6e80d19a9. --- .../sonar/batch/index/MeasurePersister.java | 1 + .../main/resources/META-INF/persistence.xml | 2 + .../jpa/session/DatabaseSessionTest.java | 34 +++++++++--- .../sonar/api/database/model/MeasureData.java | 21 ++++---- .../api/database/model/MeasureModel.java | 52 +++++++++++++++++-- 5 files changed, 86 insertions(+), 24 deletions(-) diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/MeasurePersister.java b/sonar-batch/src/main/java/org/sonar/batch/index/MeasurePersister.java index 0512f5b1aa3..80cac3ab4cb 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/MeasurePersister.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/MeasurePersister.java @@ -58,6 +58,7 @@ public final class MeasurePersister implements ScanPersister { SqlSession session = mybatis.openSession(); try { MeasureMapper mapper = session.getMapper(MeasureMapper.class); + for (Entry entry : measureCache.entries()) { String effectiveKey = entry.key()[0].toString(); Measure measure = entry.value(); diff --git a/sonar-core/src/main/resources/META-INF/persistence.xml b/sonar-core/src/main/resources/META-INF/persistence.xml index 79a9ad22f6b..034e3dffd10 100644 --- a/sonar-core/src/main/resources/META-INF/persistence.xml +++ b/sonar-core/src/main/resources/META-INF/persistence.xml @@ -11,6 +11,8 @@ org.sonar.api.database.configuration.Property org.sonar.api.database.model.User org.sonar.api.database.model.Snapshot + org.sonar.api.database.model.MeasureModel + org.sonar.api.database.model.MeasureData org.sonar.api.design.DependencyDto org.sonar.api.measures.Metric org.sonar.api.database.model.ResourceModel diff --git a/sonar-core/src/test/java/org/sonar/jpa/session/DatabaseSessionTest.java b/sonar-core/src/test/java/org/sonar/jpa/session/DatabaseSessionTest.java index 54d57081e68..99bb844283e 100644 --- a/sonar-core/src/test/java/org/sonar/jpa/session/DatabaseSessionTest.java +++ b/sonar-core/src/test/java/org/sonar/jpa/session/DatabaseSessionTest.java @@ -23,20 +23,19 @@ import org.hamcrest.Matchers; import org.junit.Before; import org.junit.Test; import org.junit.internal.matchers.IsCollectionContaining; +import org.sonar.api.database.model.MeasureModel; import org.sonar.api.database.model.ResourceModel; +import org.sonar.api.database.model.Snapshot; +import org.sonar.api.measures.CoreMetrics; +import org.sonar.api.measures.Metric; +import org.sonar.jpa.dao.MeasuresDao; import org.sonar.jpa.test.AbstractDbUnitTestCase; import javax.persistence.NonUniqueResultException; - +import java.sql.Date; import java.util.List; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.Assert.*; public class DatabaseSessionTest extends AbstractDbUnitTestCase { private static final Long NB_INSERTS = 20000l; @@ -50,6 +49,25 @@ public class DatabaseSessionTest extends AbstractDbUnitTestCase { project2 = new ResourceModel(ResourceModel.SCOPE_PROJECT, "mygroup:myartifact1", "JAV", null, "my name 2"); } + @Test + public void performanceTestOnBatchInserts() throws Exception { + getSession().save(project1); + Snapshot snapshot = new Snapshot(project1, true, "", new Date(1)); + getSession().save(snapshot); + getSession().save(CoreMetrics.CLASSES); + getSession().commit(); + + Metric metric = new MeasuresDao(getSession()).getMetric(CoreMetrics.CLASSES_KEY); + for (int i = 0; i < NB_INSERTS; i++) { + MeasureModel pm = new MeasureModel(metric.getId(), 1.0).setSnapshotId(snapshot.getId()); + getSession().save(pm); + } + + getSession().commit(); + assertEquals(NB_INSERTS, getHQLCount(MeasureModel.class)); + + } + @Test public void testGetSingleResultWithNoResults() { assertNull(getSession().getSingleResult(ResourceModel.class, "name", "test")); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/database/model/MeasureData.java b/sonar-plugin-api/src/main/java/org/sonar/api/database/model/MeasureData.java index 09b8f3b118a..b0275b64c39 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/database/model/MeasureData.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/database/model/MeasureData.java @@ -23,17 +23,23 @@ import com.google.common.base.Charsets; import com.google.common.base.Throwables; import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; +import org.sonar.api.database.BaseIdentifiable; +import javax.persistence.*; import java.io.UnsupportedEncodingException; -public class MeasureData { - - private Integer id; +@Entity +@Table(name = "measure_data") +public class MeasureData extends BaseIdentifiable { + @OneToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "measure_id") private MeasureModel measure; + @Column(name = "snapshot_id", updatable = true, nullable = true) private Integer snapshotId; + @Column(name = "data", updatable = true, nullable = true, length = 167772150) private byte[] data; public MeasureData(MeasureModel measure) { @@ -53,14 +59,6 @@ public class MeasureData { public MeasureData() { } - public Integer getId() { - return id; - } - - public void setId(Integer id) { - this.id = id; - } - public MeasureModel getMeasure() { return measure; } @@ -106,3 +104,4 @@ public class MeasureData { .toString(); } } + diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/database/model/MeasureModel.java b/sonar-plugin-api/src/main/java/org/sonar/api/database/model/MeasureModel.java index 795af64bb27..f2a00deccfb 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/database/model/MeasureModel.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/database/model/MeasureModel.java @@ -25,62 +25,96 @@ import org.sonar.api.database.DatabaseSession; import org.sonar.api.measures.Metric; import org.sonar.api.rules.RulePriority; +import javax.persistence.*; + import java.util.ArrayList; import java.util.Date; import java.util.List; +/** + * This class is the Hibernate model to store a measure in the DB + */ +@Entity +@Table(name = "project_measures") public class MeasureModel implements Cloneable { public static final int TEXT_VALUE_LENGTH = 96; + @Id + @Column(name = "id") + @GeneratedValue private Long id; + @Column(name = "value", updatable = true, nullable = true, precision = 30, scale = 20) private Double value = 0.0; + @Column(name = "text_value", updatable = true, nullable = true, length = TEXT_VALUE_LENGTH) private String textValue; + @Column(name = "tendency", updatable = true, nullable = true) private Integer tendency; + @Column(name = "metric_id", updatable = false, nullable = false) private Integer metricId; + @Column(name = "snapshot_id", updatable = true, nullable = true) private Integer snapshotId; + @Column(name = "project_id", updatable = true, nullable = true) private Integer projectId; + @Column(name = "description", updatable = true, nullable = true, length = 4000) private String description; + @Temporal(TemporalType.TIMESTAMP) + @Column(name = "measure_date", updatable = true, nullable = true) private Date measureDate; + @Column(name = "rule_id", updatable = true, nullable = true) private Integer ruleId; /** * @deprecated since 2.5 See http://jira.codehaus.org/browse/SONAR-2007 */ @Deprecated - private Integer rulesCategoryId;// NOSONAR this field is kept for backward-compatiblity of API + @Column(name = "rules_category_id", nullable = true) + private Integer rulesCategoryId;//NOSONAR this field is kept for backward-compatiblity of API + @Column(name = "rule_priority", updatable = false, nullable = true) + @Enumerated(EnumType.ORDINAL) private RulePriority rulePriority; + @Column(name = "alert_status", updatable = true, nullable = true, length = 5) private String alertStatus; + @Column(name = "alert_text", updatable = true, nullable = true, length = 4000) private String alertText; + @Column(name = "variation_value_1", updatable = true, nullable = true) private Double variationValue1; + @Column(name = "variation_value_2", updatable = true, nullable = true) private Double variationValue2; + @Column(name = "variation_value_3", updatable = true, nullable = true) private Double variationValue3; + @Column(name = "variation_value_4", updatable = true, nullable = true) private Double variationValue4; + @Column(name = "variation_value_5", updatable = true, nullable = true) private Double variationValue5; + @Column(name = "url", updatable = true, nullable = true, length = 2000) private String url; + @OneToMany(mappedBy = "measure", fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}) private List measureData = new ArrayList(); + @Column(name = "characteristic_id", nullable = true) private Integer characteristicId; + @Column(name = "person_id", updatable = true, nullable = true) private Integer personId; public Long getId() { @@ -361,7 +395,7 @@ public class MeasureModel implements Cloneable { /** * Use setData() instead */ - // @Deprecated + //@Deprecated public void setMeasureData(MeasureData data) { measureData.clear(); if (data != null) { @@ -446,11 +480,19 @@ public class MeasureModel implements Cloneable { * Saves the current object to database * * @return the current object - * @deprecated since 4.4 We don't use Hibernate anymore. See {@link MeasureMapper}. */ - @Deprecated public MeasureModel save(DatabaseSession session) { - throw new UnsupportedOperationException(); + MeasureData data = getMeasureData(); + setMeasureData(null); + session.save(this); + + if (data != null) { + data.setMeasure(session.getEntity(MeasureModel.class, getId())); + data.setSnapshotId(snapshotId); + session.save(data); + setMeasureData(data); + } + return this; } public Integer getCharacteristicId() { -- 2.39.5