Search in sources :

Example 1 with Decoder

use of org.apache.hadoop.hbase.codec.Codec.Decoder in project hbase by apache.

the class TestBufferedDataBlockEncoder method testKVCodecWithTagsForDecodedCellsWithNoTags.

@Test
public void testKVCodecWithTagsForDecodedCellsWithNoTags() throws Exception {
    KeyValue kv1 = new KeyValue(Bytes.toBytes("r"), Bytes.toBytes("f"), Bytes.toBytes("1"), HConstants.LATEST_TIMESTAMP, Bytes.toBytes("1"));
    // kv1.getKey() return a copy of the Key bytes which starts from RK_length. Means from offsets,
    // we need to reduce the KL and VL parts.
    OnheapDecodedCell c1 = new OnheapDecodedCell(kv1.getKey(), kv1.getRowLength(), kv1.getFamilyOffset() - KeyValue.ROW_OFFSET, kv1.getFamilyLength(), kv1.getQualifierOffset() - KeyValue.ROW_OFFSET, kv1.getQualifierLength(), kv1.getTimestamp(), kv1.getTypeByte(), kv1.getValueArray(), kv1.getValueOffset(), kv1.getValueLength(), kv1.getSequenceId(), kv1.getTagsArray(), kv1.getTagsOffset(), kv1.getTagsLength());
    KeyValue kv2 = new KeyValue(Bytes.toBytes("r2"), Bytes.toBytes("f"), Bytes.toBytes("2"), HConstants.LATEST_TIMESTAMP, Bytes.toBytes("2"));
    OnheapDecodedCell c2 = new OnheapDecodedCell(kv2.getKey(), kv2.getRowLength(), kv2.getFamilyOffset() - KeyValue.ROW_OFFSET, kv2.getFamilyLength(), kv2.getQualifierOffset() - KeyValue.ROW_OFFSET, kv2.getQualifierLength(), kv2.getTimestamp(), kv2.getTypeByte(), kv2.getValueArray(), kv2.getValueOffset(), kv2.getValueLength(), kv2.getSequenceId(), kv2.getTagsArray(), kv2.getTagsOffset(), kv2.getTagsLength());
    KeyValue kv3 = new KeyValue(Bytes.toBytes("r3"), Bytes.toBytes("cf"), Bytes.toBytes("qual"), HConstants.LATEST_TIMESTAMP, Bytes.toBytes("3"));
    OffheapDecodedCell c3 = new OffheapDecodedCell(ByteBuffer.wrap(kv2.getKey()), kv2.getRowLength(), kv2.getFamilyOffset() - KeyValue.ROW_OFFSET, kv2.getFamilyLength(), kv2.getQualifierOffset() - KeyValue.ROW_OFFSET, kv2.getQualifierLength(), kv2.getTimestamp(), kv2.getTypeByte(), ByteBuffer.wrap(kv2.getValueArray()), kv2.getValueOffset(), kv2.getValueLength(), kv2.getSequenceId(), ByteBuffer.wrap(kv2.getTagsArray()), kv2.getTagsOffset(), kv2.getTagsLength());
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    KeyValueCodecWithTags codec = new KeyValueCodecWithTags();
    Encoder encoder = codec.getEncoder(os);
    encoder.write(c1);
    encoder.write(c2);
    encoder.write(c3);
    ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
    Decoder decoder = codec.getDecoder(is);
    assertTrue(decoder.advance());
    assertTrue(CellUtil.equals(c1, decoder.current()));
    assertTrue(decoder.advance());
    assertTrue(CellUtil.equals(c2, decoder.current()));
    assertTrue(decoder.advance());
    assertTrue(CellUtil.equals(c3, decoder.current()));
    assertFalse(decoder.advance());
}
Also used : KeyValueCodecWithTags(org.apache.hadoop.hbase.codec.KeyValueCodecWithTags) OffheapDecodedCell(org.apache.hadoop.hbase.io.encoding.BufferedDataBlockEncoder.OffheapDecodedCell) KeyValue(org.apache.hadoop.hbase.KeyValue) ByteArrayInputStream(java.io.ByteArrayInputStream) Encoder(org.apache.hadoop.hbase.codec.Codec.Encoder) OnheapDecodedCell(org.apache.hadoop.hbase.io.encoding.BufferedDataBlockEncoder.OnheapDecodedCell) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Decoder(org.apache.hadoop.hbase.codec.Codec.Decoder) Test(org.junit.Test)

