lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sh...@apache.org
Subject svn commit: r1202592 - in /lucene/dev/branches/branch_3x/lucene/contrib: ./ facet/src/java/org/apache/lucene/facet/taxonomy/directory/ facet/src/test/org/apache/lucene/facet/taxonomy/directory/
Date Wed, 16 Nov 2011 08:55:25 GMT
Author: shaie
Date: Wed Nov 16 08:55:25 2011
New Revision: 1202592

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

Modified:
    lucene/dev/branches/branch_3x/lucene/contrib/CHANGES.txt
    lucene/dev/branches/branch_3x/lucene/contrib/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.java
    lucene/dev/branches/branch_3x/lucene/contrib/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestDirectoryTaxonomyWriter.java

Modified: lucene/dev/branches/branch_3x/lucene/contrib/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/contrib/CHANGES.txt?rev=1202592&r1=1202591&r2=1202592&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/contrib/CHANGES.txt (original)
+++ lucene/dev/branches/branch_3x/lucene/contrib/CHANGES.txt Wed Nov 16 08:55:25 2011
@@ -70,6 +70,9 @@ API Changes
    Analyzer should be configured at instantiation.  Deprecated PerFieldAnalyzerWrapper.addAnalyzer
    since it also prevents reuse.  Analyzers per field should be configured at instantiation.
    (Chris Male)
+   
+ * LUCENE-3579: DirectoryTaxonomyWriter throws AlreadyClosedException if it was
+   closed, but any of its API methods are called. (Shai Erera)
 
 Bug Fixes
 

Modified: lucene/dev/branches/branch_3x/lucene/contrib/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/contrib/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.java?rev=1202592&r1=1202591&r2=1202592&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/contrib/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.java
(original)
+++ lucene/dev/branches/branch_3x/lucene/contrib/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.java
Wed Nov 16 08:55:25 2011
@@ -29,6 +29,7 @@ import org.apache.lucene.index.LogByteSi
 import org.apache.lucene.index.Term;
 import org.apache.lucene.index.TermDocs;
 import org.apache.lucene.index.TermEnum;
+import org.apache.lucene.store.AlreadyClosedException;
 import org.apache.lucene.store.Directory;
 import org.apache.lucene.store.LockObtainFailedException;
 import org.apache.lucene.store.NativeFSLockFactory;
@@ -113,6 +114,7 @@ public class DirectoryTaxonomyWriter imp
    * objects you create for the same directory.
    */
   public void setDelimiter(char delimiter) {
+    ensureOpen();
     this.delimiter = delimiter;
   }
 
@@ -285,6 +287,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;
     }
@@ -394,8 +397,8 @@ public class DirectoryTaxonomyWriter imp
   // potentially even trigger a lengthy merge) locks out other addCategory()
   // calls - even those which could immediately return a cached value.
   // We definitely need to fix this situation!
-  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);
@@ -444,6 +447,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
@@ -560,6 +573,7 @@ public class DirectoryTaxonomyWriter imp
    * See {@link TaxonomyWriter#commit()}
    */ 
   public synchronized void commit() throws CorruptIndexException, IOException {
+    ensureOpen();
     indexWriter.commit();
     refreshReader();
   }
@@ -570,6 +584,7 @@ public class DirectoryTaxonomyWriter imp
    * See {@link TaxonomyWriter#commit(Map)}. 
    */
   public synchronized void commit(Map<String,String> commitUserData) throws CorruptIndexException,
IOException {
+    ensureOpen();
     indexWriter.commit(commitUserData);
     refreshReader();
   }
@@ -579,6 +594,7 @@ public class DirectoryTaxonomyWriter imp
    * See {@link IndexWriter#prepareCommit}.
    */
   public synchronized void prepareCommit() throws CorruptIndexException, IOException {
+    ensureOpen();
     indexWriter.prepareCommit();
   }
 
@@ -587,6 +603,7 @@ public class DirectoryTaxonomyWriter imp
    * See {@link IndexWriter#prepareCommit(Map)}
    */
   public synchronized void prepareCommit(Map<String,String> commitUserData) throws
CorruptIndexException, IOException {
+    ensureOpen();
     indexWriter.prepareCommit(commitUserData);
   }
   
@@ -602,6 +619,7 @@ public class DirectoryTaxonomyWriter imp
    * automatically (including the root, which always get ordinal 0).
    */
   synchronized public int getSize() {
+    ensureOpen();
     return indexWriter.maxDoc();
   }
 
@@ -629,8 +647,10 @@ public class DirectoryTaxonomyWriter imp
    * method. 
    */
   public void setCacheMissesUntilFill(int i) {
+    ensureOpen();
     cacheMissesUntilFill = i;
   }
+  
   private int cacheMissesUntilFill = 11;
 
   private boolean perhapsFillCache() throws IOException {
@@ -702,6 +722,7 @@ public class DirectoryTaxonomyWriter imp
     return parentArray;
   }
   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.
@@ -729,6 +750,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:
@@ -986,9 +1008,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}).
+   */
   public void rollback() throws IOException {
+    ensureOpen();
     indexWriter.rollback();
-    refreshReader();
+    // since IndexWriter.rollback() closes the IW instance, we should close too.
+    close();
   }
   
 }

Modified: lucene/dev/branches/branch_3x/lucene/contrib/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestDirectoryTaxonomyWriter.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/contrib/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestDirectoryTaxonomyWriter.java?rev=1202592&r1=1202591&r2=1202592&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/contrib/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestDirectoryTaxonomyWriter.java
(original)
+++ lucene/dev/branches/branch_3x/lucene/contrib/facet/src/test/org/apache/lucene/facet/taxonomy/directory/TestDirectoryTaxonomyWriter.java
Wed Nov 16 08:55:25 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();
+  }
+  
 }



Mime
View raw message