jiahuili430 commented on code in PR #4791:
URL: https://github.com/apache/couchdb/pull/4791#discussion_r1346205373


##########
nouveau/src/main/java/org/apache/couchdb/nouveau/core/IndexManager.java:
##########
@@ -307,12 +284,93 @@ public void onRemoval(String name, Index index, 
RemovalCause cause) {
         }
     }
 
+    private class IndexWeigher implements Weigher<String, Index> {
+
+        @Override
+        public @NonNegative int weigh(String key, Index value) {
+            // Pin active indexes
+            return value.isActive() ? 0 : 1;
+        }
+    }
+
+    private class AsyncIndexLoader implements AsyncCacheLoader<String, Index> {
+
+        @Override
+        public CompletableFuture<? extends Index> asyncLoad(String name, 
Executor executor) throws Exception {
+            final CompletableFuture<Index> future = new 
CompletableFuture<Index>();
+
+            executor.execute(() -> {
+                LOGGER.info("opening {}", name);
+                final Path path = indexPath(name);
+                Index result;
+                try {
+                    final IndexDefinition indexDefinition = 
loadIndexDefinition(name);
+                    final Analyzer analyzer = 
Lucene9AnalyzerFactory.fromDefinition(indexDefinition);
+                    final Directory dir = new 
DirectIODirectory(FSDirectory.open(path.resolve("9")));
+                    final IndexWriterConfig config = new 
IndexWriterConfig(analyzer);
+                    config.setUseCompoundFile(false);
+                    final IndexWriter writer = new IndexWriter(dir, config);
+                    final long updateSeq = getSeq(writer, "update_seq");
+                    final long purgeSeq = getSeq(writer, "purge_seq");
+                    final SearcherManager searcherManager = new 
SearcherManager(writer, searcherFactory);
+                    result = new Lucene9Index(analyzer, writer, updateSeq, 
purgeSeq, searcherManager);
+                    future.complete(result);
+                } catch (IOException e) {
+                    future.completeExceptionally(e);
+                }
+            });
+
+            return future;
+        }
+
+        @Override
+        public CompletableFuture<? extends Index> asyncReload(String name, 
Index index, Executor executor)
+                throws Exception {
+            executor.execute(() -> {
+                if (index.tryAcquire()) {
+                    try {
+                        try {
+                            if (index.commit()) {
+                                LOGGER.info("committed {}", name);
+                            }
+                        } catch (final IOException e) {
+                            LOGGER.warn("I/O exception while committing " + 
name, e);
+                        }
+                    } finally {
+                        index.release();
+                    }

Review Comment:
   Why do we need the outer `try-finally` block?
   ```java
   try {
     if (index.commit()) {
       LOGGER.info("committed {}", name);
   }}
   catch (final IOException e) {
     LOGGER.warn("I/O exception while committing " + name, e);
   }}
   catch (Exception e) { ... }
   finally {
     index.release();
   }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to