lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From yo...@apache.org
Subject svn commit: r775201 - in /lucene/java/trunk/src: java/org/apache/lucene/index/MultiReader.java java/org/apache/lucene/index/MultiSegmentReader.java test/org/apache/lucene/index/TestMultiSegmentReader.java
Date Fri, 15 May 2009 15:50:13 GMT
Author: yonik
Date: Fri May 15 15:50:12 2009
New Revision: 775201

URL: http://svn.apache.org/viewvc?rev=775201&view=rev
Log:
LUCENE-1596: check that enum and termdocs came from same reader before invoking optimization

Modified:
    lucene/java/trunk/src/java/org/apache/lucene/index/MultiReader.java
    lucene/java/trunk/src/java/org/apache/lucene/index/MultiSegmentReader.java
    lucene/java/trunk/src/test/org/apache/lucene/index/TestMultiSegmentReader.java

Modified: lucene/java/trunk/src/java/org/apache/lucene/index/MultiReader.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/index/MultiReader.java?rev=775201&r1=775200&r2=775201&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/index/MultiReader.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/index/MultiReader.java Fri May 15 15:50:12
2009
@@ -329,12 +329,12 @@
 
   public TermEnum terms() throws IOException {
     ensureOpen();
-    return new MultiTermEnum(subReaders, starts, null);
+    return new MultiTermEnum(this, subReaders, starts, null);
   }
 
   public TermEnum terms(Term term) throws IOException {
     ensureOpen();
-    return new MultiTermEnum(subReaders, starts, term);
+    return new MultiTermEnum(this, subReaders, starts, term);
   }
 
   public int docFreq(Term t) throws IOException {
@@ -347,12 +347,12 @@
 
   public TermDocs termDocs() throws IOException {
     ensureOpen();
-    return new MultiTermDocs(subReaders, starts);
+    return new MultiTermDocs(this, subReaders, starts);
   }
 
   public TermPositions termPositions() throws IOException {
     ensureOpen();
-    return new MultiTermPositions(subReaders, starts);
+    return new MultiTermPositions(this, subReaders, starts);
   }
 
   /** @deprecated */

Modified: lucene/java/trunk/src/java/org/apache/lucene/index/MultiSegmentReader.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/index/MultiSegmentReader.java?rev=775201&r1=775200&r2=775201&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/index/MultiSegmentReader.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/index/MultiSegmentReader.java Fri May 15
15:50:12 2009
@@ -437,12 +437,12 @@
 
   public TermEnum terms() throws IOException {
     ensureOpen();
-    return new MultiTermEnum(subReaders, starts, null);
+    return new MultiTermEnum(this, subReaders, starts, null);
   }
 
   public TermEnum terms(Term term) throws IOException {
     ensureOpen();
-    return new MultiTermEnum(subReaders, starts, term);
+    return new MultiTermEnum(this, subReaders, starts, term);
   }
 
   public int docFreq(Term t) throws IOException {
@@ -455,12 +455,12 @@
 
   public TermDocs termDocs() throws IOException {
     ensureOpen();
-    return new MultiTermDocs(subReaders, starts);
+    return new MultiTermDocs(this, subReaders, starts);
   }
 
   public TermPositions termPositions() throws IOException {
     ensureOpen();
-    return new MultiTermPositions(subReaders, starts);
+    return new MultiTermPositions(this, subReaders, starts);
   }
 
   protected void commitChanges() throws IOException {
@@ -529,14 +529,16 @@
   }
 
   static class MultiTermEnum extends TermEnum {
+    IndexReader topReader; // used for matching TermEnum to TermDocs
     private SegmentMergeQueue queue;
   
     private Term term;
     private int docFreq;
     final SegmentMergeInfo[] matchingSegments; // null terminated array of matching segments
 
-    public MultiTermEnum(IndexReader[] readers, int[] starts, Term t)
+    public MultiTermEnum(IndexReader topReader, IndexReader[] readers, int[] starts, Term
t)
       throws IOException {
+      this.topReader = topReader;
       queue = new SegmentMergeQueue(readers.length);
       matchingSegments = new SegmentMergeInfo[readers.length+1];
       for (int i = 0; i < readers.length; i++) {
@@ -609,6 +611,7 @@
   }
 
   static class MultiTermDocs implements TermDocs {
+    IndexReader topReader;  // used for matching TermEnum to TermDocs
     protected IndexReader[] readers;
     protected int[] starts;
     protected Term term;
@@ -623,7 +626,8 @@
     int matchingSegmentPos;  // position into the matching segments from tenum
     SegmentMergeInfo smi;     // current segment mere info... can be null
 
-    public MultiTermDocs(IndexReader[] r, int[] s) {
+    public MultiTermDocs(IndexReader topReader, IndexReader[] r, int[] s) {
+      this.topReader = topReader;
       readers = r;
       starts = s;
   
@@ -650,7 +654,9 @@
     public void seek(TermEnum termEnum) throws IOException {
       seek(termEnum.term());
       if (termEnum instanceof MultiTermEnum) {
-        this.tenum = (MultiTermEnum)termEnum;
+        tenum = (MultiTermEnum)termEnum;
+        if (topReader != tenum.topReader)
+          tenum = null;
       }
     }
   
@@ -756,8 +762,8 @@
   }
 
   static class MultiTermPositions extends MultiTermDocs implements TermPositions {
-    public MultiTermPositions(IndexReader[] r, int[] s) {
-      super(r,s);
+    public MultiTermPositions(IndexReader topReader, IndexReader[] r, int[] s) {
+      super(topReader,r,s);
     }
   
     protected TermDocs termDocs(IndexReader reader) throws IOException {

Modified: lucene/java/trunk/src/test/org/apache/lucene/index/TestMultiSegmentReader.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/index/TestMultiSegmentReader.java?rev=775201&r1=775200&r2=775201&view=diff
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/index/TestMultiSegmentReader.java (original)
+++ lucene/java/trunk/src/test/org/apache/lucene/index/TestMultiSegmentReader.java Fri May
15 15:50:12 2009
@@ -149,6 +149,36 @@
     mr.close();
   }
 
+  public void testMultiTermDocs() throws IOException {
+    RAMDirectory ramDir1=new RAMDirectory();
+    addDoc(ramDir1, "test foo", true);
+    RAMDirectory ramDir2=new RAMDirectory();
+    addDoc(ramDir2, "test blah", true);
+    RAMDirectory ramDir3=new RAMDirectory();
+    addDoc(ramDir3, "test wow", true);
+
+    IndexReader[] readers1 = new IndexReader[]{IndexReader.open(ramDir1), IndexReader.open(ramDir3)};
+    IndexReader[] readers2 = new IndexReader[]{IndexReader.open(ramDir1), IndexReader.open(ramDir2),
IndexReader.open(ramDir3)};
+    MultiReader mr2 = new MultiReader(readers1);
+    MultiReader mr3 = new MultiReader(readers2);
+
+    // test mixing up TermDocs and TermEnums from different readers.
+    TermDocs td2 = mr2.termDocs();
+    TermEnum te3 = mr3.terms(new Term("body","wow"));
+    td2.seek(te3);
+    int ret = 0;
+
+    // This should blow up if we forget to check that the TermEnum is from the same
+    // reader as the TermDocs.
+    while (td2.next()) ret += td2.doc();
+    td2.close();
+    te3.close();
+
+    // really a dummy assert to ensure that we got some docs and to ensure that
+    // nothing is optimized out.
+    assertTrue(ret > 0);
+  }
+
   public void testAllTermDocs() throws IOException {
     IndexReader reader = openReader();
     int NUM_DOCS = 2;



Mime
View raw message