Search in sources :

Example 1 with ByteArrayOutputStream

use of org.apache.hadoop.hbase.io.ByteArrayOutputStream in project hbase by apache.

the class TestIndividualBytesFieldCell method testWriteIntoOutputStream.

/**
   * @param ic An instance of IndividualBytesFieldCell to compare.
   * @param kv An instance of KeyValue to compare.
   * @param withTags Whether to write tags.
   * @throws IOException
   */
private void testWriteIntoOutputStream(IndividualBytesFieldCell ic, KeyValue kv, boolean withTags) throws IOException {
    ByteArrayOutputStream outIC = new ByteArrayOutputStream(ic.getSerializedSize(withTags));
    ByteArrayOutputStream outKV = new ByteArrayOutputStream(kv.getSerializedSize(withTags));
    // compare the number of bytes written
    assertEquals(kv.write(outKV, withTags), ic.write(outIC, withTags));
    // compare the underlying byte array
    assertArrayEquals(outKV.getBuffer(), outIC.getBuffer());
}
Also used : ByteArrayOutputStream(org.apache.hadoop.hbase.io.ByteArrayOutputStream)

Example 2 with ByteArrayOutputStream

use of org.apache.hadoop.hbase.io.ByteArrayOutputStream in project hbase by apache.

the class TestDataBlockEncoders method encodeKeyValues.

static ByteBuffer encodeKeyValues(DataBlockEncoding encoding, List<KeyValue> kvs, HFileBlockEncodingContext encodingContext, boolean useOffheapData) throws IOException {
    DataBlockEncoder encoder = encoding.getEncoder();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    baos.write(HFILEBLOCK_DUMMY_HEADER);
    DataOutputStream dos = new DataOutputStream(baos);
    encoder.startBlockEncoding(encodingContext, dos);
    for (KeyValue kv : kvs) {
        encoder.encode(kv, encodingContext, dos);
    }
    encoder.endBlockEncoding(encodingContext, dos, baos.getBuffer());
    byte[] encodedData = new byte[baos.size() - ENCODED_DATA_OFFSET];
    System.arraycopy(baos.toByteArray(), ENCODED_DATA_OFFSET, encodedData, 0, encodedData.length);
    if (useOffheapData) {
        ByteBuffer bb = ByteBuffer.allocateDirect(encodedData.length);
        bb.put(encodedData);
        bb.rewind();
        return bb;
    }
    return ByteBuffer.wrap(encodedData);
}
Also used : KeyValue(org.apache.hadoop.hbase.KeyValue) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(org.apache.hadoop.hbase.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer)

Example 3 with ByteArrayOutputStream

use of org.apache.hadoop.hbase.io.ByteArrayOutputStream in project hbase by apache.

the class TestHFileDataBlockEncoder method writeBlock.

