Index: src/java/org/apache/lucene/analysis/NumericTokenStream.java
===================================================================
--- src/java/org/apache/lucene/analysis/NumericTokenStream.java (revision 786665)
+++ src/java/org/apache/lucene/analysis/NumericTokenStream.java (working copy)
@@ -18,19 +18,26 @@
*/
import org.apache.lucene.util.NumericUtils;
+import org.apache.lucene.document.NumericField; // for javadocs
import org.apache.lucene.search.NumericRangeQuery; // for javadocs
import org.apache.lucene.search.NumericRangeFilter; // for javadocs
+import org.apache.lucene.search.FieldCache; // for javadocs
+import org.apache.lucene.search.SortField; // for javadocs
import org.apache.lucene.analysis.tokenattributes.TermAttribute;
import org.apache.lucene.analysis.tokenattributes.TypeAttribute;
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
/**
- * This class provides a {@link TokenStream} for indexing numeric values
+ * Expert: This class provides a {@link TokenStream} for indexing numeric values
* that can be used by {@link NumericRangeQuery}/{@link NumericRangeFilter}.
* For more information, how to use this class and its configuration properties
* (precisionStep)
* read the docs of {@link NumericRangeQuery}.
*
+ *
For easy usage during indexing, there is a {@link NumericField}, that uses the optimal + * indexing settings (no norms, no term freqs). {@link NumericField} is a wrapper around this + * expert token stream. + * *
This stream is not intended to be used in analyzers, its more for iterating the * different precisions during indexing a specific numeric value. * A numeric value is indexed as multiple string encoded terms, each reduced @@ -64,12 +71,15 @@ * writer.addDocument(document); * ... * + * *
Please note: Token streams are read, when the document is added to index. * If you index more than one numeric field, use a separate instance for each. * - *
Values indexed by this stream can be sorted on or loaded into the field cache. - * For that factories like {@link NumericUtils#getLongSortField} are available, - * as well as parsers for filling the field cache (e.g., {@link NumericUtils#FIELD_CACHE_LONG_PARSER}) + *
Values indexed by this stream can be loaded into the {@link FieldCache}.
+ * For that parsers (e.g., {@link FieldCache#NUMERIC_UTILS_INT_PARSER})
+ * are available, which can also be specified in
+ * {@link SortField#SortField(String,FieldCache.Parser)} to sort
+ * on such a field.
*
* @since 2.9
*/
Index: src/java/org/apache/lucene/document/DateField.java
===================================================================
--- src/java/org/apache/lucene/document/DateField.java (revision 786665)
+++ src/java/org/apache/lucene/document/DateField.java (working copy)
@@ -19,7 +19,6 @@
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.RangeQuery;
-import org.apache.lucene.analysis.NumericTokenStream; // for javadocs
import org.apache.lucene.search.NumericRangeQuery; // for javadocs
import org.apache.lucene.util.NumericUtils; // for javadocs
@@ -50,11 +49,11 @@
* date/time are.
* For indexing a {@link Date} or {@link Calendar}, just get the unix timestamp as
* long using {@link Date#getTime} or {@link Calendar#getTimeInMillis} and
- * index this as a numeric value with {@link NumericTokenStream}
+ * index this as a numeric value with {@link NumericField}
* and use {@link NumericRangeQuery} to query it.
*
* @deprecated If you build a new index, use {@link DateTools} or
- * {@link NumericTokenStream} instead.
+ * {@link NumericField} instead.
* This class is included for use with existing
* indices and will be removed in a future release.
*/
Index: src/java/org/apache/lucene/document/DateTools.java
===================================================================
--- src/java/org/apache/lucene/document/DateTools.java (revision 786665)
+++ src/java/org/apache/lucene/document/DateTools.java (working copy)
@@ -22,7 +22,6 @@
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
-import org.apache.lucene.analysis.NumericTokenStream; // for javadocs
import org.apache.lucene.search.NumericRangeQuery; // for javadocs
import org.apache.lucene.util.NumericUtils; // for javadocs
@@ -46,7 +45,7 @@
* date/time are.
* For indexing a {@link Date} or {@link Calendar}, just get the unix timestamp as
* long using {@link Date#getTime} or {@link Calendar#getTimeInMillis} and
- * index this as a numeric value with {@link NumericTokenStream}
+ * index this as a numeric value with {@link NumericField}
* and use {@link NumericRangeQuery} to query it.
*/
public class DateTools {
Index: src/java/org/apache/lucene/document/NumberTools.java
===================================================================
--- src/java/org/apache/lucene/document/NumberTools.java (revision 786665)
+++ src/java/org/apache/lucene/document/NumberTools.java (working copy)
@@ -17,7 +17,7 @@
* limitations under the License.
*/
-import org.apache.lucene.analysis.NumericTokenStream; // for javadocs
+import org.apache.lucene.document.NumericField; // for javadocs
import org.apache.lucene.search.NumericRangeQuery; // for javadocs
import org.apache.lucene.util.NumericUtils; // for javadocs
@@ -39,7 +39,7 @@
* @deprecated For new indexes use {@link NumericUtils} instead, which
* provides a sortable binary representation (prefix encoded) of numeric
* values.
- * To index and efficiently query numeric values use {@link NumericTokenStream}
+ * To index and efficiently query numeric values use {@link NumericField}
* and {@link NumericRangeQuery}.
* This class is included for use with existing
* indices and will be removed in a future release.
Index: src/java/org/apache/lucene/document/NumericField.java
===================================================================
--- src/java/org/apache/lucene/document/NumericField.java (revision 0)
+++ src/java/org/apache/lucene/document/NumericField.java (revision 0)
@@ -0,0 +1,187 @@
+package org.apache.lucene.document;
+
+/**
+ * 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.
+ */
+
+import java.io.Reader;
+
+import org.apache.lucene.analysis.TokenStream;
+import org.apache.lucene.analysis.NumericTokenStream;
+import org.apache.lucene.search.NumericRangeQuery; // javadocs
+import org.apache.lucene.search.NumericRangeFilter; // javadocs
+import org.apache.lucene.search.FieldCache; // javadocs
+import org.apache.lucene.search.SortField; // javadocs
+
+/**
+ * This class provides a {@link Field} for indexing numeric values
+ * that can be used by {@link NumericRangeQuery}/{@link NumericRangeFilter}.
+ * For more information, how to use this class and its configuration properties
+ * (precisionStep)
+ * read the docs of {@link NumericRangeQuery}.
+ *
+ *
A numeric value is indexed as multiple string encoded terms, each reduced
+ * by zeroing bits from the right. Each value is also prefixed (in the first char) by the
+ * shift value (number of bits removed) used during encoding.
+ * The number of bits removed from the right for each trie entry is called
+ * precisionStep in this API.
+ *
+ *
The usage pattern is: + *
+ * document.add( + * new NumericField(name, precisionStep, Field.Store.XXX, true).set???Value(value) + * ); + *+ *
For optimal performance, re-use the NumericField and {@link Document} instance + * for more than one document: + *
+ * // init + * NumericField field = new NumericField(name, precisionStep, Field.Store.XXX, true); + * Document doc = new Document(); + * document.add(field); + * // use this code to index many documents: + * stream.set???Value(value1) + * writer.addDocument(document); + * stream.set???Value(value2) + * writer.addDocument(document); + * ... + *+ * + *
More advanced users can instead use {@link NumericTokenStream} directly, when + * indexing numbers. This class is a wrapper around this token stream type for easier, + * more intuitive usage. + * + *
Please note: This class is only used during indexing. You can also create
+ * numeric stored fields with it, but when retrieving the stored field value
+ * from a {@link Document} instance after search, you will get a conventional
+ * {@link Fieldable} instance where the numeric values are returned as {@link String}s
+ * (according to toString(value) of the used data type).
+ *
+ *
Values indexed using this {@link Fieldable} implementation can be
+ * loaded into the {@link FieldCache}.
+ * For that parsers (e.g., {@link FieldCache#NUMERIC_UTILS_INT_PARSER})
+ * are available, which can also be specified in
+ * {@link SortField#SortField(String,FieldCache.Parser)} to sort
+ * on such a field.
+ *
+ * @since 2.9
+ */
+public final class NumericField extends AbstractField {
+
+ private final NumericTokenStream tokenStream;
+
+ /**
+ * Creates a field for numeric values. The instance is not yet initialized with
+ * a numeric value, before indexing a document containing this field,
+ * set a value using the various set???Value() methods.
+ * This constrcutor creates an indexed, but not stored field.
+ * @param name the field name
+ * @param precisionStep the used precision step
+ */
+ public NumericField(String name, int precisionStep) {
+ this(name, precisionStep, Field.Store.NO, true);
+ }
+
+ /**
+ * Creates a field for numeric values. The instance is not yet initialized with
+ * a numeric value, before indexing a document containing this field,
+ * set a value using the various set???Value() methods.
+ * @param name the field name
+ * @param precisionStep the used precision step
+ * @param store if the field should be stored in plain text form
+ * (according to toString(value) of the used data type)
+ * @param index if the field should be indexed using {@link NumericTokenStream}
+ */
+ public NumericField(String name, int precisionStep, Field.Store store, boolean index) {
+ super(name, store, index ? Field.Index.ANALYZED_NO_NORMS : Field.Index.NO, Field.TermVector.NO);
+ setOmitTermFreqAndPositions(true);
+ tokenStream = new NumericTokenStream(precisionStep);
+ }
+
+ /** Returns a {@link NumericTokenStream} for indexing the numeric value. */
+ public TokenStream tokenStreamValue() {
+ return isIndexed() ? tokenStream : null;
+ }
+
+ /** Returns always null for numeric fields */
+ public byte[] binaryValue() {
+ return null;
+ }
+
+ /** Returns always null for numeric fields */
+ public Reader readerValue() {
+ return null;
+ }
+
+ /** Returns the numeric value as a string (how it is stored, when {@link Field.Store#YES} is choosen). */
+ public String stringValue() {
+ return (fieldsData == null) ? null : fieldsData.toString();
+ }
+
+ /** Returns the current numeric value as a subclass of {@link Number}, null if not yet initialized. */
+ public Number getNumericValue() {
+ return (Number) fieldsData;
+ }
+
+ /**
+ * Initializes the field with the supplied long value.
+ * @param value the numeric value
+ * @return this instance, because of this you can use it the following way:
+ * document.add(new NumericField(name, precisionStep).setLongValue(value))
+ */
+ public NumericField setLongValue(final long value) {
+ tokenStream.setLongValue(value);
+ fieldsData = new Long(value);
+ return this;
+ }
+
+ /**
+ * Initializes the field with the supplied int value.
+ * @param value the numeric value
+ * @return this instance, because of this you can use it the following way:
+ * document.add(new NumericField(name, precisionStep).setIntValue(value))
+ */
+ public NumericField setIntValue(final int value) {
+ tokenStream.setIntValue(value);
+ fieldsData = new Integer(value);
+ return this;
+ }
+
+ /**
+ * Initializes the field with the supplied double value.
+ * @param value the numeric value
+ * @return this instance, because of this you can use it the following way:
+ * document.add(new NumericField(name, precisionStep).setDoubleValue(value))
+ */
+ public NumericField setDoubleValue(final double value) {
+ tokenStream.setDoubleValue(value);
+ fieldsData = new Double(value);
+ return this;
+ }
+
+ /**
+ * Initializes the field with the supplied float value.
+ * @param value the numeric value
+ * @return this instance, because of this you can use it the following way:
+ * document.add(new NumericField(name, precisionStep).setFloatValue(value))
+ */
+ public NumericField setFloatValue(final float value) {
+ tokenStream.setFloatValue(value);
+ fieldsData = new Float(value);
+ return this;
+ }
+
+}
Property changes on: src\java\org\apache\lucene\document\NumericField.java
___________________________________________________________________
Added: svn:eol-style
+ native
Index: src/java/org/apache/lucene/search/ExtendedFieldCache.java
===================================================================
--- src/java/org/apache/lucene/search/ExtendedFieldCache.java (revision 786665)
+++ src/java/org/apache/lucene/search/ExtendedFieldCache.java (working copy)
@@ -21,84 +21,29 @@
import java.io.IOException;
-
/**
- *
- *
+ * This interface is obsolete, use {@link FieldCache} instead.
+ * @deprecated Will be removed in Lucene 3.0
**/
public interface ExtendedFieldCache extends FieldCache {
- public interface LongParser extends Parser {
- /**
- * Return an long representation of this field's value.
- */
- public long parseLong(String string);
+
+ /** @deprecated Use {@link FieldCache#DEFAULT}; this will be removed in Lucene 3.0 */
+ public static ExtendedFieldCache EXT_DEFAULT = (ExtendedFieldCache) FieldCache.DEFAULT;
+
+ /** @deprecated Use {@link FieldCache.LongParser}, this will be removed in Lucene 3.0 */
+ public interface LongParser extends FieldCache.LongParser {
}
- public interface DoubleParser extends Parser {
- /**
- * Return an long representation of this field's value.
- */
- public double parseDouble(String string);
+ /** @deprecated Use {@link FieldCache.DoubleParser}, this will be removed in Lucene 3.0 */
+ public interface DoubleParser extends FieldCache.DoubleParser {
}
- public static ExtendedFieldCache EXT_DEFAULT = (ExtendedFieldCache)FieldCache.DEFAULT;
+ /** @deprecated Will be removed in 3.0, this is for binary compatibility only */
+ public long[] getLongs(IndexReader reader, String field, ExtendedFieldCache.LongParser parser)
+ throws IOException;
- /**
- * Checks the internal cache for an appropriate entry, and if none is
- * found, reads the terms in field as longs and returns an array
- * of size reader.maxDoc() of the value each document
- * has in the given field.
- *
- * @param reader Used to get field values.
- * @param field Which field contains the longs.
- * @return The values in the given field for each document.
- * @throws java.io.IOException If any error occurs.
- */
- public long[] getLongs(IndexReader reader, String field)
- throws IOException;
+ /** @deprecated Will be removed in 3.0, this is for binary compatibility only */
+ public double[] getDoubles(IndexReader reader, String field, ExtendedFieldCache.DoubleParser parser)
+ throws IOException;
- /**
- * Checks the internal cache for an appropriate entry, and if none is found,
- * reads the terms in field as longs and returns an array of
- * size reader.maxDoc() of the value each document has in the
- * given field.
- *
- * @param reader Used to get field values.
- * @param field Which field contains the longs.
- * @param parser Computes integer for string values.
- * @return The values in the given field for each document.
- * @throws IOException If any error occurs.
- */
- public long[] getLongs(IndexReader reader, String field, LongParser parser)
- throws IOException;
-
-
- /**
- * Checks the internal cache for an appropriate entry, and if none is
- * found, reads the terms in field as integers and returns an array
- * of size reader.maxDoc() of the value each document
- * has in the given field.
- *
- * @param reader Used to get field values.
- * @param field Which field contains the doubles.
- * @return The values in the given field for each document.
- * @throws IOException If any error occurs.
- */
- public double[] getDoubles(IndexReader reader, String field)
- throws IOException;
-
- /**
- * Checks the internal cache for an appropriate entry, and if none is found,
- * reads the terms in field as doubles and returns an array of
- * size reader.maxDoc() of the value each document has in the
- * given field.
- *
- * @param reader Used to get field values.
- * @param field Which field contains the doubles.
- * @param parser Computes integer for string values.
- * @return The values in the given field for each document.
- * @throws IOException If any error occurs.
- */
- public double[] getDoubles(IndexReader reader, String field, DoubleParser parser)
- throws IOException;
}
Index: src/java/org/apache/lucene/search/ExtendedFieldCacheImpl.java
===================================================================
--- src/java/org/apache/lucene/search/ExtendedFieldCacheImpl.java (revision 786665)
+++ src/java/org/apache/lucene/search/ExtendedFieldCacheImpl.java (working copy)
@@ -1,184 +0,0 @@
-package org.apache.lucene.search;
-
-/**
- * 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.
- */
-
-import org.apache.lucene.index.IndexReader;
-import org.apache.lucene.index.Term;
-import org.apache.lucene.index.TermDocs;
-import org.apache.lucene.index.TermEnum;
-
-import java.io.IOException;
-
-
-/**
- *
- *
- **/
-class ExtendedFieldCacheImpl extends FieldCacheImpl implements ExtendedFieldCache {
- private static final LongParser LONG_PARSER = new LongParser() {
- public long parseLong(String value) {
- return Long.parseLong(value);
- }
- };
-
- private static final DoubleParser DOUBLE_PARSER = new DoubleParser() {
- public double parseDouble(String value) {
- return Double.parseDouble(value);
- }
- };
-
-
- public long[] getLongs(IndexReader reader, String field) throws IOException {
- return getLongs(reader, field, LONG_PARSER);
- }
-
- // inherit javadocs
- public long[] getLongs(IndexReader reader, String field, LongParser parser)
- throws IOException {
- return (long[]) longsCache.get(reader, new Entry(field, parser));
- }
-
- Cache longsCache = new Cache() {
-
- protected Object createValue(IndexReader reader, Object entryKey)
- throws IOException {
- Entry entry = (Entry) entryKey;
- String field = entry.field;
- LongParser parser = (LongParser) entry.custom;
- final long[] retArray = new long[reader.maxDoc()];
- TermDocs termDocs = reader.termDocs();
- TermEnum termEnum = reader.terms (new Term(field));
- try {
- do {
- Term term = termEnum.term();
- if (term==null || term.field() != field) break;
- long termval = parser.parseLong(term.text());
- termDocs.seek (termEnum);
- while (termDocs.next()) {
- retArray[termDocs.doc()] = termval;
- }
- } while (termEnum.next());
- } catch (StopFillCacheException stop) {
- } finally {
- termDocs.close();
- termEnum.close();
- }
- return retArray;
- }
- };
-
- // inherit javadocs
- public double[] getDoubles(IndexReader reader, String field)
- throws IOException {
- return getDoubles(reader, field, DOUBLE_PARSER);
- }
-
- // inherit javadocs
- public double[] getDoubles(IndexReader reader, String field, DoubleParser parser)
- throws IOException {
- return (double[]) doublesCache.get(reader, new Entry(field, parser));
- }
-
- Cache doublesCache = new Cache() {
-
- protected Object createValue(IndexReader reader, Object entryKey)
- throws IOException {
- Entry entry = (Entry) entryKey;
- String field = entry.field;
- DoubleParser parser = (DoubleParser) entry.custom;
- final double[] retArray = new double[reader.maxDoc()];
- TermDocs termDocs = reader.termDocs();
- TermEnum termEnum = reader.terms (new Term (field));
- try {
- do {
- Term term = termEnum.term();
- if (term==null || term.field() != field) break;
- double termval = parser.parseDouble(term.text());
- termDocs.seek (termEnum);
- while (termDocs.next()) {
- retArray[termDocs.doc()] = termval;
- }
- } while (termEnum.next());
- } catch (StopFillCacheException stop) {
- } finally {
- termDocs.close();
- termEnum.close();
- }
- return retArray;
- }
- };
-
-
- // inherit javadocs
- public Object getAuto(IndexReader reader, String field) throws IOException {
- return autoCache.get(reader, field);
- }
-
- Cache autoCache = new Cache() {
-
- protected Object createValue(IndexReader reader, Object fieldKey)
- throws IOException {
- String field = ((String)fieldKey).intern();
- TermEnum enumerator = reader.terms (new Term (field));
- try {
- Term term = enumerator.term();
- if (term == null) {
- throw new RuntimeException ("no terms in field " + field + " - cannot determine sort type");
- }
- Object ret = null;
- if (term.field() == field) {
- String termtext = term.text().trim();
-
- /**
- * Java 1.4 level code:
-
- if (pIntegers.matcher(termtext).matches())
- return IntegerSortedHitQueue.comparator (reader, enumerator, field);
-
- else if (pFloats.matcher(termtext).matches())
- return FloatSortedHitQueue.comparator (reader, enumerator, field);
- */
-
- // Java 1.3 level code:
- try {
- Integer.parseInt (termtext);
- ret = getInts (reader, field);
- } catch (NumberFormatException nfe1) {
- try {
- Long.parseLong(termtext);
- ret = getLongs (reader, field);
- } catch (NumberFormatException nfe2) {
- try {
- Float.parseFloat (termtext);
- ret = getFloats (reader, field);
- } catch (NumberFormatException nfe3) {
- ret = getStringIndex (reader, field);
- }
- }
- }
- } else {
- throw new RuntimeException ("field \"" + field + "\" does not appear to be indexed");
- }
- return ret;
- } finally {
- enumerator.close();
- }
- }
- };
-
-}
Index: src/java/org/apache/lucene/search/FieldCache.java
===================================================================
--- src/java/org/apache/lucene/search/FieldCache.java (revision 786665)
+++ src/java/org/apache/lucene/search/FieldCache.java (working copy)
@@ -18,7 +18,12 @@
*/
import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.util.NumericUtils;
+import org.apache.lucene.document.NumericField; // for javadocs
+import org.apache.lucene.analysis.NumericTokenStream; // for javadocs
+
import java.io.IOException;
+import java.io.Serializable;
/**
* Expert: Maintains caches of term values.
@@ -79,21 +84,9 @@
* is used to specify a custom parser to {@link
* SortField#SortField(String, FieldCache.Parser)}.
*/
- public interface Parser {
+ public interface Parser extends Serializable {
}
- /**
- * Expert: when thrown from a custom Parser, this stops
- * processing terms and returns the current FieldCache
- * array.
- *
- *
NOTE: This API is experimental and likely to
- * change in incompatible ways, or be removed entirely, in
- * the next release.
- */
- public static class StopFillCacheException extends RuntimeException {
- }
-
/** Interface to parse bytes from document fields.
* @see FieldCache#getBytes(IndexReader, String, FieldCache.ByteParser)
*/
@@ -126,9 +119,149 @@
public float parseFloat(String string);
}
+ /** Interface to parse long from document fields.
+ * @see FieldCache#getLongs(IndexReader, String, FieldCache.LongParser)
+ */
+ public interface LongParser extends Parser {
+ /** Return an long representation of this field's value. */
+ public long parseLong(String string);
+ }
+
+ /** Interface to parse doubles from document fields.
+ * @see FieldCache#getDoubles(IndexReader, String, FieldCache.DoubleParser)
+ */
+ public interface DoubleParser extends Parser {
+ /** Return an long representation of this field's value. */
+ public double parseDouble(String string);
+ }
+
/** Expert: The cache used internally by sorting and range query classes. */
- public static FieldCache DEFAULT = new ExtendedFieldCacheImpl();
+ public static FieldCache DEFAULT = new FieldCacheImpl();
+
+ /** The default parser for byte values, which are encoded by {@link Byte#toString(byte)} */
+ public static final ByteParser DEFAULT_BYTE_PARSER = new ByteParser() {
+ public byte parseByte(String value) {
+ return Byte.parseByte(value);
+ }
+ protected Object readResolve() {
+ return DEFAULT_BYTE_PARSER;
+ }
+ };
+ /** The default parser for short values, which are encoded by {@link Short#toString(short)} */
+ public static final ShortParser DEFAULT_SHORT_PARSER = new ShortParser() {
+ public short parseShort(String value) {
+ return Short.parseShort(value);
+ }
+ protected Object readResolve() {
+ return DEFAULT_SHORT_PARSER;
+ }
+ };
+
+ /** The default parser for int values, which are encoded by {@link Integer#toString(int)} */
+ public static final IntParser DEFAULT_INT_PARSER = new IntParser() {
+ public int parseInt(String value) {
+ return Integer.parseInt(value);
+ }
+ protected Object readResolve() {
+ return DEFAULT_INT_PARSER;
+ }
+ };
+
+ /** The default parser for float values, which are encoded by {@link Float#toString(float)} */
+ public static final FloatParser DEFAULT_FLOAT_PARSER = new FloatParser() {
+ public float parseFloat(String value) {
+ return Float.parseFloat(value);
+ }
+ protected Object readResolve() {
+ return DEFAULT_FLOAT_PARSER;
+ }
+ };
+
+ /** The default parser for long values, which are encoded by {@link Long#toString(long)} */
+ public static final LongParser DEFAULT_LONG_PARSER = new LongParser() {
+ public long parseLong(String value) {
+ return Long.parseLong(value);
+ }
+ protected Object readResolve() {
+ return DEFAULT_LONG_PARSER;
+ }
+ };
+
+ /** The default parser for double values, which are encoded by {@link Double#toString(double)} */
+ public static final DoubleParser DEFAULT_DOUBLE_PARSER = new DoubleParser() {
+ public double parseDouble(String value) {
+ return Double.parseDouble(value);
+ }
+ protected Object readResolve() {
+ return DEFAULT_DOUBLE_PARSER;
+ }
+ };
+
+ /**
+ * A parser instance for int values encoded by {@link NumericUtils#intToPrefixCoded(int)}, e.g. when indexed
+ * via {@link NumericField}/{@link NumericTokenStream}.
+ */
+ public static final IntParser NUMERIC_UTILS_INT_PARSER=new IntParser(){
+ public int parseInt(String val) {
+ final int shift = val.charAt(0)-NumericUtils.SHIFT_START_INT;
+ if (shift>0 && shift<=31)
+ throw new FieldCacheImpl.StopFillCacheException();
+ return NumericUtils.prefixCodedToInt(val);
+ }
+ protected Object readResolve() {
+ return NUMERIC_UTILS_INT_PARSER;
+ }
+ };
+
+ /**
+ * A parser instance for float values encoded with {@link NumericUtils}, e.g. when indexed
+ * via {@link NumericField}/{@link NumericTokenStream}.
+ */
+ public static final FloatParser NUMERIC_UTILS_FLOAT_PARSER=new FloatParser(){
+ public float parseFloat(String val) {
+ final int shift = val.charAt(0)-NumericUtils.SHIFT_START_INT;
+ if (shift>0 && shift<=31)
+ throw new FieldCacheImpl.StopFillCacheException();
+ return NumericUtils.sortableIntToFloat(NumericUtils.prefixCodedToInt(val));
+ }
+ protected Object readResolve() {
+ return NUMERIC_UTILS_FLOAT_PARSER;
+ }
+ };
+
+ /**
+ * A parser instance for long values encoded by {@link NumericUtils#longToPrefixCoded(long)}, e.g. when indexed
+ * via {@link NumericField}/{@link NumericTokenStream}.
+ */
+ public static final LongParser NUMERIC_UTILS_LONG_PARSER = new LongParser(){
+ public long parseLong(String val) {
+ final int shift = val.charAt(0)-NumericUtils.SHIFT_START_LONG;
+ if (shift>0 && shift<=63)
+ throw new FieldCacheImpl.StopFillCacheException();
+ return NumericUtils.prefixCodedToLong(val);
+ }
+ protected Object readResolve() {
+ return NUMERIC_UTILS_LONG_PARSER;
+ }
+ };
+
+ /**
+ * A parser instance for double values encoded with {@link NumericUtils}, e.g. when indexed
+ * via {@link NumericField}/{@link NumericTokenStream}.
+ */
+ public static final DoubleParser NUMERIC_UTILS_DOUBLE_PARSER = new DoubleParser(){
+ public double parseDouble(String val) {
+ final int shift = val.charAt(0)-NumericUtils.SHIFT_START_LONG;
+ if (shift>0 && shift<=63)
+ throw new FieldCacheImpl.StopFillCacheException();
+ return NumericUtils.sortableLongToDouble(NumericUtils.prefixCodedToLong(val));
+ }
+ protected Object readResolve() {
+ return NUMERIC_UTILS_DOUBLE_PARSER;
+ }
+ };
+
/** Checks the internal cache for an appropriate entry, and if none is
* found, reads the terms in field as a single byte and returns an array
* of size reader.maxDoc() of the value each document
@@ -228,7 +361,66 @@
*/
public float[] getFloats (IndexReader reader, String field,
FloatParser parser) throws IOException;
+
+ /**
+ * Checks the internal cache for an appropriate entry, and if none is
+ * found, reads the terms in field as longs and returns an array
+ * of size reader.maxDoc() of the value each document
+ * has in the given field.
+ *
+ * @param reader Used to get field values.
+ * @param field Which field contains the longs.
+ * @return The values in the given field for each document.
+ * @throws java.io.IOException If any error occurs.
+ */
+ public long[] getLongs(IndexReader reader, String field)
+ throws IOException;
+ /**
+ * Checks the internal cache for an appropriate entry, and if none is found,
+ * reads the terms in field as longs and returns an array of
+ * size reader.maxDoc() of the value each document has in the
+ * given field.
+ *
+ * @param reader Used to get field values.
+ * @param field Which field contains the longs.
+ * @param parser Computes integer for string values.
+ * @return The values in the given field for each document.
+ * @throws IOException If any error occurs.
+ */
+ public long[] getLongs(IndexReader reader, String field, LongParser parser)
+ throws IOException;
+
+
+ /**
+ * Checks the internal cache for an appropriate entry, and if none is
+ * found, reads the terms in field as integers and returns an array
+ * of size reader.maxDoc() of the value each document
+ * has in the given field.
+ *
+ * @param reader Used to get field values.
+ * @param field Which field contains the doubles.
+ * @return The values in the given field for each document.
+ * @throws IOException If any error occurs.
+ */
+ public double[] getDoubles(IndexReader reader, String field)
+ throws IOException;
+
+ /**
+ * Checks the internal cache for an appropriate entry, and if none is found,
+ * reads the terms in field as doubles and returns an array of
+ * size reader.maxDoc() of the value each document has in the
+ * given field.
+ *
+ * @param reader Used to get field values.
+ * @param field Which field contains the doubles.
+ * @param parser Computes integer for string values.
+ * @return The values in the given field for each document.
+ * @throws IOException If any error occurs.
+ */
+ public double[] getDoubles(IndexReader reader, String field, DoubleParser parser)
+ throws IOException;
+
/** Checks the internal cache for an appropriate entry, and if none
* is found, reads the term values in field and returns an array
* of size reader.maxDoc() containing the value each document
Index: src/java/org/apache/lucene/search/FieldCacheImpl.java
===================================================================
--- src/java/org/apache/lucene/search/FieldCacheImpl.java (revision 786665)
+++ src/java/org/apache/lucene/search/FieldCacheImpl.java (working copy)
@@ -37,9 +37,17 @@
* @since lucene 1.4
* @version $Id$
*/
-class FieldCacheImpl
-implements FieldCache {
+ // TODO: change interface to FieldCache in 3.0 when removed
+class FieldCacheImpl implements ExtendedFieldCache {
+ /**
+ * Hack: When thrown from a Parser (NUMERIC_UTILS_* ones), this stops
+ * processing terms and returns the current FieldCache
+ * array.
+ */
+ static final class StopFillCacheException extends RuntimeException {
+ }
+
/** Expert: Internal cache. */
abstract static class Cache {
private final Map readerCache = new WeakHashMap();
@@ -140,34 +148,9 @@
}
}
- private static final ByteParser BYTE_PARSER = new ByteParser() {
- public byte parseByte(String value) {
- return Byte.parseByte(value);
- }
- };
-
- private static final ShortParser SHORT_PARSER = new ShortParser() {
- public short parseShort(String value) {
- return Short.parseShort(value);
- }
- };
-
- private static final IntParser INT_PARSER = new IntParser() {
- public int parseInt(String value) {
- return Integer.parseInt(value);
- }
- };
-
-
- private static final FloatParser FLOAT_PARSER = new FloatParser() {
- public float parseFloat(String value) {
- return Float.parseFloat(value);
- }
- };
-
// inherit javadocs
public byte[] getBytes (IndexReader reader, String field) throws IOException {
- return getBytes(reader, field, BYTE_PARSER);
+ return getBytes(reader, field, FieldCache.DEFAULT_BYTE_PARSER);
}
// inherit javadocs
@@ -207,7 +190,7 @@
// inherit javadocs
public short[] getShorts (IndexReader reader, String field) throws IOException {
- return getShorts(reader, field, SHORT_PARSER);
+ return getShorts(reader, field, FieldCache.DEFAULT_SHORT_PARSER);
}
// inherit javadocs
@@ -247,7 +230,7 @@
// inherit javadocs
public int[] getInts (IndexReader reader, String field) throws IOException {
- return getInts(reader, field, INT_PARSER);
+ return getInts(reader, field, FieldCache.DEFAULT_INT_PARSER);
}
// inherit javadocs
@@ -289,7 +272,7 @@
// inherit javadocs
public float[] getFloats (IndexReader reader, String field)
throws IOException {
- return getFloats(reader, field, FLOAT_PARSER);
+ return getFloats(reader, field, FieldCache.DEFAULT_FLOAT_PARSER);
}
// inherit javadocs
@@ -327,7 +310,100 @@
}
};
+
+ public long[] getLongs(IndexReader reader, String field) throws IOException {
+ return getLongs(reader, field, FieldCache.DEFAULT_LONG_PARSER);
+ }
+
// inherit javadocs
+ public long[] getLongs(IndexReader reader, String field, FieldCache.LongParser parser)
+ throws IOException {
+ return (long[]) longsCache.get(reader, new Entry(field, parser));
+ }
+
+ /** @deprecated Will be removed in 3.0, this is for binary compatibility only */
+ public long[] getLongs(IndexReader reader, String field, ExtendedFieldCache.LongParser parser)
+ throws IOException {
+ return (long[]) longsCache.get(reader, new Entry(field, parser));
+ }
+
+ Cache longsCache = new Cache() {
+
+ protected Object createValue(IndexReader reader, Object entryKey)
+ throws IOException {
+ Entry entry = (Entry) entryKey;
+ String field = entry.field;
+ FieldCache.LongParser parser = (FieldCache.LongParser) entry.custom;
+ final long[] retArray = new long[reader.maxDoc()];
+ TermDocs termDocs = reader.termDocs();
+ TermEnum termEnum = reader.terms (new Term(field));
+ try {
+ do {
+ Term term = termEnum.term();
+ if (term==null || term.field() != field) break;
+ long termval = parser.parseLong(term.text());
+ termDocs.seek (termEnum);
+ while (termDocs.next()) {
+ retArray[termDocs.doc()] = termval;
+ }
+ } while (termEnum.next());
+ } catch (StopFillCacheException stop) {
+ } finally {
+ termDocs.close();
+ termEnum.close();
+ }
+ return retArray;
+ }
+ };
+
+ // inherit javadocs
+ public double[] getDoubles(IndexReader reader, String field)
+ throws IOException {
+ return getDoubles(reader, field, FieldCache.DEFAULT_DOUBLE_PARSER);
+ }
+
+ // inherit javadocs
+ public double[] getDoubles(IndexReader reader, String field, FieldCache.DoubleParser parser)
+ throws IOException {
+ return (double[]) doublesCache.get(reader, new Entry(field, parser));
+ }
+
+ /** @deprecated Will be removed in 3.0, this is for binary compatibility only */
+ public double[] getDoubles(IndexReader reader, String field, ExtendedFieldCache.DoubleParser parser)
+ throws IOException {
+ return (double[]) doublesCache.get(reader, new Entry(field, parser));
+ }
+
+ Cache doublesCache = new Cache() {
+
+ protected Object createValue(IndexReader reader, Object entryKey)
+ throws IOException {
+ Entry entry = (Entry) entryKey;
+ String field = entry.field;
+ FieldCache.DoubleParser parser = (FieldCache.DoubleParser) entry.custom;
+ final double[] retArray = new double[reader.maxDoc()];
+ TermDocs termDocs = reader.termDocs();
+ TermEnum termEnum = reader.terms (new Term (field));
+ try {
+ do {
+ Term term = termEnum.term();
+ if (term==null || term.field() != field) break;
+ double termval = parser.parseDouble(term.text());
+ termDocs.seek (termEnum);
+ while (termDocs.next()) {
+ retArray[termDocs.doc()] = termval;
+ }
+ } while (termEnum.next());
+ } catch (StopFillCacheException stop) {
+ } finally {
+ termDocs.close();
+ termEnum.close();
+ }
+ return retArray;
+ }
+ };
+
+ // inherit javadocs
public String[] getStrings(IndexReader reader, String field)
throws IOException {
return (String[]) stringsCache.get(reader, field);
Index: src/java/org/apache/lucene/search/FieldComparator.java
===================================================================
--- src/java/org/apache/lucene/search/FieldComparator.java (revision 786665)
+++ src/java/org/apache/lucene/search/FieldComparator.java (working copy)
@@ -22,8 +22,8 @@
import java.util.Locale;
import org.apache.lucene.index.IndexReader;
-import org.apache.lucene.search.ExtendedFieldCache.DoubleParser;
-import org.apache.lucene.search.ExtendedFieldCache.LongParser;
+import org.apache.lucene.search.FieldCache.DoubleParser;
+import org.apache.lucene.search.FieldCache.LongParser;
import org.apache.lucene.search.FieldCache.ByteParser;
import org.apache.lucene.search.FieldCache.FloatParser;
import org.apache.lucene.search.FieldCache.IntParser;
@@ -71,9 +71,7 @@
}
public void setNextReader(IndexReader reader, int docBase, int numSlotsFull) throws IOException {
- currentReaderValues = parser != null ? ExtendedFieldCache.EXT_DEFAULT
- .getBytes(reader, field, parser) : ExtendedFieldCache.EXT_DEFAULT
- .getBytes(reader, field);
+ currentReaderValues = FieldCache.DEFAULT.getBytes(reader, field, parser);
}
public void setBottom(final int bottom) {
@@ -134,7 +132,7 @@
}
/** Parses field's values as double (using {@link
- * ExtendedFieldCache#getDoubles} and sorts by ascending value */
+ * FieldCache#getDoubles} and sorts by ascending value */
public static final class DoubleComparator extends FieldComparator {
private final double[] values;
private double[] currentReaderValues;
@@ -176,9 +174,7 @@
}
public void setNextReader(IndexReader reader, int docBase, int numSlotsFull) throws IOException {
- currentReaderValues = parser != null ? ExtendedFieldCache.EXT_DEFAULT
- .getDoubles(reader, field, parser) : ExtendedFieldCache.EXT_DEFAULT
- .getDoubles(reader, field);
+ currentReaderValues = FieldCache.DEFAULT.getDoubles(reader, field, parser);
}
public void setBottom(final int bottom) {
@@ -241,8 +237,7 @@
}
public void setNextReader(IndexReader reader, int docBase, int numSlotsFull) throws IOException {
- currentReaderValues = parser != null ? FieldCache.DEFAULT.getFloats(
- reader, field, parser) : FieldCache.DEFAULT.getFloats(reader, field);
+ currentReaderValues = FieldCache.DEFAULT.getFloats(reader, field, parser);
}
public void setBottom(final int bottom) {
@@ -309,8 +304,7 @@
}
public void setNextReader(IndexReader reader, int docBase, int numSlotsFull) throws IOException {
- currentReaderValues = parser != null ? FieldCache.DEFAULT.getInts(reader,
- field, parser) : FieldCache.DEFAULT.getInts(reader, field);
+ currentReaderValues = FieldCache.DEFAULT.getInts(reader, field, parser);
}
public void setBottom(final int bottom) {
@@ -327,7 +321,7 @@
}
/** Parses field's values as long (using {@link
- * ExtendedFieldCache#getLongs} and sorts by ascending value */
+ * FieldCache#getLongs} and sorts by ascending value */
public static final class LongComparator extends FieldComparator {
private final long[] values;
private long[] currentReaderValues;
@@ -373,9 +367,7 @@
}
public void setNextReader(IndexReader reader, int docBase, int numSlotsFull) throws IOException {
- currentReaderValues = parser != null ? ExtendedFieldCache.EXT_DEFAULT
- .getLongs(reader, field, parser) : ExtendedFieldCache.EXT_DEFAULT
- .getLongs(reader, field);
+ currentReaderValues = FieldCache.DEFAULT.getLongs(reader, field, parser);
}
public void setBottom(final int bottom) {
@@ -471,9 +463,7 @@
}
public void setNextReader(IndexReader reader, int docBase, int numSlotsFull) throws IOException {
- currentReaderValues = parser != null ? ExtendedFieldCache.EXT_DEFAULT
- .getShorts(reader, field, parser) : ExtendedFieldCache.EXT_DEFAULT
- .getShorts(reader, field);
+ currentReaderValues = FieldCache.DEFAULT.getShorts(reader, field, parser);
}
public void setBottom(final int bottom) {
@@ -537,8 +527,7 @@
}
public void setNextReader(IndexReader reader, int docBase, int numSlotsFull) throws IOException {
- currentReaderValues = ExtendedFieldCache.EXT_DEFAULT.getStrings(reader,
- field);
+ currentReaderValues = FieldCache.DEFAULT.getStrings(reader, field);
}
public void setBottom(final int bottom) {
@@ -664,7 +653,7 @@
}
public void setNextReader(IndexReader reader, int docBase, int numSlotsFull) throws IOException {
- StringIndex currentReaderValues = ExtendedFieldCache.EXT_DEFAULT.getStringIndex(reader, field);
+ StringIndex currentReaderValues = FieldCache.DEFAULT.getStringIndex(reader, field);
currentReaderGen++;
order = currentReaderValues.order;
lookup = currentReaderValues.lookup;
@@ -756,8 +745,7 @@
}
public void setNextReader(IndexReader reader, int docBase, int numSlotsFull) throws IOException {
- currentReaderValues = ExtendedFieldCache.EXT_DEFAULT.getStrings(reader,
- field);
+ currentReaderValues = FieldCache.DEFAULT.getStrings(reader, field);
}
public void setBottom(final int bottom) {
Index: src/java/org/apache/lucene/search/FieldSortedHitQueue.java
===================================================================
--- src/java/org/apache/lucene/search/FieldSortedHitQueue.java (revision 786665)
+++ src/java/org/apache/lucene/search/FieldSortedHitQueue.java (working copy)
@@ -205,10 +205,10 @@
comparator = comparatorFloat (reader, fieldname, (FieldCache.FloatParser)parser);
break;
case SortField.LONG:
- comparator = comparatorLong(reader, fieldname, (ExtendedFieldCache.LongParser)parser);
+ comparator = comparatorLong(reader, fieldname, (FieldCache.LongParser)parser);
break;
case SortField.DOUBLE:
- comparator = comparatorDouble(reader, fieldname, (ExtendedFieldCache.DoubleParser)parser);
+ comparator = comparatorDouble(reader, fieldname, (FieldCache.DoubleParser)parser);
break;
case SortField.SHORT:
comparator = comparatorShort(reader, fieldname, (FieldCache.ShortParser)parser);
@@ -240,9 +240,7 @@
static ScoreDocComparator comparatorByte(final IndexReader reader, final String fieldname, final FieldCache.ByteParser parser)
throws IOException {
final String field = fieldname.intern();
- final byte[] fieldOrder = (parser==null)
- ? FieldCache.DEFAULT.getBytes(reader, field)
- : FieldCache.DEFAULT.getBytes(reader, field, parser);
+ final byte[] fieldOrder = FieldCache.DEFAULT.getBytes(reader, field, parser);
return new ScoreDocComparator() {
public final int compare (final ScoreDoc i, final ScoreDoc j) {
@@ -273,9 +271,7 @@
static ScoreDocComparator comparatorShort(final IndexReader reader, final String fieldname, final FieldCache.ShortParser parser)
throws IOException {
final String field = fieldname.intern();
- final short[] fieldOrder = (parser==null)
- ? FieldCache.DEFAULT.getShorts(reader, field)
- : FieldCache.DEFAULT.getShorts(reader, field, parser);
+ final short[] fieldOrder = FieldCache.DEFAULT.getShorts(reader, field, parser);
return new ScoreDocComparator() {
public final int compare (final ScoreDoc i, final ScoreDoc j) {
@@ -306,9 +302,7 @@
static ScoreDocComparator comparatorInt (final IndexReader reader, final String fieldname, final FieldCache.IntParser parser)
throws IOException {
final String field = fieldname.intern();
- final int[] fieldOrder = (parser==null)
- ? FieldCache.DEFAULT.getInts(reader, field)
- : FieldCache.DEFAULT.getInts(reader, field, parser);
+ final int[] fieldOrder = FieldCache.DEFAULT.getInts(reader, field, parser);
return new ScoreDocComparator() {
public final int compare (final ScoreDoc i, final ScoreDoc j) {
@@ -336,12 +330,10 @@
* @return Comparator for sorting hits.
* @throws IOException If an error occurs reading the index.
*/
- static ScoreDocComparator comparatorLong (final IndexReader reader, final String fieldname, final ExtendedFieldCache.LongParser parser)
+ static ScoreDocComparator comparatorLong (final IndexReader reader, final String fieldname, final FieldCache.LongParser parser)
throws IOException {
final String field = fieldname.intern();
- final long[] fieldOrder = (parser==null)
- ? ExtendedFieldCache.EXT_DEFAULT.getLongs (reader, field)
- : ExtendedFieldCache.EXT_DEFAULT.getLongs (reader, field, parser);
+ final long[] fieldOrder = FieldCache.DEFAULT.getLongs (reader, field, parser);
return new ScoreDocComparator() {
public final int compare (final ScoreDoc i, final ScoreDoc j) {
@@ -373,9 +365,7 @@
static ScoreDocComparator comparatorFloat (final IndexReader reader, final String fieldname, final FieldCache.FloatParser parser)
throws IOException {
final String field = fieldname.intern();
- final float[] fieldOrder = (parser==null)
- ? FieldCache.DEFAULT.getFloats (reader, field)
- : FieldCache.DEFAULT.getFloats (reader, field, parser);
+ final float[] fieldOrder = FieldCache.DEFAULT.getFloats (reader, field, parser);
return new ScoreDocComparator () {
public final int compare (final ScoreDoc i, final ScoreDoc j) {
@@ -403,12 +393,10 @@
* @return Comparator for sorting hits.
* @throws IOException If an error occurs reading the index.
*/
- static ScoreDocComparator comparatorDouble(final IndexReader reader, final String fieldname, final ExtendedFieldCache.DoubleParser parser)
+ static ScoreDocComparator comparatorDouble(final IndexReader reader, final String fieldname, final FieldCache.DoubleParser parser)
throws IOException {
final String field = fieldname.intern();
- final double[] fieldOrder = (parser==null)
- ? ExtendedFieldCache.EXT_DEFAULT.getDoubles (reader, field)
- : ExtendedFieldCache.EXT_DEFAULT.getDoubles (reader, field, parser);
+ final double[] fieldOrder = FieldCache.DEFAULT.getDoubles (reader, field, parser);
return new ScoreDocComparator () {
public final int compare (final ScoreDoc i, final ScoreDoc j) {
@@ -511,7 +499,7 @@
static ScoreDocComparator comparatorAuto (final IndexReader reader, final String fieldname)
throws IOException {
final String field = fieldname.intern();
- Object lookupArray = ExtendedFieldCache.EXT_DEFAULT.getAuto (reader, field);
+ Object lookupArray = FieldCache.DEFAULT.getAuto (reader, field);
if (lookupArray instanceof FieldCache.StringIndex) {
return comparatorString (reader, field);
} else if (lookupArray instanceof int[]) {
Index: src/java/org/apache/lucene/search/function/ByteFieldSource.java
===================================================================
--- src/java/org/apache/lucene/search/function/ByteFieldSource.java (revision 786665)
+++ src/java/org/apache/lucene/search/function/ByteFieldSource.java (working copy)
@@ -44,7 +44,7 @@
* Create a cached byte field source with default string-to-byte parser.
*/
public ByteFieldSource(String field) {
- this(field, null);
+ this(field, FieldCache.DEFAULT_BYTE_PARSER);
}
/**
@@ -62,9 +62,7 @@
/*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#getCachedValues(org.apache.lucene.search.FieldCache, java.lang.String, org.apache.lucene.index.IndexReader) */
public DocValues getCachedFieldValues (FieldCache cache, String field, IndexReader reader) throws IOException {
- final byte[] arr = (parser==null) ?
- cache.getBytes(reader, field) :
- cache.getBytes(reader, field, parser);
+ final byte[] arr = cache.getBytes(reader, field, parser);
return new DocValues() {
/*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#floatVal(int) */
public float floatVal(int doc) {
Index: src/java/org/apache/lucene/search/function/FloatFieldSource.java
===================================================================
--- src/java/org/apache/lucene/search/function/FloatFieldSource.java (revision 786665)
+++ src/java/org/apache/lucene/search/function/FloatFieldSource.java (working copy)
@@ -45,7 +45,7 @@
* Create a cached float field source with default string-to-float parser.
*/
public FloatFieldSource(String field) {
- this(field, null);
+ this(field, FieldCache.DEFAULT_FLOAT_PARSER);
}
/**
@@ -63,9 +63,7 @@
/*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#getCachedValues(org.apache.lucene.search.FieldCache, java.lang.String, org.apache.lucene.index.IndexReader) */
public DocValues getCachedFieldValues (FieldCache cache, String field, IndexReader reader) throws IOException {
- final float[] arr = (parser==null) ?
- cache.getFloats(reader, field) :
- cache.getFloats(reader, field, parser);
+ final float[] arr = cache.getFloats(reader, field, parser);
return new DocValues() {
/*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#floatVal(int) */
public float floatVal(int doc) {
Index: src/java/org/apache/lucene/search/function/IntFieldSource.java
===================================================================
--- src/java/org/apache/lucene/search/function/IntFieldSource.java (revision 786665)
+++ src/java/org/apache/lucene/search/function/IntFieldSource.java (working copy)
@@ -46,7 +46,7 @@
* Create a cached int field source with default string-to-int parser.
*/
public IntFieldSource(String field) {
- this(field, null);
+ this(field, FieldCache.DEFAULT_INT_PARSER);
}
/**
@@ -64,9 +64,7 @@
/*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#getCachedValues(org.apache.lucene.search.FieldCache, java.lang.String, org.apache.lucene.index.IndexReader) */
public DocValues getCachedFieldValues (FieldCache cache, String field, IndexReader reader) throws IOException {
- final int[] arr = (parser==null) ?
- cache.getInts(reader, field) :
- cache.getInts(reader, field, parser);
+ final int[] arr = cache.getInts(reader, field, parser);
return new DocValues() {
/*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#floatVal(int) */
public float floatVal(int doc) {
Index: src/java/org/apache/lucene/search/function/ShortFieldSource.java
===================================================================
--- src/java/org/apache/lucene/search/function/ShortFieldSource.java (revision 786665)
+++ src/java/org/apache/lucene/search/function/ShortFieldSource.java (working copy)
@@ -44,7 +44,7 @@
* Create a cached short field source with default string-to-short parser.
*/
public ShortFieldSource(String field) {
- this(field, null);
+ this(field, FieldCache.DEFAULT_SHORT_PARSER);
}
/**
@@ -62,9 +62,7 @@
/*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#getCachedValues(org.apache.lucene.search.FieldCache, java.lang.String, org.apache.lucene.index.IndexReader) */
public DocValues getCachedFieldValues (FieldCache cache, String field, IndexReader reader) throws IOException {
- final short[] arr = (parser==null) ?
- cache.getShorts(reader, field) :
- cache.getShorts(reader, field, parser);
+ final short[] arr = cache.getShorts(reader, field, parser);
return new DocValues() {
/*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#floatVal(int) */
public float floatVal(int doc) {
Index: src/java/org/apache/lucene/search/NumericRangeFilter.java
===================================================================
--- src/java/org/apache/lucene/search/NumericRangeFilter.java (revision 786665)
+++ src/java/org/apache/lucene/search/NumericRangeFilter.java (working copy)
@@ -18,6 +18,7 @@
*/
import org.apache.lucene.analysis.NumericTokenStream; // for javadocs
+import org.apache.lucene.document.NumericField; // for javadocs
/**
* Implementation of a {@link Filter} that implements trie-based range filtering
@@ -25,7 +26,7 @@
* {@link NumericRangeQuery}.
*
*
This filter depends on a specific structure of terms in the index that can only be created - * by indexing using {@link NumericTokenStream}. + * by indexing using {@link NumericField} (expert: {@link NumericTokenStream}). * *
Please note: This class has no constructor, you can create filters depending on the data type * by using the static factories {@linkplain #newLongRange NumericRangeFilter.newLongRange()}, Index: src/java/org/apache/lucene/search/NumericRangeQuery.java =================================================================== --- src/java/org/apache/lucene/search/NumericRangeQuery.java (revision 786665) +++ src/java/org/apache/lucene/search/NumericRangeQuery.java (working copy) @@ -21,6 +21,7 @@ import java.util.LinkedList; import org.apache.lucene.analysis.NumericTokenStream; // for javadocs +import org.apache.lucene.document.NumericField; // for javadocs import org.apache.lucene.util.NumericUtils; import org.apache.lucene.util.ToStringUtils; import org.apache.lucene.index.IndexReader; @@ -33,12 +34,12 @@ *
precisionStep, which specifies,
* how many different precisions per numeric value are indexed to speed up range queries.
* Lower values create more terms but speed up search, higher values create less terms, but
* slow down search. Suitable values are 2, 4, or 8. A good starting point to test is 4.
- * For code examples see {@link NumericTokenStream}.
+ * For code examples see {@link NumericField}.
*
* This class has no constructor, you can create filters depending on the data type Index: src/java/org/apache/lucene/search/package.html =================================================================== --- src/java/org/apache/lucene/search/package.html (revision 786665) +++ src/java/org/apache/lucene/search/package.html (working copy) @@ -166,8 +166,8 @@ NumericRangeQuery matches all documents that occur in a numeric range. For NumericRangeQuery to work, you must index the values - using a special - NumericTokenStream. + using a special + NumericField.
null if no parser was specified. Sorting is using the default parser then.
- * @return An instance of a {@link FieldCache} parser, or null.
+ /** Returns the instance of a {@link FieldCache} parser that fits to the given numeric sort type.
+ * @return An instance of a {@link FieldCache} parser for numeric sorting, null otherwise.
*/
public FieldCache.Parser getParser() {
return parser;
@@ -400,7 +413,7 @@
}
if (locale != null) buffer.append('(').append(locale).append(')');
- if (parser != null) buffer.append('(').append(parser).append(')');
+ //if (parser != null) buffer.append('(').append(parser).append(')'); this typically only produces crap
if (reverse) buffer.append('!');
return buffer.toString();
Index: src/java/org/apache/lucene/util/NumericUtils.java
===================================================================
--- src/java/org/apache/lucene/util/NumericUtils.java (revision 786770)
+++ src/java/org/apache/lucene/util/NumericUtils.java (working copy)
@@ -20,9 +20,6 @@
import org.apache.lucene.analysis.NumericTokenStream; // for javadocs
import org.apache.lucene.search.NumericRangeQuery; // for javadocs
import org.apache.lucene.search.NumericRangeFilter; // for javadocs
-import org.apache.lucene.search.SortField;
-import org.apache.lucene.search.FieldCache;
-import org.apache.lucene.search.ExtendedFieldCache;
/**
* This is a helper class to generate prefix-encoded representations for numerical values
@@ -57,9 +54,6 @@
* {@link String#compareTo(String)}) representations of numeric data types for other
* usages (e.g. sorting).
*
- * Prefix encoded fields can also be sorted using the {@link SortField} factories
- * {@link #getLongSortField}, {@link #getIntSortField}, {@link #getDoubleSortField}
- * or {@link #getFloatSortField}.
* @since 2.9
*/
public final class NumericUtils {
@@ -93,56 +87,6 @@
public static final int INT_BUF_SIZE = 31/7 + 2;
/**
- * A parser instance for filling a {@link ExtendedFieldCache}, that parses prefix encoded fields as longs.
- */
- public static final ExtendedFieldCache.LongParser FIELD_CACHE_LONG_PARSER=new ExtendedFieldCache.LongParser(){
- public final long parseLong(final String val) {
- final int shift = val.charAt(0)-SHIFT_START_LONG;
- if (shift>0 && shift<=63)
- throw new FieldCache.StopFillCacheException();
- return prefixCodedToLong(val);
- }
- };
-
- /**
- * A parser instance for filling a {@link FieldCache}, that parses prefix encoded fields as ints.
- */
- public static final FieldCache.IntParser FIELD_CACHE_INT_PARSER=new FieldCache.IntParser(){
- public final int parseInt(final String val) {
- final int shift = val.charAt(0)-SHIFT_START_INT;
- if (shift>0 && shift<=31)
- throw new FieldCache.StopFillCacheException();
- return prefixCodedToInt(val);
- }
- };
-
- /**
- * A parser instance for filling a {@link ExtendedFieldCache}, that parses prefix encoded fields as doubles.
- * This uses {@link #sortableLongToDouble} to convert the encoded long to a double.
- */
- public static final ExtendedFieldCache.DoubleParser FIELD_CACHE_DOUBLE_PARSER=new ExtendedFieldCache.DoubleParser(){
- public final double parseDouble(final String val) {
- final int shift = val.charAt(0)-SHIFT_START_LONG;
- if (shift>0 && shift<=63)
- throw new FieldCache.StopFillCacheException();
- return sortableLongToDouble(prefixCodedToLong(val));
- }
- };
-
- /**
- * A parser instance for filling a {@link FieldCache}, that parses prefix encoded fields as floats.
- * This uses {@link #sortableIntToFloat} to convert the encoded int to a float.
- */
- public static final FieldCache.FloatParser FIELD_CACHE_FLOAT_PARSER=new FieldCache.FloatParser(){
- public final float parseFloat(final String val) {
- final int shift = val.charAt(0)-SHIFT_START_INT;
- if (shift>0 && shift<=31)
- throw new FieldCache.StopFillCacheException();
- return sortableIntToFloat(prefixCodedToInt(val));
- }
- };
-
- /**
* Expert: Returns prefix coded bits after reducing the precision by shift bits.
* This is method is used by {@link NumericTokenStream}.
* @param val the numeric value
@@ -152,6 +96,8 @@
* @return number of chars written to buffer
*/
public static int longToPrefixCoded(final long val, final int shift, final char[] buffer) {
+ if (shift>63 || shift<0)
+ throw new IllegalArgumentException("Illegal shift value, must be 0..63");
int nChars = (63-shift)/7 + 1, len = nChars+1;
buffer[0] = (char)(SHIFT_START_LONG + shift);
long sortableBits = val ^ 0x8000000000000000L;
@@ -173,8 +119,6 @@
* @param shift how many bits to strip from the right
*/
public static String longToPrefixCoded(final long val, final int shift) {
- if (shift>63 || shift<0)
- throw new IllegalArgumentException("Illegal shift value, must be 0..63");
final char[] buffer = new char[LONG_BUF_SIZE];
final int len = longToPrefixCoded(val, shift, buffer);
return new String(buffer, 0, len);
@@ -200,6 +144,8 @@
* @return number of chars written to buffer
*/
public static int intToPrefixCoded(final int val, final int shift, final char[] buffer) {
+ if (shift>31 || shift<0)
+ throw new IllegalArgumentException("Illegal shift value, must be 0..31");
int nChars = (31-shift)/7 + 1, len = nChars+1;
buffer[0] = (char)(SHIFT_START_INT + shift);
int sortableBits = val ^ 0x80000000;
@@ -221,8 +167,6 @@
* @param shift how many bits to strip from the right
*/
public static String intToPrefixCoded(final int val, final int shift) {
- if (shift>31 || shift<0)
- throw new IllegalArgumentException("Illegal shift value, must be 0..31");
final char[] buffer = new char[INT_BUF_SIZE];
final int len = intToPrefixCoded(val, shift, buffer);
return new String(buffer, 0, len);
@@ -336,26 +280,6 @@
return Float.intBitsToFloat(val);
}
- /** A factory method, that generates a {@link SortField} instance for sorting prefix encoded long values. */
- public static SortField getLongSortField(final String field, final boolean reverse) {
- return new SortField(field, FIELD_CACHE_LONG_PARSER, reverse);
- }
-
- /** A factory method, that generates a {@link SortField} instance for sorting prefix encoded int values. */
- public static SortField getIntSortField(final String field, final boolean reverse) {
- return new SortField(field, FIELD_CACHE_INT_PARSER, reverse);
- }
-
- /** A factory method, that generates a {@link SortField} instance for sorting prefix encoded double values. */
- public static SortField getDoubleSortField(final String field, final boolean reverse) {
- return new SortField(field, FIELD_CACHE_DOUBLE_PARSER, reverse);
- }
-
- /** A factory method, that generates a {@link SortField} instance for sorting prefix encoded float values. */
- public static SortField getFloatSortField(final String field, final boolean reverse) {
- return new SortField(field, FIELD_CACHE_FLOAT_PARSER, reverse);
- }
-
/**
* Expert: Splits a long range recursively.
* You may implement a builder that adds clauses to a
Index: src/test/org/apache/lucene/search/JustCompileSearch.java
===================================================================
--- src/test/org/apache/lucene/search/JustCompileSearch.java (revision 786665)
+++ src/test/org/apache/lucene/search/JustCompileSearch.java (working copy)
@@ -203,158 +203,15 @@
}
}
- static final class JustCompileFieldCache implements FieldCache {
+ static final class JustCompileExtendedFieldCacheLongParser implements FieldCache.LongParser {
- public Object getAuto(IndexReader reader, String field) throws IOException {
- throw new UnsupportedOperationException(UNSUPPORTED_MSG);
- }
-
- public byte[] getBytes(IndexReader reader, String field) throws IOException {
- throw new UnsupportedOperationException(UNSUPPORTED_MSG);
- }
-
- public byte[] getBytes(IndexReader reader, String field, ByteParser parser)
- throws IOException {
- throw new UnsupportedOperationException(UNSUPPORTED_MSG);
- }
-
- /** @deprecated */
- public Comparable[] getCustom(IndexReader reader, String field,
- SortComparator comparator) throws IOException {
- throw new UnsupportedOperationException(UNSUPPORTED_MSG);
- }
-
- public float[] getFloats(IndexReader reader, String field)
- throws IOException {
- throw new UnsupportedOperationException(UNSUPPORTED_MSG);
- }
-
- public float[] getFloats(IndexReader reader, String field,
- FloatParser parser) throws IOException {
- throw new UnsupportedOperationException(UNSUPPORTED_MSG);
- }
-
- public int[] getInts(IndexReader reader, String field) throws IOException {
- throw new UnsupportedOperationException(UNSUPPORTED_MSG);
- }
-
- public int[] getInts(IndexReader reader, String field, IntParser parser)
- throws IOException {
- throw new UnsupportedOperationException(UNSUPPORTED_MSG);
- }
-
- public short[] getShorts(IndexReader reader, String field)
- throws IOException {
- throw new UnsupportedOperationException(UNSUPPORTED_MSG);
- }
-
- public short[] getShorts(IndexReader reader, String field,
- ShortParser parser) throws IOException {
- throw new UnsupportedOperationException(UNSUPPORTED_MSG);
- }
-
- public StringIndex getStringIndex(IndexReader reader, String field)
- throws IOException {
- throw new UnsupportedOperationException(UNSUPPORTED_MSG);
- }
-
- public String[] getStrings(IndexReader reader, String field)
- throws IOException {
- throw new UnsupportedOperationException(UNSUPPORTED_MSG);
- }
-
- }
-
- static final class JustCompileExtendedFieldCache implements ExtendedFieldCache {
-
- public double[] getDoubles(IndexReader reader, String field)
- throws IOException {
- throw new UnsupportedOperationException(UNSUPPORTED_MSG);
- }
-
- public double[] getDoubles(IndexReader reader, String field,
- DoubleParser parser) throws IOException {
- throw new UnsupportedOperationException(UNSUPPORTED_MSG);
- }
-
- public long[] getLongs(IndexReader reader, String field) throws IOException {
- throw new UnsupportedOperationException(UNSUPPORTED_MSG);
- }
-
- public long[] getLongs(IndexReader reader, String field, LongParser parser)
- throws IOException {
- throw new UnsupportedOperationException(UNSUPPORTED_MSG);
- }
-
- public Object getAuto(IndexReader reader, String field) throws IOException {
- throw new UnsupportedOperationException(UNSUPPORTED_MSG);
- }
-
- public byte[] getBytes(IndexReader reader, String field) throws IOException {
- throw new UnsupportedOperationException(UNSUPPORTED_MSG);
- }
-
- public byte[] getBytes(IndexReader reader, String field, ByteParser parser)
- throws IOException {
- throw new UnsupportedOperationException(UNSUPPORTED_MSG);
- }
-
- /** @deprecated */
- public Comparable[] getCustom(IndexReader reader, String field,
- SortComparator comparator) throws IOException {
- throw new UnsupportedOperationException(UNSUPPORTED_MSG);
- }
-
- public float[] getFloats(IndexReader reader, String field)
- throws IOException {
- throw new UnsupportedOperationException(UNSUPPORTED_MSG);
- }
-
- public float[] getFloats(IndexReader reader, String field,
- FloatParser parser) throws IOException {
- throw new UnsupportedOperationException(UNSUPPORTED_MSG);
- }
-
- public int[] getInts(IndexReader reader, String field) throws IOException {
- throw new UnsupportedOperationException(UNSUPPORTED_MSG);
- }
-
- public int[] getInts(IndexReader reader, String field, IntParser parser)
- throws IOException {
- throw new UnsupportedOperationException(UNSUPPORTED_MSG);
- }
-
- public short[] getShorts(IndexReader reader, String field)
- throws IOException {
- throw new UnsupportedOperationException(UNSUPPORTED_MSG);
- }
-
- public short[] getShorts(IndexReader reader, String field,
- ShortParser parser) throws IOException {
- throw new UnsupportedOperationException(UNSUPPORTED_MSG);
- }
-
- public StringIndex getStringIndex(IndexReader reader, String field)
- throws IOException {
- throw new UnsupportedOperationException(UNSUPPORTED_MSG);
- }
-
- public String[] getStrings(IndexReader reader, String field)
- throws IOException {
- throw new UnsupportedOperationException(UNSUPPORTED_MSG);
- }
-
- }
-
- static final class JustCompileExtendedFieldCacheLongParser implements ExtendedFieldCache.LongParser {
-
public long parseLong(String string) {
throw new UnsupportedOperationException(UNSUPPORTED_MSG);
}
}
- static final class JustCompileExtendedFieldCacheDoubleParser implements ExtendedFieldCache.DoubleParser {
+ static final class JustCompileExtendedFieldCacheDoubleParser implements FieldCache.DoubleParser {
public double parseDouble(String string) {
throw new UnsupportedOperationException(UNSUPPORTED_MSG);
Index: src/test/org/apache/lucene/search/TestExtendedFieldCache.java
===================================================================
--- src/test/org/apache/lucene/search/TestExtendedFieldCache.java (revision 786665)
+++ src/test/org/apache/lucene/search/TestExtendedFieldCache.java (working copy)
@@ -1,71 +0,0 @@
-package org.apache.lucene.search;
-
-/**
- * Copyright 2004 The Apache Software Foundation
- *
- * Licensed 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.
- */
-
-import org.apache.lucene.analysis.WhitespaceAnalyzer;
-import org.apache.lucene.document.Document;
-import org.apache.lucene.document.Field;
-import org.apache.lucene.index.IndexReader;
-import org.apache.lucene.index.IndexWriter;
-import org.apache.lucene.store.RAMDirectory;
-import org.apache.lucene.util.English;
-import org.apache.lucene.util.LuceneTestCase;
-
-import java.io.IOException;
-
-public class TestExtendedFieldCache extends LuceneTestCase {
- protected IndexReader reader;
- private static final int NUM_DOCS = 1000;
-
- public TestExtendedFieldCache(String s) {
- super(s);
- }
-
- protected void setUp() throws Exception {
- super.setUp();
- RAMDirectory directory = new RAMDirectory();
- IndexWriter writer= new IndexWriter(directory, new WhitespaceAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);
- long theLong = Long.MAX_VALUE;
- double theDouble = Double.MAX_VALUE;
- for (int i = 0; i < NUM_DOCS; i++){
- Document doc = new Document();
- doc.add(new Field("theLong", String.valueOf(theLong--), Field.Store.NO, Field.Index.NOT_ANALYZED));
- doc.add(new Field("theDouble", String.valueOf(theDouble--), Field.Store.NO, Field.Index.NOT_ANALYZED));
- doc.add(new Field("text", English.intToEnglish(i), Field.Store.NO, Field.Index.ANALYZED));
- writer.addDocument(doc);
- }
- writer.close();
- reader = IndexReader.open(directory);
- }
-
-
- public void test() throws IOException {
- ExtendedFieldCache cache = new ExtendedFieldCacheImpl();
- double [] doubles = cache.getDoubles(reader, "theDouble");
- assertTrue("doubles Size: " + doubles.length + " is not: " + NUM_DOCS, doubles.length == NUM_DOCS);
- for (int i = 0; i < doubles.length; i++) {
- assertTrue(doubles[i] + " does not equal: " + (Double.MAX_VALUE - i), doubles[i] == (Double.MAX_VALUE - i));
-
- }
- long [] longs = cache.getLongs(reader, "theLong");
- assertTrue("longs Size: " + longs.length + " is not: " + NUM_DOCS, longs.length == NUM_DOCS);
- for (int i = 0; i < longs.length; i++) {
- assertTrue(longs[i] + " does not equal: " + (Long.MAX_VALUE - i), longs[i] == (Long.MAX_VALUE - i));
-
- }
- }
-}
\ No newline at end of file
Index: src/test/org/apache/lucene/search/TestFieldCache.java
===================================================================
--- src/test/org/apache/lucene/search/TestFieldCache.java (revision 0)
+++ src/test/org/apache/lucene/search/TestFieldCache.java (revision 0)
@@ -0,0 +1,101 @@
+package org.apache.lucene.search;
+
+/**
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed 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.
+ */
+
+import org.apache.lucene.analysis.WhitespaceAnalyzer;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.store.RAMDirectory;
+import org.apache.lucene.util.LuceneTestCase;
+
+import java.io.IOException;
+
+public class TestFieldCache extends LuceneTestCase {
+ protected IndexReader reader;
+ private static final int NUM_DOCS = 1000;
+
+ public TestFieldCache(String s) {
+ super(s);
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ RAMDirectory directory = new RAMDirectory();
+ IndexWriter writer= new IndexWriter(directory, new WhitespaceAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);
+ long theLong = Long.MAX_VALUE;
+ double theDouble = Double.MAX_VALUE;
+ byte theByte = Byte.MAX_VALUE;
+ short theShort = Short.MAX_VALUE;
+ int theInt = Integer.MAX_VALUE;
+ float theFloat = Float.MAX_VALUE;
+ for (int i = 0; i < NUM_DOCS; i++){
+ Document doc = new Document();
+ doc.add(new Field("theLong", String.valueOf(theLong--), Field.Store.NO, Field.Index.NOT_ANALYZED));
+ doc.add(new Field("theDouble", String.valueOf(theDouble--), Field.Store.NO, Field.Index.NOT_ANALYZED));
+ doc.add(new Field("theByte", String.valueOf(theByte--), Field.Store.NO, Field.Index.NOT_ANALYZED));
+ doc.add(new Field("theShort", String.valueOf(theShort--), Field.Store.NO, Field.Index.NOT_ANALYZED));
+ doc.add(new Field("theInt", String.valueOf(theInt--), Field.Store.NO, Field.Index.NOT_ANALYZED));
+ doc.add(new Field("theFloat", String.valueOf(theFloat--), Field.Store.NO, Field.Index.NOT_ANALYZED));
+ writer.addDocument(doc);
+ }
+ writer.close();
+ reader = IndexReader.open(directory);
+ }
+
+
+ public void test() throws IOException {
+ FieldCache cache = FieldCache.DEFAULT;
+ double [] doubles = cache.getDoubles(reader, "theDouble");
+ assertTrue("doubles Size: " + doubles.length + " is not: " + NUM_DOCS, doubles.length == NUM_DOCS);
+ for (int i = 0; i < doubles.length; i++) {
+ assertTrue(doubles[i] + " does not equal: " + (Double.MAX_VALUE - i), doubles[i] == (Double.MAX_VALUE - i));
+
+ }
+ long [] longs = cache.getLongs(reader, "theLong");
+ assertTrue("longs Size: " + longs.length + " is not: " + NUM_DOCS, longs.length == NUM_DOCS);
+ for (int i = 0; i < longs.length; i++) {
+ assertTrue(longs[i] + " does not equal: " + (Long.MAX_VALUE - i), longs[i] == (Long.MAX_VALUE - i));
+
+ }
+ byte [] bytes = cache.getBytes(reader, "theByte");
+ assertTrue("bytes Size: " + bytes.length + " is not: " + NUM_DOCS, bytes.length == NUM_DOCS);
+ for (int i = 0; i < bytes.length; i++) {
+ assertTrue(bytes[i] + " does not equal: " + (Byte.MAX_VALUE - i), bytes[i] == (byte) (Byte.MAX_VALUE - i));
+
+ }
+ short [] shorts = cache.getShorts(reader, "theShort");
+ assertTrue("shorts Size: " + shorts.length + " is not: " + NUM_DOCS, shorts.length == NUM_DOCS);
+ for (int i = 0; i < shorts.length; i++) {
+ assertTrue(shorts[i] + " does not equal: " + (Short.MAX_VALUE - i), shorts[i] == (short) (Short.MAX_VALUE - i));
+
+ }
+ int [] ints = cache.getInts(reader, "theInt");
+ assertTrue("ints Size: " + ints.length + " is not: " + NUM_DOCS, ints.length == NUM_DOCS);
+ for (int i = 0; i < ints.length; i++) {
+ assertTrue(ints[i] + " does not equal: " + (Integer.MAX_VALUE - i), ints[i] == (Integer.MAX_VALUE - i));
+
+ }
+ float [] floats = cache.getFloats(reader, "theFloat");
+ assertTrue("floats Size: " + floats.length + " is not: " + NUM_DOCS, floats.length == NUM_DOCS);
+ for (int i = 0; i < floats.length; i++) {
+ assertTrue(floats[i] + " does not equal: " + (Float.MAX_VALUE - i), floats[i] == (Float.MAX_VALUE - i));
+
+ }
+ }
+}
\ No newline at end of file
Property changes on: src\test\org\apache\lucene\search\TestFieldCache.java
___________________________________________________________________
Added: svn:eol-style
+ native
Index: src/test/org/apache/lucene/search/TestNumericRangeQuery32.java
===================================================================
--- src/test/org/apache/lucene/search/TestNumericRangeQuery32.java (revision 786665)
+++ src/test/org/apache/lucene/search/TestNumericRangeQuery32.java (working copy)
@@ -19,12 +19,14 @@
import java.util.Random;
-import org.apache.lucene.analysis.NumericTokenStream;
import org.apache.lucene.analysis.WhitespaceAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
+import org.apache.lucene.document.NumericField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
+import org.apache.lucene.search.SortField;
+import org.apache.lucene.search.FieldCache;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.NumericUtils;
@@ -37,15 +39,6 @@
// number of docs to generate for testing
private static final int noDocs = 10000;
- private static Field newField(String name, int precisionStep) {
- NumericTokenStream stream = new NumericTokenStream(precisionStep);
- stream.setUseNewAPI(true);
- Field f=new Field(name, stream);
- f.setOmitTermFreqAndPositions(true);
- f.setOmitNorms(true);
- return f;
- }
-
private static final RAMDirectory directory;
private static final IndexSearcher searcher;
static {
@@ -57,34 +50,31 @@
IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(),
true, MaxFieldLength.UNLIMITED);
- Field
- field8 = newField("field8", 8),
- field4 = newField("field4", 4),
- field2 = newField("field2", 2),
- ascfield8 = newField("ascfield8", 8),
- ascfield4 = newField("ascfield4", 4),
- ascfield2 = newField("ascfield2", 2);
+ NumericField
+ field8 = new NumericField("field8", 8, Field.Store.YES, true),
+ field4 = new NumericField("field4", 4, Field.Store.YES, true),
+ field2 = new NumericField("field2", 2, Field.Store.YES, true),
+ ascfield8 = new NumericField("ascfield8", 8, Field.Store.NO, true),
+ ascfield4 = new NumericField("ascfield4", 4, Field.Store.NO, true),
+ ascfield2 = new NumericField("ascfield2", 2, Field.Store.NO, true);
+ Document doc = new Document();
+ // add fields, that have a distance to test general functionality
+ doc.add(field8); doc.add(field4); doc.add(field2);
+ // add ascending fields with a distance of 1, beginning at -noDocs/2 to test the correct splitting of range and inclusive/exclusive
+ doc.add(ascfield8); doc.add(ascfield4); doc.add(ascfield2);
+
// Add a series of noDocs docs with increasing int values
for (int l=0; l