Skip to content

Apache Lucene open-source search software

LUCENE-3589: fix BytesRef.copy(short) to set its length and clean house on unused methods

Robert Muir0ec321de3f9d
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 368ac0941f0..b77035e17ef 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -636,6 +636,9 @@ Bug fixes
when the directory isn't MMapDirectory, due to failing to reuse
DocsAndPositionsEnum while merging (Marc Sturlese, Erick Erickson,
Robert Muir, Simon Willnauer, Mike McCandless)
+
+* LUCENE-3589: BytesRef copy(short) didnt set length.
+ (Peter Chang via Robert Muir)
======================= Lucene 3.6.0 =======================
diff --git a/lucene/src/java/org/apache/lucene/index/values/PackedIntValues.java b/lucene/src/java/org/apache/lucene/index/values/PackedIntValues.java
index ba0829e2122..7ee814f8b42 100644
--- a/lucene/src/java/org/apache/lucene/index/values/PackedIntValues.java
+++ b/lucene/src/java/org/apache/lucene/index/values/PackedIntValues.java
@@ -128,7 +128,17 @@ class PackedIntValues {
PackedInts.bitsRequired(maxValue - minValue));
for (int i = 0; i < lastDocID + 1; i++) {
set(bytesRef, i);
- long asLong = bytesRef.asLong();
+ byte[] bytes = bytesRef.bytes;
+ int offset = bytesRef.offset;
+ long asLong =
+ (((long)(bytes[offset+0] & 0xff) << 56) |
+ ((long)(bytes[offset+1] & 0xff) << 48) |
+ ((long)(bytes[offset+2] & 0xff) << 40) |
+ ((long)(bytes[offset+3] & 0xff) << 32) |
+ ((long)(bytes[offset+4] & 0xff) << 24) |
+ ((long)(bytes[offset+5] & 0xff) << 16) |
+ ((long)(bytes[offset+6] & 0xff) << 8) |
+ ((long)(bytes[offset+7] & 0xff)));
w.add(asLong == 0 ? defaultValue : asLong - minValue);
}
for (int i = lastDocID + 1; i < docCount; i++) {
diff --git a/lucene/src/java/org/apache/lucene/util/BytesRef.java b/lucene/src/java/org/apache/lucene/util/BytesRef.java
index 6473fba9639..43e8aeae859 100644
--- a/lucene/src/java/org/apache/lucene/util/BytesRef.java
+++ b/lucene/src/java/org/apache/lucene/util/BytesRef.java
@@ -65,18 +65,6 @@ public final class BytesRef implements Comparable<BytesRef> {
this.bytes = new byte[capacity];
}
- /** Incoming IntsRef values must be Byte.MIN_VALUE -
- * Byte.MAX_VALUE. */
- public BytesRef(IntsRef intsRef) {
- bytes = new byte[intsRef.length];
- for(int idx=0;idx<intsRef.length;idx++) {
- final int v = intsRef.ints[intsRef.offset + idx];
- assert v >= Byte.MIN_VALUE && v <= Byte.MAX_VALUE;
- bytes[idx] = (byte) v;
- }
- length = intsRef.length;
- }
-
/**
* @param text Initialize the byte[] from the UTF8 bytes
* for the provided String. This must be well-formed
@@ -86,16 +74,6 @@ public final class BytesRef implements Comparable<BytesRef> {
this();
copy(text);
}
-
- /**
- * @param text Initialize the byte[] from the UTF8 bytes
- * for the provided array. This must be well-formed
- * unicode text, with no unpaired surrogates or U+FFFF.
- */
- public BytesRef(char text[], int offset, int length) {
- this(length * 4);
- copy(text, offset, length);
- }
public BytesRef(BytesRef other) {
this();
@@ -124,16 +102,6 @@ public final class BytesRef implements Comparable<BytesRef> {
public void copy(CharSequence text) {
UnicodeUtil.UTF16toUTF8(text, 0, text.length(), this);
}
-
- /**
- * Copies the UTF8 bytes for this string.
- *
- * @param text Must be well-formed unicode text, with no
- * unpaired surrogates or invalid UTF16 code units.
- */
- public void copy(char text[], int offset, int length) {
- UnicodeUtil.UTF16toUTF8(text, offset, length, this);
- }
public boolean bytesEquals(BytesRef other) {
if (length == other.length) {
@@ -294,38 +262,7 @@ public final class BytesRef implements Comparable<BytesRef> {
}
bytes[offset] = (byte) (value >> 8);
bytes[offset + 1] = (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 short asShort() {
- int pos = offset;
- return (short) (0xFFFF & ((bytes[pos++] & 0xFF) << 8) | (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.
- */
- public int asInt() {
- return asIntInternal(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 long asLong() {
- return (((long) asIntInternal(offset) << 32) | asIntInternal(offset + 4) & 0xFFFFFFFFL);
+ length = 2;
}
private void copyInternal(int value, int startOffset) {
@@ -335,11 +272,6 @@ public final class BytesRef implements Comparable<BytesRef> {
bytes[startOffset + 3] = (byte) (value);
}
- private int asIntInternal(int pos) {
- return ((bytes[pos++] & 0xFF) << 24) | ((bytes[pos++] & 0xFF) << 16)
- | ((bytes[pos++] & 0xFF) << 8) | (bytes[pos] & 0xFF);
- }
-
public void append(BytesRef other) {
int newLen = length + other.length;
if (bytes.length < newLen) {
diff --git a/lucene/src/test/org/apache/lucene/index/values/TestTypePromotion.java b/lucene/src/test/org/apache/lucene/index/values/TestTypePromotion.java
index dbbb37a62ce..2e0aed4e01a 100644
--- a/lucene/src/test/org/apache/lucene/index/values/TestTypePromotion.java
+++ b/lucene/src/test/org/apache/lucene/index/values/TestTypePromotion.java
@@ -116,6 +116,36 @@ public class TestTypePromotion extends LuceneTestCase {
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)
throws CorruptIndexException, IOException {
IndexReader reader = IndexReader.open(dir);
@@ -137,13 +167,13 @@ public class TestTypePromotion extends LuceneTestCase {
value = bytes.bytes[bytes.offset];
break;
case 2:
- value = bytes.asShort();
+ value = asShort(bytes);
break;
case 4:
- value = bytes.asInt();
+ value = asInt(bytes);
break;
case 8:
- value = bytes.asLong();
+ value = asLong(bytes);
break;
default:
@@ -217,10 +247,10 @@ public class TestTypePromotion extends LuceneTestCase {
case BYTES_VAR_STRAIGHT:
if (random.nextBoolean()) {
ref.copy(random.nextInt());
- values[i] = ref.asInt();
+ values[i] = asInt(ref);
} else {
ref.copy(random.nextLong());
- values[i] = ref.asLong();
+ values[i] = asLong(ref);
}
valField.setBytes(ref, valueType);
break;