lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mikemcc...@apache.org
Subject svn commit: r888621 - in /lucene/java/trunk: CHANGES.txt common-build.xml src/java/org/apache/lucene/index/SegmentReader.java src/test/org/apache/lucene/index/TestIndexReaderClone.java src/test/org/apache/lucene/index/TestIndexReaderCloneNorms.java
Date Tue, 08 Dec 2009 23:05:29 GMT
Author: mikemccand
Date: Tue Dec  8 23:05:29 2009
New Revision: 888621

URL: http://svn.apache.org/viewvc?rev=888621&view=rev
Log:
LUCENE-2137: switch to AtomicInteger for some ref counts

Modified:
    lucene/java/trunk/CHANGES.txt
    lucene/java/trunk/common-build.xml
    lucene/java/trunk/src/java/org/apache/lucene/index/SegmentReader.java
    lucene/java/trunk/src/test/org/apache/lucene/index/TestIndexReaderClone.java
    lucene/java/trunk/src/test/org/apache/lucene/index/TestIndexReaderCloneNorms.java

Modified: lucene/java/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/java/trunk/CHANGES.txt?rev=888621&r1=888620&r2=888621&view=diff
==============================================================================
--- lucene/java/trunk/CHANGES.txt (original)
+++ lucene/java/trunk/CHANGES.txt Tue Dec  8 23:05:29 2009
@@ -78,6 +78,9 @@
   This avoid the overhead of using a PQ unecessarily.  (Mike
   McCandless)
 
+* LUCENE-2137: Switch to AtomicInteger for some ref counting (Earwin
+  Burrfoot via Mike McCandless)
+
 Build
 
 Test Cases

Modified: lucene/java/trunk/common-build.xml
URL: http://svn.apache.org/viewvc/lucene/java/trunk/common-build.xml?rev=888621&r1=888620&r2=888621&view=diff
==============================================================================
--- lucene/java/trunk/common-build.xml (original)
+++ lucene/java/trunk/common-build.xml Tue Dec  8 23:05:29 2009
@@ -42,7 +42,7 @@
   <property name="Name" value="Lucene"/>
   <property name="dev.version" value="3.1-dev"/>
   <property name="version" value="${dev.version}"/>
-  <property name="compatibility.tag" value="lucene_3_0_back_compat_tests_20091207"/>
+  <property name="compatibility.tag" value="lucene_3_0_back_compat_tests_20091208"/>
   <property name="spec.version" value="${version}"/>	
   <property name="year" value="2000-${current.year}"/>
   <property name="final.name" value="lucene-${name}-${version}"/>

Modified: lucene/java/trunk/src/java/org/apache/lucene/index/SegmentReader.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/index/SegmentReader.java?rev=888621&r1=888620&r2=888621&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/index/SegmentReader.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/index/SegmentReader.java Tue Dec  8 23:05:29
2009
@@ -28,6 +28,7 @@
 import java.util.Map;
 import java.util.Set;
 
+import java.util.concurrent.atomic.AtomicInteger;
 import org.apache.lucene.document.Document;
 import org.apache.lucene.document.FieldSelector;
 import org.apache.lucene.search.Similarity;
@@ -53,7 +54,7 @@
   CloseableThreadLocal<TermVectorsReader> termVectorsLocal = new CloseableThreadLocal<TermVectorsReader>();
 
   BitVector deletedDocs = null;
-  Ref deletedDocsRef = null;
+  AtomicInteger deletedDocsRef = null;
   private boolean deletedDocsDirty = false;
   private boolean normsDirty = false;
   private int pendingDeleteCount;
@@ -65,7 +66,7 @@
 
   // optionally used for the .nrm file shared by multiple norms
   private IndexInput singleNormStream;
-  private Ref singleNormRef;
+  private AtomicInteger singleNormRef;
 
   CoreReaders core;
 
@@ -79,7 +80,7 @@
     // closed.  A given instance of SegmentReader may be
     // closed, even those it shares core objects with other
     // SegmentReaders:
-    private final Ref ref = new Ref();
+    private final AtomicInteger ref = new AtomicInteger(1);
 
     final String segment;
     final FieldInfos fieldInfos;
