]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2501 purge project graphs
authorSimon Brandhof <simon.brandhof@gmail.com>
Tue, 29 Jan 2013 09:40:17 +0000 (10:40 +0100)
committerSimon Brandhof <simon.brandhof@gmail.com>
Tue, 29 Jan 2013 11:28:32 +0000 (12:28 +0100)
13 files changed:
sonar-core/src/main/java/org/sonar/core/component/ComponentVertex.java
sonar-core/src/main/java/org/sonar/core/component/ResourceComponent.java
sonar-core/src/main/java/org/sonar/core/component/ScanGraphStore.java
sonar-core/src/main/java/org/sonar/core/graph/jdbc/GraphDto.java
sonar-core/src/main/java/org/sonar/core/purge/PurgeCommands.java
sonar-core/src/main/java/org/sonar/core/purge/PurgeMapper.java
sonar-core/src/main/resources/org/sonar/core/graph/jdbc/GraphDtoMapper.xml
sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl
sonar-core/src/main/resources/org/sonar/core/purge/PurgeMapper.xml
sonar-core/src/test/java/org/sonar/core/component/ResourceComponentTest.java
sonar-core/src/test/java/org/sonar/core/graph/jdbc/GraphDaoTest.java
sonar-core/src/test/resources/org/sonar/core/graph/jdbc/GraphDaoTest/shared.xml
sonar-server/src/main/webapp/WEB-INF/db/migrate/370_create_graphs.rb

index e4fc8b75b16c5f0696ded0251b6f6309ac980310..8d139007b36f54aff2c59c849031707bd3d2d9a2 100644 (file)
@@ -42,6 +42,7 @@ public class ComponentVertex extends BeanVertex implements Component {
     setProperty("qualifier", component.qualifier());
     if (component instanceof ResourceComponent) {
       setProperty("sid", ((ResourceComponent) component).snapshotId());
+      setProperty("rid", ((ResourceComponent) component).resourceId());
     }
   }
 }
