add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / java / org / apache / lucene / search / function / ReverseOrdFieldSource.java
1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.apache.lucene.search.function;
19
20 import org.apache.lucene.index.IndexReader;
21 import org.apache.lucene.search.FieldCache;
22
23 import java.io.IOException;
24
25 /**
26  * Expert: obtains the ordinal of the field value from the default Lucene 
27  * {@link org.apache.lucene.search.FieldCache FieldCache} using getStringIndex()
28  * and reverses the order.
29  * <p>
30  * The native lucene index order is used to assign an ordinal value for each field value.
31  * <p>
32  * Field values (terms) are lexicographically ordered by unicode value, and numbered starting at 1.
33  * <br>
34  * Example of reverse ordinal (rord):
35  * <br>If there were only three field values: "apple","banana","pear"
36  * <br>then rord("apple")=3, rord("banana")=2, ord("pear")=1
37  * <p>
38  * WARNING: 
39  * rord() depends on the position in an index and can thus change 
40  * when other documents are inserted or deleted,
41  * or if a MultiSearcher is used. 
42  * 
43  * @lucene.experimental
44  *
45  * <p><b>NOTE</b>: with the switch in 2.9 to segment-based
46  * searching, if {@link #getValues} is invoked with a
47  * composite (multi-segment) reader, this can easily cause
48  * double RAM usage for the values in the FieldCache.  It's
49  * best to switch your application to pass only atomic
50  * (single segment) readers to this API.</p>
51  */
52
53 public class ReverseOrdFieldSource extends ValueSource {
54   public String field;
55
56   /** 
57    * Contructor for a certain field.
58    * @param field field whose values reverse order is used.  
59    */
60   public ReverseOrdFieldSource(String field) {
61     this.field = field;
62   }
63
64   /*(non-Javadoc) @see org.apache.lucene.search.function.ValueSource#description() */
65   @Override
66   public String description() {
67     return "rord("+field+')';
68   }
69
70   /*(non-Javadoc) @see org.apache.lucene.search.function.ValueSource#getValues(org.apache.lucene.index.IndexReader) */
71   @Override
72   public DocValues getValues(IndexReader reader) throws IOException {
73     final FieldCache.StringIndex sindex = FieldCache.DEFAULT.getStringIndex(reader, field);
74
75     final int arr[] = sindex.order;
76     final int end = sindex.lookup.length;
77
78     return new DocValues() {
79       /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#floatVal(int) */
80       @Override
81       public float floatVal(int doc) {
82         return (end - arr[doc]);
83       }
84       /* (non-Javadoc) @see org.apache.lucene.search.function.DocValues#intVal(int) */
85       @Override
86       public int intVal(int doc) {
87         return end - arr[doc];
88       }
89       /* (non-Javadoc) @see org.apache.lucene.search.function.DocValues#strVal(int) */
90       @Override
91       public String strVal(int doc) {
92         // the string value of the ordinal, not the string itself
93         return Integer.toString(intVal(doc));
94       }
95       /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#toString(int) */
96       @Override
97       public String toString(int doc) {
98         return description() + '=' + strVal(doc);
99       }
100       /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#getInnerArray() */
101       @Override
102       Object getInnerArray() {
103         return arr;
104       }
105     };
106   }
107
108   /*(non-Javadoc) @see java.lang.Object#equals(java.lang.Object) */
109   @Override
110   public boolean equals(Object o) {
111     if (o == this) return true;
112     if (o == null) return false;
113     if (o.getClass() != ReverseOrdFieldSource.class) return false;
114     ReverseOrdFieldSource other = (ReverseOrdFieldSource)o;
115     return this.field.equals(other.field); 
116   }
117
118   private static final int hcode = ReverseOrdFieldSource.class.hashCode();
119   
120   /*(non-Javadoc) @see java.lang.Object#hashCode() */
121   @Override
122   public int hashCode() {
123     return hcode + field.hashCode();
124   }
125 }