You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by mr...@apache.org on 2007/03/21 12:22:14 UTC
svn commit: r520841 - in
/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene:
AbstractWeight.java MatchAllWeight.java RangeQuery.java WildcardQuery.java
Author: mreutegg
Date: Wed Mar 21 04:22:13 2007
New Revision: 520841
URL: http://svn.apache.org/viewvc?view=rev&rev=520841
Log:
JCR-804: Avoid using MultiTermDocs
Added:
jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/AbstractWeight.java (with props)
Modified:
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/RangeQuery.java
jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/WildcardQuery.java
Added: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/AbstractWeight.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/AbstractWeight.java?view=auto&rev=520841
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/AbstractWeight.java (added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/AbstractWeight.java Wed Mar 21 04:22:13 2007
@@ -0,0 +1,98 @@
+/*
+ * 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.Weight;
+import org.apache.lucene.search.Searcher;
+import org.apache.lucene.index.IndexReader;
+
+import java.io.IOException;
+
+/**
+ * <code>AbstractWeight</code> implements base functionality for custom lucene
+ * weights in jackrabbit.
+ */
+abstract class AbstractWeight implements Weight {
+
+ /**
+ * The searcher for this weight.
+ */
+ protected final Searcher searcher;
+
+ /**
+ * The factory, which creates the scorer instances for this weight.
+ */
+ private final ScorerFactory factory;
+
+ /**
+ * Creates a new <code>AbstractWeight</code> for the given
+ * <code>searcher</code>. Specific scorer instances are created using the
+ * provided scorer <code>factory</code>.
+ *
+ * @param searcher the searcher instance for this weight.
+ * @param factory the scorer factory.
+ */
+ public AbstractWeight(Searcher searcher, ScorerFactory factory) {
+ this.searcher = searcher;
+ this.factory = factory;
+ }
+
+ /**
+ * {@inheritDoc}
+ * <p/>
+ * Returns a {@link MultiScorer} if the passed <code>reader</code> is of
+ * type {@link MultiIndexReader}.
+ */
+ public Scorer scorer(IndexReader reader) throws IOException {
+ 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 factory.createScorer(reader);
+ }
+ }
+
+ /**
+ * Simple factory for creating scorer instances.
+ */
+ public interface ScorerFactory {
+
+ /**
+ * Creates a Scorer instance for the specified reader.
+ *
+ * @param reader the index reader the created scorer instance should
+ * use.
+ * @return the scorer instance.
+ * @throws IOException if an error occurs while reading from the index.
+ */
+ public Scorer createScorer(IndexReader reader) throws IOException;
+ }
+}
Propchange: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/AbstractWeight.java
------------------------------------------------------------------------------
svn:eol-style = native
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=520841&r1=520840&r2=520841
==============================================================================
--- 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 Wed Mar 21 04:22:13 2007
@@ -22,27 +22,19 @@
import org.apache.lucene.search.Scorer;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.Similarity;
-import org.apache.lucene.search.Weight;
import java.io.IOException;
/**
* This class implements the Weight calculation for the MatchAllQuery.
*/
-class MatchAllWeight implements Weight {
+class MatchAllWeight extends AbstractWeight {
/**
* the MatchAllQuery
*/
private final Query query;
- private final String field;
-
- /**
- * the current Searcher instance
- */
- private final Searcher searcher;
-
/**
* the weight value
*/
@@ -62,10 +54,13 @@
* @param query
* @param searcher
*/
- MatchAllWeight(Query query, Searcher searcher, String field) {
+ MatchAllWeight(Query query, Searcher searcher, final String field) {
+ super(searcher, new ScorerFactory() {
+ public Scorer createScorer(IndexReader reader) throws IOException {
+ return new MatchAllScorer(reader, field);
+ }
+ });
this.query = query;
- this.searcher = searcher;
- this.field = field;
}
/**
@@ -97,31 +92,6 @@
public void normalize(float queryNorm) {
queryWeight *= queryNorm; // normalize query weight
value = queryWeight * idf; // idf for document
- }
-
- /**
- * {@inheritDoc}
- */
- public Scorer scorer(IndexReader reader) throws IOException {
- 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);
- }
}
/**
Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/RangeQuery.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/RangeQuery.java?view=diff&rev=520841&r1=520840&r2=520841
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/RangeQuery.java (original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/RangeQuery.java Wed Mar 21 04:22:13 2007
@@ -182,12 +182,7 @@
/**
* The <code>Weight</code> implementation for this <code>RangeQuery</code>.
*/
- private class RangeQueryWeight implements Weight {
-
- /**
- * The searcher in use
- */
- private final Searcher searcher;
+ private class RangeQueryWeight extends AbstractWeight {
/**
* Creates a new <code>RangeQueryWeight</code> instance using
@@ -195,8 +190,12 @@
*
* @param searcher a <code>Searcher</code> instance.
*/
- RangeQueryWeight(Searcher searcher) {
- this.searcher = searcher;
+ RangeQueryWeight(final Searcher searcher) {
+ super(searcher, new ScorerFactory() {
+ public Scorer createScorer(IndexReader reader) {
+ return new RangeQueryScorer(searcher.getSimilarity(), reader);
+ }
+ });
}
/**
@@ -229,17 +228,6 @@
}
/**
- * Creates a scorer for this <code>Rangequery</code>.
- *
- * @param reader a reader for accessing the index.
- * @return a <code>RangeQueryScorer</code>.
- * @throws IOException if an error occurs while reading from the index.
- */
- public Scorer scorer(IndexReader reader) throws IOException {
- return new RangeQueryScorer(searcher.getSimilarity(), reader);
- }
-
- /**
* {@inheritDoc}
*/
public Explanation explain(IndexReader reader, int doc) throws IOException {
@@ -306,21 +294,21 @@
// check cache
synchronized (cache) {
Map m = (Map) cache.get(reader);
- if (m == null) {
+ if (m == null) {
m = new LRUMap(10);
cache.put(reader, m);
- }
- resultMap = m;
+ }
+ resultMap = m;
}
synchronized (resultMap) {
- BitSet result = (BitSet) resultMap.get(cacheKey);
- if (result == null) {
- result = new BitSet(reader.maxDoc());
- } else {
- hitsCalculated = true;
- }
- hits = result;
+ BitSet result = (BitSet) resultMap.get(cacheKey);
+ if (result == null) {
+ result = new BitSet(reader.maxDoc());
+ } else {
+ hitsCalculated = true;
}
+ hits = result;
+ }
}
/**
@@ -349,7 +337,8 @@
/**
* {@inheritDoc}
*/
- public boolean skipTo(int target) {
+ public boolean skipTo(int target) throws IOException {
+ calculateHits();
nextDoc = hits.nextSetBit(target);
return nextDoc > -1;
}
@@ -466,9 +455,7 @@
hitsCalculated = true;
// put to cache
- synchronized (resultMap) {
- resultMap.put(cacheKey, hits);
- }
+ resultMap.put(cacheKey, hits);
}
/**
Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/WildcardQuery.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/WildcardQuery.java?view=diff&rev=520841&r1=520840&r2=520841
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/WildcardQuery.java (original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/WildcardQuery.java Wed Mar 21 04:22:13 2007
@@ -156,12 +156,7 @@
/**
* The <code>Weight</code> implementation for this <code>WildcardQuery</code>.
*/
- private class WildcardQueryWeight implements Weight {
-
- /**
- * The searcher in use
- */
- private final Searcher searcher;
+ private class WildcardQueryWeight extends AbstractWeight {
/**
* Creates a new <code>WildcardQueryWeight</code> instance using
@@ -169,8 +164,12 @@
*
* @param searcher a <code>Searcher</code> instance.
*/
- public WildcardQueryWeight(Searcher searcher) {
- this.searcher = searcher;
+ public WildcardQueryWeight(final Searcher searcher) {
+ super(searcher, new ScorerFactory() {
+ public Scorer createScorer(IndexReader reader) {
+ return new WildcardQueryScorer(searcher.getSimilarity(), reader);
+ }
+ });
}
/**
@@ -203,17 +202,6 @@
}
/**
- * Creates a scorer for this <code>WildcardQuery</code>.
- *
- * @param reader a reader for accessing the index.
- * @return a <code>WildcardQueryScorer</code>.
- * @throws IOException if an error occurs while reading from the index.
- */
- public Scorer scorer(IndexReader reader) throws IOException {
- return new WildcardQueryScorer(searcher.getSimilarity(), reader);
- }
-
- /**
* {@inheritDoc}
*/
public Explanation explain(IndexReader reader, int doc) throws IOException {
@@ -269,21 +257,21 @@
// check cache
synchronized (cache) {
Map m = (Map) cache.get(reader);
- if (m == null) {
+ if (m == null) {
m = new LRUMap(10);
cache.put(reader, m);
- }
- resultMap = m;
+ }
+ resultMap = m;
}
synchronized (resultMap) {
- BitSet result = (BitSet) resultMap.get(cacheKey);
- if (result == null) {
- result = new BitSet(reader.maxDoc());
- } else {
- hitsCalculated = true;
- }
- hits = result;
+ BitSet result = (BitSet) resultMap.get(cacheKey);
+ if (result == null) {
+ result = new BitSet(reader.maxDoc());
+ } else {
+ hitsCalculated = true;
}
+ hits = result;
+ }
}
/**
@@ -312,7 +300,8 @@
/**
* {@inheritDoc}
*/
- public boolean skipTo(int target) {
+ public boolean skipTo(int target) throws IOException {
+ calculateHits();
nextDoc = hits.nextSetBit(target);
return nextDoc > -1;
}
@@ -355,9 +344,7 @@
}
hitsCalculated = true;
// put to cache
- synchronized (resultMap) {
- resultMap.put(cacheKey, hits);
- }
+ resultMap.put(cacheKey, hits);
}
}