You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by ca...@apache.org on 2016/01/27 19:29:17 UTC

svn commit: r1727149 - /jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/util/SuggestHelper.java

Author: catholicon
Date: Wed Jan 27 18:29:17 2016
New Revision: 1727149

URL: http://svn.apache.org/viewvc?rev=1727149&view=rev
Log:
OAK-3917: SuggestionHelper creating unnecessary temporary directories

The only temp dir requirement is at AnalyzingInfixSuggester.build() where it gets a temp dir with ".tmp" suffixed as a sibling to initial path that we provide it in its constructor. So, now we would just create a temp directory -> create a placehold file under it and pass it to suggester -> use the placeholder path to decide if OakDirectory is to be returned -> After build delete the parent temp directory

Modified:
    jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/util/SuggestHelper.java

Modified: jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/util/SuggestHelper.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/util/SuggestHelper.java?rev=1727149&r1=1727148&r2=1727149&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/util/SuggestHelper.java (original)
+++ jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/util/SuggestHelper.java Wed Jan 27 18:29:17 2016
@@ -25,6 +25,7 @@ import java.util.Collections;
 import java.util.List;
 
 import com.google.common.io.Files;
+import org.apache.commons.io.FileUtils;
 import org.apache.jackrabbit.oak.plugins.index.lucene.FieldNames;
 import org.apache.lucene.analysis.Analyzer;
 import org.apache.lucene.index.IndexReader;
@@ -54,11 +55,26 @@ public class SuggestHelper {
     };
 
     public static void updateSuggester(Directory directory, Analyzer analyzer, IndexReader reader) throws IOException {
+        File tempDir = null;
         try {
+            //Analyzing infix suggester takes a file parameter. It uses its path to getDirectory()
+            //for actual storage of suggester data. BUT, while building it also does getDirectory() to
+            //a temporary location (original path + ".tmp"). So, instead we create a temp dir and also
+            //create a placeholder non-existing-sub-child which would mark the location when we want to return
+            //our internal suggestion OakDirectory. After build is done, we'd delete the temp directory
+            //thereby removing any temp stuff that suggester created in the interim.
+            tempDir = Files.createTempDir();
+            File tempSubChild = new File(tempDir, "non-existing-sub-child");
+
             Dictionary dictionary = new LuceneDictionary(reader, FieldNames.SUGGEST);
-            getLookup(directory, analyzer).build(dictionary);
+            getLookup(directory, analyzer, tempSubChild).build(dictionary);
         } catch (RuntimeException e) {
             log.debug("could not update the suggester", e);
+        } finally {
+            //cleanup temp dir
+            if (tempDir != null && !FileUtils.deleteQuietly(tempDir)) {
+                log.error("Cleanup failed for temp dir {}", tempDir.getAbsolutePath());
+            }
         }
     }
 
@@ -104,11 +120,14 @@ public class SuggestHelper {
     }
 
     public static AnalyzingInfixSuggester getLookup(final Directory suggestDirectory, Analyzer analyzer) throws IOException {
-        final File tempDir = Files.createTempDir();
+        return getLookup(suggestDirectory, analyzer, null);
+    }
+    public static AnalyzingInfixSuggester getLookup(final Directory suggestDirectory, Analyzer analyzer,
+                                                    final File tempDir) throws IOException {
         return new AnalyzingInfixSuggester(Version.LUCENE_47, tempDir, analyzer, analyzer, 3) {
             @Override
             protected Directory getDirectory(File path) throws IOException {
-                if (tempDir.getAbsolutePath().equals(path.getAbsolutePath())) {
+                if (tempDir == null || tempDir.getAbsolutePath().equals(path.getAbsolutePath())) {
                     return suggestDirectory; // use oak directory for writing suggest index
                 } else {
                     return FSDirectory.open(path); // use FS for temp index used at build time