1 package org.apache.lucene.search.function;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import org.apache.lucene.index.IndexReader;
21 import org.apache.lucene.search.FieldCache;
22 import org.apache.lucene.search.function.DocValues;
24 import java.io.IOException;
27 * Expert: obtains short field values from the
28 * {@link org.apache.lucene.search.FieldCache FieldCache}
29 * using <code>getShorts()</code> and makes those values
30 * available as other numeric types, casting as needed.
32 * @lucene.experimental
34 * @see org.apache.lucene.search.function.FieldCacheSource for requirements
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>
44 public class ShortFieldSource extends FieldCacheSource {
45 private FieldCache.ShortParser parser;
48 * Create a cached short field source with default string-to-short parser.
50 public ShortFieldSource(String field) {
55 * Create a cached short field source with a specific string-to-short parser.
57 public ShortFieldSource(String field, FieldCache.ShortParser parser) {
62 /*(non-Javadoc) @see org.apache.lucene.search.function.ValueSource#description() */
64 public String description() {
65 return "short(" + super.description() + ')';
68 /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#getCachedValues(org.apache.lucene.search.FieldCache, java.lang.String, org.apache.lucene.index.IndexReader) */
70 public DocValues getCachedFieldValues (FieldCache cache, String field, IndexReader reader) throws IOException {
71 final short[] arr = cache.getShorts(reader, field, parser);
72 return new DocValues() {
73 /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#floatVal(int) */
75 public float floatVal(int doc) {
78 /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#intVal(int) */
80 public int intVal(int doc) {
83 /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#toString(int) */
85 public String toString(int doc) {
86 return description() + '=' + intVal(doc);
88 /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#getInnerArray() */
90 Object getInnerArray() {
96 /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#cachedFieldSourceEquals(org.apache.lucene.search.function.FieldCacheSource) */
98 public boolean cachedFieldSourceEquals(FieldCacheSource o) {
99 if (o.getClass() != ShortFieldSource.class) {
102 ShortFieldSource other = (ShortFieldSource)o;
103 return this.parser==null ?
105 this.parser.getClass() == other.parser.getClass();
108 /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#cachedFieldSourceHashCode() */
110 public int cachedFieldSourceHashCode() {
111 return parser==null ?
112 Short.class.hashCode() : parser.getClass().hashCode();