|
@@ -0,0 +1,126 @@
|
|
|
|
|
+package com.tzld.piaoquan.longarticle.job;
|
|
|
|
|
+
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+
|
|
|
|
|
+import javax.sql.DataSource;
|
|
|
|
|
+import java.sql.Connection;
|
|
|
|
|
+import java.sql.PreparedStatement;
|
|
|
|
|
+import java.sql.ResultSet;
|
|
|
|
|
+import java.sql.Date;
|
|
|
|
|
+import java.sql.Timestamp;
|
|
|
|
|
+import java.time.LocalDate;
|
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+public final class DayBatchDeleteSupport {
|
|
|
|
|
+
|
|
|
|
|
+ private static final int DELETE_BATCH_SIZE = 10_000;
|
|
|
|
|
+
|
|
|
|
|
+ private DayBatchDeleteSupport() {
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static int deleteByDay(DataSource dataSource, String tableName, String timeColumn, LocalDateTime thresholdDate) throws Exception {
|
|
|
|
|
+ List<LocalDate> staleDays = queryStaleDays(dataSource, tableName, timeColumn, thresholdDate);
|
|
|
|
|
+ int totalDeletedCount = 0;
|
|
|
|
|
+ for (LocalDate staleDay : staleDays) {
|
|
|
|
|
+ LocalDateTime dayStart = staleDay.atStartOfDay();
|
|
|
|
|
+ LocalDateTime dayEnd = staleDay.plusDays(1).atStartOfDay();
|
|
|
|
|
+ LocalDateTime actualEnd = staleDay.equals(thresholdDate.toLocalDate()) ? thresholdDate : dayEnd;
|
|
|
|
|
+ if (!actualEnd.isAfter(dayStart)) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ int deletedCount = deleteRange(dataSource, tableName, timeColumn, dayStart, actualEnd);
|
|
|
|
|
+ totalDeletedCount += deletedCount;
|
|
|
|
|
+ log.info("deleteByDay success, tableName={}, day={}, deletedCount={}", tableName, staleDay, deletedCount);
|
|
|
|
|
+ }
|
|
|
|
|
+ return totalDeletedCount;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static int deleteByDay(DataSource dataSource, String tableName, String dateColumn, LocalDate thresholdDate) throws Exception {
|
|
|
|
|
+ List<LocalDate> staleDays = queryStaleDays(dataSource, tableName, dateColumn, thresholdDate);
|
|
|
|
|
+ int totalDeletedCount = 0;
|
|
|
|
|
+ for (LocalDate staleDay : staleDays) {
|
|
|
|
|
+ int deletedCount = deleteDate(dataSource, tableName, dateColumn, staleDay);
|
|
|
|
|
+ totalDeletedCount += deletedCount;
|
|
|
|
|
+ log.info("deleteByDay success, tableName={}, day={}, deletedCount={}", tableName, staleDay, deletedCount);
|
|
|
|
|
+ }
|
|
|
|
|
+ return totalDeletedCount;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static int deleteByStatus(DataSource dataSource, String tableName, String statusColumn, int status) throws Exception {
|
|
|
|
|
+ String sql = "delete from " + tableName + " where " + statusColumn + " = ? limit " + DELETE_BATCH_SIZE;
|
|
|
|
|
+ int totalDeletedCount = 0;
|
|
|
|
|
+ while (true) {
|
|
|
|
|
+ int deletedCount;
|
|
|
|
|
+ try (Connection connection = dataSource.getConnection();
|
|
|
|
|
+ PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
|
|
|
|
|
+ preparedStatement.setInt(1, status);
|
|
|
|
|
+ deletedCount = preparedStatement.executeUpdate();
|
|
|
|
|
+ }
|
|
|
|
|
+ totalDeletedCount += deletedCount;
|
|
|
|
|
+ if (deletedCount == 0) {
|
|
|
|
|
+ return totalDeletedCount;
|
|
|
|
|
+ }
|
|
|
|
|
+ log.info("deleteByStatus batch success, tableName={}, status={}, deletedCount={}", tableName, status, deletedCount);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static List<LocalDate> queryStaleDays(DataSource dataSource, String tableName, String timeColumn, LocalDateTime thresholdDate) throws Exception {
|
|
|
|
|
+ String sql = "select distinct date(" + timeColumn + ") as cleanup_day " +
|
|
|
|
|
+ "from " + tableName + " where " + timeColumn + " < ? order by cleanup_day";
|
|
|
|
|
+ List<LocalDate> staleDays = new ArrayList<>();
|
|
|
|
|
+ try (Connection connection = dataSource.getConnection();
|
|
|
|
|
+ PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
|
|
|
|
|
+ preparedStatement.setTimestamp(1, Timestamp.valueOf(thresholdDate));
|
|
|
|
|
+ try (ResultSet resultSet = preparedStatement.executeQuery()) {
|
|
|
|
|
+ while (resultSet.next()) {
|
|
|
|
|
+ java.sql.Date cleanupDay = resultSet.getDate("cleanup_day");
|
|
|
|
|
+ if (cleanupDay != null) {
|
|
|
|
|
+ staleDays.add(cleanupDay.toLocalDate());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return staleDays;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static List<LocalDate> queryStaleDays(DataSource dataSource, String tableName, String dateColumn, LocalDate thresholdDate) throws Exception {
|
|
|
|
|
+ String sql = "select distinct " + dateColumn + " as cleanup_day " +
|
|
|
|
|
+ "from " + tableName + " where " + dateColumn + " < ? order by cleanup_day";
|
|
|
|
|
+ List<LocalDate> staleDays = new ArrayList<>();
|
|
|
|
|
+ try (Connection connection = dataSource.getConnection();
|
|
|
|
|
+ PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
|
|
|
|
|
+ preparedStatement.setDate(1, Date.valueOf(thresholdDate));
|
|
|
|
|
+ try (ResultSet resultSet = preparedStatement.executeQuery()) {
|
|
|
|
|
+ while (resultSet.next()) {
|
|
|
|
|
+ Date cleanupDay = resultSet.getDate("cleanup_day");
|
|
|
|
|
+ if (cleanupDay != null) {
|
|
|
|
|
+ staleDays.add(cleanupDay.toLocalDate());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return staleDays;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static int deleteRange(DataSource dataSource, String tableName, String timeColumn, LocalDateTime start, LocalDateTime end) throws Exception {
|
|
|
|
|
+ String sql = "delete from " + tableName + " where " + timeColumn + " >= ? and " + timeColumn + " < ?";
|
|
|
|
|
+ try (Connection connection = dataSource.getConnection();
|
|
|
|
|
+ PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
|
|
|
|
|
+ preparedStatement.setTimestamp(1, Timestamp.valueOf(start));
|
|
|
|
|
+ preparedStatement.setTimestamp(2, Timestamp.valueOf(end));
|
|
|
|
|
+ return preparedStatement.executeUpdate();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static int deleteDate(DataSource dataSource, String tableName, String dateColumn, LocalDate date) throws Exception {
|
|
|
|
|
+ String sql = "delete from " + tableName + " where " + dateColumn + " = ?";
|
|
|
|
|
+ try (Connection connection = dataSource.getConnection();
|
|
|
|
|
+ PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
|
|
|
|
|
+ preparedStatement.setDate(1, Date.valueOf(date));
|
|
|
|
|
+ return preparedStatement.executeUpdate();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|