001: package org.apache.lucene.search;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import org.apache.lucene.index.IndexReader;
021: import java.io.IOException;
022:
023: /**
024: * Expert: Maintains caches of term values.
025: *
026: * <p>Created: May 19, 2004 11:13:14 AM
027: *
028: * @author Tim Jones (Nacimiento Software)
029: * @since lucene 1.4
030: * @version $Id: FieldCache.java 544546 2007-06-05 16:29:35Z doronc $
031: */
032: public interface FieldCache {
033:
034: /** Indicator for StringIndex values in the cache. */
035: // NOTE: the value assigned to this constant must not be
036: // the same as any of those in SortField!!
037: public static final int STRING_INDEX = -1;
038:
039: /** Expert: Stores term text values and document ordering data. */
040: public static class StringIndex {
041:
042: /** All the term values, in natural order. */
043: public final String[] lookup;
044:
045: /** For each document, an index into the lookup array. */
046: public final int[] order;
047:
048: /** Creates one of these objects */
049: public StringIndex(int[] values, String[] lookup) {
050: this .order = values;
051: this .lookup = lookup;
052: }
053: }
054:
055: /** Interface to parse bytes from document fields.
056: * @see FieldCache#getBytes(IndexReader, String, FieldCache.ByteParser)
057: */
058: public interface ByteParser {
059: /** Return a single Byte representation of this field's value. */
060: public byte parseByte(String string);
061: }
062:
063: /** Interface to parse shorts from document fields.
064: * @see FieldCache#getShorts(IndexReader, String, FieldCache.ShortParser)
065: */
066: public interface ShortParser {
067: /** Return a short representation of this field's value. */
068: public short parseShort(String string);
069: }
070:
071: /** Interface to parse ints from document fields.
072: * @see FieldCache#getInts(IndexReader, String, FieldCache.IntParser)
073: */
074: public interface IntParser {
075: /** Return an integer representation of this field's value. */
076: public int parseInt(String string);
077: }
078:
079: /** Interface to parse floats from document fields.
080: * @see FieldCache#getFloats(IndexReader, String, FieldCache.FloatParser)
081: */
082: public interface FloatParser {
083: /** Return an float representation of this field's value. */
084: public float parseFloat(String string);
085: }
086:
087: /** Expert: The cache used internally by sorting and range query classes. */
088: public static FieldCache DEFAULT = new FieldCacheImpl();
089:
090: /** Checks the internal cache for an appropriate entry, and if none is
091: * found, reads the terms in <code>field</code> as a single byte and returns an array
092: * of size <code>reader.maxDoc()</code> of the value each document
093: * has in the given field.
094: * @param reader Used to get field values.
095: * @param field Which field contains the single byte values.
096: * @return The values in the given field for each document.
097: * @throws IOException If any error occurs.
098: */
099: public byte[] getBytes(IndexReader reader, String field)
100: throws IOException;
101:
102: /** Checks the internal cache for an appropriate entry, and if none is found,
103: * reads the terms in <code>field</code> as bytes and returns an array of
104: * size <code>reader.maxDoc()</code> of the value each document has in the
105: * given field.
106: * @param reader Used to get field values.
107: * @param field Which field contains the bytes.
108: * @param parser Computes byte for string values.
109: * @return The values in the given field for each document.
110: * @throws IOException If any error occurs.
111: */
112: public byte[] getBytes(IndexReader reader, String field,
113: ByteParser parser) throws IOException;
114:
115: /** Checks the internal cache for an appropriate entry, and if none is
116: * found, reads the terms in <code>field</code> as shorts and returns an array
117: * of size <code>reader.maxDoc()</code> of the value each document
118: * has in the given field.
119: * @param reader Used to get field values.
120: * @param field Which field contains the shorts.
121: * @return The values in the given field for each document.
122: * @throws IOException If any error occurs.
123: */
124: public short[] getShorts(IndexReader reader, String field)
125: throws IOException;
126:
127: /** Checks the internal cache for an appropriate entry, and if none is found,
128: * reads the terms in <code>field</code> as shorts and returns an array of
129: * size <code>reader.maxDoc()</code> of the value each document has in the
130: * given field.
131: * @param reader Used to get field values.
132: * @param field Which field contains the shorts.
133: * @param parser Computes short for string values.
134: * @return The values in the given field for each document.
135: * @throws IOException If any error occurs.
136: */
137: public short[] getShorts(IndexReader reader, String field,
138: ShortParser parser) throws IOException;
139:
140: /** Checks the internal cache for an appropriate entry, and if none is
141: * found, reads the terms in <code>field</code> as integers and returns an array
142: * of size <code>reader.maxDoc()</code> of the value each document
143: * has in the given field.
144: * @param reader Used to get field values.
145: * @param field Which field contains the integers.
146: * @return The values in the given field for each document.
147: * @throws IOException If any error occurs.
148: */
149: public int[] getInts(IndexReader reader, String field)
150: throws IOException;
151:
152: /** Checks the internal cache for an appropriate entry, and if none is found,
153: * reads the terms in <code>field</code> as integers and returns an array of
154: * size <code>reader.maxDoc()</code> of the value each document has in the
155: * given field.
156: * @param reader Used to get field values.
157: * @param field Which field contains the integers.
158: * @param parser Computes integer for string values.
159: * @return The values in the given field for each document.
160: * @throws IOException If any error occurs.
161: */
162: public int[] getInts(IndexReader reader, String field,
163: IntParser parser) throws IOException;
164:
165: /** Checks the internal cache for an appropriate entry, and if
166: * none is found, reads the terms in <code>field</code> as floats and returns an array
167: * of size <code>reader.maxDoc()</code> of the value each document
168: * has in the given field.
169: * @param reader Used to get field values.
170: * @param field Which field contains the floats.
171: * @return The values in the given field for each document.
172: * @throws IOException If any error occurs.
173: */
174: public float[] getFloats(IndexReader reader, String field)
175: throws IOException;
176:
177: /** Checks the internal cache for an appropriate entry, and if
178: * none is found, reads the terms in <code>field</code> as floats and returns an array
179: * of size <code>reader.maxDoc()</code> of the value each document
180: * has in the given field.
181: * @param reader Used to get field values.
182: * @param field Which field contains the floats.
183: * @param parser Computes float for string values.
184: * @return The values in the given field for each document.
185: * @throws IOException If any error occurs.
186: */
187: public float[] getFloats(IndexReader reader, String field,
188: FloatParser parser) throws IOException;
189:
190: /** Checks the internal cache for an appropriate entry, and if none
191: * is found, reads the term values in <code>field</code> and returns an array
192: * of size <code>reader.maxDoc()</code> containing the value each document
193: * has in the given field.
194: * @param reader Used to get field values.
195: * @param field Which field contains the strings.
196: * @return The values in the given field for each document.
197: * @throws IOException If any error occurs.
198: */
199: public String[] getStrings(IndexReader reader, String field)
200: throws IOException;
201:
202: /** Checks the internal cache for an appropriate entry, and if none
203: * is found reads the term values in <code>field</code> and returns
204: * an array of them in natural order, along with an array telling
205: * which element in the term array each document uses.
206: * @param reader Used to get field values.
207: * @param field Which field contains the strings.
208: * @return Array of terms and index into the array for each document.
209: * @throws IOException If any error occurs.
210: */
211: public StringIndex getStringIndex(IndexReader reader, String field)
212: throws IOException;
213:
214: /** Checks the internal cache for an appropriate entry, and if
215: * none is found reads <code>field</code> to see if it contains integers, floats
216: * or strings, and then calls one of the other methods in this class to get the
217: * values. For string values, a StringIndex is returned. After
218: * calling this method, there is an entry in the cache for both
219: * type <code>AUTO</code> and the actual found type.
220: * @param reader Used to get field values.
221: * @param field Which field contains the values.
222: * @return int[], float[] or StringIndex.
223: * @throws IOException If any error occurs.
224: */
225: public Object getAuto(IndexReader reader, String field)
226: throws IOException;
227:
228: /** Checks the internal cache for an appropriate entry, and if none
229: * is found reads the terms out of <code>field</code> and calls the given SortComparator
230: * to get the sort values. A hit in the cache will happen if <code>reader</code>,
231: * <code>field</code>, and <code>comparator</code> are the same (using <code>equals()</code>)
232: * as a previous call to this method.
233: * @param reader Used to get field values.
234: * @param field Which field contains the values.
235: * @param comparator Used to convert terms into something to sort by.
236: * @return Array of sort objects, one for each document.
237: * @throws IOException If any error occurs.
238: */
239: public Comparable[] getCustom(IndexReader reader, String field,
240: SortComparator comparator) throws IOException;
241:
242: }
|