<commit_before>package io.khasang.wlogs.model;

import io.khasang.wlogs.form.DeleteDataForm;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.UUID;

public class DeleteDataTable {
    private TransactionTemplate sharedTransactionTemplate;
    private JdbcTemplate jdbcTemplate;
    private String tableName;
    private LogRepository logRepository;

    public void setLogRepository(LogRepository logRepository) {
        this.logRepository = logRepository;
    }

    public void setSharedTransactionTemplate(TransactionTemplate sharedTransactionTemplate) {
        this.sharedTransactionTemplate = sharedTransactionTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public void deleteAll() {
        String newTable = tableName + "_" + UUID.randomUUID().toString().replace("-", "");
        String oldTable = tableName + "_" + UUID.randomUUID().toString().replace("-", "");
        String[] sql = {
                "CREATE TABLE IF NOT EXISTS " + newTable + " LIKE " + tableName,
                "RENAME TABLE " + tableName + " TO " + oldTable + ", " + newTable + " TO " + tableName,
                "DROP TABLE " + oldTable
        };
        jdbcTemplate.batchUpdate(sql);
    }

    public Integer deleteByDateInterval(DeleteDataForm.DateIntervalType dateIntervalType, final Integer dateIntervalSize) {
        String sql = "DELETE FROM " + tableName + " WHERE occurred_at < DATE_SUB(CURDATE(), INTERVAL ? " + dateIntervalType.toString() + ")";
        return jdbcTemplate.update(sql, new PreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps) throws SQLException {
                ps.setInt(1, dateIntervalSize);
            }
        });
    }

    public Integer deleteByCriteria(final String errorSource, final String errorLevel,
                                    final DeleteDataForm.DateIntervalType dateIntervalType, final Integer dateIntervalSize) {
        // TODO: research java db criteria...
        final LinkedList<String> filters = new LinkedList<String>();
        if (null != errorSource) {
            filters.add("error_source = ?");
        }
        if (null != errorLevel) {
            filters.add("error_level = ?");
        }
        if (null != dateIntervalType) {
            String filterOccurredAt = "occurred_at < DATE_SUB(CURDATE(), INTERVAL ? " + dateIntervalType.name() + ")";
            filters.add(filterOccurredAt);
        }
        if (filters.size() == 0) {
            return 0;
        }
        String sql = "DELETE FROM " + tableName + " WHERE " + String.join(" AND ", filters);
        return jdbcTemplate.update(sql, new PreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps) throws SQLException {
                Integer parameterIndex = 1;
                if (null != errorSource) {
                    ps.setString(parameterIndex++, errorSource);
                }
                if (null != errorLevel) {
                    ps.setString(parameterIndex++, errorLevel);
                }
                if (null != dateIntervalType) {
                    ps.setInt(parameterIndex, dateIntervalSize);
                }
            }
        });
    }

    public void deleteOne(final LogModel log) {
        deleteOne(log.getId());
    }

    public void deleteOne(final Integer logRecordId) {
        String sql = "DELETE FROM :tableName WHERE id = ?".replace(":tableName", tableName);<commit_msg>added method deleteAllExceptLastNRecords here<commit_after>package io.khasang.wlogs.model;

import io.khasang.wlogs.form.DeleteDataForm;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.UUID;

public class DeleteDataTable {
    private TransactionTemplate sharedTransactionTemplate;
    private JdbcTemplate jdbcTemplate;
    private String tableName;
    private LogRepository logRepository;

    public void setLogRepository(LogRepository logRepository) {
        this.logRepository = logRepository;
    }

    public void setSharedTransactionTemplate(TransactionTemplate sharedTransactionTemplate) {
        this.sharedTransactionTemplate = sharedTransactionTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public void deleteAll() {
        String newTable = tableName + "_" + UUID.randomUUID().toString().replace("-", "");
        String oldTable = tableName + "_" + UUID.randomUUID().toString().replace("-", "");
        String[] sql = {
                "CREATE TABLE IF NOT EXISTS " + newTable + " LIKE " + tableName,
                "RENAME TABLE " + tableName + " TO " + oldTable + ", " + newTable + " TO " + tableName,
                "DROP TABLE " + oldTable
        };
        jdbcTemplate.batchUpdate(sql);
    }

    public Integer deleteByDateInterval(DeleteDataForm.DateIntervalType dateIntervalType, final Integer dateIntervalSize) {
        String sql = "DELETE FROM " + tableName + " WHERE occurred_at < DATE_SUB(CURDATE(), INTERVAL ? " + dateIntervalType.toString() + ")";
        return jdbcTemplate.update(sql, new PreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps) throws SQLException {
                ps.setInt(1, dateIntervalSize);
            }
        });
    }

    public Integer deleteByCriteria(final String errorSource, final String errorLevel,
                                    final DeleteDataForm.DateIntervalType dateIntervalType, final Integer dateIntervalSize) {
        // TODO: research java db criteria...
        final LinkedList<String> filters = new LinkedList<String>();
        if (null != errorSource) {
            filters.add("error_source = ?");
        }
        if (null != errorLevel) {
            filters.add("error_level = ?");
        }
        if (null != dateIntervalType) {
            String filterOccurredAt = "occurred_at < DATE_SUB(CURDATE(), INTERVAL ? " + dateIntervalType.name() + ")";
            filters.add(filterOccurredAt);
        }
        if (filters.size() == 0) {
            return 0;
        }
        String sql = "DELETE FROM " + tableName + " WHERE " + String.join(" AND ", filters);
        return jdbcTemplate.update(sql, new PreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps) throws SQLException {
                Integer parameterIndex = 1;
                if (null != errorSource) {
                    ps.setString(parameterIndex++, errorSource);
                }
                if (null != errorLevel) {
                    ps.setString(parameterIndex++, errorLevel);
                }
                if (null != dateIntervalType) {
                    ps.setInt(parameterIndex, dateIntervalSize);
                }
            }
        });
    }

    public int deleteAllExceptLastNRecords(final int recordsAmountToKeepAlive) throws DataAccessException {
        Integer logRecordsCountTotal = logRepository.countAll();
        if (recordsAmountToKeepAlive >= logRecordsCountTotal) {
            return 0;
        }
        final String temporaryTableNameNew = tableName + "_" + UUID.randomUUID().toString().replace("-", "");
        final String temporaryTableNameOld = tableName + "_" + UUID.randomUUID().toString().replace("-", "");
        sharedTransactionTemplate.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                Integer limit = recordsAmountToKeepAlive;
                String[] sql = {
                        "CREATE TABLE IF NOT EXISTS " + temporaryTableNameNew + " LIKE " + tableName,
                        "INSERT INTO " + temporaryTableNameNew + " SELECT * FROM " + tableName + " ORDER BY occurred_at DESC LIMIT " + limit,
                        "RENAME TABLE " + tableName + " TO " + temporaryTableNameOld + ", " + temporaryTableNameNew + " TO " + tableName,
                        "DROP TABLE " + temporaryTableNameOld
                };
                jdbcTemplate.batchUpdate(sql);
            }
        });
        return logRecordsCountTotal - recordsAmountToKeepAlive;
    }

    public void deleteOne(final LogModel log) {
        deleteOne(log.getId());
    }

    public void deleteOne(final Integer logRecordId) {
        String sql = "DELETE FROM :tableName WHERE id = ?".replace(":tableName", tableName);
{'fraction_non_alphanumeric': 0.096875, 'fraction_numerical': 0.0009375, 'mean_word_length': 2.5388868411352745, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '829241', 'n_tokens_mistral': 2449, 'n_tokens_neox': 2365, 'n_words': 540}