001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2011 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020 package org.sonar.plugins.dbcleaner.api;
021
022 import org.apache.commons.configuration.Configuration;
023 import org.sonar.api.batch.Event;
024 import org.sonar.api.database.DatabaseSession;
025 import org.sonar.api.database.model.*;
026 import org.sonar.api.design.DependencyDto;
027 import org.sonar.api.utils.TimeProfiler;
028
029 import com.google.common.annotations.VisibleForTesting;
030
031 import javax.persistence.Query;
032 import java.util.List;
033
034 /**
035 * @since 2.5
036 */
037 public final class PurgeUtils {
038
039 public static final int DEFAULT_MINIMUM_PERIOD_IN_HOURS = 12;
040 public static final String PROP_KEY_MINIMUM_PERIOD_IN_HOURS = "sonar.purge.minimumPeriodInHours";
041
042 /**
043 * Maximum elements in the SQL statement "IN" due to an Oracle limitation (see error ORA-01795)
044 */
045 public static final int MAX_IN_ELEMENTS = 950;
046
047 private PurgeUtils() {
048 // only static methods
049 }
050
051 public static int getMinimumPeriodInHours(Configuration conf) {
052 int hours = DEFAULT_MINIMUM_PERIOD_IN_HOURS;
053 if (conf != null) {
054 hours = conf.getInt(PROP_KEY_MINIMUM_PERIOD_IN_HOURS, DEFAULT_MINIMUM_PERIOD_IN_HOURS);
055 }
056 return hours;
057 }
058
059 public static void deleteSnapshotsData(DatabaseSession session, List<Integer> snapshotIds) {
060 deleteMeasuresBySnapshotId(session, snapshotIds);
061 deleteSources(session, snapshotIds);
062 deleteViolations(session, snapshotIds);
063 deleteDependencies(session, snapshotIds);
064 deleteDuplicationBlocks(session, snapshotIds);
065 deleteEvents(session, snapshotIds);
066 deleteSnapshots(session, snapshotIds);
067 }
068
069 public static void deleteDependencies(DatabaseSession session, List<Integer> snapshotIds) {
070 executeQuery(session, "delete dependencies", snapshotIds, "delete from " + DependencyDto.class.getSimpleName() + " d where d.fromSnapshotId in (:ids)");
071 executeQuery(session, "delete dependencies", snapshotIds, "delete from " + DependencyDto.class.getSimpleName() + " d where d.toSnapshotId in (:ids)");
072 }
073
074 /**
075 * Delete all measures, including MEASURE_DATA
076 */
077 public static void deleteMeasuresBySnapshotId(DatabaseSession session, List<Integer> snapshotIds) {
078 executeQuery(session, "delete measures by snapshot id", snapshotIds, "delete from " + MeasureData.class.getSimpleName() + " m where m.snapshotId in (:ids)");
079 executeQuery(session, "delete measures by snapshot id", snapshotIds, "delete from " + MeasureModel.class.getSimpleName() + " m where m.snapshotId in (:ids)");
080 }
081
082 /**
083 * Delete all measures, including MEASURE_DATA
084 */
085 public static void deleteMeasuresById(DatabaseSession session, List<Integer> measureIds) {
086 executeQuery(session, "delete measures by id", measureIds, "delete from " + MeasureData.class.getSimpleName() + " m where m.measure.id in (:ids)");
087 executeQuery(session, "delete measures by id", measureIds, "delete from " + MeasureModel.class.getSimpleName() + " m where m.id in (:ids)");
088 }
089
090 /**
091 * Delete SNAPSHOT_SOURCES table
092 */
093 public static void deleteSources(DatabaseSession session, List<Integer> snapshotIds) {
094 executeQuery(session, "delete sources", snapshotIds, "delete from " + SnapshotSource.class.getSimpleName() + " e where e.snapshotId in (:ids)");
095 }
096
097 /**
098 * Delete violations (RULE_FAILURES table)
099 */
100 public static void deleteViolations(DatabaseSession session, List<Integer> snapshotIds) {
101 executeQuery(session, "delete violations", snapshotIds, "delete from " + RuleFailureModel.class.getSimpleName() + " e where e.snapshotId in (:ids)");
102 }
103
104 /**
105 * Delete DUPLICATIONS_INDEX table
106 *
107 * @since 2.11
108 */
109 private static void deleteDuplicationBlocks(DatabaseSession session, List<Integer> snapshotIds) {
110 executeNativeQuery(session, "delete duplication blocks", snapshotIds, "delete from duplications_index where snapshot_id in (:ids)");
111 }
112
113 /**
114 * Delete EVENTS table
115 */
116 public static void deleteEvents(DatabaseSession session, List<Integer> snapshotIds) {
117 executeQuery(session, "delete events", snapshotIds, "delete from " + Event.class.getSimpleName() + " e where e.snapshot.id in (:ids)");
118 }
119
120 /**
121 * Delete SNAPSHOTS table
122 */
123 public static void deleteSnapshots(DatabaseSession session, List<Integer> snapshotIds) {
124 executeQuery(session, "delete snapshots", snapshotIds, "delete from " + Snapshot.class.getSimpleName() + " s where s.id in (:ids)");
125 }
126
127 public static void deleteResources(DatabaseSession session, List<Integer> ids) {
128 executeQuery(session, "", ids, "DELETE FROM " + ResourceModel.class.getSimpleName() + " WHERE id in (:ids)");
129 deleteResourceIndex(session, ids);
130 }
131
132 /**
133 * Delete RESOURCE_INDEX table
134 */
135 public static void deleteResourceIndex(DatabaseSession session, List<Integer> resourceIds) {
136 executeNativeQuery(session, "delete resource_index", resourceIds, "delete from resource_index where resource_id in (:ids)");
137 }
138
139 /**
140 * Paginate execution of SQL requests to avoid exceeding size of rollback segment
141 */
142 public static void executeQuery(DatabaseSession session, String description, List<Integer> ids, String hql) {
143 if (ids == null || ids.isEmpty()) {
144 return;
145 }
146 TimeProfiler profiler = new TimeProfiler().setLevelToDebug().start("Execute " + description);
147 int index = 0;
148 while (index < ids.size()) {
149 List<Integer> paginedSids = ids.subList(index, Math.min(ids.size(), index + MAX_IN_ELEMENTS));
150 Query query = session.createQuery(hql);
151 query.setParameter("ids", paginedSids);
152 query.executeUpdate();
153 index += MAX_IN_ELEMENTS;
154 session.commit();
155 }
156 profiler.stop();
157 }
158
159 /**
160 * @since 2.13
161 */
162 @VisibleForTesting
163 static void executeNativeQuery(DatabaseSession session, String description, List<Integer> ids, String sql) {
164 if (ids == null || ids.isEmpty()) {
165 return;
166 }
167 TimeProfiler profiler = new TimeProfiler().setLevelToDebug().start("Execute " + description);
168 int index = 0;
169 while (index < ids.size()) {
170 List<Integer> paginedSids = ids.subList(index, Math.min(ids.size(), index + MAX_IN_ELEMENTS));
171 Query query = session.createNativeQuery(sql);
172 query.setParameter("ids", paginedSids);
173 query.executeUpdate();
174 index += MAX_IN_ELEMENTS;
175 session.commit();
176 }
177 profiler.stop();
178 }
179
180 }