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.util.PriorityQueue;
021:
022: import java.text.Collator;
023: import java.util.Locale;
024:
025: /**
026: * Expert: Collects sorted results from Searchable's and collates them.
027: * The elements put into this queue must be of type FieldDoc.
028: *
029: * <p>Created: Feb 11, 2004 2:04:21 PM
030: *
031: * @author Tim Jones (Nacimiento Software)
032: * @since lucene 1.4
033: * @version $Id: FieldDocSortedHitQueue.java 590530 2007-10-31 01:28:25Z gsingers $
034: */
035: class FieldDocSortedHitQueue extends PriorityQueue {
036:
037: // this cannot contain AUTO fields - any AUTO fields should
038: // have been resolved by the time this class is used.
039: volatile SortField[] fields;
040:
041: // used in the case where the fields are sorted by locale
042: // based strings
043: volatile Collator[] collators;
044:
045: /**
046: * Creates a hit queue sorted by the given list of fields.
047: * @param fields Fieldable names, in priority order (highest priority first).
048: * @param size The number of hits to retain. Must be greater than zero.
049: */
050: FieldDocSortedHitQueue(SortField[] fields, int size) {
051: this .fields = fields;
052: this .collators = hasCollators(fields);
053: initialize(size);
054: }
055:
056: /**
057: * Allows redefinition of sort fields if they are <code>null</code>.
058: * This is to handle the case using ParallelMultiSearcher where the
059: * original list contains AUTO and we don't know the actual sort
060: * type until the values come back. The fields can only be set once.
061: * This method is thread safe.
062: * @param fields
063: */
064: synchronized void setFields(SortField[] fields) {
065: if (this .fields == null) {
066: this .fields = fields;
067: this .collators = hasCollators(fields);
068: }
069: }
070:
071: /** Returns the fields being used to sort. */
072: SortField[] getFields() {
073: return fields;
074: }
075:
076: /** Returns an array of collators, possibly <code>null</code>. The collators
077: * correspond to any SortFields which were given a specific locale.
078: * @param fields Array of sort fields.
079: * @return Array, possibly <code>null</code>.
080: */
081: private Collator[] hasCollators(final SortField[] fields) {
082: if (fields == null)
083: return null;
084: Collator[] ret = new Collator[fields.length];
085: for (int i = 0; i < fields.length; ++i) {
086: Locale locale = fields[i].getLocale();
087: if (locale != null)
088: ret[i] = Collator.getInstance(locale);
089: }
090: return ret;
091: }
092:
093: /**
094: * Returns whether <code>a</code> is less relevant than <code>b</code>.
095: * @param a ScoreDoc
096: * @param b ScoreDoc
097: * @return <code>true</code> if document <code>a</code> should be sorted after document <code>b</code>.
098: */
099: protected final boolean lessThan(final Object a, final Object b) {
100: final FieldDoc docA = (FieldDoc) a;
101: final FieldDoc docB = (FieldDoc) b;
102: final int n = fields.length;
103: int c = 0;
104: for (int i = 0; i < n && c == 0; ++i) {
105: final int type = fields[i].getType();
106: switch (type) {
107: case SortField.SCORE: {
108: float r1 = ((Float) docA.fields[i]).floatValue();
109: float r2 = ((Float) docB.fields[i]).floatValue();
110: if (r1 > r2)
111: c = -1;
112: if (r1 < r2)
113: c = 1;
114: break;
115: }
116: case SortField.DOC:
117: case SortField.INT: {
118: int i1 = ((Integer) docA.fields[i]).intValue();
119: int i2 = ((Integer) docB.fields[i]).intValue();
120: if (i1 < i2)
121: c = -1;
122: if (i1 > i2)
123: c = 1;
124: break;
125: }
126: case SortField.LONG: {
127: long l1 = ((Long) docA.fields[i]).longValue();
128: long l2 = ((Long) docB.fields[i]).longValue();
129: if (l1 < l2)
130: c = -1;
131: if (l1 > l2)
132: c = 1;
133: break;
134: }
135: case SortField.STRING: {
136: String s1 = (String) docA.fields[i];
137: String s2 = (String) docB.fields[i];
138: // null values need to be sorted first, because of how FieldCache.getStringIndex()
139: // works - in that routine, any documents without a value in the given field are
140: // put first. If both are null, the next SortField is used
141: if (s1 == null)
142: c = (s2 == null) ? 0 : -1;
143: else if (s2 == null)
144: c = 1; //
145: else if (fields[i].getLocale() == null) {
146: c = s1.compareTo(s2);
147: } else {
148: c = collators[i].compare(s1, s2);
149: }
150: break;
151: }
152: case SortField.FLOAT: {
153: float f1 = ((Float) docA.fields[i]).floatValue();
154: float f2 = ((Float) docB.fields[i]).floatValue();
155: if (f1 < f2)
156: c = -1;
157: if (f1 > f2)
158: c = 1;
159: break;
160: }
161: case SortField.DOUBLE: {
162: double d1 = ((Double) docA.fields[i]).doubleValue();
163: double d2 = ((Double) docB.fields[i]).doubleValue();
164: if (d1 < d2)
165: c = -1;
166: if (d1 > d2)
167: c = 1;
168: break;
169: }
170: case SortField.CUSTOM: {
171: c = docA.fields[i].compareTo(docB.fields[i]);
172: break;
173: }
174: case SortField.AUTO: {
175: // we cannot handle this - even if we determine the type of object (Float or
176: // Integer), we don't necessarily know how to compare them (both SCORE and
177: // FLOAT contain floats, but are sorted opposite of each other). Before
178: // we get here, each AUTO should have been replaced with its actual value.
179: throw new RuntimeException(
180: "FieldDocSortedHitQueue cannot use an AUTO SortField");
181: }
182: default: {
183: throw new RuntimeException("invalid SortField type: "
184: + type);
185: }
186: }
187: if (fields[i].getReverse()) {
188: c = -c;
189: }
190: }
191:
192: // avoid random sort order that could lead to duplicates (bug #31241):
193: if (c == 0)
194: return docA.doc > docB.doc;
195:
196: return c > 0;
197: }
198: }
|