001: package org.apache.lucene.search;
002:
003: import org.apache.lucene.index.IndexReader;
004: import org.apache.lucene.index.Term;
005: import org.apache.lucene.index.TermDocs;
006: import org.apache.lucene.index.TermEnum;
007:
008: import java.io.IOException;
009:
010: /**
011: *
012: *
013: **/
014: class ExtendedFieldCacheImpl extends FieldCacheImpl implements
015: ExtendedFieldCache {
016: private static final LongParser LONG_PARSER = new LongParser() {
017: public long parseLong(String value) {
018: return Long.parseLong(value);
019: }
020: };
021:
022: private static final DoubleParser DOUBLE_PARSER = new DoubleParser() {
023: public double parseDouble(String value) {
024: return Double.parseDouble(value);
025: }
026: };
027:
028: public long[] getLongs(IndexReader reader, String field)
029: throws IOException {
030: return getLongs(reader, field, LONG_PARSER);
031: }
032:
033: // inherit javadocs
034: public long[] getLongs(IndexReader reader, String field,
035: LongParser parser) throws IOException {
036: return (long[]) longsCache
037: .get(reader, new Entry(field, parser));
038: }
039:
040: Cache longsCache = new Cache() {
041:
042: protected Object createValue(IndexReader reader, Object entryKey)
043: throws IOException {
044: Entry entry = (Entry) entryKey;
045: String field = entry.field;
046: LongParser parser = (LongParser) entry.custom;
047: final long[] retArray = new long[reader.maxDoc()];
048: TermDocs termDocs = reader.termDocs();
049: TermEnum termEnum = reader.terms(new Term(field, ""));
050: try {
051: do {
052: Term term = termEnum.term();
053: if (term == null || term.field() != field)
054: break;
055: long termval = parser.parseLong(term.text());
056: termDocs.seek(termEnum);
057: while (termDocs.next()) {
058: retArray[termDocs.doc()] = termval;
059: }
060: } while (termEnum.next());
061: } finally {
062: termDocs.close();
063: termEnum.close();
064: }
065: return retArray;
066: }
067: };
068:
069: // inherit javadocs
070: public double[] getDoubles(IndexReader reader, String field)
071: throws IOException {
072: return getDoubles(reader, field, DOUBLE_PARSER);
073: }
074:
075: // inherit javadocs
076: public double[] getDoubles(IndexReader reader, String field,
077: DoubleParser parser) throws IOException {
078: return (double[]) doublesCache.get(reader, new Entry(field,
079: parser));
080: }
081:
082: Cache doublesCache = new Cache() {
083:
084: protected Object createValue(IndexReader reader, Object entryKey)
085: throws IOException {
086: Entry entry = (Entry) entryKey;
087: String field = entry.field;
088: DoubleParser parser = (DoubleParser) entry.custom;
089: final double[] retArray = new double[reader.maxDoc()];
090: TermDocs termDocs = reader.termDocs();
091: TermEnum termEnum = reader.terms(new Term(field, ""));
092: try {
093: do {
094: Term term = termEnum.term();
095: if (term == null || term.field() != field)
096: break;
097: double termval = parser.parseDouble(term.text());
098: termDocs.seek(termEnum);
099: while (termDocs.next()) {
100: retArray[termDocs.doc()] = termval;
101: }
102: } while (termEnum.next());
103: } finally {
104: termDocs.close();
105: termEnum.close();
106: }
107: return retArray;
108: }
109: };
110:
111: // inherit javadocs
112: public Object getAuto(IndexReader reader, String field)
113: throws IOException {
114: return autoCache.get(reader, field);
115: }
116:
117: Cache autoCache = new Cache() {
118:
119: protected Object createValue(IndexReader reader, Object fieldKey)
120: throws IOException {
121: String field = ((String) fieldKey).intern();
122: TermEnum enumerator = reader.terms(new Term(field, ""));
123: try {
124: Term term = enumerator.term();
125: if (term == null) {
126: throw new RuntimeException("no terms in field "
127: + field + " - cannot determine sort type");
128: }
129: Object ret = null;
130: if (term.field() == field) {
131: String termtext = term.text().trim();
132:
133: /**
134: * Java 1.4 level code:
135:
136: if (pIntegers.matcher(termtext).matches())
137: return IntegerSortedHitQueue.comparator (reader, enumerator, field);
138:
139: else if (pFloats.matcher(termtext).matches())
140: return FloatSortedHitQueue.comparator (reader, enumerator, field);
141: */
142:
143: // Java 1.3 level code:
144: try {
145: Integer.parseInt(termtext);
146: ret = getInts(reader, field);
147: } catch (NumberFormatException nfe1) {
148: try {
149: Long.parseLong(termtext);
150: ret = getLongs(reader, field);
151: } catch (NumberFormatException nfe2) {
152: try {
153: Float.parseFloat(termtext);
154: ret = getFloats(reader, field);
155: } catch (NumberFormatException nfe3) {
156: ret = getStringIndex(reader, field);
157: }
158: }
159: }
160: } else {
161: throw new RuntimeException("field \"" + field
162: + "\" does not appear to be indexed");
163: }
164: return ret;
165: } finally {
166: enumerator.close();
167: }
168: }
169: };
170:
171: }
|