@Test public void testBackupNowLocalFileInfo() throws IOException { Map<String, Set<String>> allFileList = new HashMap<>(); Random r = new Random(0); for (int i = 0; i < 3; i++) { for (int j = 0; j < 5; j++) { if (!allFileList.containsKey(String.valueOf(i))) { allFileList.put(String.valueOf(i), new HashSet<>()); } String rand = String.valueOf(r.nextInt(10000)); String fileName = SENDER_FILE_PATH_TEST + File.separator + String.valueOf(i) + File.separator + rand; File file = new File(fileName); allFileList.get(String.valueOf(i)).add(file.getAbsolutePath()); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } if (!file.exists()) { file.createNewFile(); } } } Set<String> lastFileList = new HashSet<>(); manager.getLastLocalFileList(LAST_FILE_INFO_TEST); lastFileList = manager.getLastLocalFiles(); assert (lastFileList.isEmpty()); manager.getNowLocalFileList(new String[]{SENDER_FILE_PATH_TEST}); manager.backupNowLocalFileInfo(LAST_FILE_INFO_TEST); manager.getLastLocalFileList(LAST_FILE_INFO_TEST); lastFileList = manager.getLastLocalFiles(); for (Entry<String, Set<String>> entry : allFileList.entrySet()) { assert (lastFileList.containsAll(entry.getValue())); } r = new Random(1); for (int i = 0; i < 3; i++) { for (int j = 0; j < 5; j++) { if (!allFileList.containsKey(String.valueOf(i))) { allFileList.put(String.valueOf(i), new HashSet<>()); } String rand = String.valueOf(r.nextInt(10000)); String fileName = SENDER_FILE_PATH_TEST + File.separator + String.valueOf(i) + File.separator + rand; File file = new File(fileName); allFileList.get(String.valueOf(i)).add(file.getAbsolutePath()); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } if (!file.exists()) { file.createNewFile(); } } } int count = 0; Map<String, Set<String>> deleteFile = new HashMap<>(); for (Entry<String, Set<String>> entry : allFileList.entrySet()) { deleteFile.put(entry.getKey(), new HashSet<>()); for (String path : entry.getValue()) { count++; if (count % 3 == 0) { deleteFile.get(entry.getKey()).add(path); } } } for (Entry<String, Set<String>> entry : deleteFile.entrySet()) { for (String path : entry.getValue()) { new File(path).delete(); allFileList.get(entry.getKey()).remove(path); } } manager.getNowLocalFileList(new String[]{SENDER_FILE_PATH_TEST}); manager.backupNowLocalFileInfo(LAST_FILE_INFO_TEST); manager.getLastLocalFileList(LAST_FILE_INFO_TEST); lastFileList = manager.getLastLocalFiles(); for (Entry<String, Set<String>> entry : allFileList.entrySet()) { assert (lastFileList.containsAll(entry.getValue())); } }
public void backupNowLocalFileInfo(String backupFile) { BufferedWriter bufferedWriter = null; try { bufferedWriter = new BufferedWriter(new FileWriter(backupFile)); for (Entry<String, Set<String>> entry : nowLocalFiles.entrySet()) { for (String file : entry.getValue()) { bufferedWriter.write(file + "\n"); } } } catch (IOException e) { LOGGER.error("IoTDB post back sender: cannot back up now local file info because {}", e); } finally { if (bufferedWriter != null) { try { bufferedWriter.close(); } catch (IOException e) { LOGGER.error( "IoTDB post back sender: cannot close stream after backing up now local file info " + "because {}", e); } } } }
FileManager { public void backupNowLocalFileInfo(String backupFile) { BufferedWriter bufferedWriter = null; try { bufferedWriter = new BufferedWriter(new FileWriter(backupFile)); for (Entry<String, Set<String>> entry : nowLocalFiles.entrySet()) { for (String file : entry.getValue()) { bufferedWriter.write(file + "\n"); } } } catch (IOException e) { LOGGER.error("IoTDB post back sender: cannot back up now local file info because {}", e); } finally { if (bufferedWriter != null) { try { bufferedWriter.close(); } catch (IOException e) { LOGGER.error( "IoTDB post back sender: cannot close stream after backing up now local file info " + "because {}", e); } } } } }
FileManager { public void backupNowLocalFileInfo(String backupFile) { BufferedWriter bufferedWriter = null; try { bufferedWriter = new BufferedWriter(new FileWriter(backupFile)); for (Entry<String, Set<String>> entry : nowLocalFiles.entrySet()) { for (String file : entry.getValue()) { bufferedWriter.write(file + "\n"); } } } catch (IOException e) { LOGGER.error("IoTDB post back sender: cannot back up now local file info because {}", e); } finally { if (bufferedWriter != null) { try { bufferedWriter.close(); } catch (IOException e) { LOGGER.error( "IoTDB post back sender: cannot close stream after backing up now local file info " + "because {}", e); } } } } private  FileManager(); }
FileManager { public void backupNowLocalFileInfo(String backupFile) { BufferedWriter bufferedWriter = null; try { bufferedWriter = new BufferedWriter(new FileWriter(backupFile)); for (Entry<String, Set<String>> entry : nowLocalFiles.entrySet()) { for (String file : entry.getValue()) { bufferedWriter.write(file + "\n"); } } } catch (IOException e) { LOGGER.error("IoTDB post back sender: cannot back up now local file info because {}", e); } finally { if (bufferedWriter != null) { try { bufferedWriter.close(); } catch (IOException e) { LOGGER.error( "IoTDB post back sender: cannot close stream after backing up now local file info " + "because {}", e); } } } } private  FileManager(); static final FileManager getInstance(); void init(); void getSendingFileList(); void getLastLocalFileList(String path); void getNowLocalFileList(String[] paths); void backupNowLocalFileInfo(String backupFile); Map<String, Set<String>> getSendingFiles(); Set<String> getLastLocalFiles(); Map<String, Set<String>> getNowLocalFiles(); void setNowLocalFiles(Map<String, Set<String>> newNowLocalFiles); }
FileManager { public void backupNowLocalFileInfo(String backupFile) { BufferedWriter bufferedWriter = null; try { bufferedWriter = new BufferedWriter(new FileWriter(backupFile)); for (Entry<String, Set<String>> entry : nowLocalFiles.entrySet()) { for (String file : entry.getValue()) { bufferedWriter.write(file + "\n"); } } } catch (IOException e) { LOGGER.error("IoTDB post back sender: cannot back up now local file info because {}", e); } finally { if (bufferedWriter != null) { try { bufferedWriter.close(); } catch (IOException e) { LOGGER.error( "IoTDB post back sender: cannot close stream after backing up now local file info " + "because {}", e); } } } } private  FileManager(); static final FileManager getInstance(); void init(); void getSendingFileList(); void getLastLocalFileList(String path); void getNowLocalFileList(String[] paths); void backupNowLocalFileInfo(String backupFile); Map<String, Set<String>> getSendingFiles(); Set<String> getLastLocalFiles(); Map<String, Set<String>> getNowLocalFiles(); void setNowLocalFiles(Map<String, Set<String>> newNowLocalFiles);  }