jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mreut...@apache.org
Subject svn commit: r518979 - in /jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene: CachingMultiReader.java MatchAllWeight.java MultiIndexReader.java MultiScorer.java SearchIndex.java
Date Fri, 16 Mar 2007 14:08:04 GMT
Author: mreutegg
Date: Fri Mar 16 07:08:02 2007
New Revision: 518979

URL: http://svn.apache.org/viewvc?view=rev&rev=518979
Log:
JCR-791: Improve performance of MatchAllScorer

Added:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/MultiIndexReader.java
  (with props)
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/MultiScorer.java
  (with props)
Modified:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/CachingMultiReader.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/MatchAllWeight.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/SearchIndex.java

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/CachingMultiReader.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/CachingMultiReader.java?view=diff&rev=518979&r1=518978&r2=518979
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/CachingMultiReader.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/CachingMultiReader.java
Fri Mar 16 07:08:02 2007
@@ -19,6 +19,7 @@
 import org.apache.lucene.index.MultiReader;
 import org.apache.lucene.index.Term;
 import org.apache.lucene.index.TermDocs;
+import org.apache.lucene.index.IndexReader;
 
 import java.io.IOException;
 import java.util.Map;
@@ -28,7 +29,9 @@
  * Extends a <code>MultiReader</code> with support for cached <code>TermDocs</code>
  * on {@link FieldNames#UUID} field.
  */
-public final class CachingMultiReader extends MultiReader implements HierarchyResolver {
+public final class CachingMultiReader
+        extends MultiReader
+        implements HierarchyResolver, MultiIndexReader {
 
     /**
      * The sub readers.
@@ -160,6 +163,17 @@
         if (--refCount == 0) {
             super.doClose();
         }
+    }
+
+    //-------------------------< MultiIndexReader >-----------------------------
+
+    /**
+     * {@inheritDoc}
+     */ 
+    public IndexReader[] getIndexReaders() {
+        IndexReader readers[] = new IndexReader[subReaders.length];
+        System.arraycopy(subReaders, 0, readers, 0, subReaders.length);
+        return readers;
     }
 
     //------------------------< internal >--------------------------------------

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/MatchAllWeight.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/MatchAllWeight.java?view=diff&rev=518979&r1=518978&r2=518979
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/MatchAllWeight.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/MatchAllWeight.java
Fri Mar 16 07:08:02 2007
@@ -103,7 +103,25 @@
      * {@inheritDoc}
      */
     public Scorer scorer(IndexReader reader) throws IOException {
-        return new MatchAllScorer(reader, field);
+        if (reader instanceof MultiIndexReader) {
+            MultiIndexReader mir = (MultiIndexReader) reader;
+            IndexReader readers[] = mir.getIndexReaders();
+            int starts[] = new int[readers.length + 1];
+            int maxDoc = 0;
+            for (int i = 0; i < readers.length; i++) {
+                starts[i] = maxDoc;
+                maxDoc += readers[i].maxDoc();
+            }
+
+            starts[readers.length] = maxDoc;
+            Scorer scorers[] = new Scorer[readers.length];
+            for (int i = 0; i < readers.length; i++)
+                scorers[i] = scorer(readers[i]);
+
+            return new MultiScorer(searcher.getSimilarity(), scorers, starts);
+        } else {
+            return new MatchAllScorer(reader, field);
+        }
     }
 
     /**

Added: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/MultiIndexReader.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/MultiIndexReader.java?view=auto&rev=518979
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/MultiIndexReader.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/MultiIndexReader.java
Fri Mar 16 07:08:02 2007
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.core.query.lucene;
+
+import org.apache.lucene.index.IndexReader;
+
+/**
+ * <code>MultiIndexReader</code> exposes methods to get access to the contained
+ * {@link IndexReader}s of this <code>MultiIndexReader</code>.
+ */
+public interface MultiIndexReader {
+
+    /**
+     * @return the <code>IndexReader</code>s that are contained in this
+     *         <code>MultiIndexReader</code>.
+     */
+    public IndexReader[] getIndexReaders();
+}

Propchange: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/MultiIndexReader.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/MultiScorer.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/MultiScorer.java?view=auto&rev=518979
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/MultiScorer.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/MultiScorer.java
Fri Mar 16 07:08:02 2007
@@ -0,0 +1,152 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.core.query.lucene;
+
+import org.apache.lucene.search.Scorer;
+import org.apache.lucene.search.Explanation;
+import org.apache.lucene.search.Similarity;
+
+import java.io.IOException;
+
+/**
+ * <code>MultiScorer</code> spans multiple Scorers and returns document numbers
+ * and score values in the order as supplied to the constructor of this
+ * <code>MultiScorer</code>.
+ */
+class MultiScorer extends Scorer {
+
+    /**
+     * The sub scorers.
+     */
+    private final Scorer[] scorers;
+
+    /**
+     * The document start numbers of the sub scorers.
+     */
+    private final int[] starts;
+
+    /**
+     * Index of the current scorer.
+     */
+    private int current = 0;
+
+    /**
+     * Indicates if there are more documents.
+     */
+    private boolean hasNext = true;
+
+    /**
+     * Creates a new <code>MultiScorer</code> that spans multiple
+     * <code>scorers</code>.
+     *
+     * @param similarity the similarity implementation that should be use.
+     * @param scorers the sub scorers.
+     * @param starts the document number start for each sub scorer.
+     */
+    MultiScorer(Similarity similarity, Scorer[] scorers, int[] starts) {
+        super(similarity);
+        this.scorers = scorers;
+        this.starts = starts;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean next() throws IOException {
+        while (hasNext) {
+            if (scorers[current].next()) {
+                return true;
+            } else if (++current < scorers.length) {
+                // advance to next scorer
+            } else {
+                // no more scorers
+                hasNext = false;
+            }
+        }
+        return hasNext;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public int doc() {
+        return scorers[current].doc() + starts[current];
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public float score() throws IOException {
+        return scorers[current].score();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean skipTo(int target) throws IOException {
+        current = scorerIndex(target);
+        if (scorers[current].skipTo(target - starts[current])) {
+            return true;
+        } else {
+            if (++current < scorers.length) {
+                // simply move to the next if there is any
+                return next();
+            } else {
+                // no more document
+                return hasNext = false;
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Explanation explain(int doc) throws IOException {
+        int scorerIndex = scorerIndex(doc);
+        return scorers[scorerIndex].explain(doc - starts[scorerIndex]);
+    }
+
+    //--------------------------< internal >------------------------------------
+
+    /**
+     * Returns the scorer index for document <code>n</code>.
+     * Implementation copied from lucene MultiReader class.
+     *
+     * @param n document number.
+     * @return the scorer index.
+     */
+    private int scorerIndex(int n) {
+        int lo = 0;                                      // search starts array
+        int hi = scorers.length - 1;                  // for first element less
+
+        while (hi >= lo) {
+            int mid = (lo + hi) >> 1;
+            int midValue = starts[mid];
+            if (n < midValue) {
+                hi = mid - 1;
+            } else if (n > midValue) {
+                lo = mid + 1;
+            } else {                                      // found a match
+                while (mid + 1 < scorers.length && starts[mid + 1] == midValue)
{
+                    mid++;                                  // scan to last match
+                }
+                return mid;
+            }
+        }
+        return hi;
+    }
+}

Propchange: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/MultiScorer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/SearchIndex.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/SearchIndex.java?view=diff&rev=518979&r1=518978&r2=518979
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/SearchIndex.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/SearchIndex.java
Fri Mar 16 07:08:02 2007
@@ -547,7 +547,9 @@
      * Combines multiple {@link CachingMultiReader} into a <code>MultiReader</code>
      * with {@link HierarchyResolver} support.
      */
-    protected static final class CombinedIndexReader extends MultiReader implements HierarchyResolver
{
+    protected static final class CombinedIndexReader
+            extends MultiReader
+            implements HierarchyResolver, MultiIndexReader {
 
         /**
          * The sub readers.
@@ -581,6 +583,19 @@
             id = id.applyOffset(starts[i]);
             return id.getDocumentNumber(this);
         }
+
+        //-------------------------< MultiIndexReader >-------------------------
+
+        /**
+         * {@inheritDoc}
+         */
+        public IndexReader[] getIndexReaders() {
+            IndexReader readers[] = new IndexReader[subReaders.length];
+            System.arraycopy(subReaders, 0, readers, 0, subReaders.length);
+            return readers;
+        }
+
+        //---------------------------< internal >-------------------------------
 
         /**
          * Returns the reader index for document <code>n</code>.



Mime
View raw message