HBASE-2635. ImmutableBytesWritable ignores offset in several cases

git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@950242 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Todd Lipcon 2010-06-01 20:42:10 +00:00
parent dacf350835
commit 8e870fa13e
3 changed files with 47 additions and 13 deletions

View File

@ -366,6 +366,7 @@ Release 0.21.0 - Unreleased
(Jeff Hammerbacher via Todd Lipcon)
HBASE-2371 Fix 'list' command in shell (Alexey Kovyrin via Todd Lipcon)
HBASE-2620 REST tests don't use ephemeral ports
HBASE-2635 ImmutableBytesWritable ignores offset in several cases
IMPROVEMENTS
HBASE-1760 Cleanup TODOs in HTable

View File

@ -83,7 +83,7 @@ implements WritableComparable<ImmutableBytesWritable> {
/**
* Get the data from the BytesWritable.
* @return The data is only valid between 0 and getSize() - 1.
* @return The data is only valid between offset and offset+length.
*/
public byte [] get() {
if (this.bytes == null) {
@ -112,7 +112,7 @@ implements WritableComparable<ImmutableBytesWritable> {
}
/**
* @return the current size of the buffer.
* @return the number of valid bytes in the buffer
*/
public int getSize() {
if (this.bytes == null) {
@ -123,7 +123,7 @@ implements WritableComparable<ImmutableBytesWritable> {
}
/**
* @return the current length of the buffer. same as getSize()
* @return the number of valid bytes in the buffer
*/
//Should probably deprecate getSize() so that we keep the same calls for all
//byte []
@ -155,20 +155,24 @@ implements WritableComparable<ImmutableBytesWritable> {
}
// Below methods copied from BytesWritable
@Override
public int hashCode() {
return WritableComparator.hashBytes(bytes, this.length);
int hash = 1;
for (int i = offset; i < offset + length; i++)
hash = (31 * hash) + (int)bytes[i];
return hash;
}
/**
* Define the sort order of the BytesWritable.
* @param right_obj The other bytes writable
* @param that The other bytes writable
* @return Positive if left is bigger than right, 0 if they are equal, and
* negative if left is smaller than right.
*/
public int compareTo(ImmutableBytesWritable right_obj) {
return compareTo(right_obj.get());
public int compareTo(ImmutableBytesWritable that) {
return WritableComparator.compareBytes(
this.bytes, this.offset, this.length,
that.bytes, that.offset, that.length);
}
/**
@ -178,8 +182,9 @@ implements WritableComparable<ImmutableBytesWritable> {
* negative if left is smaller than right.
*/
public int compareTo(final byte [] that) {
return WritableComparator.compareBytes(this.bytes, 0, this.length, that,
0, that.length);
return WritableComparator.compareBytes(
this.bytes, this.offset, this.length,
that, 0, that.length);
}
/**
@ -202,9 +207,9 @@ implements WritableComparable<ImmutableBytesWritable> {
@Override
public String toString() {
StringBuilder sb = new StringBuilder(3*this.bytes.length);
for (int idx = 0; idx < this.bytes.length; idx++) {
for (int idx = offset; idx < offset + length; idx++) {
// if not the first, put a blank separator in
if (idx != 0) {
if (idx != offset) {
sb.append(' ');
}
String num = Integer.toHexString(bytes[idx]);

View File

@ -28,6 +28,18 @@ import java.io.DataOutputStream;
import java.io.IOException;
public class TestImmutableBytesWritable extends TestCase {
public void testHash() throws Exception {
assertEquals(
new ImmutableBytesWritable(Bytes.toBytes("xxabc"), 2, 3).hashCode(),
new ImmutableBytesWritable(Bytes.toBytes("abc")).hashCode());
assertEquals(
new ImmutableBytesWritable(Bytes.toBytes("xxabcd"), 2, 3).hashCode(),
new ImmutableBytesWritable(Bytes.toBytes("abc")).hashCode());
assertNotSame(
new ImmutableBytesWritable(Bytes.toBytes("xxabc"), 2, 3).hashCode(),
new ImmutableBytesWritable(Bytes.toBytes("xxabc"), 2, 2).hashCode());
}
public void testComparison() throws Exception {
runTests("aa", "b", -1);
runTests("aa", "aa", 0);
@ -46,6 +58,22 @@ public class TestImmutableBytesWritable extends TestCase {
doComparisonsOnObjects(a, b, signum);
doComparisonsOnRaw(a, b, signum);
// Tests for when the offset is non-zero
a = new ImmutableBytesWritable(Bytes.toBytes("xxx" + aStr),
3, aStr.length());
b = new ImmutableBytesWritable(Bytes.toBytes("yy" + bStr),
2, bStr.length());
doComparisonsOnObjects(a, b, signum);
doComparisonsOnRaw(a, b, signum);
// Tests for when offset is nonzero and length doesn't extend to end
a = new ImmutableBytesWritable(Bytes.toBytes("xxx" + aStr + "zzz"),
3, aStr.length());
b = new ImmutableBytesWritable(Bytes.toBytes("yy" + bStr + "aaa"),
2, bStr.length());
doComparisonsOnObjects(a, b, signum);
doComparisonsOnRaw(a, b, signum);
}
@ -93,4 +121,4 @@ public class TestImmutableBytesWritable extends TestCase {
"Comparing " + a + " and " + b + " as objects (inverse)",
-signum(comparator.compare(b, a)), expectedSignum);
}
}
}