@Test public void testDeleteUselessRecords() { FileDiff[] fileDiffs = new FileDiff[2]; fileDiffs[0] = new FileDiff(); fileDiffs[0].setDiffId(1); fileDiffs[0].setRuleId(1); fileDiffs[0].setParameters(new HashMap<String, String>()); fileDiffs[0].setSrc("test"); fileDiffs[0].setState(FileDiffState.PENDING); fileDiffs[0].setDiffType(FileDiffType.APPEND); fileDiffs[0].setCreateTime(1); fileDiffs[1] = new FileDiff(); fileDiffs[1].setDiffId(2); fileDiffs[0].setRuleId(1); fileDiffs[1].setParameters(new HashMap<String, String>()); fileDiffs[1].setSrc("src"); fileDiffs[1].setState(FileDiffState.PENDING); fileDiffs[1].setDiffType(FileDiffType.APPEND); fileDiffs[1].setCreateTime(2); fileDiffDao.insert(fileDiffs); Assert.assertEquals(fileDiffDao.getUselessRecordsNum(), 0); fileDiffDao.update(1, FileDiffState.APPLIED); Assert.assertEquals(fileDiffDao.getUselessRecordsNum(), 1); fileDiffDao.update(2, FileDiffState.FAILED); Assert.assertEquals(fileDiffDao.getUselessRecordsNum(), 2); fileDiffDao.update(2, FileDiffState.DELETED); Assert.assertEquals(fileDiffDao.getUselessRecordsNum(), 2); fileDiffDao.deleteUselessRecords(1); Assert.assertEquals(fileDiffDao.getAll().size(), 1); }
public int deleteUselessRecords(int num) { JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); final String queryDids = "SELECT did FROM " + TABLE_NAME + " WHERE state IN (" + uselessFileDiffStates + ") ORDER BY create_time DESC LIMIT 1000 OFFSET " + num; List<Long> dids = jdbcTemplate.queryForList(queryDids, Long.class); if (dids.isEmpty()) { return 0; } String unusedDids = StringUtils.join(dids, ","); final String deleteUnusedFileDiff = "DELETE FROM " + TABLE_NAME + " where did IN (" + unusedDids + ")"; jdbcTemplate.update(deleteUnusedFileDiff); return dids.size(); }
FileDiffDao { public int deleteUselessRecords(int num) { JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); final String queryDids = "SELECT did FROM " + TABLE_NAME + " WHERE state IN (" + uselessFileDiffStates + ") ORDER BY create_time DESC LIMIT 1000 OFFSET " + num; List<Long> dids = jdbcTemplate.queryForList(queryDids, Long.class); if (dids.isEmpty()) { return 0; } String unusedDids = StringUtils.join(dids, ","); final String deleteUnusedFileDiff = "DELETE FROM " + TABLE_NAME + " where did IN (" + unusedDids + ")"; jdbcTemplate.update(deleteUnusedFileDiff); return dids.size(); } }
FileDiffDao { public int deleteUselessRecords(int num) { JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); final String queryDids = "SELECT did FROM " + TABLE_NAME + " WHERE state IN (" + uselessFileDiffStates + ") ORDER BY create_time DESC LIMIT 1000 OFFSET " + num; List<Long> dids = jdbcTemplate.queryForList(queryDids, Long.class); if (dids.isEmpty()) { return 0; } String unusedDids = StringUtils.join(dids, ","); final String deleteUnusedFileDiff = "DELETE FROM " + TABLE_NAME + " where did IN (" + unusedDids + ")"; jdbcTemplate.update(deleteUnusedFileDiff); return dids.size(); } FileDiffDao(DataSource dataSource); }
FileDiffDao { public int deleteUselessRecords(int num) { JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); final String queryDids = "SELECT did FROM " + TABLE_NAME + " WHERE state IN (" + uselessFileDiffStates + ") ORDER BY create_time DESC LIMIT 1000 OFFSET " + num; List<Long> dids = jdbcTemplate.queryForList(queryDids, Long.class); if (dids.isEmpty()) { return 0; } String unusedDids = StringUtils.join(dids, ","); final String deleteUnusedFileDiff = "DELETE FROM " + TABLE_NAME + " where did IN (" + unusedDids + ")"; jdbcTemplate.update(deleteUnusedFileDiff); return dids.size(); } FileDiffDao(DataSource dataSource); void setDataSource(DataSource dataSource); String getUselessFileDiffState(); List<FileDiff> getAll(); List<FileDiff> getPendingDiff(); List<FileDiff> getByState(FileDiffState fileDiffState); List<FileDiff> getByState(String prefix, FileDiffState fileDiffState); List<FileDiff> getPendingDiff(long rid); List<FileDiff> getPendingDiff(String prefix); List<FileDiff> getByIds(List<Long> dids); List<FileDiff> getByFileName(String fileName); List<String> getSyncPath(int size); FileDiff getById(long did); void delete(long did); int getUselessRecordsNum(); int deleteUselessRecords(int num); long insert(FileDiff fileDiff); void insert(FileDiff[] fileDiffs); Long[] insert(List<FileDiff> fileDiffs); int[] batchUpdate(
      final List<Long> dids, final List<FileDiffState> states,
      final List<String> parameters); int[] batchUpdate(
      final List<Long> dids, final FileDiffState state); int update(long did, FileDiffState state); int update(long did, String src); int update(long did, FileDiffState state,
       String parameters); int[] update(final FileDiff[] fileDiffs); int update(final FileDiff fileDiff); void deleteAll(); }
FileDiffDao { public int deleteUselessRecords(int num) { JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); final String queryDids = "SELECT did FROM " + TABLE_NAME + " WHERE state IN (" + uselessFileDiffStates + ") ORDER BY create_time DESC LIMIT 1000 OFFSET " + num; List<Long> dids = jdbcTemplate.queryForList(queryDids, Long.class); if (dids.isEmpty()) { return 0; } String unusedDids = StringUtils.join(dids, ","); final String deleteUnusedFileDiff = "DELETE FROM " + TABLE_NAME + " where did IN (" + unusedDids + ")"; jdbcTemplate.update(deleteUnusedFileDiff); return dids.size(); } FileDiffDao(DataSource dataSource); void setDataSource(DataSource dataSource); String getUselessFileDiffState(); List<FileDiff> getAll(); List<FileDiff> getPendingDiff(); List<FileDiff> getByState(FileDiffState fileDiffState); List<FileDiff> getByState(String prefix, FileDiffState fileDiffState); List<FileDiff> getPendingDiff(long rid); List<FileDiff> getPendingDiff(String prefix); List<FileDiff> getByIds(List<Long> dids); List<FileDiff> getByFileName(String fileName); List<String> getSyncPath(int size); FileDiff getById(long did); void delete(long did); int getUselessRecordsNum(); int deleteUselessRecords(int num); long insert(FileDiff fileDiff); void insert(FileDiff[] fileDiffs); Long[] insert(List<FileDiff> fileDiffs); int[] batchUpdate(
      final List<Long> dids, final List<FileDiffState> states,
      final List<String> parameters); int[] batchUpdate(
      final List<Long> dids, final FileDiffState state); int update(long did, FileDiffState state); int update(long did, String src); int update(long did, FileDiffState state,
       String parameters); int[] update(final FileDiff[] fileDiffs); int update(final FileDiff fileDiff); void deleteAll(); public String uselessFileDiffStates; }