Search in sources :

Example 11 with SingleByteBuff

use of org.apache.hadoop.hbase.nio.SingleByteBuff in project hbase by apache.

the class TestCellBlockBuilder method doBuildCellBlockUndoCellBlock.

static void doBuildCellBlockUndoCellBlock(final CellBlockBuilder builder, final Codec codec, final CompressionCodec compressor, final int count, final int size, final boolean sized) throws IOException {
    Cell[] cells = getCells(count, size);
    CellScanner cellScanner = sized ? getSizedCellScanner(cells) : CellUtil.createCellScanner(Arrays.asList(cells).iterator());
    ByteBuffer bb = builder.buildCellBlock(codec, compressor, cellScanner);
    cellScanner = builder.createCellScannerReusingBuffers(codec, compressor, new SingleByteBuff(bb));
    int i = 0;
    while (cellScanner.advance()) {
        i++;
    }
    assertEquals(count, i);
}
Also used : SingleByteBuff(org.apache.hadoop.hbase.nio.SingleByteBuff) Cell(org.apache.hadoop.hbase.Cell) CellScanner(org.apache.hadoop.hbase.CellScanner) SizedCellScanner(org.apache.hadoop.hbase.io.SizedCellScanner) ByteBuffer(java.nio.ByteBuffer)

Example 12 with SingleByteBuff

use of org.apache.hadoop.hbase.nio.SingleByteBuff in project hbase by apache.

the class TestTagCompressionContext method testCompressUncompressTagsWithOffheapKeyValue1.

@Test
public void testCompressUncompressTagsWithOffheapKeyValue1() throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream daos = new ByteBufferWriterDataOutputStream(baos);
    TagCompressionContext context = new TagCompressionContext(LRUDictionary.class, Byte.MAX_VALUE);
    ByteBufferExtendedCell kv1 = (ByteBufferExtendedCell) createOffheapKVWithTags(2);
    int tagsLength1 = kv1.getTagsLength();
    context.compressTags(daos, kv1.getTagsByteBuffer(), kv1.getTagsPosition(), tagsLength1);
    ByteBufferExtendedCell kv2 = (ByteBufferExtendedCell) createOffheapKVWithTags(3);
    int tagsLength2 = kv2.getTagsLength();
    context.compressTags(daos, kv2.getTagsByteBuffer(), kv2.getTagsPosition(), tagsLength2);
    context.clear();
    byte[] dest = new byte[tagsLength1];
    ByteBuffer ob = ByteBuffer.wrap(baos.getBuffer());
    context.uncompressTags(new SingleByteBuff(ob), dest, 0, tagsLength1);
    assertTrue(Bytes.equals(kv1.getTagsArray(), kv1.getTagsOffset(), tagsLength1, dest, 0, tagsLength1));
    dest = new byte[tagsLength2];
    context.uncompressTags(new SingleByteBuff(ob), dest, 0, tagsLength2);
    assertTrue(Bytes.equals(kv2.getTagsArray(), kv2.getTagsOffset(), tagsLength2, dest, 0, tagsLength2));
}
Also used : DataOutputStream(java.io.DataOutputStream) SingleByteBuff(org.apache.hadoop.hbase.nio.SingleByteBuff) ByteBufferExtendedCell(org.apache.hadoop.hbase.ByteBufferExtendedCell) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 13 with SingleByteBuff

use of org.apache.hadoop.hbase.nio.SingleByteBuff in project hbase by apache.

the class TestByteBufferArray method testAsSubByteBuff.

private void testAsSubByteBuff(ByteBufferArray array, int off, int len, boolean isMulti) {
    ByteBuff ret = ByteBuff.wrap(array.asSubByteBuffers(off, len));
    if (isMulti) {
        assertTrue(ret instanceof MultiByteBuff);
    } else {
        assertTrue(ret instanceof SingleByteBuff);
    }
    assertTrue(!ret.hasArray());
    assertEquals(len, ret.remaining());
    ByteBuff tmp = createByteBuff(len);
    int pos = tmp.position(), lim = tmp.limit();
    try {
        assertEquals(len, array.read(off, tmp));
        assertEquals(0, tmp.remaining());
    } finally {
        tmp.position(pos).limit(lim);
    }
    assertByteBuffEquals(ret, tmp);
}
Also used : SingleByteBuff(org.apache.hadoop.hbase.nio.SingleByteBuff) MultiByteBuff(org.apache.hadoop.hbase.nio.MultiByteBuff) MultiByteBuff(org.apache.hadoop.hbase.nio.MultiByteBuff) SingleByteBuff(org.apache.hadoop.hbase.nio.SingleByteBuff) ByteBuff(org.apache.hadoop.hbase.nio.ByteBuff)

Example 14 with SingleByteBuff

use of org.apache.hadoop.hbase.nio.SingleByteBuff in project hbase by apache.

the class TestBlockIOUtils method testReadWithExtra.

