]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-18386 Refactor audit delete query
authorLéo Geoffroy <leo.geoffroy@sonarsource.com>
Thu, 2 Feb 2023 12:43:04 +0000 (13:43 +0100)
committersonartech <sonartech@sonarsource.com>
Thu, 2 Feb 2023 20:03:41 +0000 (20:03 +0000)
server/sonar-db-dao/src/main/java/org/sonar/db/audit/AuditDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/audit/AuditMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/audit/AuditMapper.xml

index dc138d6e78f96e5d40b94934ba01b940f33bb04a..bf1fb96cc5a2b2a21e8d1f9024c8f6326b20d070 100644 (file)
@@ -23,6 +23,7 @@ import java.util.List;
 import org.sonar.api.utils.System2;
 import org.sonar.core.util.UuidFactory;
 import org.sonar.db.Dao;
+import org.sonar.db.DatabaseUtils;
 import org.sonar.db.DbSession;
 import org.sonar.db.Pagination;
 
@@ -57,7 +58,7 @@ public class AuditDao implements Dao {
       long now = system2.now();
       auditDto.setCreatedAt(now);
     }
-    if(auditDto.getNewValue().length() > MAX_SIZE) {
+    if (auditDto.getNewValue().length() > MAX_SIZE) {
       auditDto.setNewValue(EXCEEDED_LENGTH);
     }
     getMapper(dbSession).insert(auditDto);
@@ -68,7 +69,12 @@ public class AuditDao implements Dao {
   }
 
   public long deleteBefore(DbSession dbSession, long threshold) {
-    return getMapper(dbSession).purge(threshold);
+    List<String> uuids = getMapper(dbSession).selectUuidsOlderThan(threshold);
+    DatabaseUtils.executeLargeInputsWithoutOutput(uuids, list -> {
+      getMapper(dbSession).purgeUuids(list);
+      dbSession.commit();
+    });
+    return uuids.size();
   }
 
 }
index 88c08866db661f100c0459f1f74848b7e93170f2..929f12e76ac6daef68a1519497a2f80220bee222 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.db.audit;
 
+import java.util.Collection;
 import java.util.List;
 import org.apache.ibatis.annotations.Param;
 import org.sonar.db.Pagination;
@@ -31,5 +32,7 @@ public interface AuditMapper {
 
   List<AuditDto> selectOlderThan(@Param("beforeTimestamp") long beforeTimestamp);
 
-  long purge(long threshold);
+  List<String> selectUuidsOlderThan(@Param("beforeTimestamp") long beforeTimestamp);
+
+  void purgeUuids(@Param("uuids") Collection<String> uuids);
 }
index edf3aaa73290e704485bf3441fb4b5e50fbc875c..a532172fc604ca848b716ee2ab8abe53a49415dd 100644 (file)
       a.created_at &lt; #{beforeTimestamp,jdbcType=BIGINT}
   </select>
 
-  <delete id="purge" parameterType="long">
-    delete from audits
-    where uuid in (select a.uuid from audits a where a.created_at &lt; #{threshold,jdbcType=BIGINT}
-    order by a.created_at limit 100000)
-  </delete>
+  <select id="selectUuidsOlderThan" parameterType="long" resultType="string">
+    select a.uuid
+    from audits a
+    where a.created_at &lt; #{beforeTimestamp,jdbcType=BIGINT}
+    order by a.created_at
+    limit 100000
+  </select>
 
-  <delete id="purge" parameterType="long" databaseId="mssql">
-    delete from audits
-    where uuid in (select top 100000 a.uuid from audits a where a.created_at &lt; #{threshold,jdbcType=BIGINT}
-      order by a.created_at)
-  </delete>
+    <select id="selectUuidsOlderThan" parameterType="long" resultType="string" databaseId="mssql">
+    select top 100000 a.uuid
+    from audits a
+    where a.created_at &lt; #{beforeTimestamp,jdbcType=BIGINT}
+    order by a.created_at
+  </select>
+
+    <select id="selectUuidsOlderThan" parameterType="long" resultType="string" databaseId="oracle">
+    select a.uuid
+    from audits a
+    where a.created_at &lt; #{beforeTimestamp,jdbcType=BIGINT}
+    order by a.created_at
+    fetch first 100000 rows only
+  </select>
 
-    <delete id="purge" parameterType="long" databaseId="oracle">
-    delete from audits
-    where uuid in (select a.uuid from audits a where a.created_at &lt; #{threshold,jdbcType=BIGINT}
-      order by a.created_at fetch first 100000 rows only)
+  <delete id="purgeUuids" parameterType="string">
+    delete from audits where uuid in
+    <foreach collection="uuids" open="(" close=")" item="uuid" separator=",">#{uuid,jdbcType=VARCHAR}</foreach>
   </delete>
 </mapper>