private void writeBlock(List<Cell> kvs, HFileContext fileContext, boolean useTags) throws IOException {
    HFileBlockEncodingContext context = new HFileBlockDefaultEncodingContext(blockEncoder.getDataBlockEncoding(), HConstants.HFILEBLOCK_DUMMY_HEADER, fileContext);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    baos.write(HConstants.HFILEBLOCK_DUMMY_HEADER);
    DataOutputStream dos = new DataOutputStream(baos);
    blockEncoder.startBlockEncoding(context, dos);
    for (Cell kv : kvs) {
        blockEncoder.encode(kv, context, dos);
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) HFileBlockDefaultEncodingContext(org.apache.hadoop.hbase.io.encoding.HFileBlockDefaultEncodingContext) HFileBlockEncodingContext(org.apache.hadoop.hbase.io.encoding.HFileBlockEncodingContext) ByteArrayOutputStream(org.apache.hadoop.hbase.io.ByteArrayOutputStream) Cell(org.apache.hadoop.hbase.Cell)

Example 4 with ByteArrayOutputStream

use of org.apache.hadoop.hbase.io.ByteArrayOutputStream in project hbase by apache.

the class TestHFileDataBlockEncoder method createBlockOnDisk.

private HFileBlock createBlockOnDisk(List<KeyValue> kvs, HFileBlock block, boolean useTags) throws IOException {
    int size;
    HFileBlockEncodingContext context = new HFileBlockDefaultEncodingContext(blockEncoder.getDataBlockEncoding(), HConstants.HFILEBLOCK_DUMMY_HEADER, block.getHFileContext());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    baos.write(block.getDummyHeaderForVersion());
    DataOutputStream dos = new DataOutputStream(baos);
    blockEncoder.startBlockEncoding(context, dos);
    for (KeyValue kv : kvs) {
        blockEncoder.encode(kv, context, dos);
    }
    blockEncoder.endBlockEncoding(context, dos, baos.getBuffer(), BlockType.DATA);
    byte[] encodedBytes = baos.toByteArray();
    size = encodedBytes.length - block.getDummyHeaderForVersion().length;
    return new HFileBlock(context.getBlockType(), size, size, -1, ByteBuffer.wrap(encodedBytes), HFileBlock.FILL_HEADER, 0, block.getOnDiskDataSizeWithHeader(), -1, block.getHFileContext());
}
Also used : KeyValue(org.apache.hadoop.hbase.KeyValue) DataOutputStream(java.io.DataOutputStream) HFileBlockDefaultEncodingContext(org.apache.hadoop.hbase.io.encoding.HFileBlockDefaultEncodingContext) HFileBlockEncodingContext(org.apache.hadoop.hbase.io.encoding.HFileBlockEncodingContext) ByteArrayOutputStream(org.apache.hadoop.hbase.io.ByteArrayOutputStream)

Example 5 with ByteArrayOutputStream

use of org.apache.hadoop.hbase.io.ByteArrayOutputStream in project hbase by apache.

the class TestDataBlockEncoders method testEncodersOnDataset.

private void testEncodersOnDataset(List<KeyValue> kvList, boolean includesMemstoreTS, boolean includesTags) throws IOException {
    ByteBuffer unencodedDataBuf = RedundantKVGenerator.convertKvToByteBuffer(kvList, includesMemstoreTS);
    HFileContext fileContext = new HFileContextBuilder().withIncludesMvcc(includesMemstoreTS).withIncludesTags(includesTags).build();
    for (DataBlockEncoding encoding : DataBlockEncoding.values()) {
        DataBlockEncoder encoder = encoding.getEncoder();
        if (encoder == null) {
            continue;
        }
        HFileBlockEncodingContext encodingContext = new HFileBlockDefaultEncodingContext(encoding, HFILEBLOCK_DUMMY_HEADER, fileContext);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        baos.write(HFILEBLOCK_DUMMY_HEADER);
        DataOutputStream dos = new DataOutputStream(baos);
        encoder.startBlockEncoding(encodingContext, dos);
        for (KeyValue kv : kvList) {
            encoder.encode(kv, encodingContext, dos);
        }
        encoder.endBlockEncoding(encodingContext, dos, baos.getBuffer());
        byte[] encodedData = baos.toByteArray();
        testAlgorithm(encodedData, unencodedDataBuf, encoder);
    }
}
Also used : KeyValue(org.apache.hadoop.hbase.KeyValue) DataOutputStream(java.io.DataOutputStream) HFileContextBuilder(org.apache.hadoop.hbase.io.hfile.HFileContextBuilder) ByteArrayOutputStream(org.apache.hadoop.hbase.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) HFileContext(org.apache.hadoop.hbase.io.hfile.HFileContext)

Aggregations

ByteArrayOutputStream (org.apache.hadoop.hbase.io.ByteArrayOutputStream)7 DataOutputStream (java.io.DataOutputStream)5 ByteBuffer (java.nio.ByteBuffer)4 KeyValue (org.apache.hadoop.hbase.KeyValue)4 Cell (org.apache.hadoop.hbase.Cell)2 HFileBlockDefaultEncodingContext (org.apache.hadoop.hbase.io.encoding.HFileBlockDefaultEncodingContext)2 HFileBlockEncodingContext (org.apache.hadoop.hbase.io.encoding.HFileBlockEncodingContext)2 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 IOException (java.io.IOException)1 InterruptedIOException (java.io.InterruptedIOException)1 ArrayList (java.util.ArrayList)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 FSDataOutputStream (org.apache.hadoop.fs.FSDataOutputStream)1 HFileContext (org.apache.hadoop.hbase.io.hfile.HFileContext)1 HFileContextBuilder (org.apache.hadoop.hbase.io.hfile.HFileContextBuilder)1 SingleByteBuff (org.apache.hadoop.hbase.nio.SingleByteBuff)1 CancelableProgressable (org.apache.hadoop.hbase.util.CancelableProgressable)1 DistributedFileSystem (org.apache.hadoop.hdfs.DistributedFileSystem)1