You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by jp...@apache.org on 2015/02/17 16:33:54 UTC

svn commit: r1660416 - in /lucene/dev/branches/branch_5x: ./ lucene/ lucene/test-framework/ lucene/test-framework/src/java/org/apache/lucene/search/RandomApproximationQuery.java

Author: jpountz
Date: Tue Feb 17 15:33:54 2015
New Revision: 1660416

URL: http://svn.apache.org/r1660416
Log:
Fix RandomApproximationQuery to not share Random instances across threads.

Modified:
    lucene/dev/branches/branch_5x/   (props changed)
    lucene/dev/branches/branch_5x/lucene/   (props changed)
    lucene/dev/branches/branch_5x/lucene/test-framework/   (props changed)
    lucene/dev/branches/branch_5x/lucene/test-framework/src/java/org/apache/lucene/search/RandomApproximationQuery.java

Modified: lucene/dev/branches/branch_5x/lucene/test-framework/src/java/org/apache/lucene/search/RandomApproximationQuery.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/test-framework/src/java/org/apache/lucene/search/RandomApproximationQuery.java?rev=1660416&r1=1660415&r2=1660416&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/test-framework/src/java/org/apache/lucene/search/RandomApproximationQuery.java (original)
+++ lucene/dev/branches/branch_5x/lucene/test-framework/src/java/org/apache/lucene/search/RandomApproximationQuery.java Tue Feb 17 15:33:54 2015
@@ -76,91 +76,112 @@ public class RandomApproximationQuery ex
   @Override
   public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {
     final Weight weight = query.createWeight(searcher, needsScores);
-    return new Weight(RandomApproximationQuery.this) {
+    return new RandomApproximationWeight(weight, new Random(random.nextLong()));
+  }
 
-      @Override
-      public Explanation explain(LeafReaderContext context, int doc) throws IOException {
-        return weight.explain(context, doc);
-      }
+  private static class RandomApproximationWeight extends Weight {
 
-      @Override
-      public float getValueForNormalization() throws IOException {
-        return weight.getValueForNormalization();
-      }
+    private final Weight weight;
+    private final Random random;
 
-      @Override
-      public void normalize(float norm, float topLevelBoost) {
-        weight.normalize(norm, topLevelBoost);
-      }
+    RandomApproximationWeight(Weight weight, Random random) {
+      super(weight.getQuery());
+      this.weight = weight;
+      this.random = random;
+    }
+
+    @Override
+    public Explanation explain(LeafReaderContext context, int doc) throws IOException {
+      return weight.explain(context, doc);
+    }
+
+    @Override
+    public float getValueForNormalization() throws IOException {
+      return weight.getValueForNormalization();
+    }
+
+    @Override
+    public void normalize(float norm, float topLevelBoost) {
+      weight.normalize(norm, topLevelBoost);
+    }
 
-      @Override
-      public Scorer scorer(LeafReaderContext context, Bits acceptDocs) throws IOException {
-        final Scorer scorer = weight.scorer(context, acceptDocs);
-        if (scorer == null) {
-          return null;
-        }
-        final RandomTwoPhaseView twoPhaseView = new RandomTwoPhaseView(random, scorer);
-        return new Scorer(this) {
-
-          @Override
-          public TwoPhaseDocIdSetIterator asTwoPhaseIterator() {
-            return twoPhaseView;
-          }
-
-          @Override
-          public float score() throws IOException {
-            return scorer.score();
-          }
-
-          @Override
-          public int freq() throws IOException {
-            return scorer.freq();
-          }
-
-          @Override
-          public int nextPosition() throws IOException {
-            return scorer.nextPosition();
-          }
-
-          @Override
-          public int startOffset() throws IOException {
-            return scorer.startOffset();
-          }
-
-          @Override
-          public int endOffset() throws IOException {
-            return scorer.endOffset();
-          }
-
-          @Override
-          public BytesRef getPayload() throws IOException {
-            return scorer.getPayload();
-          }
-
-          @Override
-          public int docID() {
-            return scorer.docID();
-          }
-
-          @Override
-          public int nextDoc() throws IOException {
-            return scorer.nextDoc();
-          }
-
-          @Override
-          public int advance(int target) throws IOException {
-            return scorer.advance(target);
-          }
-
-          @Override
-          public long cost() {
-            return scorer.cost();
-          }
-          
-        };
+    @Override
+    public Scorer scorer(LeafReaderContext context, Bits acceptDocs) throws IOException {
+      final Scorer scorer = weight.scorer(context, acceptDocs);
+      if (scorer == null) {
+        return null;
       }
+      return new RandomApproximationScorer(scorer, new Random(random.nextLong()));
+    }
+
+  }
+
+  private static class RandomApproximationScorer extends Scorer {
+
+    private final Scorer scorer;
+    private final RandomTwoPhaseView twoPhaseView;
+
+    RandomApproximationScorer(Scorer scorer, Random random) {
+      super(scorer.getWeight());
+      this.scorer = scorer;
+      this.twoPhaseView = new RandomTwoPhaseView(random, scorer);
+    }
+
+    @Override
+    public TwoPhaseDocIdSetIterator asTwoPhaseIterator() {
+      return twoPhaseView;
+    }
+
+    @Override
+    public float score() throws IOException {
+      return scorer.score();
+    }
+
+    @Override
+    public int freq() throws IOException {
+      return scorer.freq();
+    }
+
+    @Override
+    public int nextPosition() throws IOException {
+      return scorer.nextPosition();
+    }
+
+    @Override
+    public int startOffset() throws IOException {
+      return scorer.startOffset();
+    }
+
+    @Override
+    public int endOffset() throws IOException {
+      return scorer.endOffset();
+    }
+
+    @Override
+    public BytesRef getPayload() throws IOException {
+      return scorer.getPayload();
+    }
+
+    @Override
+    public int docID() {
+      return scorer.docID();
+    }
+
+    @Override
+    public int nextDoc() throws IOException {
+      return scorer.nextDoc();
+    }
+
+    @Override
+    public int advance(int target) throws IOException {
+      return scorer.advance(target);
+    }
+
+    @Override
+    public long cost() {
+      return scorer.cost();
+    }
 
-    };
   }
 
   private static class RandomTwoPhaseView extends TwoPhaseDocIdSetIterator {
@@ -182,7 +203,7 @@ public class RandomApproximationQuery ex
     public boolean matches() throws IOException {
       return approximation.doc == disi.docID();
     }
-    
+
   }
 
   private static class RandomApproximation extends DocIdSetIterator {
@@ -191,7 +212,7 @@ public class RandomApproximationQuery ex
     private final DocIdSetIterator disi;
 
     int doc = -1;
-    
+
     public RandomApproximation(Random random, DocIdSetIterator disi) {
       this.random = random;
       this.disi = disi;
@@ -201,7 +222,7 @@ public class RandomApproximationQuery ex
     public int docID() {
       return doc;
     }
-    
+
     @Override
     public int nextDoc() throws IOException {
       return advance(doc + 1);