HBASE-9348 TerminatedWrapper error decoding, skipping skippable types (Nick)

git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1517858 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Zhihong Yu 2013-08-27 16:06:09 +00:00
parent e075bcd699
commit 91ef1fda93
2 changed files with 59 additions and 11 deletions

View File

@ -107,7 +107,9 @@ public class TerminatedWrapper<T> implements DataType<T> {
@Override @Override
public int skip(PositionedByteRange src) { public int skip(PositionedByteRange src) {
if (wrapped.isSkippable()) { if (wrapped.isSkippable()) {
return wrapped.skip(src); int ret = wrapped.skip(src);
src.setPosition(src.getPosition() + term.length);
return ret + term.length;
} else { } else {
// find the terminator position // find the terminator position
final int start = src.getPosition(); final int start = src.getPosition();
@ -122,7 +124,9 @@ public class TerminatedWrapper<T> implements DataType<T> {
@Override @Override
public T decode(PositionedByteRange src) { public T decode(PositionedByteRange src) {
if (wrapped.isSkippable()) { if (wrapped.isSkippable()) {
return wrapped.decode(src); T ret = wrapped.decode(src);
src.setPosition(src.getPosition() + term.length);
return ret;
} else { } else {
// find the terminator position // find the terminator position
int term = terminatorPosition(src); int term = terminatorPosition(src);

View File

@ -31,13 +31,18 @@ import org.junit.experimental.categories.Category;
@Category(SmallTests.class) @Category(SmallTests.class)
public class TestTerminatedWrapper { public class TestTerminatedWrapper {
static final byte[][] VALUES = new byte[][] { static final String[] VALUES_STRINGS = new String[] {
Bytes.toBytes(""), Bytes.toBytes("1"), Bytes.toBytes("22"), Bytes.toBytes("333"), "", "1", "22", "333", "4444", "55555", "666666", "7777777", "88888888", "999999999",
Bytes.toBytes("4444"), Bytes.toBytes("55555"), Bytes.toBytes("666666"),
Bytes.toBytes("7777777"), Bytes.toBytes("88888888"), Bytes.toBytes("999999999"),
}; };
static final byte[][] TERMINATORS = new byte[][] { new byte[] { -1 }, Bytes.toBytes("foo") }; static final byte[][] VALUES_BYTES = new byte[VALUES_STRINGS.length][];
static {
for (int i = 0; i < VALUES_STRINGS.length; i++) {
VALUES_BYTES[i] = Bytes.toBytes(VALUES_STRINGS[i]);
}
}
static final byte[][] TERMINATORS = new byte[][] { new byte[] { -2 }, Bytes.toBytes("foo") };
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testEmptyDelimiter() { public void testEmptyDelimiter() {
@ -58,11 +63,30 @@ public class TestTerminatedWrapper {
} }
@Test @Test
public void testReadWrite() { public void testReadWriteSkippable() {
PositionedByteRange buff = new SimplePositionedByteRange(14);
for (OrderedString t : new OrderedString[] {
OrderedString.ASCENDING, OrderedString.DESCENDING
}) {
for (byte[] term : TERMINATORS) {
for (String val : VALUES_STRINGS) {
buff.setPosition(0);
DataType<String> type = new TerminatedWrapper<String>(t, term);
assertEquals(val.length() + 2 + term.length, type.encode(buff, val));
buff.setPosition(0);
assertEquals(val, type.decode(buff));
assertEquals(val.length() + 2 + term.length, buff.getPosition());
}
}
}
}
@Test
public void testReadWriteNonSkippable() {
PositionedByteRange buff = new SimplePositionedByteRange(12); PositionedByteRange buff = new SimplePositionedByteRange(12);
for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) { for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
for (byte[] term : TERMINATORS) { for (byte[] term : TERMINATORS) {
for (byte[] val : VALUES) { for (byte[] val : VALUES_BYTES) {
buff.setPosition(0); buff.setPosition(0);
DataType<byte[]> type = new TerminatedWrapper<byte[]>(new RawBytes(ord), term); DataType<byte[]> type = new TerminatedWrapper<byte[]>(new RawBytes(ord), term);
assertEquals(val.length + term.length, type.encode(buff, val)); assertEquals(val.length + term.length, type.encode(buff, val));
@ -75,11 +99,31 @@ public class TestTerminatedWrapper {
} }
@Test @Test
public void testSkip() { public void testSkipSkippable() {
PositionedByteRange buff = new SimplePositionedByteRange(14);
for (OrderedString t : new OrderedString[] {
OrderedString.ASCENDING, OrderedString.DESCENDING
}) {
for (byte[] term : TERMINATORS) {
for (String val : VALUES_STRINGS) {
buff.setPosition(0);
DataType<String> type = new TerminatedWrapper<String>(t, term);
int expected = val.length() + 2 + term.length;
assertEquals(expected, type.encode(buff, val));
buff.setPosition(0);
assertEquals(expected, type.skip(buff));
assertEquals(expected, buff.getPosition());
}
}
}
}
@Test
public void testSkipNonSkippable() {
PositionedByteRange buff = new SimplePositionedByteRange(12); PositionedByteRange buff = new SimplePositionedByteRange(12);
for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) { for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
for (byte[] term : TERMINATORS) { for (byte[] term : TERMINATORS) {
for (byte[] val : VALUES) { for (byte[] val : VALUES_BYTES) {
buff.setPosition(0); buff.setPosition(0);
DataType<byte[]> type = new TerminatedWrapper<byte[]>(new RawBytes(ord), term); DataType<byte[]> type = new TerminatedWrapper<byte[]>(new RawBytes(ord), term);
int expected = type.encode(buff, val); int expected = type.encode(buff, val);