You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by sh...@apache.org on 2011/11/16 09:56:31 UTC

svn commit: r1202593 - in /lucene/dev/trunk/modules/facet/src: java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.java test/org/apache/lucene/facet/taxonomy/directory/TestDirectoryTaxonomyWriter.java

Author: shaie
Date: Wed Nov 16 08:56:31 2011
New Revision: 1202593

URL: http://svn.apache.org/viewvc?rev=1202593&view=rev
Log:
LUCENE-3579: DirectoryTaxonomyWriter should throw a proper exception if it was closed

Modified:
    lucene/dev/trunk/modules/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.java
    lucene/dev/trunk/modules/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestDirectoryTaxonomyWriter.java

Modified: lucene/dev/trunk/modules/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/modules/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.java?rev=1202593&r1=1202592&r2=1202593&view=diff
==============================================================================
--- lucene/dev/trunk/modules/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.java (original)
+++ lucene/dev/trunk/modules/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.java Wed Nov 16 08:56:31 2011
@@ -31,6 +31,7 @@ import org.apache.lucene.index.MultiFiel
 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 imp
    * objects you create for the same directory.
    */
   public void setDelimiter(char delimiter) {
+    ensureOpen();
     this.delimiter = delimiter;
   }
 
@@ -290,6 +292,7 @@ public class DirectoryTaxonomyWriter imp
    * @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 imp
   // 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 imp
     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 imp
    */ 
   @Override
   public synchronized void commit() throws CorruptIndexException, IOException {
+    ensureOpen();
     indexWriter.commit();
     refreshReader();
   }
@@ -581,6 +595,7 @@ public class DirectoryTaxonomyWriter imp
    */
   @Override
   public synchronized void commit(Map<String,String> commitUserData) throws CorruptIndexException, IOException {
+    ensureOpen();
     indexWriter.commit(commitUserData);
     refreshReader();
   }
@@ -591,6 +606,7 @@ public class DirectoryTaxonomyWriter imp
    */
   @Override
   public synchronized void prepareCommit() throws CorruptIndexException, IOException {
+    ensureOpen();
     indexWriter.prepareCommit();
   }
 
@@ -600,6 +616,7 @@ public class DirectoryTaxonomyWriter imp
    */
   @Override
   public synchronized void prepareCommit(Map<String,String> commitUserData) throws CorruptIndexException, IOException {
+    ensureOpen();
     indexWriter.prepareCommit(commitUserData);
   }
   
@@ -616,6 +633,7 @@ public class DirectoryTaxonomyWriter imp
    */
   @Override
   synchronized public int getSize() {
+    ensureOpen();
     return indexWriter.maxDoc();
   }
 
@@ -643,8 +661,10 @@ public class DirectoryTaxonomyWriter imp
    * 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 imp
   }
   @Override
   public 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 imp
    * 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 imp
     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}).
+   */
   @Override
   public void rollback() throws IOException {
+    ensureOpen();
     indexWriter.rollback();
-    refreshReader();
+    close();
   }
   
 }

Modified: lucene/dev/trunk/modules/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestDirectoryTaxonomyWriter.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/modules/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestDirectoryTaxonomyWriter.java?rev=1202593&r1=1202592&r2=1202593&view=diff
==============================================================================
--- lucene/dev/trunk/modules/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestDirectoryTaxonomyWriter.java (original)
+++ lucene/dev/trunk/modules/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestDirectoryTaxonomyWriter.java Wed Nov 16 08:56:31 2011
@@ -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
     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();
+  }
+  
 }