LUCENE-3591: Make BytesRef.copy[Int,Short,Long] package private for IndexDocValues

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1205937 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Simon Willnauer 2011-11-24 17:20:35 +00:00
parent 145d6e8d92
commit 6bdb9f9f06
5 changed files with 135 additions and 93 deletions

View File

@ -0,0 +1,120 @@
package org.apache.lucene.index.values;
/**
* 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.util.BytesRef;
/**
* Package private BytesRefUtils - can move this into the o.a.l.utils package if
* needed.
*
* @lucene.internal
*/
final class BytesRefUtils {
private BytesRefUtils() {
}
/**
* Copies the given long value and encodes it as 8 byte Big-Endian.
* <p>
* NOTE: this method resets the offset to 0, length to 8 and resizes the
* reference array if needed.
*/
public static void copyLong(BytesRef ref, long value) {
if (ref.bytes.length < 8) {
ref.bytes = new byte[8];
}
copyInternal(ref, (int) (value >> 32), ref.offset = 0);
copyInternal(ref, (int) value, 4);
ref.length = 8;
}
/**
* Copies the given int value and encodes it as 4 byte Big-Endian.
* <p>
* NOTE: this method resets the offset to 0, length to 4 and resizes the
* reference array if needed.
*/
public static void copyInt(BytesRef ref, int value) {
if (ref.bytes.length < 4) {
ref.bytes = new byte[4];
}
copyInternal(ref, value, ref.offset = 0);
ref.length = 4;
}
/**
* Copies the given short value and encodes it as a 2 byte Big-Endian.
* <p>
* NOTE: this method resets the offset to 0, length to 2 and resizes the
* reference array if needed.
*/
public static void copyShort(BytesRef ref, short value) {
if (ref.bytes.length < 2) {
ref.bytes = new byte[2];
}
ref.bytes[ref.offset] = (byte) (value >> 8);
ref.bytes[ref.offset + 1] = (byte) (value);
ref.length = 2;
}
private static void copyInternal(BytesRef ref, int value, int startOffset) {
ref.bytes[startOffset] = (byte) (value >> 24);
ref.bytes[startOffset + 1] = (byte) (value >> 16);
ref.bytes[startOffset + 2] = (byte) (value >> 8);
ref.bytes[startOffset + 3] = (byte) (value);
}
/**
* Converts 2 consecutive bytes from the current offset to a short. Bytes are
* interpreted as Big-Endian (most significant bit first)
* <p>
* NOTE: this method does <b>NOT</b> check the bounds of the referenced array.
*/
public static short asShort(BytesRef b) {
return (short) (0xFFFF & ((b.bytes[b.offset] & 0xFF) << 8) | (b.bytes[b.offset + 1] & 0xFF));
}
/**
* Converts 4 consecutive bytes from the current offset to an int. Bytes are
* interpreted as Big-Endian (most significant bit first)
* <p>
* NOTE: this method does <b>NOT</b> check the bounds of the referenced array.
*/
public static int asInt(BytesRef b) {
return asIntInternal(b, b.offset);
}
/**
* Converts 8 consecutive bytes from the current offset to a long. Bytes are
* interpreted as Big-Endian (most significant bit first)
* <p>
* NOTE: this method does <b>NOT</b> check the bounds of the referenced array.
*/
public static long asLong(BytesRef b) {
return (((long) asIntInternal(b, b.offset) << 32) | asIntInternal(b,
b.offset + 4) & 0xFFFFFFFFL);
}
private static int asIntInternal(BytesRef b, int pos) {
return ((b.bytes[pos++] & 0xFF) << 24) | ((b.bytes[pos++] & 0xFF) << 16)
| ((b.bytes[pos++] & 0xFF) << 8) | (b.bytes[pos] & 0xFF);
}
}

View File

