Home » lucene-3.0.1-src » org.apache » lucene » search » function » [javadoc | source]

    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 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.
   31    * 
   32    * <p><font color="#FF0000">
   33    * WARNING: The status of the <b>search.function</b> package is experimental. 
   34    * The APIs introduced here might change in the future and will not be 
   35    * supported anymore in such a case.</font>
   36    * 
   37    * @see org.apache.lucene.search.function.FieldCacheSource for requirements 
   38    * on the field.
   39    *
   40    * <p><b>NOTE</b>: with the switch in 2.9 to segment-based
   41    * searching, if {@link #getValues} is invoked with a
   42    * composite (multi-segment) reader, this can easily cause
   43    * double RAM usage for the values in the FieldCache.  It's
   44    * best to switch your application to pass only atomic
   45    * (single segment) readers to this API.  Alternatively, for
   46    * a short-term fix, you could wrap your ValueSource using
   47    * {@link MultiValueSource}, which costs more CPU per lookup
   48    * but will not consume double the FieldCache RAM.</p>
   49    */
   50   public class ShortFieldSource extends FieldCacheSource {
   51     private FieldCache.ShortParser parser;
   52   
   53     /**
   54      * Create a cached short field source with default string-to-short parser. 
   55      */
   56     public ShortFieldSource(String field) {
   57       this(field, null);
   58     }
   59   
   60     /**
   61      * Create a cached short field source with a specific string-to-short parser. 
   62      */
   63     public ShortFieldSource(String field, FieldCache.ShortParser parser) {
   64       super(field);
   65       this.parser = parser;
   66     }
   67   
   68     /*(non-Javadoc) @see org.apache.lucene.search.function.ValueSource#description() */
   69     @Override
   70     public String description() {
   71       return "short(" + super.description() + ')';
   72     }
   73   
   74     /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#getCachedValues(org.apache.lucene.search.FieldCache, java.lang.String, org.apache.lucene.index.IndexReader) */
   75     @Override
   76     public DocValues getCachedFieldValues (FieldCache cache, String field, IndexReader reader) throws IOException {
   77       final short[] arr = cache.getShorts(reader, field, parser);
   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 (float) 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 arr[doc]; 
   88         }
   89         /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#toString(int) */
   90         @Override
   91         public String toString(int doc) { 
   92           return  description() + '=' + intVal(doc);  
   93         }
   94         /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#getInnerArray() */
   95         @Override
   96         Object getInnerArray() {
   97           return arr;
   98         }
   99       };
  100     }
  101   
  102     /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#cachedFieldSourceEquals(org.apache.lucene.search.function.FieldCacheSource) */
  103     @Override
  104     public boolean cachedFieldSourceEquals(FieldCacheSource o) {
  105       if (o.getClass() !=  ShortFieldSource.class) {
  106         return false;
  107       }
  108       ShortFieldSource other = (ShortFieldSource)o;
  109       return this.parser==null ? 
  110         other.parser==null :
  111         this.parser.getClass() == other.parser.getClass();
  112     }
  113   
  114     /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#cachedFieldSourceHashCode() */
  115     @Override
  116     public int cachedFieldSourceHashCode() {
  117       return parser==null ? 
  118         Short.class.hashCode() : parser.getClass().hashCode();
  119     }
  120   
  121   }

Home » lucene-3.0.1-src » org.apache » lucene » search » function » [javadoc | source]