@Test public void testPurgeData() throws IOException { String prefix = getPrefix() + "/dptest"; createData(prefix); DateTimeFormatter formatter = DateTimeFormat.forPattern("yy/MM/dd"); DateTime currentTime = formatter.parseDateTime("11/01/04"); DataPurgeConfig dataPurgeConfig = new DataPurgeConfig(); dataPurgeConfig.setMaxAgeInDays(1); dataPurgeConfig.setDirectoryPrefix(new String[]{prefix}); dataPurgeConfig.setDatePathFormat("yy/MM/dd"); assertTrue(dataPurgeService.purgeData(dataPurgeConfig, currentTime)); List<File> directoryList = dataPurgeService.getSubdirectoryList(new File(prefix), null); assertNotNull(directoryList); assertEquals(7, directoryList.size()); }
@Override public boolean purgeData(DataPurgeConfig dataPurgeConfig, DateTime currentTime) { if (!validatePurgeParameters(dataPurgeConfig, currentTime)) { return false; } DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(dataPurgeConfig.getDatePathFormat()); DateTime oldestDataAllowed; if (dataPurgeConfig.getMaxAgeInDays() > 0) { oldestDataAllowed = currentTime.plusDays(-dataPurgeConfig.getMaxAgeInDays()); } else { oldestDataAllowed = currentTime.plusHours(-dataPurgeConfig.getMaxAgeInHours()); } logger.debug("Oldest data allowed {} , current time is {}", new Object[]{oldestDataAllowed, currentTime}); for (String directoryPrefix : dataPurgeConfig.getDirectoryPrefix()) { for (File prefixDirectory : expandPrefix(directoryPrefix)) { List<File> subdirectories = getSubdirectoryList(prefixDirectory, null); for (File subdirectory : subdirectories) { logger.trace("Considering directory {} for purge", subdirectory); safeDelete(prefixDirectory.getPath(), dateTimeFormatter, oldestDataAllowed, subdirectory, dataPurgeConfig.isFileBasedPurge(), dataPurgeConfig.getDateStartIndex(), dataPurgeConfig.getDateStringLength()); } if (dataPurgeConfig.getCleanEmptyParents()) { for (File directory : subdirectories) { if (directory.list() != null && directory.list().length == 0) { try { FileUtils.deleteDirectory(directory); } catch (IOException e) { logger.warn("Failed to delete empty directory {}", directory); } } } } } } return true; }
DataPurgeServiceImpl implements DataPurgeService { @Override public boolean purgeData(DataPurgeConfig dataPurgeConfig, DateTime currentTime) { if (!validatePurgeParameters(dataPurgeConfig, currentTime)) { return false; } DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(dataPurgeConfig.getDatePathFormat()); DateTime oldestDataAllowed; if (dataPurgeConfig.getMaxAgeInDays() > 0) { oldestDataAllowed = currentTime.plusDays(-dataPurgeConfig.getMaxAgeInDays()); } else { oldestDataAllowed = currentTime.plusHours(-dataPurgeConfig.getMaxAgeInHours()); } logger.debug("Oldest data allowed {} , current time is {}", new Object[]{oldestDataAllowed, currentTime}); for (String directoryPrefix : dataPurgeConfig.getDirectoryPrefix()) { for (File prefixDirectory : expandPrefix(directoryPrefix)) { List<File> subdirectories = getSubdirectoryList(prefixDirectory, null); for (File subdirectory : subdirectories) { logger.trace("Considering directory {} for purge", subdirectory); safeDelete(prefixDirectory.getPath(), dateTimeFormatter, oldestDataAllowed, subdirectory, dataPurgeConfig.isFileBasedPurge(), dataPurgeConfig.getDateStartIndex(), dataPurgeConfig.getDateStringLength()); } if (dataPurgeConfig.getCleanEmptyParents()) { for (File directory : subdirectories) { if (directory.list() != null && directory.list().length == 0) { try { FileUtils.deleteDirectory(directory); } catch (IOException e) { logger.warn("Failed to delete empty directory {}", directory); } } } } } } return true; } }
DataPurgeServiceImpl implements DataPurgeService { @Override public boolean purgeData(DataPurgeConfig dataPurgeConfig, DateTime currentTime) { if (!validatePurgeParameters(dataPurgeConfig, currentTime)) { return false; } DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(dataPurgeConfig.getDatePathFormat()); DateTime oldestDataAllowed; if (dataPurgeConfig.getMaxAgeInDays() > 0) { oldestDataAllowed = currentTime.plusDays(-dataPurgeConfig.getMaxAgeInDays()); } else { oldestDataAllowed = currentTime.plusHours(-dataPurgeConfig.getMaxAgeInHours()); } logger.debug("Oldest data allowed {} , current time is {}", new Object[]{oldestDataAllowed, currentTime}); for (String directoryPrefix : dataPurgeConfig.getDirectoryPrefix()) { for (File prefixDirectory : expandPrefix(directoryPrefix)) { List<File> subdirectories = getSubdirectoryList(prefixDirectory, null); for (File subdirectory : subdirectories) { logger.trace("Considering directory {} for purge", subdirectory); safeDelete(prefixDirectory.getPath(), dateTimeFormatter, oldestDataAllowed, subdirectory, dataPurgeConfig.isFileBasedPurge(), dataPurgeConfig.getDateStartIndex(), dataPurgeConfig.getDateStringLength()); } if (dataPurgeConfig.getCleanEmptyParents()) { for (File directory : subdirectories) { if (directory.list() != null && directory.list().length == 0) { try { FileUtils.deleteDirectory(directory); } catch (IOException e) { logger.warn("Failed to delete empty directory {}", directory); } } } } } } return true; } DataPurgeServiceImpl(); }
DataPurgeServiceImpl implements DataPurgeService { @Override public boolean purgeData(DataPurgeConfig dataPurgeConfig, DateTime currentTime) { if (!validatePurgeParameters(dataPurgeConfig, currentTime)) { return false; } DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(dataPurgeConfig.getDatePathFormat()); DateTime oldestDataAllowed; if (dataPurgeConfig.getMaxAgeInDays() > 0) { oldestDataAllowed = currentTime.plusDays(-dataPurgeConfig.getMaxAgeInDays()); } else { oldestDataAllowed = currentTime.plusHours(-dataPurgeConfig.getMaxAgeInHours()); } logger.debug("Oldest data allowed {} , current time is {}", new Object[]{oldestDataAllowed, currentTime}); for (String directoryPrefix : dataPurgeConfig.getDirectoryPrefix()) { for (File prefixDirectory : expandPrefix(directoryPrefix)) { List<File> subdirectories = getSubdirectoryList(prefixDirectory, null); for (File subdirectory : subdirectories) { logger.trace("Considering directory {} for purge", subdirectory); safeDelete(prefixDirectory.getPath(), dateTimeFormatter, oldestDataAllowed, subdirectory, dataPurgeConfig.isFileBasedPurge(), dataPurgeConfig.getDateStartIndex(), dataPurgeConfig.getDateStringLength()); } if (dataPurgeConfig.getCleanEmptyParents()) { for (File directory : subdirectories) { if (directory.list() != null && directory.list().length == 0) { try { FileUtils.deleteDirectory(directory); } catch (IOException e) { logger.warn("Failed to delete empty directory {}", directory); } } } } } } return true; } DataPurgeServiceImpl(); @Override boolean purgeData(DataPurgeConfig dataPurgeConfig, DateTime currentTime); }
DataPurgeServiceImpl implements DataPurgeService { @Override public boolean purgeData(DataPurgeConfig dataPurgeConfig, DateTime currentTime) { if (!validatePurgeParameters(dataPurgeConfig, currentTime)) { return false; } DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(dataPurgeConfig.getDatePathFormat()); DateTime oldestDataAllowed; if (dataPurgeConfig.getMaxAgeInDays() > 0) { oldestDataAllowed = currentTime.plusDays(-dataPurgeConfig.getMaxAgeInDays()); } else { oldestDataAllowed = currentTime.plusHours(-dataPurgeConfig.getMaxAgeInHours()); } logger.debug("Oldest data allowed {} , current time is {}", new Object[]{oldestDataAllowed, currentTime}); for (String directoryPrefix : dataPurgeConfig.getDirectoryPrefix()) { for (File prefixDirectory : expandPrefix(directoryPrefix)) { List<File> subdirectories = getSubdirectoryList(prefixDirectory, null); for (File subdirectory : subdirectories) { logger.trace("Considering directory {} for purge", subdirectory); safeDelete(prefixDirectory.getPath(), dateTimeFormatter, oldestDataAllowed, subdirectory, dataPurgeConfig.isFileBasedPurge(), dataPurgeConfig.getDateStartIndex(), dataPurgeConfig.getDateStringLength()); } if (dataPurgeConfig.getCleanEmptyParents()) { for (File directory : subdirectories) { if (directory.list() != null && directory.list().length == 0) { try { FileUtils.deleteDirectory(directory); } catch (IOException e) { logger.warn("Failed to delete empty directory {}", directory); } } } } } } return true; } DataPurgeServiceImpl(); @Override boolean purgeData(DataPurgeConfig dataPurgeConfig, DateTime currentTime);  }