@ -62,11 +62,11 @@ abstract class IndexDocValuesArray extends Source {
} }
void toBytes(long value, BytesRef bytesRef) { void toBytes(long value, BytesRef bytesRef) {
bytesRef.copyLong(value); BytesRefUtils.copyLong(bytesRef, value);
} }
void toBytes(double value, BytesRef bytesRef) { void toBytes(double value, BytesRef bytesRef) {
bytesRef.copyLong(Double.doubleToRawLongBits(value)); BytesRefUtils.copyLong(bytesRef, Double.doubleToRawLongBits(value));
} }
final static class ByteValues extends IndexDocValuesArray { final static class ByteValues extends IndexDocValuesArray {
@ -140,7 +140,7 @@ abstract class IndexDocValuesArray extends Source {
} }
void toBytes(long value, BytesRef bytesRef) { void toBytes(long value, BytesRef bytesRef) {
bytesRef.copyShort((short) (0xFFFFL & value)); BytesRefUtils.copyShort(bytesRef, (short) (0xFFFFL & value));
} }
}; };
@ -179,7 +179,7 @@ abstract class IndexDocValuesArray extends Source {
} }
void toBytes(long value, BytesRef bytesRef) { void toBytes(long value, BytesRef bytesRef) {
bytesRef.copyInt((int) (0xFFFFFFFF & value)); BytesRefUtils.copyInt(bytesRef, (int) (0xFFFFFFFF & value));
} }
}; };
@ -252,7 +252,7 @@ abstract class IndexDocValuesArray extends Source {
@Override @Override
void toBytes(double value, BytesRef bytesRef) { void toBytes(double value, BytesRef bytesRef) {
bytesRef.copyInt(Float.floatToRawIntBits((float)value)); BytesRefUtils.copyInt(bytesRef, Float.floatToRawIntBits((float)value));
} }

View File

@ -73,7 +73,7 @@ class PackedIntValues {
} }
} }
lastDocId = docID; lastDocId = docID;
bytesRef.copyLong(v); BytesRefUtils.copyLong(bytesRef, v);
add(docID, bytesRef); add(docID, bytesRef);
} }
@ -244,7 +244,7 @@ class PackedIntValues {
@Override @Override
public BytesRef getBytes(int docID, BytesRef ref) { public BytesRef getBytes(int docID, BytesRef ref) {
ref.grow(8); ref.grow(8);
ref.copyLong(getInt(docID)); BytesRefUtils.copyLong(ref, getInt(docID));
return ref; return ref;
} }

View File

@ -221,56 +221,7 @@ public final class BytesRef implements Comparable<BytesRef> {
offset = 0; offset = 0;
} }
/**
* Copies the given long value and encodes it as 8 byte Big-Endian.
* <p>
* NOTE: this method resets the offset to 0, length to 8 and resizes the reference array
* if needed.
*/
public void copyLong(long value) {
if (bytes.length < 8) {
bytes = new byte[8];
}
copyInternal((int) (value >> 32), offset = 0);
copyInternal((int) value, 4);
length = 8;
}
/**
* Copies the given int value and encodes it as 4 byte Big-Endian.
* <p>
* NOTE: this method resets the offset to 0, length to 4 and resizes the reference array
* if needed.
*/
public void copyInt(int value) {
if (bytes.length < 4) {
bytes = new byte[4];
}
copyInternal(value, offset = 0);
length = 4;
}
/**
* Copies the given short value and encodes it as a 2 byte Big-Endian.
* <p>
* NOTE: this method resets the offset to 0, length to 2 and resizes the reference array
* if needed.
*/
public void copyShort(short value) {
if (bytes.length < 2) {
bytes = new byte[2];
}
bytes[offset] = (byte) (value >> 8);
bytes[offset + 1] = (byte) (value);
length = 2;
}
private void copyInternal(int value, int startOffset) {
bytes[startOffset] = (byte) (value >> 24);
bytes[startOffset + 1] = (byte) (value >> 16);
bytes[startOffset + 2] = (byte) (value >> 8);
bytes[startOffset + 3] = (byte) (value);
}
public void append(BytesRef other) { public void append(BytesRef other) {
int newLen = length + other.length; int newLen = length + other.length;

View File

@ -116,35 +116,6 @@ public class TestTypePromotion extends LuceneTestCase {
dir.close(); dir.close();
} }
private short asShort(BytesRef b) {
int pos = b.offset;
return (short) (0xFFFF & ((b.bytes[pos++] & 0xFF) << 8) | (b.bytes[pos] & 0xFF));
}
/**
* Converts 4 consecutive bytes from the current offset to an int. Bytes are
* interpreted as Big-Endian (most significant bit first)
* <p>
* NOTE: this method does <b>NOT</b> check the bounds of the referenced array.
*/
private int asInt(BytesRef b) {
return asIntInternal(b, b.offset);
}
/**
* Converts 8 consecutive bytes from the current offset to a long. Bytes are
* interpreted as Big-Endian (most significant bit first)
* <p>
* NOTE: this method does <b>NOT</b> check the bounds of the referenced array.
*/
private long asLong(BytesRef b) {
return (((long) asIntInternal(b, b.offset) << 32) | asIntInternal(b, b.offset + 4) & 0xFFFFFFFFL);
}
private int asIntInternal(BytesRef b, int pos) {
return ((b.bytes[pos++] & 0xFF) << 24) | ((b.bytes[pos++] & 0xFF) << 16)
| ((b.bytes[pos++] & 0xFF) << 8) | (b.bytes[pos] & 0xFF);
}
private void assertValues(TestType type, Directory dir, long[] values) private void assertValues(TestType type, Directory dir, long[] values)
throws CorruptIndexException, IOException { throws CorruptIndexException, IOException {
@ -167,13 +138,13 @@ public class TestTypePromotion extends LuceneTestCase {
value = bytes.bytes[bytes.offset]; value = bytes.bytes[bytes.offset];
break; break;
case 2: case 2:
value = asShort(bytes); value = BytesRefUtils.asShort(bytes);
break; break;
case 4: case 4:
value = asInt(bytes); value = BytesRefUtils.asInt(bytes);
break; break;
case 8: case 8:
value = asLong(bytes); value = BytesRefUtils.asLong(bytes);
break; break;
default: default:
@ -239,18 +210,18 @@ public class TestTypePromotion extends LuceneTestCase {
case BYTES_FIXED_SORTED: case BYTES_FIXED_SORTED:
case BYTES_FIXED_STRAIGHT: case BYTES_FIXED_STRAIGHT:
values[i] = random.nextLong(); values[i] = random.nextLong();
ref.copyLong(values[i]); BytesRefUtils.copyLong(ref, values[i]);
valField.setBytes(ref, valueType); valField.setBytes(ref, valueType);
break; break;
case BYTES_VAR_DEREF: case BYTES_VAR_DEREF:
case BYTES_VAR_SORTED: case BYTES_VAR_SORTED:
case BYTES_VAR_STRAIGHT: case BYTES_VAR_STRAIGHT:
if (random.nextBoolean()) { if (random.nextBoolean()) {
ref.copyInt(random.nextInt()); BytesRefUtils.copyInt(ref, random.nextInt());
values[i] = asInt(ref); values[i] = BytesRefUtils.asInt(ref);
} else { } else {
ref.copyLong(random.nextLong()); BytesRefUtils.copyLong(ref, random.nextLong());
values[i] = asLong(ref); values[i] = BytesRefUtils.asLong(ref);
} }
valField.setBytes(ref, valueType); valField.setBytes(ref, valueType);
break; break;