Skip to content

Apache Lucene open-source search software

Add IntParser and FloatParser interfaces to FieldCache.

Doug Cutting4ade1d46064c
diff --git a/CHANGES.txt b/CHANGES.txt
index 5061f725c1e..ed45e921422 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -109,6 +109,11 @@ New features
16. Add ParallelReader, an IndexReader that combines separate indexes
over different fields into a single virtual index. (Doug Cutting)
+17. Add IntParser and FloatParser interfaces to FieldCache, so that
+ fields in arbitrarily formats can be cached as ints and floats.
+ (Doug Cutting)
+
+
API Changes
1. Several methods and fields have been deprecated. The API documentation
diff --git a/src/java/org/apache/lucene/search/FieldCache.java b/src/java/org/apache/lucene/search/FieldCache.java
index 276eb605390..510297a4ea8 100644
--- a/src/java/org/apache/lucene/search/FieldCache.java
+++ b/src/java/org/apache/lucene/search/FieldCache.java
@@ -52,6 +52,22 @@ public interface FieldCache {
}
}
+ /** Interface to parse ints from document fields.
+ * @see #getInts(IndexReader, String, IntParser)
+ */
+ public interface IntParser {
+ /** Return an integer representation of this field's value. */
+ public int parseInt(String string);
+ }
+
+
+ /** Interface to parse floats from document fields.
+ * @see #getFloats(IndexReader, String, FloatParser)
+ */
+ public interface FloatParser {
+ /** Return an float representation of this field's value. */
+ public float parseFloat(String string);
+ }
/** Expert: The cache used internally by sorting and range query classes. */
public static FieldCache DEFAULT = new FieldCacheImpl();
@@ -69,6 +85,19 @@ public interface FieldCache {
public int[] getInts (IndexReader reader, String field)
throws IOException;
+ /** Checks the internal cache for an appropriate entry, and if none is found,
+ * reads the terms in <code>field</code> as integers and returns an array of
+ * size <code>reader.maxDoc()</code> of the value each document has in the
+ * given field.
+ * @param reader Used to get field values.
+ * @param field Which field contains the integers.
+ * @param parser Computes integer for string values.
+ * @return The values in the given field for each document.
+ * @throws IOException If any error occurs.
+ */
+ public int[] getInts (IndexReader reader, String field, IntParser parser)
+ throws IOException;
+
/** Checks the internal cache for an appropriate entry, and if
* none is found, reads the terms in <code>field</code> as floats and returns an array
* of size <code>reader.maxDoc()</code> of the value each document
@@ -81,6 +110,19 @@ public interface FieldCache {
public float[] getFloats (IndexReader reader, String field)
throws IOException;
+ /** Checks the internal cache for an appropriate entry, and if
+ * none is found, reads the terms in <code>field</code> as floats and returns an array
+ * of size <code>reader.maxDoc()</code> of the value each document
+ * has in the given field.
+ * @param reader Used to get field values.
+ * @param field Which field contains the floats.
+ * @param parser Computes float for string values.
+ * @return The values in the given field for each document.
+ * @throws IOException If any error occurs.
+ */
+ 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 term values in <code>field</code> and returns an array
* of size <code>reader.maxDoc()</code> containing the value each document
diff --git a/src/java/org/apache/lucene/search/FieldCacheImpl.java b/src/java/org/apache/lucene/search/FieldCacheImpl.java
index dfa28df64eb..af8e6613562 100644
--- a/src/java/org/apache/lucene/search/FieldCacheImpl.java
+++ b/src/java/org/apache/lucene/search/FieldCacheImpl.java
@@ -81,6 +81,17 @@ implements FieldCache {
}
}
+ 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);
+ }
+ };
/** The internal cache. Maps Entry to array of interpreted term values. **/
final Map cache = new WeakHashMap();
@@ -132,10 +143,15 @@ implements FieldCache {
}
// inherit javadocs
- public int[] getInts (IndexReader reader, String field)
+ public int[] getInts (IndexReader reader, String field) throws IOException {
+ return getInts(reader, field, INT_PARSER);
+ }
+
+ // inherit javadocs
+ public int[] getInts (IndexReader reader, String field, IntParser parser)
throws IOException {
field = field.intern();
- Object ret = lookup (reader, field, SortField.INT);
+ Object ret = lookup (reader, field, parser);
if (ret == null) {
final int[] retArray = new int[reader.maxDoc()];
if (retArray.length > 0) {
@@ -159,7 +175,7 @@ implements FieldCache {
termEnum.close();
}
}
- store (reader, field, SortField.INT, retArray);
+ store (reader, field, parser, retArray);
return retArray;
}
return (int[]) ret;
@@ -167,9 +183,15 @@ implements FieldCache {
// inherit javadocs
public float[] getFloats (IndexReader reader, String field)
- throws IOException {
+ throws IOException {
+ return getFloats(reader, field, FLOAT_PARSER);
+ }
+
+ // inherit javadocs
+ public float[] getFloats (IndexReader reader, String field,
+ FloatParser parser) throws IOException {
field = field.intern();
- Object ret = lookup (reader, field, SortField.FLOAT);
+ Object ret = lookup (reader, field, parser);
if (ret == null) {
final float[] retArray = new float[reader.maxDoc()];
if (retArray.length > 0) {
@@ -193,7 +215,7 @@ implements FieldCache {
termEnum.close();
}
}
- store (reader, field, SortField.FLOAT, retArray);
+ store (reader, field, parser, retArray);
return retArray;
}
return (float[]) ret;
@@ -388,7 +410,7 @@ implements FieldCache {
termEnum.close();
}
}
- store (reader, field, SortField.CUSTOM, retArray);
+ store (reader, field, comparator, retArray);
return retArray;
}
return (Comparable[]) ret;