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:
parent
dacf350835
commit
8e870fa13e
|
@ -366,6 +366,7 @@ Release 0.21.0 - Unreleased
|
||||||
(Jeff Hammerbacher via Todd Lipcon)
|
(Jeff Hammerbacher via Todd Lipcon)
|
||||||
HBASE-2371 Fix 'list' command in shell (Alexey Kovyrin 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-2620 REST tests don't use ephemeral ports
|
||||||
|
HBASE-2635 ImmutableBytesWritable ignores offset in several cases
|
||||||
|
|
||||||
IMPROVEMENTS
|
IMPROVEMENTS
|
||||||
HBASE-1760 Cleanup TODOs in HTable
|
HBASE-1760 Cleanup TODOs in HTable
|
||||||
|
|
|
@ -83,7 +83,7 @@ implements WritableComparable<ImmutableBytesWritable> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the data from the BytesWritable.
|
* 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() {
|
public byte [] get() {
|
||||||
if (this.bytes == null) {
|
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() {
|
public int getSize() {
|
||||||
if (this.bytes == null) {
|
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
|
//Should probably deprecate getSize() so that we keep the same calls for all
|
||||||
//byte []
|
//byte []
|
||||||
|
@ -155,20 +155,24 @@ implements WritableComparable<ImmutableBytesWritable> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Below methods copied from BytesWritable
|
// Below methods copied from BytesWritable
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
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.
|
* 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
|
* @return Positive if left is bigger than right, 0 if they are equal, and
|
||||||
* negative if left is smaller than right.
|
* negative if left is smaller than right.
|
||||||
*/
|
*/
|
||||||
public int compareTo(ImmutableBytesWritable right_obj) {
|
public int compareTo(ImmutableBytesWritable that) {
|
||||||
return compareTo(right_obj.get());
|
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.
|
* negative if left is smaller than right.
|
||||||
*/
|
*/
|
||||||
public int compareTo(final byte [] that) {
|
public int compareTo(final byte [] that) {
|
||||||
return WritableComparator.compareBytes(this.bytes, 0, this.length, that,
|
return WritableComparator.compareBytes(
|
||||||
0, that.length);
|
this.bytes, this.offset, this.length,
|
||||||
|
that, 0, that.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -202,9 +207,9 @@ implements WritableComparable<ImmutableBytesWritable> {
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder sb = new StringBuilder(3*this.bytes.length);
|
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 not the first, put a blank separator in
|
||||||
if (idx != 0) {
|
if (idx != offset) {
|
||||||
sb.append(' ');
|
sb.append(' ');
|
||||||
}
|
}
|
||||||
String num = Integer.toHexString(bytes[idx]);
|
String num = Integer.toHexString(bytes[idx]);
|
||||||
|
|
|
@ -28,6 +28,18 @@ import java.io.DataOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public class TestImmutableBytesWritable extends TestCase {
|
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 {
|
public void testComparison() throws Exception {
|
||||||
runTests("aa", "b", -1);
|
runTests("aa", "b", -1);
|
||||||
runTests("aa", "aa", 0);
|
runTests("aa", "aa", 0);
|
||||||
|
@ -46,6 +58,22 @@ public class TestImmutableBytesWritable extends TestCase {
|
||||||
|
|
||||||
doComparisonsOnObjects(a, b, signum);
|
doComparisonsOnObjects(a, b, signum);
|
||||||
doComparisonsOnRaw(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)",
|
"Comparing " + a + " and " + b + " as objects (inverse)",
|
||||||
-signum(comparator.compare(b, a)), expectedSignum);
|
-signum(comparator.compare(b, a)), expectedSignum);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue