You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by si...@apache.org on 2011/05/23 12:58:45 UTC

svn commit: r1126430 - in /lucene/dev/branches/docvalues/lucene/src: java/org/apache/lucene/index/values/Floats.java test/org/apache/lucene/index/values/TestDocValuesIndexing.java

Author: simonw
Date: Mon May 23 10:58:45 2011
New Revision: 1126430

URL: http://svn.apache.org/viewvc?rev=1126430&view=rev
Log:
LUCENE-3108: convert Float on load instead of converting for every lookup through FloatsBuffer

Modified:
    lucene/dev/branches/docvalues/lucene/src/java/org/apache/lucene/index/values/Floats.java
    lucene/dev/branches/docvalues/lucene/src/test/org/apache/lucene/index/values/TestDocValuesIndexing.java

Modified: lucene/dev/branches/docvalues/lucene/src/java/org/apache/lucene/index/values/Floats.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/docvalues/lucene/src/java/org/apache/lucene/index/values/Floats.java?rev=1126430&r1=1126429&r2=1126430&view=diff
==============================================================================
--- lucene/dev/branches/docvalues/lucene/src/java/org/apache/lucene/index/values/Floats.java (original)
+++ lucene/dev/branches/docvalues/lucene/src/java/org/apache/lucene/index/values/Floats.java Mon May 23 10:58:45 2011
@@ -18,8 +18,6 @@ package org.apache.lucene.index.values;
  */
 import java.io.IOException;
 import java.nio.ByteBuffer;
-import java.nio.DoubleBuffer;
-import java.nio.FloatBuffer;
 import java.util.Collection;
 import java.util.concurrent.atomic.AtomicLong;
 
@@ -262,33 +260,41 @@ public class Floats {
      */
     @Override
     public Source load() throws IOException {
-      /*
-       *  the allocated byteBuffer always uses BIG_ENDIAN here
-       *  and since the writer uses DataOutput#writeInt() / writeLong()
-       *  we can allways assume BIGE_ENDIAN
-       */
-      final ByteBuffer buffer = ByteBuffer.allocate(precisionBytes * maxDoc);
-      IndexInput indexInput = (IndexInput) datIn.clone();
+      /* we always read BIG_ENDIAN here since the writer uses
+       * DataOutput#writeInt() / writeLong() we can simply read the ints / longs
+       * back in using readInt / readLong */
+      final IndexInput indexInput = (IndexInput) datIn.clone();
       indexInput.seek(CodecUtil.headerLength(CODEC_NAME));
       // skip precision:
       indexInput.readByte();
-      assert buffer.hasArray() : "Buffer must support Array";
-      final byte[] arr = buffer.array();
-      indexInput.readBytes(arr, 0, arr.length);
-      return precisionBytes == 4 ? new Source4(buffer) : new Source8(buffer);
+      if (precisionBytes == 4) {
+        final float[] values = new float[(4 * maxDoc) >> 2];
+        assert values.length == maxDoc;
+        for (int i = 0; i < values.length; i++) {
+          values[i] = Float.intBitsToFloat(indexInput.readInt());
+        }
+        return new Source4(values);
+      } else {
+        final double[] values = new double[(8 * maxDoc) >> 3];
+        assert values.length == maxDoc;
+        for (int i = 0; i < values.length; i++) {
+          values[i] = Double.longBitsToDouble(indexInput.readLong());
+        }
+        return new Source8(values);
+      }
     }
 
     private class Source4 extends Source {
-      private final FloatBuffer values;
+      private final float[] values;
 
-      Source4(ByteBuffer buffer) {
-        values = buffer.asFloatBuffer();
+      Source4(final float[] values ) throws IOException {
+        this.values = values;
         missingValue.doubleValue = Float.NEGATIVE_INFINITY;
       }
 
       @Override
       public double getFloat(int docID) {
-        return values.get(docID);
+        return values[docID];
       }
 
       @Override
@@ -318,17 +324,16 @@ public class Floats {
     }
 
     private class Source8 extends Source {
-      private final DoubleBuffer values;
+      private final double[] values;
 
-      Source8(ByteBuffer buffer) {
-        values = buffer.asDoubleBuffer();
+      Source8(final double[] values) throws IOException {
+        this.values = values;
         missingValue.doubleValue = Double.NEGATIVE_INFINITY;
-
       }
 
       @Override
       public double getFloat(int docID) {
-        return values.get(docID);
+        return values[docID];
       }
 
       @Override

Modified: lucene/dev/branches/docvalues/lucene/src/test/org/apache/lucene/index/values/TestDocValuesIndexing.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/docvalues/lucene/src/test/org/apache/lucene/index/values/TestDocValuesIndexing.java?rev=1126430&r1=1126429&r2=1126430&view=diff
==============================================================================
--- lucene/dev/branches/docvalues/lucene/src/test/org/apache/lucene/index/values/TestDocValuesIndexing.java (original)
+++ lucene/dev/branches/docvalues/lucene/src/test/org/apache/lucene/index/values/TestDocValuesIndexing.java Mon May 23 10:58:45 2011
@@ -298,7 +298,7 @@ public class TestDocValuesIndexing exten
 
         for (int i = 0; i < base; i++) {
           double value = floats.getFloat(i);
-          assertEquals(" floats failed for doc: " + i + " base: " + base,
+          assertEquals(val + " failed for doc: " + i + " base: " + base,
               missing.doubleValue, value, 0.0d);
         }
         DocValuesEnum floatEnum = getValuesEnum(floatReader);
@@ -528,9 +528,11 @@ public class TestDocValuesIndexing exten
           valField.setInt(i);
           break;
         case FLOAT_32:
-        case FLOAT_64:
           valField.setFloat(2.0f * i);
           break;
+        case FLOAT_64:
+          valField.setFloat(2.0d * i);
+          break;
         default:
           fail("unexpected value " + value);
         }