index c200a5bf4a4b3093da87aef50efb5c27a8f6d30d..b519d7a922dabef771bde1dbf6e88a7433248122 100644 (file)
@@ -30,6 +30,7 @@ class ResourceComponent implements Component {
   private String name;
   private String qualifier;
   private Long snapshotId;
+  private Long resourceId;
 
   ResourceComponent(Resource resource, @Nullable Snapshot snapshot) {
     this.key = resource.getEffectiveKey();
@@ -37,6 +38,7 @@ class ResourceComponent implements Component {
     this.qualifier = resource.getQualifier();
     if (snapshot != null && snapshot.getId() != null) {
       this.snapshotId = snapshot.getId().longValue();
+      this.resourceId = snapshot.getResourceId().longValue();
     }
   }
 
@@ -59,4 +61,8 @@ class ResourceComponent implements Component {
   public Long snapshotId() {
     return snapshotId;
   }
+
+  public Long resourceId() {
+    return resourceId;
+  }
 }
\ No newline at end of file
index 0af38668cabfa8cb20eab586e0af7fca7bd12ca8..2baaddc271cb46f1b3ab7f6cd7b153f4932b9fd9 100644 (file)
@@ -58,6 +58,7 @@ public class ScanGraphStore {
                 .setFormat("graphson")
                 .setPerspective(builder.getPerspectiveKey())
                 .setVersion(1)
+                .setResourceId((Long) component.element().getProperty("rid"))
                 .setSnapshotId(snapshotId)
                 .setRootVertexId(component.element().getId().toString())
               );
index 64ae196f765f3bc21c51681fb60644b84dd4818d..d081cfe8d61a3b0001f21be6bebfce0724bb91be 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.core.graph.jdbc;
 
 public class GraphDto {
   private long id;
+  private long resourceId;
   private long snapshotId;
   private String format;
   private String perspective;
@@ -37,6 +38,15 @@ public class GraphDto {
     return this;
   }
 
+  public long getResourceId() {
+    return resourceId;
+  }
+
+  public GraphDto setResourceId(long resourceId) {
+    this.resourceId = resourceId;
+    return this;
+  }
+
   public long getSnapshotId() {
     return snapshotId;
   }
index 9d3b77a486a7e87bcc615a79b04c7f8f2c557a04..13f5b9bea230586b8fc9fbc4871e091cc291a524 100644 (file)
@@ -111,6 +111,11 @@ class PurgeCommands {
     }
     session.commit();
 
+    for (Long resourceId : resourceIds) {
+      purgeMapper.deleteResourceGraphs(resourceId);
+    }
+    session.commit();
+
     for (Long resourceId : resourceIds) {
       purgeMapper.deleteResource(resourceId);
     }
@@ -162,6 +167,11 @@ class PurgeCommands {
     }
     session.commit();
 
+    for (Long snapshotId : snapshotIds) {
+      purgeMapper.deleteSnapshotGraphs(snapshotId);
+    }
+    session.commit();
+
     for (Long snapshotId : snapshotIds) {
       purgeMapper.deleteSnapshot(snapshotId);
     }
@@ -195,6 +205,11 @@ class PurgeCommands {
     }
     session.commit();
 
+    for (Long snapshotId : snapshotIds) {
+      purgeMapper.deleteSnapshotGraphs(snapshotId);
+    }
+    session.commit();
+
     List<Long> metricIdsWithoutHistoricalData = purgeMapper.selectMetricIdsWithoutHistoricalData();
     if (!metricIdsWithoutHistoricalData.isEmpty()) {
       for (Long snapshotId : snapshotIds) {
index 3dedc06b3f08aee0d8345fc71763791bc2e63ae6..84234fa1caca3215fe21b2c0f0f5692d701344c6 100644 (file)
@@ -45,6 +45,8 @@ public interface PurgeMapper {
 
   void deleteSnapshotViolations(long snapshotId);
 
+  void deleteSnapshotGraphs(long snapshotId);
+
   List<Long> selectMetricIdsWithoutHistoricalData();
 
   List<Long> selectCharacteristicIdsToPurge();
@@ -81,6 +83,8 @@ public interface PurgeMapper {
 
   void deleteResourceActionPlans(long resourceId);
 
+  void deleteResourceGraphs(long resourceId);
+
   void deleteAuthors(long developerId);
 
   void closeResourceReviews(long resourceId);
index 81d9dec73271a2d094d56d833fbcc161106e6daa..ee684f90ddf6f5cd6922ef37b2848622da009849 100644 (file)
@@ -4,13 +4,13 @@
 <mapper namespace="org.sonar.core.graph.jdbc.GraphDtoMapper">
 
   <select id="selectBySnapshot" parameterType="map" resultType="Graph">
-    SELECT id, snapshot_id as snapshotId, format, version, perspective, root_vertex_id as rootVertexId, data
+    SELECT id, resource_id as resourceId, snapshot_id as snapshotId, format, version, perspective, root_vertex_id as rootVertexId, data
     FROM graphs
     WHERE snapshot_id = #{sid} AND perspective = #{perspective}
   </select>
 
   <select id="selectByComponent" parameterType="map" resultType="Graph">
-    SELECT g.id, g.snapshot_id as snapshotId, g.format, g.version, g.perspective, g.root_vertex_id as rootVertexId, g.data
+    SELECT g.id, g.resource_id as resourceId, g.snapshot_id as snapshotId, g.format, g.version, g.perspective, g.root_vertex_id as rootVertexId, g.data
     FROM graphs g, snapshots s
     WHERE g.perspective = #{perspective} AND g.snapshot_id=s.id AND s.islast=${_true} and s.project_id=(
     select id from projects where enabled=${_true} and kee=#{key} and person_id is null and copy_resource_id is null
@@ -19,9 +19,9 @@
 
   <insert id="insert" parameterType="Graph" useGeneratedKeys="false">
     insert into graphs
-    (snapshot_id, format, version, perspective, root_vertex_id, data, created_at, updated_at)
+    (resource_id, snapshot_id, format, version, perspective, root_vertex_id, data, created_at, updated_at)
     values (
-    #{snapshotId}, #{format}, #{version}, #{perspective}, #{rootVertexId},
+    #{resourceId}, #{snapshotId}, #{format}, #{version}, #{perspective}, #{rootVertexId},
     #{data}, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
   </insert>
 
@@ -31,9 +31,9 @@
       select graphs_seq.NEXTVAL from DUAL
     </selectKey>
     insert into graphs
-    (id, snapshot_id, format, version, perspective, root_vertex_id, data, created_at, updated_at)
+    (id, resource_id, snapshot_id, format, version, perspective, root_vertex_id, data, created_at, updated_at)
     values (
-    #{id}, #{snapshotId}, #{format}, #{version}, #{perspective}, #{rootVertexId},
+    #{id}, #{resourceId}, #{snapshotId}, #{format}, #{version}, #{perspective}, #{rootVertexId},
     #{data}, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
   </insert>
 </mapper>
index 413c2f65832645e38d220e19cb84ea206ee80853..aea283eb183c3c4fde50bc3d27212bfc06a2551a 100644 (file)
@@ -501,6 +501,7 @@ CREATE TABLE "MEASURE_FILTER_FAVOURITES" (
 
 CREATE TABLE "GRAPHS" (
   "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+  "RESOURCE_ID" INTEGER NOT NULL,
   "SNAPSHOT_ID" INTEGER NOT NULL,
   "FORMAT" VARCHAR(20),
   "PERSPECTIVE" VARCHAR(30),
index 33975927cc12e171a7ce1bc78c680b4f09b221ea..affa97b86b1d0407ac62dac1ed60112654acf225 100644 (file)
     delete from rule_failures where snapshot_id=#{id}
   </delete>
 
+  <delete id="deleteSnapshotGraphs" parameterType="long">
+    delete from graphs where snapshot_id=#{id}
+  </delete>
+
   <delete id="deleteSnapshotDependencies" parameterType="long">
     delete from dependencies where from_snapshot_id=#{id} or to_snapshot_id=#{id} or project_snapshot_id=#{id}
   </delete>
   <delete id="deleteSnapshotWastedMeasures" parameterType="map">
     delete from project_measures where snapshot_id=#{sid} and
     (rule_id is not null or person_id is not null or metric_id in
-      <foreach item="mid" index="index" collection="mids" open="(" separator="," close=")">#{mid}</foreach>
+    <foreach item="mid" index="index" collection="mids" open="(" separator="," close=")">#{mid}</foreach>
     )
   </delete>
 
   <delete id="deleteSnapshotMeasuresOnCharacteristics" parameterType="map">
     delete from project_measures where snapshot_id=#{sid}
     and (
-      <foreach item="cid" index="index" collection="cids" open="" separator=" OR " close="">characteristic_id=#{cid}</foreach>
+    <foreach item="cid" index="index" collection="cids" open="" separator=" OR " close="">characteristic_id=#{cid}</foreach>
     )
   </delete>
 
     delete from action_plans where project_id=#{id}
   </delete>
 
+  <delete id="deleteResourceGraphs" parameterType="long">
+    delete from graphs where resource_id=#{id}
+  </delete>
+
   <delete id="deleteAuthors" parameterType="long">
     delete from authors where person_id=#{id}
   </delete>
index efdf18a307dae7249ebb4dbede634f3deefa3c58..61c55b931c9eb33d3a69d2ecd37f69c146fe4465 100644 (file)
@@ -27,19 +27,22 @@ import static org.fest.assertions.Assertions.assertThat;
 
 public class ResourceComponentTest {
   @Test
-  public void snapshot_id_should_be_optional() {
+  public void db_ids_should_be_optional() {
     ResourceComponent component = new ResourceComponent(new File("foo.c"), new Snapshot());
 
     assertThat(component.snapshotId()).isNull();
+    assertThat(component.resourceId()).isNull();
   }
 
   @Test
-  public void snapshot_id_should_be_set() {
+  public void db_ids_should_be_set() {
     Snapshot snapshot = new Snapshot();
     snapshot.setId(123);
+    snapshot.setResourceId(456);
     ResourceComponent component = new ResourceComponent(new File("foo.c"), snapshot);
 
     assertThat(component.snapshotId()).isEqualTo(123);
+    assertThat(component.resourceId()).isEqualTo(456);
   }
 
   @Test
index 38d6cc8dfc30e979eec830461070497c3dd82fbb..09f46951ae2a9a5983199b4d3a5a318211c69fb3 100644 (file)
@@ -39,6 +39,7 @@ public class GraphDaoTest extends AbstractDaoTestCase {
     GraphDto testPlan = dao.selectBySnapshot("testplan", 11L);
 
     assertThat(testPlan.getId()).isEqualTo(101L);
+    assertThat(testPlan.getResourceId()).isEqualTo(1L);
     assertThat(testPlan.getSnapshotId()).isEqualTo(11L);
     assertThat(testPlan.getFormat()).isEqualTo("graphson");
     assertThat(testPlan.getVersion()).isEqualTo(1);
@@ -62,6 +63,7 @@ public class GraphDaoTest extends AbstractDaoTestCase {
     GraphDto testPlan = dao.selectByComponent("testplan", "org.apache.struts:struts");
 
     assertThat(testPlan.getId()).isEqualTo(101L);
+    assertThat(testPlan.getResourceId()).isEqualTo(1L);
     assertThat(testPlan.getSnapshotId()).isEqualTo(11L);
     assertThat(testPlan.getFormat()).isEqualTo("graphson");
     assertThat(testPlan.getVersion()).isEqualTo(1);
index 6df3aaca20caf7f811cf1058f2dab25642b5dd00..9345cfcf40208018c3dd2f0bec3fbc1eb356d410 100644 (file)
@@ -4,6 +4,6 @@
   <snapshots id="10" project_id="1" islast="[false]" />
   <snapshots id="11" project_id="1" islast="[true]" />
 
-  <graphs id="100" snapshot_id="11" format="graphson" version="1" perspective="testable" root_vertex_id="7890" data="{testable of snapshot 123}"/>
-  <graphs id="101" snapshot_id="11" format="graphson" version="1" perspective="testplan" root_vertex_id="3456" data="{testplan of snapshot 123}"/>
+  <graphs id="100" resource_id="1" snapshot_id="11" format="graphson" version="1" perspective="testable" root_vertex_id="7890" data="{testable of snapshot 123}"/>
+  <graphs id="101" resource_id="1" snapshot_id="11" format="graphson" version="1" perspective="testplan" root_vertex_id="3456" data="{testplan of snapshot 123}"/>
 </dataset>
\ No newline at end of file
index 0b37503f165c9992b9cf5432d445c037a6b65dc1..f21902395a03112b90c429ebacc5ffa2dbed4a17 100644 (file)
@@ -25,6 +25,7 @@ class CreateGraphs < ActiveRecord::Migration
   def self.up
     create_table 'graphs' do |t|
       t.column 'snapshot_id', :integer, :null => false
+      t.column 'resource_id', :integer, :null => false
       t.column 'format', :string, :null => true, :limit => 20
       t.column 'perspective', :string, :null => true, :limit => 30
       t.column 'version', :string, :null => true, :limit => 20