|
20 | 20 | import java.nio.file.StandardCopyOption; |
21 | 21 | import java.util.List; |
22 | 22 | import java.util.Properties; |
| 23 | +import java.util.stream.Stream; |
23 | 24 | import lombok.extern.slf4j.Slf4j; |
24 | 25 |
|
25 | 26 | @Slf4j |
@@ -137,39 +138,44 @@ public static boolean isSymbolicLink(File file) throws IOException { |
137 | 138 | */ |
138 | 139 | public static void copyDatabases(Path src, Path dest, List<String> subDirs) |
139 | 140 | throws IOException { |
140 | | - // create subdirs, as using parallel() to run, so should create dirs first. |
141 | | - subDirs.forEach(dir -> { |
142 | | - if (isExists(Paths.get(src.toString(), dir).toString())) { |
143 | | - try { |
144 | | - Files.walk(Paths.get(src.toString(), dir), FileVisitOption.FOLLOW_LINKS) |
145 | | - .forEach(source -> copy(source, dest.resolve(src.relativize(source)))); |
146 | | - } catch (IOException e) { |
147 | | - logger.error("copy database failed, src: {}, dest: {}, error: {}", |
148 | | - Paths.get(src.toString(), dir), Paths.get(dest.toString(), dir), e.getMessage()); |
149 | | - throw new RuntimeException(e); |
150 | | - } |
| 141 | + for (String dir : subDirs) { |
| 142 | + if (!isExists(Paths.get(src.toString(), dir).toString())) { |
| 143 | + continue; |
151 | 144 | } |
152 | | - }); |
153 | | - } |
154 | | - |
155 | | - public static void copyDir(Path src, Path dest, String dir) { |
156 | | - if (isExists(Paths.get(src.toString(), dir).toString())) { |
157 | 145 | try { |
158 | | - if (createDirIfNotExists(Paths.get(dest.toString(), dir).toString())) { |
159 | | - Files.walk(Paths.get(src.toString(), dir), FileVisitOption.FOLLOW_LINKS) |
160 | | - .forEach(source -> copy(source, dest.resolve(src.relativize(source)))); |
161 | | - } else { |
162 | | - throw new IOException(String.format("dest %s create fail ", |
163 | | - Paths.get(dest.toString(), dir))); |
164 | | - } |
| 146 | + copyTree(src, dest, dir); |
165 | 147 | } catch (IOException e) { |
166 | | - logger.error("copy dir failed, src: {}, dest: {}, error: {}", |
| 148 | + logger.error("copy database failed, src: {}, dest: {}, error: {}", |
167 | 149 | Paths.get(src.toString(), dir), Paths.get(dest.toString(), dir), e.getMessage()); |
168 | 150 | throw new RuntimeException(e); |
169 | 151 | } |
170 | 152 | } |
171 | 153 | } |
172 | 154 |
|
| 155 | + public static void copyDir(Path src, Path dest, String dir) { |
| 156 | + if (!isExists(Paths.get(src.toString(), dir).toString())) { |
| 157 | + return; |
| 158 | + } |
| 159 | + try { |
| 160 | + if (!createDirIfNotExists(Paths.get(dest.toString(), dir).toString())) { |
| 161 | + throw new IOException(String.format("dest %s create fail ", |
| 162 | + Paths.get(dest.toString(), dir))); |
| 163 | + } |
| 164 | + copyTree(src, dest, dir); |
| 165 | + } catch (IOException e) { |
| 166 | + logger.error("copy dir failed, src: {}, dest: {}, error: {}", |
| 167 | + Paths.get(src.toString(), dir), Paths.get(dest.toString(), dir), e.getMessage()); |
| 168 | + throw new RuntimeException(e); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + private static void copyTree(Path src, Path dest, String dir) throws IOException { |
| 173 | + try (Stream<Path> paths = Files.walk( |
| 174 | + Paths.get(src.toString(), dir), FileVisitOption.FOLLOW_LINKS)) { |
| 175 | + paths.forEach(source -> copy(source, dest.resolve(src.relativize(source)))); |
| 176 | + } |
| 177 | + } |
| 178 | + |
173 | 179 | public static void copy(Path source, Path dest) { |
174 | 180 | try { |
175 | 181 | // create hard link when file is .sst |
|
0 commit comments