/** * Get the file or directory name (the last element of the path). * This method is similar to Java 7 <code>java.nio.file.Path.getName</code>. * * @param path the directory and file name * @return just the file name */ public static String getName(String path) { return FilePath.get(path).getName(); }
private List<Integer> getAllChunkIds() { ArrayList<Integer> ids = new ArrayList<>(); int prefixLength = RedoLogChunk.CHUNK_FILE_NAME_PREFIX.length(); FilePath dir = FilePath.get(config.get("storagePath")); for (FilePath fp : dir.newDirectoryStream()) { String fullName = fp.getName(); if (fullName.startsWith(RedoLogChunk.CHUNK_FILE_NAME_PREFIX)) { int id = Integer.parseInt(fullName.substring(prefixLength)); ids.add(id); } } Collections.sort(ids); // 必须排序,按id从小到大的顺序读取文件,才能正确的redo return ids; }
@Override public ArrayList<FilePath> newDirectoryStream() { List<FilePath> list = getBase().newDirectoryStream(); ArrayList<FilePath> newList = new ArrayList<>(); for (int i = 0, size = list.size(); i < size; i++) { FilePath f = list.get(i); if (!f.getName().endsWith(PART_SUFFIX)) { newList.add(wrap(f)); } } return newList; }
private static void backupFiles(String path, String toFile) { try (OutputStream zip = FileUtils.newOutputStream(toFile, false); ZipOutputStream out = new ZipOutputStream(zip)) { FilePath p = FilePath.get(path); if (p.isDirectory()) { String pathShortName = path.replace('\\', '/'); pathShortName = pathShortName.substring(pathShortName.lastIndexOf('/') + 1); FilePath dir = FilePath.get(path); for (FilePath map : dir.newDirectoryStream()) { String entryNameBase = pathShortName + "/" + map.getName(); for (FilePath file : map.newDirectoryStream()) { backupFile(out, file.newInputStream(), entryNameBase + "/" + file.getName()); } } } else { backupFile(out, p.newInputStream(), p.getName()); } } catch (IOException e) { throw DbException.convertIOException(e, toFile); } }
AOStorage(Map<String, Object> config) { super(config); this.db = (IDatabase) config.get("db"); String storagePath = getStoragePath(); DataUtils.checkArgument(storagePath != null, "The storage path may not be null"); if (!FileUtils.exists(storagePath)) FileUtils.createDirectories(storagePath); FilePath dir = FilePath.get(storagePath); for (FilePath fp : dir.newDirectoryStream()) { String mapFullName = fp.getName(); if (mapFullName.startsWith(TEMP_NAME_PREFIX)) { fp.delete(); } } }