add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / java / org / apache / lucene / search / function / ByteFieldSource.java
1 package org.apache.lucene.search.function;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import org.apache.lucene.index.IndexReader;
21 import org.apache.lucene.search.FieldCache;
22 import org.apache.lucene.search.function.DocValues;
23
24 import java.io.IOException;
25
26 /**
27  * Expert: obtains single byte field values from the 
28  * {@link org.apache.lucene.search.FieldCache FieldCache}
29  * using <code>getBytes()</code> and makes those values 
30  * available as other numeric types, casting as needed.
31  * 
32  * @lucene.experimental
33  * 
34  * @see org.apache.lucene.search.function.FieldCacheSource for requirements 
35  * on the field. 
36  *
37  * <p><b>NOTE</b>: with the switch in 2.9 to segment-based
38  * searching, if {@link #getValues} is invoked with a
39  * composite (multi-segment) reader, this can easily cause
40  * double RAM usage for the values in the FieldCache.  It's
41  * best to switch your application to pass only atomic
42  * (single segment) readers to this API.</p>
43  */
44 public class ByteFieldSource extends FieldCacheSource {
45   private FieldCache.ByteParser parser;
46
47   /**
48    * Create a cached byte field source with default string-to-byte parser. 
49    */
50   public ByteFieldSource(String field) {
51     this(field, null);
52   }
53
54   /**
55    * Create a cached byte field source with a specific string-to-byte parser. 
56    */
57   public ByteFieldSource(String field, FieldCache.ByteParser parser) {
58     super(field);
59     this.parser = parser;
60   }
61
62   /*(non-Javadoc) @see org.apache.lucene.search.function.ValueSource#description() */
63   @Override
64   public String description() {
65     return "byte(" + super.description() + ')';
66   }
67
68   /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#getCachedValues(org.apache.lucene.search.FieldCache, java.lang.String, org.apache.lucene.index.IndexReader) */
69   @Override
70   public DocValues getCachedFieldValues (FieldCache cache, String field, IndexReader reader) throws IOException {
71     final byte[] arr = cache.getBytes(reader, field, parser);
72     return new DocValues() {
73       /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#floatVal(int) */
74       @Override
75       public float floatVal(int doc) { 
76         return arr[doc]; 
77       }
78       /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#intVal(int) */
79       @Override
80       public  int intVal(int doc) { 
81         return arr[doc]; 
82       }
83       /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#toString(int) */
84       @Override
85       public String toString(int doc) { 
86         return  description() + '=' + intVal(doc);  
87       }
88       /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#getInnerArray() */
89       @Override
90       Object getInnerArray() {
91         return arr;
92       }
93     };
94   }
95
96   /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#cachedFieldSourceEquals(org.apache.lucene.search.function.FieldCacheSource) */
97   @Override
98   public boolean cachedFieldSourceEquals(FieldCacheSource o) {
99     if (o.getClass() !=  ByteFieldSource.class) {
100       return false;
101     }
102     ByteFieldSource other = (ByteFieldSource)o;
103     return this.parser==null ? 
104       other.parser==null :
105       this.parser.getClass() == other.parser.getClass();
106   }
107
108   /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#cachedFieldSourceHashCode() */
109   @Override
110   public int cachedFieldSourceHashCode() {
111     return parser==null ? 
112       Byte.class.hashCode() : parser.getClass().hashCode();
113   }
114
115 }