Search in sources :

Example 11 with PositionedByteRange

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]));
    }
}
Also used : SimplePositionedMutableByteRange(org.apache.hadoop.hbase.util.SimplePositionedMutableByteRange) PositionedByteRange(org.apache.hadoop.hbase.util.PositionedByteRange) Test(org.junit.Test)

Example 12 with PositionedByteRange

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;
}
Also used : PositionedByteRange(org.apache.hadoop.hbase.util.PositionedByteRange)

Example 13 with PositionedByteRange

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);
}
Also used : HColumnDescriptor(org.apache.hadoop.hbase.HColumnDescriptor) BufferedMutator(org.apache.hadoop.hbase.client.BufferedMutator) SimplePositionedMutableByteRange(org.apache.hadoop.hbase.util.SimplePositionedMutableByteRange) PositionedByteRange(org.apache.hadoop.hbase.util.PositionedByteRange) Put(org.apache.hadoop.hbase.client.Put) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor)

Example 14 with PositionedByteRange

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));
}
Also used : SimplePositionedMutableByteRange(org.apache.hadoop.hbase.util.SimplePositionedMutableByteRange) PositionedByteRange(org.apache.hadoop.hbase.util.PositionedByteRange) Test(org.junit.Test)

Example 15 with PositionedByteRange

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());
            }
        }
    }
}
Also used : SimplePositionedMutableByteRange(org.apache.hadoop.hbase.util.SimplePositionedMutableByteRange) PositionedByteRange(org.apache.hadoop.hbase.util.PositionedByteRange) Test(org.junit.Test)

Aggregations

PositionedByteRange (org.apache.hadoop.hbase.util.PositionedByteRange)32 SimplePositionedMutableByteRange (org.apache.hadoop.hbase.util.SimplePositionedMutableByteRange)30 Test (org.junit.Test)20 HColumnDescriptor (org.apache.hadoop.hbase.HColumnDescriptor)8 HTableDescriptor (org.apache.hadoop.hbase.HTableDescriptor)8 BufferedMutator (org.apache.hadoop.hbase.client.BufferedMutator)8 Put (org.apache.hadoop.hbase.client.Put)8 Order (org.apache.hadoop.hbase.util.Order)4 ByteBuf (io.netty.buffer.ByteBuf)2 FunctionCall (org.apache.drill.common.expression.FunctionCall)2 LogicalExpression (org.apache.drill.common.expression.LogicalExpression)2 SchemaPath (org.apache.drill.common.expression.SchemaPath)2 BooleanExpression (org.apache.drill.common.expression.ValueExpressions.BooleanExpression)2 DateExpression (org.apache.drill.common.expression.ValueExpressions.DateExpression)2 DoubleExpression (org.apache.drill.common.expression.ValueExpressions.DoubleExpression)2 FloatExpression (org.apache.drill.common.expression.ValueExpressions.FloatExpression)2 IntExpression (org.apache.drill.common.expression.ValueExpressions.IntExpression)2 LongExpression (org.apache.drill.common.expression.ValueExpressions.LongExpression)2 QuotedString (org.apache.drill.common.expression.ValueExpressions.QuotedString)2 TimeExpression (org.apache.drill.common.expression.ValueExpressions.TimeExpression)2