Example 2 with Decoder

use of org.apache.hadoop.hbase.codec.Codec.Decoder in project hbase by apache.

the class TestWALCellCodecWithCompression method doTest.

private void doTest(boolean compressTags, boolean offheapKV) throws Exception {
    Configuration conf = new Configuration(false);
    conf.setBoolean(CompressionContext.ENABLE_WAL_TAGS_COMPRESSION, compressTags);
    WALCellCodec codec = new WALCellCodec(conf, new CompressionContext(LRUDictionary.class, false, compressTags));
    ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
    Encoder encoder = codec.getEncoder(bos);
    if (offheapKV) {
        encoder.write(createOffheapKV(1));
        encoder.write(createOffheapKV(0));
        encoder.write(createOffheapKV(2));
    } else {
        encoder.write(createKV(1));
        encoder.write(createKV(0));
        encoder.write(createKV(2));
    }
    InputStream is = new ByteArrayInputStream(bos.toByteArray());
    Decoder decoder = codec.getDecoder(is);
    decoder.advance();
    KeyValue kv = (KeyValue) decoder.current();
    List<Tag> tags = kv.getTags();
    assertEquals(1, tags.size());
    assertEquals("tagValue1", Bytes.toString(TagUtil.cloneValue(tags.get(0))));
    decoder.advance();
    kv = (KeyValue) decoder.current();
    tags = kv.getTags();
    assertEquals(0, tags.size());
    decoder.advance();
    kv = (KeyValue) decoder.current();
    tags = kv.getTags();
    assertEquals(2, tags.size());
    assertEquals("tagValue1", Bytes.toString(TagUtil.cloneValue(tags.get(0))));
    assertEquals("tagValue2", Bytes.toString(TagUtil.cloneValue(tags.get(1))));
}
Also used : ByteBufferKeyValue(org.apache.hadoop.hbase.ByteBufferKeyValue) KeyValue(org.apache.hadoop.hbase.KeyValue) Configuration(org.apache.hadoop.conf.Configuration) ByteArrayInputStream(java.io.ByteArrayInputStream) LRUDictionary(org.apache.hadoop.hbase.io.util.LRUDictionary) Encoder(org.apache.hadoop.hbase.codec.Codec.Encoder) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ArrayBackedTag(org.apache.hadoop.hbase.ArrayBackedTag) Tag(org.apache.hadoop.hbase.Tag) Decoder(org.apache.hadoop.hbase.codec.Codec.Decoder)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 KeyValue (org.apache.hadoop.hbase.KeyValue)2 Decoder (org.apache.hadoop.hbase.codec.Codec.Decoder)2 Encoder (org.apache.hadoop.hbase.codec.Codec.Encoder)2 InputStream (java.io.InputStream)1 Configuration (org.apache.hadoop.conf.Configuration)1 ArrayBackedTag (org.apache.hadoop.hbase.ArrayBackedTag)1 ByteBufferKeyValue (org.apache.hadoop.hbase.ByteBufferKeyValue)1 Tag (org.apache.hadoop.hbase.Tag)1 KeyValueCodecWithTags (org.apache.hadoop.hbase.codec.KeyValueCodecWithTags)1 OffheapDecodedCell (org.apache.hadoop.hbase.io.encoding.BufferedDataBlockEncoder.OffheapDecodedCell)1 OnheapDecodedCell (org.apache.hadoop.hbase.io.encoding.BufferedDataBlockEncoder.OnheapDecodedCell)1 LRUDictionary (org.apache.hadoop.hbase.io.util.LRUDictionary)1 Test (org.junit.Test)1