This is an automated email from the ASF dual-hosted git repository. rnewson pushed a commit to branch nouveau-walkfiletree in repository https://gitbox.apache.org/repos/asf/couchdb.git
commit 7e43c8cc110922ebd27b591df696a2763336f0a8 Author: Robert Newson <[email protected]> AuthorDate: Sat Oct 31 22:47:01 2026 +0000 nouveau: use Files.walkFileTree for deleteAll --- .../apache/couchdb/nouveau/core/IndexManager.java | 83 +++++++++++----------- 1 file changed, 41 insertions(+), 42 deletions(-) diff --git a/extra/nouveau/src/main/java/org/apache/couchdb/nouveau/core/IndexManager.java b/extra/nouveau/src/main/java/org/apache/couchdb/nouveau/core/IndexManager.java index 26cef0dc4..33aa1b11e 100644 --- a/extra/nouveau/src/main/java/org/apache/couchdb/nouveau/core/IndexManager.java +++ b/extra/nouveau/src/main/java/org/apache/couchdb/nouveau/core/IndexManager.java @@ -18,11 +18,13 @@ import io.dropwizard.lifecycle.Managed; import jakarta.validation.constraints.PositiveOrZero; import jakarta.ws.rs.WebApplicationException; import jakarta.ws.rs.core.Response.Status; -import java.io.File; import java.io.IOException; +import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; import java.nio.file.StandardCopyOption; +import java.nio.file.attribute.BasicFileAttributes; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; @@ -258,55 +260,52 @@ public final class IndexManager implements Managed { return Files.exists(indexDefinitionPath(name)); } - public void deleteAll(final String path, final List<String> exclusions) throws IOException { + public void deleteAll(final String pattern, final List<String> exclusions) throws IOException { + Objects.requireNonNull(pattern); LOGGER.info( "deleting indexes matching {} (excluding {})", - path, + pattern, exclusions == null || exclusions.isEmpty() ? "nothing" : exclusions); - var parts = path.split("/"); - deleteAll(rootDir, parts, 0, exclusions); - } + var globPath = rootDir.resolve(pattern); - private void deleteAll(final Path path, final String[] parts, final int index, final List<String> exclusions) - throws IOException { - // End of the path - if (index == parts.length - 1) { - try (var stream = Files.newDirectoryStream(path, parts[index])) { - stream.forEach(p -> { - if (exclusions != null && exclusions.indexOf(p.getFileName().toString()) != -1) { - return; - } - final Path relativeName = rootDir.relativize(p); - try { - deleteIndex(relativeName); - } catch (final IOException | InterruptedException e) { - LOGGER.error("Exception deleting {}", p, e); + Files.walkFileTree(rootDir, new SimpleFileVisitor<Path>() { + + @Override + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { + // Skip subtree on first path component mismatch + for (int i = 0; i < dir.getNameCount(); i++) { + var m = dir.getFileSystem().getPathMatcher("glob:" + globPath.getName(i)); + if (!m.matches(dir.subpath(i, i + 1))) { + return FileVisitResult.SKIP_SUBTREE; } - // Clean any newly empty directories. - do { - final File f = p.toFile(); - if (f.isDirectory() && f.list().length == 0) { - f.delete(); - } - } while ((p = p.getParent()) != null && !rootDir.equals(p)); - }); - } - return; - } - // Recurse - try (var stream = Files.newDirectoryStream(path, parts[index])) { - stream.forEach(p -> { + } + // We're not at full depth yet + if (dir.getNameCount() < globPath.getNameCount()) { + return FileVisitResult.CONTINUE; + } + // Skip exclusions + if (exclusions != null && exclusions.contains(dir.getFileName().toString())) { + return FileVisitResult.SKIP_SUBTREE; + } + // Delete this index try { - deleteAll(p, parts, index + 1, exclusions); - } catch (IOException e) { - LOGGER.warn("Exception during delete of " + rootDir.relativize(p), e); + unload(rootDir.relativize(dir), true); + } catch (InterruptedException e) { + LOGGER.info("Interrupted during deleteAll", e); + return FileVisitResult.TERMINATE; } - }); - } - } + return FileVisitResult.SKIP_SUBTREE; + } - private void deleteIndex(final Path name) throws IOException, InterruptedException { - unload(name, true); + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { + // Delete parent dir if it just became empty, but don't delete root dir. + if (!rootDir.equals(dir) && Files.list(dir).count() == 0) { + Files.delete(dir); + } + return FileVisitResult.CONTINUE; + } + }); } public void setMaxIndexesOpen(int maxIndexesOpen) {
