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;
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);
}
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();
}
}
*/
package org.sonar.db.audit;
+import java.util.Collection;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.sonar.db.Pagination;
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);
}
a.created_at < #{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 < #{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 < #{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 < #{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 < #{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 < #{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 < #{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>