use of org.apache.hadoop.hbase.util.PositionedByteRange in project hbase by apache.
the class TestStruct method testOrderPreservation.
@Test
@SuppressWarnings("unchecked")
public void testOrderPreservation() throws Exception {
Object[] vals = new Object[constructorArgs.length];
PositionedByteRange[] encodedGeneric = new PositionedByteRange[constructorArgs.length];
PositionedByteRange[] encodedSpecialized = new PositionedByteRange[constructorArgs.length];
Constructor<?> ctor = specialized.encodedClass().getConstructor(Object[].class);
for (int i = 0; i < vals.length; i++) {
vals[i] = ctor.newInstance(new Object[] { constructorArgs[i] });
encodedGeneric[i] = new SimplePositionedMutableByteRange(generic.encodedLength(constructorArgs[i]));
encodedSpecialized[i] = new SimplePositionedMutableByteRange(specialized.encodedLength(vals[i]));
}
// populate our arrays
for (int i = 0; i < vals.length; i++) {
generic.encode(encodedGeneric[i], constructorArgs[i]);
encodedGeneric[i].setPosition(0);
specialized.encode(encodedSpecialized[i], vals[i]);
encodedSpecialized[i].setPosition(0);
assertArrayEquals(encodedGeneric[i].getBytes(), encodedSpecialized[i].getBytes());
}
Arrays.sort(vals);
Arrays.sort(encodedGeneric);
Arrays.sort(encodedSpecialized);
for (int i = 0; i < vals.length; i++) {
assertEquals("Struct encoder does not preserve sort order at position " + i, vals[i], ctor.newInstance(new Object[] { generic.decode(encodedGeneric[i]) }));
assertEquals("Specialized encoder does not preserve sort order at position " + i, vals[i], specialized.decode(encodedSpecialized[i]));
}
}
use of org.apache.hadoop.hbase.util.PositionedByteRange in project hbase by apache.
the class TerminatedWrapper method encode.
/**
* Write instance {@code val} into buffer {@code dst}.
* @throws IllegalArgumentException when the encoded representation of
* {@code val} contains the {@code term} sequence.
*/
@Override
public int encode(PositionedByteRange dst, T val) {
final int start = dst.getPosition();
int written = wrapped.encode(dst, val);
PositionedByteRange b = dst.shallowCopy();
b.setLength(dst.getPosition());
b.setPosition(start);
if (-1 != terminatorPosition(b)) {
dst.setPosition(start);
throw new IllegalArgumentException("Encoded value contains terminator sequence.");
}
dst.put(term);
return written + term.length;
}
use of org.apache.hadoop.hbase.util.PositionedByteRange in project drill by apache.
the class TestTableGenerator method generateHBaseDatasetBigIntOBDesc.
public static void generateHBaseDatasetBigIntOBDesc(Connection conn, Admin admin, TableName tableName, int numberRegions) throws Exception {
if (admin.tableExists(tableName)) {
admin.disableTable(tableName);
admin.deleteTable(tableName);
}
HTableDescriptor desc = new HTableDescriptor(tableName);
desc.addFamily(new HColumnDescriptor(FAMILY_F));
if (numberRegions > 1) {
admin.createTable(desc, Arrays.copyOfRange(SPLIT_KEYS, 0, numberRegions - 1));
} else {
admin.createTable(desc);
}
BufferedMutator table = conn.getBufferedMutator(tableName);
long startTime = (long) 1438034423 * 1000;
for (long i = startTime; i <= startTime + 100; i++) {
byte[] bytes = new byte[9];
PositionedByteRange br = new SimplePositionedMutableByteRange(bytes, 0, 9);
OrderedBytes.encodeInt64(br, i, Order.DESCENDING);
Put p = new Put(bytes);
p.addColumn(FAMILY_F, COLUMN_C, String.format("value %d", i).getBytes());
table.mutate(p);
}
table.close();
admin.flush(tableName);
}
use of org.apache.hadoop.hbase.util.PositionedByteRange in project hbase by apache.
the class TestStructNullExtension method testNullableNullExtension.
/**
* Positive cases for null extension.
*/
@Test
public void testNullableNullExtension() {
// the following field members are used because they're all nullable
StructBuilder builder = new StructBuilder().add(OrderedNumeric.ASCENDING).add(OrderedString.ASCENDING);
Struct shorter = builder.toStruct();
Struct longer = builder.add(new TerminatedWrapper<>(OrderedString.ASCENDING, "/")).add(OrderedNumeric.ASCENDING).toStruct();
PositionedByteRange buf1 = new SimplePositionedMutableByteRange(7);
// => 2 bytes + 5 bytes
Object[] val1 = new Object[] { BigDecimal.ONE, "foo" };
assertEquals("Encoding shorter value wrote a surprising number of bytes.", buf1.getLength(), shorter.encode(buf1, val1));
int shortLen = buf1.getLength();
// test iterator
buf1.setPosition(0);
StructIterator it = longer.iterator(buf1);
it.skip();
it.skip();
assertEquals("Position should be at end. Broken test.", buf1.getLength(), buf1.getPosition());
assertEquals("Failed to skip null element with extended struct.", 0, it.skip());
assertEquals("Failed to skip null element with extended struct.", 0, it.skip());
buf1.setPosition(0);
it = longer.iterator(buf1);
assertEquals(BigDecimal.ONE, it.next());
assertEquals("foo", it.next());
assertEquals("Position should be at end. Broken test.", buf1.getLength(), buf1.getPosition());
assertNull("Failed to skip null element with extended struct.", it.next());
assertNull("Failed to skip null element with extended struct.", it.next());
// test Struct
buf1.setPosition(0);
assertArrayEquals("Simple struct decoding is broken.", val1, shorter.decode(buf1));
buf1.setPosition(0);
assertArrayEquals("Decoding short value with extended struct should append null elements.", Arrays.copyOf(val1, 4), longer.decode(buf1));
// test omission of trailing members
PositionedByteRange buf2 = new SimplePositionedMutableByteRange(7);
buf1.setPosition(0);
assertEquals("Encoding a short value with extended struct should have same result as using short struct.", shortLen, longer.encode(buf2, val1));
assertArrayEquals("Encoding a short value with extended struct should have same result as using short struct", buf1.getBytes(), buf2.getBytes());
// test null trailing members
// all fields are nullable, so nothing should hit the buffer.
// => 0 bytes
val1 = new Object[] { null, null, null, null };
buf1.set(0);
buf2.set(0);
assertEquals("Encoding null-truncated value wrote a surprising number of bytes.", buf1.getLength(), longer.encode(buf1, new Object[0]));
assertEquals("Encoding null-extended value wrote a surprising number of bytes.", buf1.getLength(), longer.encode(buf1, val1));
assertArrayEquals("Encoded unexpected result.", buf1.getBytes(), buf2.getBytes());
assertArrayEquals("Decoded unexpected result.", val1, longer.decode(buf2));
// all fields are nullable, so only 1 should hit the buffer.
// => 2 bytes
Object[] val2 = new Object[] { BigDecimal.ONE, null, null, null };
buf1.set(2);
buf2.set(2);
assertEquals("Encoding null-truncated value wrote a surprising number of bytes.", buf1.getLength(), longer.encode(buf1, Arrays.copyOf(val2, 1)));
assertEquals("Encoding null-extended value wrote a surprising number of bytes.", buf2.getLength(), longer.encode(buf2, val2));
assertArrayEquals("Encoded unexpected result.", buf1.getBytes(), buf2.getBytes());
buf2.setPosition(0);
assertArrayEquals("Decoded unexpected result.", val2, longer.decode(buf2));
// all fields are nullable, so only 1, null, "foo" should hit the buffer.
// => 2 bytes + 1 byte + 6 bytes
Object[] val3 = new Object[] { BigDecimal.ONE, null, "foo", null };
buf1.set(9);
buf2.set(9);
assertEquals("Encoding null-truncated value wrote a surprising number of bytes.", buf1.getLength(), longer.encode(buf1, Arrays.copyOf(val3, 3)));
assertEquals("Encoding null-extended value wrote a surprising number of bytes.", buf2.getLength(), longer.encode(buf2, val3));
assertArrayEquals("Encoded unexpected result.", buf1.getBytes(), buf2.getBytes());
buf2.setPosition(0);
assertArrayEquals("Decoded unexpected result.", val3, longer.decode(buf2));
}
use of org.apache.hadoop.hbase.util.PositionedByteRange in project hbase by apache.
the class TestTerminatedWrapper method testSkipSkippable.
@Test
public void testSkipSkippable() {
PositionedByteRange buff = new SimplePositionedMutableByteRange(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<>(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());
}
}
}
}
Aggregations