|
1 | 1 | package com.instaclustr.esop.impl; |
2 | 2 |
|
3 | 3 | import static java.lang.String.format; |
4 | | -import static java.util.stream.Collectors.groupingBy; |
| 4 | +import static java.util.stream.Collectors.toList; |
5 | 5 |
|
| 6 | +import java.io.IOException; |
6 | 7 | import java.nio.file.FileVisitResult; |
7 | 8 | import java.nio.file.Files; |
8 | 9 | import java.nio.file.Path; |
|
17 | 18 | import java.util.Map.Entry; |
18 | 19 | import java.util.Optional; |
19 | 20 | import java.util.function.Predicate; |
| 21 | +import java.util.stream.Collectors; |
20 | 22 |
|
21 | 23 | import com.instaclustr.esop.impl.RenamedEntities.Renamed; |
22 | 24 |
|
@@ -158,27 +160,102 @@ public RenamedEntities getRenamedEntities() { |
158 | 160 | return renamedEntities; |
159 | 161 | } |
160 | 162 |
|
| 163 | + public static class SnapshotsLister extends SimpleFileVisitor<Path> { |
| 164 | + |
| 165 | + private boolean isDropped = false; |
| 166 | + |
| 167 | + @Override |
| 168 | + public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs) throws IOException { |
| 169 | + if (dir.getParent().getFileName().toString().equals("snapshots")) { |
| 170 | + if (dir.getFileName().toString().startsWith("dropped-")) { |
| 171 | + isDropped = true; |
| 172 | + return FileVisitResult.TERMINATE; |
| 173 | + } else { |
| 174 | + return FileVisitResult.SKIP_SUBTREE; |
| 175 | + } |
| 176 | + } else { |
| 177 | + return FileVisitResult.CONTINUE; |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + public boolean isDropped() { |
| 182 | + return isDropped; |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + public static class KeyspaceTableLister extends SimpleFileVisitor<Path> { |
| 187 | + |
| 188 | + private final Path cassandraDir; |
| 189 | + private final Map<Path, List<Path>> dataDirs = new HashMap<>(); |
| 190 | + |
| 191 | + public KeyspaceTableLister(final Path cassandraDir) { |
| 192 | + this.cassandraDir = cassandraDir; |
| 193 | + } |
| 194 | + |
| 195 | + @Override |
| 196 | + public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs) throws IOException { |
| 197 | + // we hit keyspace |
| 198 | + if (dir.getParent().equals(cassandraDir)) { |
| 199 | + dataDirs.putIfAbsent(dir, new ArrayList<>()); |
| 200 | + return FileVisitResult.CONTINUE; |
| 201 | + // we hit table |
| 202 | + } else if (dir.getParent().getParent().equals(cassandraDir)) { |
| 203 | + // detect if it is a dropped table |
| 204 | + Path snapshotsDir = dir.resolve("snapshots"); |
| 205 | + if (Files.exists(snapshotsDir)) { |
| 206 | + SnapshotsLister snapshotsLister = new SnapshotsLister(); |
| 207 | + Files.walkFileTree(snapshotsDir, snapshotsLister); |
| 208 | + if (!snapshotsLister.isDropped()) { |
| 209 | + dataDirs.get(dir.getParent()).add(dir); |
| 210 | + } |
| 211 | + } else { |
| 212 | + dataDirs.get(dir.getParent()).add(dir); |
| 213 | + } |
| 214 | + |
| 215 | + return FileVisitResult.SKIP_SUBTREE; |
| 216 | + } else { |
| 217 | + return FileVisitResult.CONTINUE; |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + /** |
| 222 | + * Remove keyspaces which have 0 tables, it means that each table has a snapshot with "dropped-" snapshot name |
| 223 | + */ |
| 224 | + public void removeDroppedKeyspaces() { |
| 225 | + final List<Path> droppedKeyspaces = dataDirs |
| 226 | + .entrySet() |
| 227 | + .stream() |
| 228 | + .filter(entry -> entry.getValue().isEmpty()) |
| 229 | + .map(Entry::getKey) |
| 230 | + .collect(toList()); |
| 231 | + |
| 232 | + for (final Path droppedKeyspace : droppedKeyspaces) { |
| 233 | + dataDirs.remove(droppedKeyspace); |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + public Map<Path, List<Path>> getDataDirs() { |
| 238 | + return dataDirs; |
| 239 | + } |
| 240 | + |
| 241 | + @Override |
| 242 | + public String toString() { |
| 243 | + return dataDirs.toString(); |
| 244 | + } |
| 245 | + } |
| 246 | + |
161 | 247 | public static CassandraData parse(final Path cassandraDir) throws Exception { |
162 | 248 |
|
163 | 249 | if (!Files.exists(cassandraDir)) { |
164 | 250 | return CassandraData.empty(); |
165 | 251 | } |
166 | 252 |
|
167 | | - final Map<Path, List<Path>> dataDirs = Files.find(cassandraDir, |
168 | | - 2, |
169 | | - (path, basicFileAttributes) -> basicFileAttributes.isDirectory() && |
170 | | - !path.getParent().equals(cassandraDir) && |
171 | | - !path.equals(cassandraDir)) |
172 | | - // take only these into consideration which do not have "snapshots/dropped-" |
173 | | - .filter(table -> { |
174 | | - try { |
175 | | - return Files.find(table, 2, (p, b) -> b.isDirectory() && p.toString().contains("snapshots/dropped-")).count() == 0; |
176 | | - } catch (final Exception ex) { |
177 | | - return false; |
178 | | - } |
| 253 | + final KeyspaceTableLister lister = new KeyspaceTableLister(cassandraDir); |
| 254 | + |
| 255 | + Files.walkFileTree(cassandraDir, lister); |
| 256 | + lister.removeDroppedKeyspaces(); |
179 | 257 |
|
180 | | - }) |
181 | | - .collect(groupingBy(Path::getParent)); |
| 258 | + final Map<Path, List<Path>> dataDirs = lister.getDataDirs(); |
182 | 259 |
|
183 | 260 | final Map<String, Map<String, String>> tableIdsMap = new HashMap<>(); |
184 | 261 |
|
|
0 commit comments