You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by so...@apache.org on 2021/08/18 19:44:02 UTC

[lucene] branch main updated: Close vector dictionary when exiting the demo

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

sokolov pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/lucene.git


The following commit(s) were added to refs/heads/main by this push:
     new b8210de  Close vector dictionary when exiting the demo
b8210de is described below

commit b8210dee7aa63e2a888e392f11109c7e65df0fc5
Author: Michael Sokolov <so...@amazon.com>
AuthorDate: Wed Aug 18 15:43:33 2021 -0400

    Close vector dictionary when exiting the demo
---
 .../java/org/apache/lucene/demo/IndexFiles.java    | 42 +++++++++++++---------
 .../java/org/apache/lucene/demo/SearchFiles.java   |  3 ++
 2 files changed, 28 insertions(+), 17 deletions(-)

diff --git a/lucene/demo/src/java/org/apache/lucene/demo/IndexFiles.java b/lucene/demo/src/java/org/apache/lucene/demo/IndexFiles.java
index e9bc581..1e23c5d 100644
--- a/lucene/demo/src/java/org/apache/lucene/demo/IndexFiles.java
+++ b/lucene/demo/src/java/org/apache/lucene/demo/IndexFiles.java
@@ -54,15 +54,18 @@ import org.apache.lucene.store.FSDirectory;
  * <p>This is a command-line application demonstrating simple Lucene indexing. Run it with no
  * command-line arguments for usage information.
  */
-public class IndexFiles {
+public class IndexFiles implements AutoCloseable {
 
   // Calculates embedding vectors for KnnVector search
   private final DemoEmbeddings demoEmbeddings;
+  private final KnnVectorDict vectorDict;
 
   private IndexFiles(Path vectorDictPath) throws IOException {
     if (vectorDictPath != null) {
-      demoEmbeddings = new DemoEmbeddings(new KnnVectorDict(vectorDictPath));
+      vectorDict = new KnnVectorDict(vectorDictPath);
+      demoEmbeddings = new DemoEmbeddings(vectorDict);
     } else {
+      vectorDict = null;
       demoEmbeddings = null;
     }
   }
@@ -139,19 +142,18 @@ public class IndexFiles {
       //
       // iwc.setRAMBufferSizeMB(256.0);
 
-      IndexWriter writer = new IndexWriter(dir, iwc);
-      IndexFiles indexFiles = new IndexFiles(vectorDictPath);
-      indexFiles.indexDocs(writer, docDir);
+      try (IndexWriter writer = new IndexWriter(dir, iwc);
+          IndexFiles indexFiles = new IndexFiles(vectorDictPath)) {
+        indexFiles.indexDocs(writer, docDir);
 
-      // NOTE: if you want to maximize search performance,
-      // you can optionally call forceMerge here.  This can be
-      // a terribly costly operation, so generally it's only
-      // worth it when your index is relatively static (ie
-      // you're done adding documents to it):
-      //
-      // writer.forceMerge(1);
-
-      writer.close();
+        // NOTE: if you want to maximize search performance,
+        // you can optionally call forceMerge here.  This can be
+        // a terribly costly operation, so generally it's only
+        // worth it when your index is relatively static (ie
+        // you're done adding documents to it):
+        //
+        // writer.forceMerge(1);
+      }
 
       Date end = new Date();
       try (IndexReader reader = DirectoryReader.open(dir)) {
@@ -185,10 +187,9 @@ public class IndexFiles {
     if (Files.isDirectory(path)) {
       Files.walkFileTree(
           path,
-          new SimpleFileVisitor<Path>() {
+          new SimpleFileVisitor<>() {
             @Override
-            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
-                throws IOException {
+            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
               try {
                 indexDoc(writer, file, attrs.lastModifiedTime().toMillis());
               } catch (
@@ -259,4 +260,11 @@ public class IndexFiles {
       }
     }
   }
+
+  @Override
+  public void close() throws IOException {
+    if (vectorDict != null) {
+      vectorDict.close();
+    }
+  }
 }
diff --git a/lucene/demo/src/java/org/apache/lucene/demo/SearchFiles.java b/lucene/demo/src/java/org/apache/lucene/demo/SearchFiles.java
index 37da184..eeaaa95 100644
--- a/lucene/demo/src/java/org/apache/lucene/demo/SearchFiles.java
+++ b/lucene/demo/src/java/org/apache/lucene/demo/SearchFiles.java
@@ -154,6 +154,9 @@ public class SearchFiles {
         break;
       }
     }
+    if (vectorDict != null) {
+      vectorDict.close();
+    }
     reader.close();
   }