@@ -150,7 +151,7 @@
     }
 
     synchronized void incRef() {
-      ref.incRef();
+      ref.incrementAndGet();
     }
 
     synchronized Directory getCFSReader() {
@@ -196,7 +197,7 @@
 
     synchronized void decRef() throws IOException {
 
-      if (ref.decRef() == 0) {
+      if (ref.decrementAndGet() == 0) {
 
         // close everything, nothing is shared anymore with other readers
         if (tis != null) {
@@ -299,31 +300,6 @@
     }
   }
   
-  static class Ref {
-    private int refCount = 1;
-    
-    @Override
-    public String toString() {
-      return "refcount: "+refCount;
-    }
-    
-    public synchronized int refCount() {
-      return refCount;
-    }
-    
-    public synchronized int incRef() {
-      assert refCount > 0;
-      refCount++;
-      return refCount;
-    }
-
-    public synchronized int decRef() {
-      assert refCount > 0;
-      refCount--;
-      return refCount;
-    }
-  }
-  
   /**
    * Byte[] referencing is used because a new norm object needs 
    * to be created for each clone, and the byte array is all 
@@ -344,7 +320,7 @@
     private long normSeek;
 
     // null until bytes is set
-    private Ref bytesRef;
+    private AtomicInteger bytesRef;
     private byte[] bytes;
     private boolean dirty;
     private int number;
@@ -369,7 +345,7 @@
         } else {
           // We are sharing this with others -- decRef and
           // maybe close the shared norm stream
-          if (singleNormRef.decRef() == 0) {
+          if (singleNormRef.decrementAndGet() == 0) {
             singleNormStream.close();
             singleNormStream = null;
           }
@@ -392,7 +368,7 @@
 
         if (bytes != null) {
           assert bytesRef != null;
-          bytesRef.decRef();
+          bytesRef.decrementAndGet();
           bytes = null;
           bytesRef = null;
         } else {
@@ -435,7 +411,7 @@
           // byte[]
           bytes = origNorm.bytes();
           bytesRef = origNorm.bytesRef;
-          bytesRef.incRef();
+          bytesRef.incrementAndGet();
 
           // Once we've loaded the bytes we no longer need
           // origNorm:
@@ -457,7 +433,7 @@
             in.readBytes(bytes, 0, count, false);
           }
 
-          bytesRef = new Ref();
+          bytesRef = new AtomicInteger(1);
           closeInput();
         }
       }
@@ -466,7 +442,7 @@
     }
 
     // Only for testing
-    Ref bytesRef() {
+    AtomicInteger bytesRef() {
       return bytesRef;
     }
 
@@ -477,15 +453,15 @@
       bytes();
       assert bytes != null;
       assert bytesRef != null;
-      if (bytesRef.refCount() > 1) {
+      if (bytesRef.get() > 1) {
         // I cannot be the origNorm for another norm
         // instance if I'm being changed.  Ie, only the
         // "head Norm" can be changed:
         assert refCount == 1;
-        final Ref oldRef = bytesRef;
+        final AtomicInteger oldRef = bytesRef;
         bytes = cloneNormBytes(bytes);
-        bytesRef = new Ref();
-        oldRef.decRef();
+        bytesRef = new AtomicInteger(1);
+        oldRef.decrementAndGet();
       }
       dirty = true;
       return bytes;
@@ -511,7 +487,7 @@
         assert origNorm == null;
 
         // Clone holds a reference to my bytes:
-        clone.bytesRef.incRef();
+        clone.bytesRef.incrementAndGet();
       } else {
         assert bytesRef == null;
         if (origNorm == null) {
@@ -602,7 +578,7 @@
     // NOTE: the bitvector is stored using the regular directory, not cfs
     if (hasDeletions(si)) {
       deletedDocs = new BitVector(directory(), si.getDelFileName());
-      deletedDocsRef = new Ref();
+      deletedDocsRef = new AtomicInteger(1);
      
       assert si.getDelCount() == deletedDocs.count() : 
         "delete count mismatch: info=" + si.getDelCount() + " vs BitVector=" + deletedDocs.count();
@@ -696,7 +672,7 @@
       
       if (doClone) {
         if (deletedDocs != null) {
-          deletedDocsRef.incRef();
+          deletedDocsRef.incrementAndGet();
           clone.deletedDocs = deletedDocs;
           clone.deletedDocsRef = deletedDocsRef;
         }
@@ -706,7 +682,7 @@
           assert clone.deletedDocs == null;
           clone.loadDeletedDocs();
         } else if (deletedDocs != null) {
-          deletedDocsRef.incRef();
+          deletedDocsRef.incrementAndGet();
           clone.deletedDocs = deletedDocs;
           clone.deletedDocsRef = deletedDocsRef;
         }
@@ -784,7 +760,7 @@
     fieldsReaderLocal.close();
     
     if (deletedDocs != null) {
-      deletedDocsRef.decRef();
+      deletedDocsRef.decrementAndGet();
       // null so if an app hangs on to us we still free most ram
       deletedDocs = null;
     }
@@ -820,16 +796,16 @@
   protected void doDelete(int docNum) {
     if (deletedDocs == null) {
       deletedDocs = new BitVector(maxDoc());
-      deletedDocsRef = new Ref();
+      deletedDocsRef = new AtomicInteger(1);
     }
     // there is more than 1 SegmentReader with a reference to this
     // deletedDocs BitVector so decRef the current deletedDocsRef,
     // clone the BitVector, create a new deletedDocsRef
-    if (deletedDocsRef.refCount() > 1) {
-      Ref oldRef = deletedDocsRef;
+    if (deletedDocsRef.get() > 1) {
+      AtomicInteger oldRef = deletedDocsRef;
       deletedDocs = cloneDeletedDocs(deletedDocs);
-      deletedDocsRef = new Ref();
-      oldRef.decRef();
+      deletedDocsRef = new AtomicInteger(1);
+      oldRef.decrementAndGet();
     }
     deletedDocsDirty = true;
     if (!deletedDocs.getAndSet(docNum))
@@ -841,7 +817,7 @@
     deletedDocsDirty = false;
     if (deletedDocs != null) {
       assert deletedDocsRef != null;
-      deletedDocsRef.decRef();
+      deletedDocsRef.decrementAndGet();
       deletedDocs = null;
       deletedDocsRef = null;
       pendingDeleteCount = 0;
@@ -1056,9 +1032,9 @@
           normSeek = nextNormSeek;
           if (singleNormStream == null) {
             singleNormStream = d.openInput(fileName, readBufferSize);
-            singleNormRef = new Ref();
+            singleNormRef = new AtomicInteger(1);
           } else {
-            singleNormRef.incRef();
+            singleNormRef.incrementAndGet();
           }
           // All norms in the .nrm file can share a single IndexInput since
           // they are only used in a synchronized context.

Modified: lucene/java/trunk/src/test/org/apache/lucene/index/TestIndexReaderClone.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/index/TestIndexReaderClone.java?rev=888621&r1=888620&r2=888621&view=diff
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/index/TestIndexReaderClone.java (original)
+++ lucene/java/trunk/src/test/org/apache/lucene/index/TestIndexReaderClone.java Tue Dec 
8 23:05:29 2009
@@ -331,7 +331,7 @@
     assertDelDocsRefCountEquals(1, origSegmentReader);
     // check the norm refs
     Norm norm = clonedSegmentReader.norms.get("field1");
-    assertEquals(1, norm.bytesRef().refCount());
+    assertEquals(1, norm.bytesRef().get());
     clonedSegmentReader.close();
     dir1.close();
   }
@@ -442,7 +442,7 @@
   }
 
   private void assertDelDocsRefCountEquals(int refCount, SegmentReader reader) {
-    assertEquals(refCount, reader.deletedDocsRef.refCount());
+    assertEquals(refCount, reader.deletedDocsRef.get());
   }
   
   public void testCloneSubreaders() throws Exception {

Modified: lucene/java/trunk/src/test/org/apache/lucene/index/TestIndexReaderCloneNorms.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/index/TestIndexReaderCloneNorms.java?rev=888621&r1=888620&r2=888621&view=diff
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/index/TestIndexReaderCloneNorms.java (original)
+++ lucene/java/trunk/src/test/org/apache/lucene/index/TestIndexReaderCloneNorms.java Tue
Dec  8 23:05:29 2009
@@ -21,6 +21,7 @@
 import java.io.IOException;
 import java.util.ArrayList;
 
+import java.util.concurrent.atomic.AtomicInteger;
 import org.apache.lucene.analysis.Analyzer;
 import org.apache.lucene.analysis.standard.StandardAnalyzer;
 import org.apache.lucene.document.Document;
@@ -173,11 +174,11 @@
     SegmentReader reader1 = SegmentReader.getOnlySegmentReader(dir1);
     reader1.norms("field1");
     Norm r1norm = reader1.norms.get("field1");
-    SegmentReader.Ref r1BytesRef = r1norm.bytesRef();
+    AtomicInteger r1BytesRef = r1norm.bytesRef();
     SegmentReader reader2 = (SegmentReader)reader1.clone();
-    assertEquals(2, r1norm.bytesRef().refCount());
+    assertEquals(2, r1norm.bytesRef().get());
     reader1.close();
-    assertEquals(1, r1BytesRef.refCount());
+    assertEquals(1, r1BytesRef.get());
     reader2.norms("field1");
     reader2.close();
     dir1.close();
@@ -192,19 +193,19 @@
     SegmentReader segmentReader2C = SegmentReader.getOnlySegmentReader(reader2C);
     segmentReader2C.norms("field1"); // load the norms for the field
     Norm reader2CNorm = segmentReader2C.norms.get("field1");
-    assertTrue("reader2CNorm.bytesRef()=" + reader2CNorm.bytesRef(), reader2CNorm.bytesRef().refCount()
== 2);
+    assertTrue("reader2CNorm.bytesRef()=" + reader2CNorm.bytesRef(), reader2CNorm.bytesRef().get()
== 2);
     
     
     
     IndexReader reader3C = (IndexReader) reader2C.clone();
     SegmentReader segmentReader3C = SegmentReader.getOnlySegmentReader(reader3C);
     Norm reader3CCNorm = segmentReader3C.norms.get("field1");
-    assertEquals(3, reader3CCNorm.bytesRef().refCount());
+    assertEquals(3, reader3CCNorm.bytesRef().get());
     
     // edit a norm and the refcount should be 1
     IndexReader reader4C = (IndexReader) reader3C.clone();
     SegmentReader segmentReader4C = SegmentReader.getOnlySegmentReader(reader4C);
-    assertEquals(4, reader3CCNorm.bytesRef().refCount());
+    assertEquals(4, reader3CCNorm.bytesRef().get());
     reader4C.setNorm(5, "field1", 0.33f);
     
     // generate a cannot update exception in reader1
@@ -219,14 +220,14 @@
     assertTrue(Similarity.getDefault().decodeNormValue(segmentReader3C.norms("field1")[5])

     		!= Similarity.getDefault().decodeNormValue(segmentReader4C.norms("field1")[5]));
     Norm reader4CCNorm = segmentReader4C.norms.get("field1");
-    assertEquals(3, reader3CCNorm.bytesRef().refCount());
-    assertEquals(1, reader4CCNorm.bytesRef().refCount());
+    assertEquals(3, reader3CCNorm.bytesRef().get());
+    assertEquals(1, reader4CCNorm.bytesRef().get());
         
     IndexReader reader5C = (IndexReader) reader4C.clone();
     SegmentReader segmentReader5C = SegmentReader.getOnlySegmentReader(reader5C);
     Norm reader5CCNorm = segmentReader5C.norms.get("field1");
     reader5C.setNorm(5, "field1", 0.7f);
-    assertEquals(1, reader5CCNorm.bytesRef().refCount());    
+    assertEquals(1, reader5CCNorm.bytesRef().get());
 
     reader5C.close();
     reader4C.close();



Mime
View raw message