Apache Solr open-source search software
LUCENE-3579: DirectoryTaxonomyWriter should throw a proper exception if it was closed
Shai Erera5408d4d36c32
diff --git a/modules/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.java b/modules/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.javaindex c462747c663..f5bcb59d23b 100644--- a/modules/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.java+++ b/modules/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.java@@ -31,6 +31,7 @@ import org.apache.lucene.index.MultiFields;import org.apache.lucene.index.Terms;import org.apache.lucene.index.TermsEnum;import org.apache.lucene.search.DocIdSetIterator;+import org.apache.lucene.store.AlreadyClosedException;import org.apache.lucene.store.Directory;import org.apache.lucene.store.LockObtainFailedException;import org.apache.lucene.store.NativeFSLockFactory;@@ -117,6 +118,7 @@ public class DirectoryTaxonomyWriter implements TaxonomyWriter {* objects you create for the same directory.*/public void setDelimiter(char delimiter) {+ ensureOpen();this.delimiter = delimiter;}@@ -290,6 +292,7 @@ public class DirectoryTaxonomyWriter implements TaxonomyWriter {* @return Number of cache bytes in memory, for CL2O only; zero otherwise.*/public int getCacheMemoryUsage() {+ ensureOpen();if (this.cache == null || !(this.cache instanceof Cl2oTaxonomyWriterCache)) {return 0;}@@ -403,8 +406,8 @@ public class DirectoryTaxonomyWriter implements TaxonomyWriter {// calls - even those which could immediately return a cached value.// We definitely need to fix this situation!@Override- public synchronized int addCategory(CategoryPath categoryPath)- throws IOException {+ public synchronized int addCategory(CategoryPath categoryPath) throws IOException {+ ensureOpen();// If the category is already in the cache and/or the taxonomy, we// should return its existing ordinal:int res = findCategory(categoryPath);@@ -453,6 +456,16 @@ public class DirectoryTaxonomyWriter implements TaxonomyWriter {return id;}+ /**+ * Verifies that this instance wasn't closed, or throws+ * {@link AlreadyClosedException} if it is.+ */+ protected final void ensureOpen() {+ if (indexWriter == null) {+ throw new AlreadyClosedException("The taxonomy writer has already been closed");+ }+ }+// Note that the methods calling addCategoryDocument() are synchornized,// so this method is effectively synchronized as well, but we'll add// synchronized to be on the safe side, and we can reuse class-local objects@@ -570,6 +583,7 @@ public class DirectoryTaxonomyWriter implements TaxonomyWriter {*/@Overridepublic synchronized void commit() throws CorruptIndexException, IOException {+ ensureOpen();indexWriter.commit();refreshReader();}@@ -581,6 +595,7 @@ public class DirectoryTaxonomyWriter implements TaxonomyWriter {*/@Overridepublic synchronized void commit(Map<String,String> commitUserData) throws CorruptIndexException, IOException {+ ensureOpen();indexWriter.commit(commitUserData);refreshReader();}@@ -591,6 +606,7 @@ public class DirectoryTaxonomyWriter implements TaxonomyWriter {*/@Overridepublic synchronized void prepareCommit() throws CorruptIndexException, IOException {+ ensureOpen();indexWriter.prepareCommit();}@@ -600,6 +616,7 @@ public class DirectoryTaxonomyWriter implements TaxonomyWriter {*/@Overridepublic synchronized void prepareCommit(Map<String,String> commitUserData) throws CorruptIndexException, IOException {+ ensureOpen();indexWriter.prepareCommit(commitUserData);}@@ -616,6 +633,7 @@ public class DirectoryTaxonomyWriter implements TaxonomyWriter {*/@Overridesynchronized public int getSize() {+ ensureOpen();return indexWriter.maxDoc();}@@ -643,8 +661,10 @@ public class DirectoryTaxonomyWriter implements TaxonomyWriter {* method.*/public void setCacheMissesUntilFill(int i) {+ ensureOpen();cacheMissesUntilFill = i;}+private int cacheMissesUntilFill = 11;private boolean perhapsFillCache() throws IOException {@@ -717,6 +737,7 @@ public class DirectoryTaxonomyWriter implements TaxonomyWriter {}@Overridepublic int getParent(int ordinal) throws IOException {+ ensureOpen();// Note: the following if() just enforces that a user can never ask// for the parent of a nonexistant category - even if the parent array// was allocated bigger than it really needs to be.@@ -744,6 +765,7 @@ public class DirectoryTaxonomyWriter implements TaxonomyWriter {* and does not need to be commit()ed before this call.*/public void addTaxonomies(Directory[] taxonomies, OrdinalMap[] ordinalMaps) throws IOException {+ ensureOpen();// To prevent us stepping on the rest of this class's decisions on when// to open a reader, and when not, we'll be opening a new reader instead// of using the existing "reader" object:@@ -1009,10 +1031,16 @@ public class DirectoryTaxonomyWriter implements TaxonomyWriter {return null;}+ /**+ * Rollback changes to the taxonomy writer and closes the instance. Following+ * this method the instance becomes unusable (calling any of its API methods+ * will yield an {@link AlreadyClosedException}).+ */@Overridepublic void rollback() throws IOException {+ ensureOpen();indexWriter.rollback();- refreshReader();+ close();}}diff --git a/modules/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestDirectoryTaxonomyWriter.java b/modules/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestDirectoryTaxonomyWriter.javaindex 73bafde0736..a9cbf5e7ddd 100644--- a/modules/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestDirectoryTaxonomyWriter.java+++ b/modules/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestDirectoryTaxonomyWriter.java@@ -5,6 +5,7 @@ import java.util.Map;import org.apache.lucene.index.IndexReader;import org.apache.lucene.index.IndexWriterConfig.OpenMode;+import org.apache.lucene.store.AlreadyClosedException;import org.apache.lucene.store.Directory;import org.junit.Test;@@ -86,4 +87,35 @@ public class TestDirectoryTaxonomyWriter extends LuceneTestCase {dir.close();}+ @Test+ public void testRollback() throws Exception {+ // Verifies that if callback is called, DTW is closed.+ Directory dir = newDirectory();+ DirectoryTaxonomyWriter dtw = new DirectoryTaxonomyWriter(dir);+ dtw.addCategory(new CategoryPath("a"));+ dtw.rollback();+ try {+ dtw.addCategory(new CategoryPath("a"));+ fail("should not have succeeded to add a category following rollback.");+ } catch (AlreadyClosedException e) {+ // expected+ }+ dir.close();+ }++ @Test+ public void testEnsureOpen() throws Exception {+ // verifies that an exception is thrown if DTW was closed+ Directory dir = newDirectory();+ DirectoryTaxonomyWriter dtw = new DirectoryTaxonomyWriter(dir);+ dtw.close();+ try {+ dtw.addCategory(new CategoryPath("a"));+ fail("should not have succeeded to add a category following close.");+ } catch (AlreadyClosedException e) {+ // expected+ }+ dir.close();+ }+}