]> source.dussan.org Git - gitblit.git/commitdiff
Ensure repository indexes are properly closed before deletion
authorJames Moger <james.moger@gitblit.com>
Mon, 19 Mar 2012 20:28:58 +0000 (16:28 -0400)
committerJames Moger <james.moger@gitblit.com>
Mon, 19 Mar 2012 20:28:58 +0000 (16:28 -0400)
src/com/gitblit/LuceneExecutor.java

index 220967befcd455e6998c807e7d86d191e7829f1f..e859947d97eff185877bd538795aef92a80172ca 100644 (file)
@@ -228,31 +228,31 @@ public class LuceneExecutor implements Runnable {
         * \r
         * @param repositoryName\r
         */\r
-       public void close(String repositoryName) {\r
-               try {\r
-                       IndexWriter writer = writers.remove(repositoryName);\r
-                       if (writer != null) {\r
-                               writer.close();\r
-                       }\r
-               } catch (Exception e) {\r
-                       logger.error("Failed to close index writer for " + repositoryName, e);\r
-               }\r
-\r
+       public synchronized void close(String repositoryName) {\r
                try {\r
                        IndexSearcher searcher = searchers.remove(repositoryName);\r
                        if (searcher != null) {\r
-                               searcher.close();\r
+                               searcher.getIndexReader().close();\r
                        }\r
                } catch (Exception e) {\r
                        logger.error("Failed to close index searcher for " + repositoryName, e);\r
                }\r
+               \r
+               try {\r
+                       IndexWriter writer = writers.remove(repositoryName);\r
+                       if (writer != null) {\r
+                               writer.close();\r
+                       }\r
+               } catch (Exception e) {\r
+                       logger.error("Failed to close index writer for " + repositoryName, e);\r
+               }               \r
        }\r
 \r
        /**\r
         * Close all Lucene indexers.\r
         * \r
         */\r
-       public void close() {\r
+       public synchronized void close() {\r
                // close all writers\r
                for (String writer : writers.keySet()) {\r
                        try {\r
@@ -266,7 +266,7 @@ public class LuceneExecutor implements Runnable {
                // close all searchers\r
                for (String searcher : searchers.keySet()) {\r
                        try {\r
-                               searchers.get(searcher).close();\r
+                               searchers.get(searcher).getIndexReader().close();\r
                        } catch (Throwable t) {\r
                                logger.error("Failed to close Lucene searcher for " + searcher, t);\r
                        }\r
@@ -283,18 +283,9 @@ public class LuceneExecutor implements Runnable {
         */\r
        public boolean deleteIndex(String repositoryName) {\r
                try {\r
-                       // remove the repository index writer from the cache and close it\r
-                       IndexWriter writer = writers.remove(repositoryName);\r
-                       if (writer != null) {\r
-                               writer.close();\r
-                               writer = null;\r
-                       }\r
-                       // remove the repository index searcher from the cache and close it\r
-                       IndexSearcher searcher = searchers.remove(repositoryName);\r
-                       if (searcher != null) {\r
-                               searcher.close();\r
-                               searcher = null;\r
-                       }\r
+                       // close any open writer/searcher\r
+                       close(repositoryName);\r
+\r
                        // delete the index folder\r
                        File repositoryFolder = new File(repositoriesFolder, repositoryName);\r
                        File luceneIndex = new File(repositoryFolder, LUCENE_DIR);\r
@@ -420,7 +411,7 @@ public class LuceneExecutor implements Runnable {
         * @return IndexResult\r
         */\r
        public IndexResult reindex(RepositoryModel model, Repository repository) {\r
-               IndexResult result = new IndexResult();\r
+               IndexResult result = new IndexResult();         \r
                if (!deleteIndex(model.name)) {\r
                        return result;\r
                }\r
@@ -615,8 +606,8 @@ public class LuceneExecutor implements Runnable {
                        // commit all changes and reset the searcher\r
                        config.setInt(CONF_INDEX, null, CONF_VERSION, INDEX_VERSION);\r
                        config.save();\r
-                       resetIndexSearcher(model.name);\r
                        writer.commit();\r
+                       resetIndexSearcher(model.name);\r
                        result.success();\r
                } catch (Exception e) {\r
                        logger.error("Exception while reindexing " + model.name, e);\r
@@ -931,8 +922,8 @@ public class LuceneExecutor implements Runnable {
                try {                   \r
                        IndexWriter writer = getIndexWriter(repositoryName);\r
                        writer.addDocument(doc);\r
-                       resetIndexSearcher(repositoryName);\r
                        writer.commit();\r
+                       resetIndexSearcher(repositoryName);\r
                        return true;\r
                } catch (Exception e) {\r
                        logger.error(MessageFormat.format("Exception while incrementally updating {0} Lucene index", repositoryName), e);\r
@@ -966,7 +957,7 @@ public class LuceneExecutor implements Runnable {
        private synchronized void resetIndexSearcher(String repository) throws IOException {\r
                IndexSearcher searcher = searchers.remove(repository);\r
                if (searcher != null) {\r
-                       searcher.close();\r
+                       searcher.getIndexReader().close();\r
                }\r
        }\r
 \r