pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / search / function / ByteFieldSource.java
diff --git a/lucene-java-3.5.0/lucene/src/java/org/apache/lucene/search/function/ByteFieldSource.java b/lucene-java-3.5.0/lucene/src/java/org/apache/lucene/search/function/ByteFieldSource.java
new file mode 100644 (file)
index 0000000..2d9775f
--- /dev/null
@@ -0,0 +1,115 @@
+package org.apache.lucene.search.function;
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.search.FieldCache;
+import org.apache.lucene.search.function.DocValues;
+
+import java.io.IOException;
+
+/**
+ * Expert: obtains single byte field values from the 
+ * {@link org.apache.lucene.search.FieldCache FieldCache}
+ * using <code>getBytes()</code> and makes those values 
+ * available as other numeric types, casting as needed.
+ * 
+ * @lucene.experimental
+ * 
+ * @see org.apache.lucene.search.function.FieldCacheSource for requirements 
+ * on the field. 
+ *
+ * <p><b>NOTE</b>: with the switch in 2.9 to segment-based
+ * searching, if {@link #getValues} is invoked with a
+ * composite (multi-segment) reader, this can easily cause
+ * double RAM usage for the values in the FieldCache.  It's
+ * best to switch your application to pass only atomic
+ * (single segment) readers to this API.</p>
+ */
+public class ByteFieldSource extends FieldCacheSource {
+  private FieldCache.ByteParser parser;
+
+  /**
+   * Create a cached byte field source with default string-to-byte parser. 
+   */
+  public ByteFieldSource(String field) {
+    this(field, null);
+  }
+
+  /**
+   * Create a cached byte field source with a specific string-to-byte parser. 
+   */
+  public ByteFieldSource(String field, FieldCache.ByteParser parser) {
+    super(field);
+    this.parser = parser;
+  }
+
+  /*(non-Javadoc) @see org.apache.lucene.search.function.ValueSource#description() */
+  @Override
+  public String description() {
+    return "byte(" + super.description() + ')';
+  }
+
+  /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#getCachedValues(org.apache.lucene.search.FieldCache, java.lang.String, org.apache.lucene.index.IndexReader) */
+  @Override
+  public DocValues getCachedFieldValues (FieldCache cache, String field, IndexReader reader) throws IOException {
+    final byte[] arr = cache.getBytes(reader, field, parser);
+    return new DocValues() {
+      /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#floatVal(int) */
+      @Override
+      public float floatVal(int doc) { 
+        return arr[doc]; 
+      }
+      /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#intVal(int) */
+      @Override
+      public  int intVal(int doc) { 
+        return arr[doc]; 
+      }
+      /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#toString(int) */
+      @Override
+      public String toString(int doc) { 
+        return  description() + '=' + intVal(doc);  
+      }
+      /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#getInnerArray() */
+      @Override
+      Object getInnerArray() {
+        return arr;
+      }
+    };
+  }
+
+  /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#cachedFieldSourceEquals(org.apache.lucene.search.function.FieldCacheSource) */
+  @Override
+  public boolean cachedFieldSourceEquals(FieldCacheSource o) {
+    if (o.getClass() !=  ByteFieldSource.class) {
+      return false;
+    }
+    ByteFieldSource other = (ByteFieldSource)o;
+    return this.parser==null ? 
+      other.parser==null :
+      this.parser.getClass() == other.parser.getClass();
+  }
+
+  /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#cachedFieldSourceHashCode() */
+  @Override
+  public int cachedFieldSourceHashCode() {
+    return parser==null ? 
+      Byte.class.hashCode() : parser.getClass().hashCode();
+  }
+
+}