@Test
public void testReadWithExtra() throws IOException {
    FileSystem fs = TEST_UTIL.getTestFileSystem();
    Path p = new Path(TEST_UTIL.getDataTestDirOnTestFS(), "testReadWithExtra");
    String s = "hello world";
    try (FSDataOutputStream out = fs.create(p)) {
        out.writeBytes(s);
    }
    ByteBuff buf = new SingleByteBuff(ByteBuffer.allocate(8));
    try (FSDataInputStream in = fs.open(p)) {
        assertTrue(BlockIOUtils.readWithExtra(buf, in, 6, 2));
    }
    buf.rewind();
    byte[] heapBuf = new byte[buf.capacity()];
    buf.get(heapBuf, 0, heapBuf.length);
    assertArrayEquals(Bytes.toBytes("hello wo"), heapBuf);
    buf = new MultiByteBuff(ByteBuffer.allocate(4), ByteBuffer.allocate(4), ByteBuffer.allocate(4));
    try (FSDataInputStream in = fs.open(p)) {
        assertTrue(BlockIOUtils.readWithExtra(buf, in, 8, 3));
    }
    buf.rewind();
    heapBuf = new byte[11];
    buf.get(heapBuf, 0, heapBuf.length);
    assertArrayEquals(Bytes.toBytes("hello world"), heapBuf);
    buf.position(0).limit(12);
    try (FSDataInputStream in = fs.open(p)) {
        try {
            BlockIOUtils.readWithExtra(buf, in, 12, 0);
            fail("Should only read 11 bytes");
        } catch (IOException e) {
        }
    }
}
Also used : Path(org.apache.hadoop.fs.Path) FileSystem(org.apache.hadoop.fs.FileSystem) SingleByteBuff(org.apache.hadoop.hbase.nio.SingleByteBuff) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) MultiByteBuff(org.apache.hadoop.hbase.nio.MultiByteBuff) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream) MultiByteBuff(org.apache.hadoop.hbase.nio.MultiByteBuff) SingleByteBuff(org.apache.hadoop.hbase.nio.SingleByteBuff) ByteBuff(org.apache.hadoop.hbase.nio.ByteBuff) IOException(java.io.IOException) Test(org.junit.Test)

Example 15 with SingleByteBuff

use of org.apache.hadoop.hbase.nio.SingleByteBuff in project hbase by apache.

the class TestBlockIOUtils method testPositionalReadPrematureEOF.

@Test
public void testPositionalReadPrematureEOF() throws IOException {
    long position = 0;
    int bufOffset = 0;
    int necessaryLen = 10;
    int extraLen = 0;
    int totalLen = necessaryLen + extraLen;
    byte[] buf = new byte[totalLen];
    ByteBuff bb = new SingleByteBuff(ByteBuffer.wrap(buf, 0, totalLen));
    FSDataInputStream in = mock(FSDataInputStream.class);
    when(in.read(position, buf, bufOffset, totalLen)).thenReturn(9);
    when(in.read(position, buf, bufOffset, totalLen)).thenReturn(-1);
    when(in.hasCapability(anyString())).thenReturn(false);
    exception.expect(IOException.class);
    exception.expectMessage("EOF");
    BlockIOUtils.preadWithExtra(bb, in, position, necessaryLen, extraLen);
}
Also used : SingleByteBuff(org.apache.hadoop.hbase.nio.SingleByteBuff) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) MultiByteBuff(org.apache.hadoop.hbase.nio.MultiByteBuff) SingleByteBuff(org.apache.hadoop.hbase.nio.SingleByteBuff) ByteBuff(org.apache.hadoop.hbase.nio.ByteBuff) Test(org.junit.Test)

Aggregations

SingleByteBuff (org.apache.hadoop.hbase.nio.SingleByteBuff)47 ByteBuffer (java.nio.ByteBuffer)27 Test (org.junit.Test)27 MultiByteBuff (org.apache.hadoop.hbase.nio.MultiByteBuff)21 ByteBuff (org.apache.hadoop.hbase.nio.ByteBuff)19 FSDataInputStream (org.apache.hadoop.fs.FSDataInputStream)12 ArrayList (java.util.ArrayList)9 KeyValue (org.apache.hadoop.hbase.KeyValue)9 Cell (org.apache.hadoop.hbase.Cell)8 DataOutputStream (java.io.DataOutputStream)7 Path (org.apache.hadoop.fs.Path)7 FSDataOutputStream (org.apache.hadoop.fs.FSDataOutputStream)6 HFileContext (org.apache.hadoop.hbase.io.hfile.HFileContext)6 HFileContextBuilder (org.apache.hadoop.hbase.io.hfile.HFileContextBuilder)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 FSDataInputStreamWrapper (org.apache.hadoop.hbase.io.FSDataInputStreamWrapper)5 Compression (org.apache.hadoop.hbase.io.compress.Compression)4 Configuration (org.apache.hadoop.conf.Configuration)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Random (java.util.Random)2