From 516143dea0cfa827a538373ddf88bbefda65acf0 Mon Sep 17 00:00:00 2001 From: Grant Ingersoll Date: Tue, 18 Dec 2007 15:13:05 +0000 Subject: [PATCH] LUCENE-1045: Applied original LUCENE-1045.patch that refactors original 1045 patch to use ExtendedFieldCache and DOES NOT make FieldCache a class git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@605225 13f79535-47bb-0310-9956-ffa450edef68 --- .../lucene/search/ExtendedFieldCacheImpl.java | 70 ++++++++++++++++--- .../apache/lucene/search/FieldCacheImpl.java | 51 +------------- .../lucene/search/FieldSortedHitQueue.java | 2 +- 3 files changed, 61 insertions(+), 62 deletions(-) diff --git a/src/java/org/apache/lucene/search/ExtendedFieldCacheImpl.java b/src/java/org/apache/lucene/search/ExtendedFieldCacheImpl.java index 256f113c94b..a189b266d76 100644 --- a/src/java/org/apache/lucene/search/ExtendedFieldCacheImpl.java +++ b/src/java/org/apache/lucene/search/ExtendedFieldCacheImpl.java @@ -25,17 +25,6 @@ class ExtendedFieldCacheImpl extends FieldCacheImpl implements ExtendedFieldCach } }; - private static final ByteParser BYTE_PARSER = new ByteParser() { - public byte parseByte(String string) { - return Byte.parseByte(string); - } - }; - - private static final ShortParser SHORT_PARSER = new ShortParser() { - public short parseShort(String string) { - return Short.parseShort(string); - } - }; public long[] getLongs(IndexReader reader, String field) throws IOException { return getLongs(reader, field, LONG_PARSER); @@ -114,4 +103,63 @@ class ExtendedFieldCacheImpl extends FieldCacheImpl implements ExtendedFieldCach 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(); + } + } + }; + } diff --git a/src/java/org/apache/lucene/search/FieldCacheImpl.java b/src/java/org/apache/lucene/search/FieldCacheImpl.java index 65ca40ef6ea..4ac9f91c8e7 100644 --- a/src/java/org/apache/lucene/search/FieldCacheImpl.java +++ b/src/java/org/apache/lucene/search/FieldCacheImpl.java @@ -21,13 +21,12 @@ 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 org.apache.lucene.search.ExtendedFieldCache.LongParser; import java.io.IOException; +import java.util.HashMap; import java.util.Locale; import java.util.Map; import java.util.WeakHashMap; -import java.util.HashMap; /** * Expert: The default cache implementation, storing all values in memory. @@ -150,11 +149,6 @@ implements FieldCache { } }; - private static final LongParser LONG_PARSER = new LongParser() { - public long parseLong(String value) { - return Long.parseLong(value); - } - }; private static final FloatParser FLOAT_PARSER = new FloatParser() { public float parseFloat(String value) { @@ -279,44 +273,6 @@ implements FieldCache { } }; - // inherit javadocs - 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()); - } finally { - termDocs.close(); - termEnum.close(); - } - return retArray; - } - }; // inherit javadocs public float[] getFloats (IndexReader reader, String field) @@ -501,16 +457,11 @@ implements FieldCache { 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"); diff --git a/src/java/org/apache/lucene/search/FieldSortedHitQueue.java b/src/java/org/apache/lucene/search/FieldSortedHitQueue.java index aee8b3de39e..a1d56ea20da 100644 --- a/src/java/org/apache/lucene/search/FieldSortedHitQueue.java +++ b/src/java/org/apache/lucene/search/FieldSortedHitQueue.java @@ -417,7 +417,7 @@ extends PriorityQueue { static ScoreDocComparator comparatorAuto (final IndexReader reader, final String fieldname) throws IOException { final String field = fieldname.intern(); - Object lookupArray = FieldCache.DEFAULT.getAuto (reader, field); + Object lookupArray = ExtendedFieldCache.EXT_DEFAULT.getAuto (reader, field); if (lookupArray instanceof FieldCache.StringIndex) { return comparatorString (reader, field); } else if (lookupArray instanceof int[]) {