This is an automated email from the ASF dual-hosted git repository.

rnewson pushed a commit to branch nouveau-db-delete-glob
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit 8a006aa7ab0d6843a3b70d8efb875bfde0f4f2d4
Author: Robert Newson <[email protected]>
AuthorDate: Wed Oct 28 22:03:21 2026 +0000

    nouveau: fix glob pattern when cleaning up after db deletion
    
    Fix the glob pattern but also ensure we match indexes on Windows by
    using Path instead of String for index naming (so that we preserve
    the OS-specific path separator).
---
 .../apache/couchdb/nouveau/core/IndexManager.java  | 42 +++++++++------
 .../couchdb/nouveau/core/IndexManagerTest.java     | 60 ++++++++++++++--------
 src/nouveau/src/nouveau_index_manager.erl          |  2 +-
 3 files changed, 66 insertions(+), 38 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 bd55ced16..26cef0dc4 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
@@ -54,7 +54,7 @@ import org.slf4j.LoggerFactory;
  */
 public final class IndexManager implements Managed {
 
-    private class LRUMap extends LinkedHashMap<String, IndexHolder> {
+    private class LRUMap extends LinkedHashMap<Path, IndexHolder> {
 
         private LRUMap(final int capacity) {
             super(capacity, 0.75f, true);
@@ -100,10 +100,15 @@ public final class IndexManager implements Managed {
 
     private LRUMap cache;
 
-    private StripedLock<String> createLock;
+    private StripedLock<Path> createLock;
 
     public <R> R with(final String name, final IndexFunction<Index, R> 
indexFun)
             throws IOException, InterruptedException {
+        return with(Path.of(name), indexFun);
+    }
+
+    private <R> R with(final Path name, final IndexFunction<Index, R> indexFun)
+            throws IOException, InterruptedException {
         evictIfOverCapacity();
 
         retry:
@@ -167,7 +172,7 @@ public final class IndexManager implements Managed {
 
     private void evictIfOverCapacity() throws IOException, 
InterruptedException {
         while (true) {
-            final String candidate;
+            final Path candidate;
             synchronized (cache) {
                 if (cache.size() <= maxIndexesOpen) {
                     return;
@@ -178,7 +183,7 @@ public final class IndexManager implements Managed {
         }
     }
 
-    public void unload(final String name, final boolean forceDelete) throws 
IOException, InterruptedException {
+    private void unload(final Path name, final boolean forceDelete) throws 
IOException, InterruptedException {
         final IndexHolder holder;
         synchronized (cache) {
             holder = cache.computeIfAbsent(name, (k) -> new IndexHolder());
@@ -202,6 +207,7 @@ public final class IndexManager implements Managed {
                 case NOT_LOADED:
                 case UNLOADED:
                     if (forceDelete) {
+                        LOGGER.debug("deleting unloaded index {}", name);
                         IOUtils.rm(indexRootPath(name));
                     }
                     break;
@@ -215,6 +221,10 @@ public final class IndexManager implements Managed {
     }
 
     public void create(final String name, IndexDefinition indexDefinition) 
throws IOException {
+        create(Path.of(name), indexDefinition);
+    }
+
+    private void create(final Path name, IndexDefinition indexDefinition) 
throws IOException {
         if (exists(name)) {
             assertSame(indexDefinition, loadIndexDefinition(name));
             return;
@@ -244,7 +254,7 @@ public final class IndexManager implements Managed {
         }
     }
 
-    public boolean exists(final String name) {
+    public boolean exists(final Path name) {
         return Files.exists(indexDefinitionPath(name));
     }
 
@@ -266,7 +276,7 @@ public final class IndexManager implements Managed {
                     if (exclusions != null && 
exclusions.indexOf(p.getFileName().toString()) != -1) {
                         return;
                     }
-                    final String relativeName = 
rootDir.relativize(p).toString();
+                    final Path relativeName = rootDir.relativize(p);
                     try {
                         deleteIndex(relativeName);
                     } catch (final IOException | InterruptedException e) {
@@ -295,7 +305,7 @@ public final class IndexManager implements Managed {
         }
     }
 
-    private void deleteIndex(final String name) throws IOException, 
InterruptedException {
+    private void deleteIndex(final Path name) throws IOException, 
InterruptedException {
         unload(name, true);
     }
 
@@ -335,10 +345,10 @@ public final class IndexManager implements Managed {
         Objects.requireNonNull(this.schedulerExecutorService, 
"schedulerExecutorService must not be null");
 
         this.cache = new LRUMap(this.maxIndexesOpen);
-        this.createLock = new StripedLock<String>(100);
+        this.createLock = new StripedLock<Path>(100);
     }
 
-    private Runnable commitFun(final String name, final IndexHolder holder) {
+    private Runnable commitFun(final Path name, final IndexHolder holder) {
         return () -> {
             holder.lock.readLock().lock();
             try {
@@ -356,7 +366,7 @@ public final class IndexManager implements Managed {
     @Override
     public void stop() throws IOException, InterruptedException {
         synchronized (cache) {
-            var names = new HashSet<String>(cache.keySet());
+            var names = new HashSet<Path>(cache.keySet());
             for (var name : names) {
                 unload(name, false);
             }
@@ -368,19 +378,19 @@ public final class IndexManager implements Managed {
         return path.resolve("index_definition.json").toFile().exists();
     }
 
-    private Path indexDefinitionPath(final String name) {
+    private Path indexDefinitionPath(final Path name) {
         return indexRootPath(name).resolve("index_definition.json");
     }
 
-    private Path indexPath(final String name) {
+    private Path indexPath(final Path name) {
         return indexRootPath(name).resolve("index");
     }
 
-    private IndexDefinition loadIndexDefinition(final String name) throws 
IOException {
+    private IndexDefinition loadIndexDefinition(final Path name) throws 
IOException {
         return objectMapper.readValue(indexDefinitionPath(name).toFile(), 
IndexDefinition.class);
     }
 
-    private Path indexRootPath(final String name) {
+    private Path indexRootPath(final Path name) {
         final Path result = rootDir.resolve(name);
         if (result.startsWith(rootDir)) {
             return result;
@@ -388,7 +398,7 @@ public final class IndexManager implements Managed {
         throw new WebApplicationException(name + " attempts to escape from 
index root directory", Status.BAD_REQUEST);
     }
 
-    private Index load(final String name) throws IOException {
+    private Index load(final Path name) throws IOException {
         LOGGER.info("opening {}", name);
         final Path path = indexPath(name);
         final IndexDefinition indexDefinition = loadIndexDefinition(name);
@@ -420,7 +430,7 @@ public final class IndexManager implements Managed {
         return defaultValue;
     }
 
-    private void close(final String name, final IndexHolder holder) throws 
IOException {
+    private void close(final Path name, final IndexHolder holder) throws 
IOException {
         if (!holder.lock.isWriteLockedByCurrentThread()) {
             throw new IllegalStateException();
         }
diff --git 
a/extra/nouveau/src/test/java/org/apache/couchdb/nouveau/core/IndexManagerTest.java
 
b/extra/nouveau/src/test/java/org/apache/couchdb/nouveau/core/IndexManagerTest.java
index b76ff1268..1cbf64f34 100644
--- 
a/extra/nouveau/src/test/java/org/apache/couchdb/nouveau/core/IndexManagerTest.java
+++ 
b/extra/nouveau/src/test/java/org/apache/couchdb/nouveau/core/IndexManagerTest.java
@@ -31,6 +31,8 @@ import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.io.TempDir;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
 
 public class IndexManagerTest {
 
@@ -88,51 +90,67 @@ public class IndexManagerTest {
         assertThat(isOpen);
     }
 
-    @Test
-    public void deleteAllRemovesIndexByName() throws Exception {
-        final IndexDefinition indexDefinition = new IndexDefinition();
-
+    @ParameterizedTest
+    @ValueSource(booleans = {true, false})
+    public void deleteAllRemovesIndexByName(final boolean shouldOpen) throws 
Exception {
         assertThat(countIndexes()).isEqualTo(0);
-        manager.create("bar", indexDefinition);
+        var index = create("bar", shouldOpen);
         assertThat(countIndexes()).isEqualTo(1);
         manager.deleteAll("bar", null);
+        assertClosed(index);
         assertThat(countIndexes()).isEqualTo(0);
     }
 
-    @Test
-    public void deleteAllRemovesIndexByPath() throws Exception {
-        final IndexDefinition indexDefinition = new IndexDefinition();
-
+    @ParameterizedTest
+    @ValueSource(booleans = {true, false})
+    public void deleteAllRemovesIndexByPath(final boolean shouldOpen) throws 
Exception {
         assertThat(countIndexes()).isEqualTo(0);
-        manager.create("foo/bar", indexDefinition);
+        var index = create("foo/bar", shouldOpen);
         assertThat(countIndexes()).isEqualTo(1);
         manager.deleteAll("foo/bar", null);
+        assertClosed(index);
         assertThat(countIndexes()).isEqualTo(0);
     }
 
-    @Test
-    public void deleteAllRemovesIndexByGlob() throws Exception {
-        final IndexDefinition indexDefinition = new IndexDefinition();
-
+    @ParameterizedTest
+    @ValueSource(booleans = {true, false})
+    public void deleteAllRemovesIndexByGlob(final boolean shouldOpen) throws 
Exception {
         assertThat(countIndexes()).isEqualTo(0);
-        manager.create("foo/bar", indexDefinition);
+        var index = create("foo/bar", shouldOpen);
         assertThat(countIndexes()).isEqualTo(1);
         manager.deleteAll("foo/*", null);
+        assertClosed(index);
         assertThat(countIndexes()).isEqualTo(0);
     }
 
-    @Test
-    public void deleteAllRemovesIndexByGlobExceptExclusions() throws Exception 
{
-        final IndexDefinition indexDefinition = new IndexDefinition();
-
+    @ParameterizedTest
+    @ValueSource(booleans = {true, false})
+    public void deleteAllRemovesIndexByGlobExceptExclusions(final boolean 
shouldOpen) throws Exception {
         assertThat(countIndexes()).isEqualTo(0);
-        manager.create("foo/bar", indexDefinition);
-        manager.create("foo/baz", indexDefinition);
+        create("foo/bar", shouldOpen);
+        var index = create("foo/baz", shouldOpen);
         assertThat(countIndexes()).isEqualTo(2);
         manager.deleteAll("foo/*", List.of("bar"));
+        assertClosed(index);
         assertThat(countIndexes()).isEqualTo(1);
     }
 
+    private Index create(final String name, final boolean shouldOpen) throws 
Exception {
+        manager.create(name, new IndexDefinition());
+        if (shouldOpen) {
+            final Index index = manager.with(name, (i) -> i);
+            assertThat(index.isOpen()).isTrue();
+            return index;
+        }
+        return null;
+    }
+
+    private void assertClosed(final Index index) {
+        if (index != null) {
+            assertThat(index.isOpen()).isFalse();
+        }
+    }
+
     private long countIndexes() throws IOException {
         try (var stream =
                 Files.find(rootDir, 10, (p, attr) -> 
p.getFileName().toString().equals("index_definition.json"))) {
diff --git a/src/nouveau/src/nouveau_index_manager.erl 
b/src/nouveau/src/nouveau_index_manager.erl
index 44ee17a4d..b11df004f 100644
--- a/src/nouveau/src/nouveau_index_manager.erl
+++ b/src/nouveau/src/nouveau_index_manager.erl
@@ -122,7 +122,7 @@ handle_info(_Msg, State) ->
 
 handle_db_event(DbName, deleted, State) ->
     couch_log:notice("Deleting indexes for ~s as database was deleted", 
[DbName]),
-    nouveau_api:delete_path(nouveau_util:index_name(DbName)),
+    nouveau_api:delete_path(nouveau_util:index_name(<<DbName/binary, "/*">>)),
     {ok, State};
 handle_db_event(_DbName, _Event, State) ->
     {ok, State}.

Reply via email to