lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mikemcc...@apache.org
Subject svn commit: r891202 - in /lucene/java/trunk/src/java/org/apache/lucene/index: DirectoryReader.java IndexReader.java IndexWriter.java
Date Wed, 16 Dec 2009 11:47:01 GMT
Author: mikemccand
Date: Wed Dec 16 11:47:01 2009
New Revision: 891202

URL: http://svn.apache.org/viewvc?rev=891202&view=rev
Log:
LUCENE-2156: switch to AtomicInteger for IR's ref count; remove sync from IW's ensureOpen
(it's only 'best effort')

Modified:
    lucene/java/trunk/src/java/org/apache/lucene/index/DirectoryReader.java
    lucene/java/trunk/src/java/org/apache/lucene/index/IndexReader.java
    lucene/java/trunk/src/java/org/apache/lucene/index/IndexWriter.java

Modified: lucene/java/trunk/src/java/org/apache/lucene/index/DirectoryReader.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/index/DirectoryReader.java?rev=891202&r1=891201&r2=891202&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/index/DirectoryReader.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/index/DirectoryReader.java Wed Dec 16 11:47:01
2009
@@ -32,7 +32,6 @@
 import org.apache.lucene.document.Document;
 import org.apache.lucene.document.FieldSelector;
 import org.apache.lucene.search.Similarity;
-import org.apache.lucene.store.AlreadyClosedException;
 import org.apache.lucene.store.Directory;
 import org.apache.lucene.store.Lock;
 import org.apache.lucene.store.LockObtainFailedException;
@@ -379,10 +378,6 @@
         throw new IllegalArgumentException("a reader obtained from IndexWriter.getReader()
cannot currently accept a commit");
       }
 
-      if (!writer.isOpen(true)) {
-        throw new AlreadyClosedException("cannot reopen: the IndexWriter this reader was
obtained from is now closed");
-      }
-
       // TODO: right now we *always* make a new reader; in
       // the future we could have write make some effort to
       // detect that no changes have occurred

Modified: lucene/java/trunk/src/java/org/apache/lucene/index/IndexReader.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/index/IndexReader.java?rev=891202&r1=891201&r2=891202&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/index/IndexReader.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/index/IndexReader.java Wed Dec 16 11:47:01
2009
@@ -29,6 +29,7 @@
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
 
 /** IndexReader is an abstract class, providing an interface for accessing an
  index.  Search of an index is done entirely through this abstract interface,
@@ -116,13 +117,13 @@
   private boolean closed;
   protected boolean hasChanges;
   
-  private int refCount;
+  private final AtomicInteger refCount = new AtomicInteger();
 
   static int DEFAULT_TERMS_INDEX_DIVISOR = 1;
 
   /** Expert: returns the current refCount for this reader */
-  public synchronized int getRefCount() {
-    return refCount;
+  public int getRefCount() {
+    return refCount.get();
   }
   
   /**
@@ -139,41 +140,48 @@
    *
    * @see #decRef
    */
-  public synchronized void incRef() {
-    assert refCount > 0;
+  public void incRef() {
     ensureOpen();
-    refCount++;
+    refCount.incrementAndGet();
   }
 
   /**
    * Expert: decreases the refCount of this IndexReader
    * instance.  If the refCount drops to 0, then pending
    * changes (if any) are committed to the index and this
-   * reader is closed.
-   * 
+   * reader is closed.  If an exception is hit, the refCount
+   * is unchanged.
+   *
    * @throws IOException in case an IOException occurs in commit() or doClose()
    *
    * @see #incRef
    */
-  public synchronized void decRef() throws IOException {
-    assert refCount > 0;
+  public void decRef() throws IOException {
     ensureOpen();
-    if (refCount == 1) {
-      commit();
-      doClose();
+    if (refCount.getAndDecrement() == 1) {
+      boolean success = false;
+      try {
+        commit();
+        doClose();
+        success = true;
+      } finally {
+        if (!success) {
+          // Put reference back on failure
+          refCount.incrementAndGet();
+        }
+      }
     }
-    refCount--;
   }
   
   protected IndexReader() { 
-    refCount = 1;
+    refCount.set(1);
   }
   
   /**
    * @throws AlreadyClosedException if this IndexReader is closed
    */
   protected final void ensureOpen() throws AlreadyClosedException {
-    if (refCount <= 0) {
+    if (refCount.get() <= 0) {
       throw new AlreadyClosedException("this IndexReader is closed");
     }
   }

Modified: lucene/java/trunk/src/java/org/apache/lucene/index/IndexWriter.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/index/IndexWriter.java?rev=891202&r1=891201&r2=891202&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/index/IndexWriter.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/index/IndexWriter.java Wed Dec 16 11:47:01
2009
@@ -386,6 +386,9 @@
    *  loading a TermInfo.  The default value is 1.  Set this
    *  to -1 to skip loading the terms index entirely. */
   public IndexReader getReader(int termInfosIndexDivisor) throws IOException {
+
+    ensureOpen();
+
     if (infoStream != null) {
       message("flush at getReader");
     }
@@ -703,23 +706,19 @@
     notifyAll();
   }
 
-  synchronized final boolean isOpen(boolean includePendingClose) {
-    return !(closed || (includePendingClose && closing));
-  }
-
   /**
    * Used internally to throw an {@link
    * AlreadyClosedException} if this IndexWriter has been
    * closed.
    * @throws AlreadyClosedException if this IndexWriter is
    */
-  protected synchronized final void ensureOpen(boolean includePendingClose) throws AlreadyClosedException
{
-    if (!isOpen(includePendingClose)) {
+  protected final void ensureOpen(boolean includePendingClose) throws AlreadyClosedException
{
+    if (closed || (includePendingClose && closing)) {
       throw new AlreadyClosedException("this IndexWriter is closed");
     }
   }
 
-  protected synchronized final void ensureOpen() throws AlreadyClosedException {
+  protected final void ensureOpen() throws AlreadyClosedException {
     ensureOpen(true);
   }
 
@@ -2906,7 +2905,7 @@
     releaseWrite();
   }
 
-  private void blockAddIndexes(boolean includePendingClose) {
+  private void blockAddIndexes() {
 
     acquireRead();
 
@@ -2915,7 +2914,7 @@
 
       // Make sure we are still open since we could have
       // waited quite a while for last addIndexes to finish
-      ensureOpen(includePendingClose);
+      ensureOpen(false);
       success = true;
     } finally {
       if (!success)
@@ -4588,7 +4587,7 @@
         // Wait for any running addIndexes to complete
         // first, then block any from running until we've
         // copied the segmentInfos we intend to sync:
-        blockAddIndexes(false);
+        blockAddIndexes();
 
         // On commit the segmentInfos must never
         // reference a segment in another directory:



Mime
View raw message