Search in sources :

Example 11 with ByteBufferKeyValue

use of org.apache.hadoop.hbase.ByteBufferKeyValue in project hbase by apache.

the class TestSeekTo method testSeekBeforeWithReSeekToInternals.

protected void testSeekBeforeWithReSeekToInternals(TagUsage tagUsage) throws IOException {
    Path p = makeNewFile(tagUsage);
    FileSystem fs = TEST_UTIL.getTestFileSystem();
    Configuration conf = TEST_UTIL.getConfiguration();
    HFile.Reader reader = HFile.createReader(fs, p, new CacheConfig(conf), true, conf);
    HFileScanner scanner = reader.getScanner(conf, false, true);
    assertFalse(scanner.seekBefore(toKV("a", tagUsage)));
    assertFalse(scanner.seekBefore(toKV("b", tagUsage)));
    assertFalse(scanner.seekBefore(toKV("c", tagUsage)));
    // seekBefore d, so the scanner points to c
    assertTrue(scanner.seekBefore(toKV("d", tagUsage)));
    assertFalse(scanner.getCell() instanceof ByteBufferKeyValue);
    assertEquals("c", toRowStr(scanner.getCell()));
    // reseekTo e and g
    assertEquals(0, scanner.reseekTo(toKV("c", tagUsage)));
    assertEquals("c", toRowStr(scanner.getCell()));
    assertEquals(0, scanner.reseekTo(toKV("g", tagUsage)));
    assertEquals("g", toRowStr(scanner.getCell()));
    // seekBefore e, so the scanner points to c
    assertTrue(scanner.seekBefore(toKV("e", tagUsage)));
    assertEquals("c", toRowStr(scanner.getCell()));
    // reseekTo e and g
    assertEquals(0, scanner.reseekTo(toKV("e", tagUsage)));
    assertEquals("e", toRowStr(scanner.getCell()));
    assertEquals(0, scanner.reseekTo(toKV("g", tagUsage)));
    assertEquals("g", toRowStr(scanner.getCell()));
    // seekBefore f, so the scanner points to e
    assertTrue(scanner.seekBefore(toKV("f", tagUsage)));
    assertEquals("e", toRowStr(scanner.getCell()));
    // reseekTo e and g
    assertEquals(0, scanner.reseekTo(toKV("e", tagUsage)));
    assertEquals("e", toRowStr(scanner.getCell()));
    assertEquals(0, scanner.reseekTo(toKV("g", tagUsage)));
    assertEquals("g", toRowStr(scanner.getCell()));
    // seekBefore g, so the scanner points to e
    assertTrue(scanner.seekBefore(toKV("g", tagUsage)));
    assertEquals("e", toRowStr(scanner.getCell()));
    // reseekTo e and g again
    assertEquals(0, scanner.reseekTo(toKV("e", tagUsage)));
    assertEquals("e", toRowStr(scanner.getCell()));
    assertEquals(0, scanner.reseekTo(toKV("g", tagUsage)));
    assertEquals("g", toRowStr(scanner.getCell()));
    // seekBefore h, so the scanner points to g
    assertTrue(scanner.seekBefore(toKV("h", tagUsage)));
    assertEquals("g", toRowStr(scanner.getCell()));
    // reseekTo g
    assertEquals(0, scanner.reseekTo(toKV("g", tagUsage)));
    assertEquals("g", toRowStr(scanner.getCell()));
    // seekBefore i, so the scanner points to g
    assertTrue(scanner.seekBefore(toKV("i", tagUsage)));
    assertEquals("g", toRowStr(scanner.getCell()));
    // reseekTo g
    assertEquals(0, scanner.reseekTo(toKV("g", tagUsage)));
    assertEquals("g", toRowStr(scanner.getCell()));
    // seekBefore j, so the scanner points to i
    assertTrue(scanner.seekBefore(toKV("j", tagUsage)));
    assertEquals("i", toRowStr(scanner.getCell()));
    // reseekTo i
    assertEquals(0, scanner.reseekTo(toKV("i", tagUsage)));
    assertEquals("i", toRowStr(scanner.getCell()));
    // seekBefore k, so the scanner points to i
    assertTrue(scanner.seekBefore(toKV("k", tagUsage)));
    assertEquals("i", toRowStr(scanner.getCell()));
    // reseekTo i and k
    assertEquals(0, scanner.reseekTo(toKV("i", tagUsage)));
    assertEquals("i", toRowStr(scanner.getCell()));
    assertEquals(0, scanner.reseekTo(toKV("k", tagUsage)));
    assertEquals("k", toRowStr(scanner.getCell()));
    // seekBefore l, so the scanner points to k
    assertTrue(scanner.seekBefore(toKV("l", tagUsage)));
    assertEquals("k", toRowStr(scanner.getCell()));
    // reseekTo k
    assertEquals(0, scanner.reseekTo(toKV("k", tagUsage)));
    assertEquals("k", toRowStr(scanner.getCell()));
    deleteTestDir(fs);
}
Also used : Path(org.apache.hadoop.fs.Path) ByteBufferKeyValue(org.apache.hadoop.hbase.ByteBufferKeyValue) Configuration(org.apache.hadoop.conf.Configuration) FileSystem(org.apache.hadoop.fs.FileSystem)

Example 12 with ByteBufferKeyValue

use of org.apache.hadoop.hbase.ByteBufferKeyValue in project hbase by apache.

the class TestResult method getByteBufferBackedTagResult.

private Result getByteBufferBackedTagResult(Tag tag) {
    List<Tag> tags = null;
    if (tag != null) {
        tags = Arrays.asList(tag);
    }
    KeyValue kvCell = new KeyValue(row, family, qual, 0L, KeyValue.Type.Put, value, tags);
    ByteBuffer buf = ByteBuffer.allocateDirect(kvCell.getBuffer().length);
    ByteBufferUtils.copyFromArrayToBuffer(buf, kvCell.getBuffer(), 0, kvCell.getBuffer().length);
    ByteBufferKeyValue bbKV = new ByteBufferKeyValue(buf, 0, buf.capacity(), 0L);
    return Result.create(new Cell[] { bbKV });
}
Also used : ByteBufferKeyValue(org.apache.hadoop.hbase.ByteBufferKeyValue) ByteBufferKeyValue(org.apache.hadoop.hbase.ByteBufferKeyValue) KeyValue(org.apache.hadoop.hbase.KeyValue) ArrayBackedTag(org.apache.hadoop.hbase.ArrayBackedTag) Tag(org.apache.hadoop.hbase.Tag) ByteBuffer(java.nio.ByteBuffer)

Example 13 with ByteBufferKeyValue

use of org.apache.hadoop.hbase.ByteBufferKeyValue in project hbase by apache.

the class TestMemstoreLABWithoutPool method testLABRandomAllocation.

/**
 * Test a bunch of random allocations
 */
@Test
public void testLABRandomAllocation() {
    Random rand = new Random();
    MemStoreLAB mslab = new MemStoreLABImpl();
    int expectedOff = 0;
    ByteBuffer lastBuffer = null;
    int lastChunkId = -1;
    // behavior
    for (int i = 0; i < 100000; i++) {
        int valSize = rand.nextInt(1000);
        KeyValue kv = new KeyValue(rk, cf, q, new byte[valSize]);
        int size = kv.getSerializedSize();
        ByteBufferKeyValue newKv = (ByteBufferKeyValue) mslab.copyCellInto(kv);
        if (newKv.getBuffer() != lastBuffer) {
            // since we add the chunkID at the 0th offset of the chunk and the
            // chunkid is an int we need to account for those 4 bytes
            expectedOff = Bytes.SIZEOF_INT;
            lastBuffer = newKv.getBuffer();
            int chunkId = newKv.getBuffer().getInt(0);
            assertTrue("chunkid should be different", chunkId != lastChunkId);
            lastChunkId = chunkId;
        }
        assertEquals(expectedOff, newKv.getOffset());
        assertTrue("Allocation overruns buffer", newKv.getOffset() + size <= newKv.getBuffer().capacity());
        expectedOff += size;
    }
}
Also used : ByteBufferKeyValue(org.apache.hadoop.hbase.ByteBufferKeyValue) ByteBufferKeyValue(org.apache.hadoop.hbase.ByteBufferKeyValue) KeyValue(org.apache.hadoop.hbase.KeyValue) Random(java.util.Random) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 14 with ByteBufferKeyValue

use of org.apache.hadoop.hbase.ByteBufferKeyValue in project hbase by apache.

the class TestMemStoreChunkPool method testReusingChunks.

@Test
public void testReusingChunks() {
    Random rand = new Random();
    MemStoreLAB mslab = new MemStoreLABImpl(conf);
    int expectedOff = 0;
    ByteBuffer lastBuffer = null;
    final byte[] rk = Bytes.toBytes("r1");
    final byte[] cf = Bytes.toBytes("f");
    final byte[] q = Bytes.toBytes("q");
    // Randomly allocate some bytes
    for (int i = 0; i < 100; i++) {
        int valSize = rand.nextInt(1000);
        KeyValue kv = new KeyValue(rk, cf, q, new byte[valSize]);
        int size = kv.getSerializedSize();
        ByteBufferKeyValue newKv = (ByteBufferKeyValue) mslab.copyCellInto(kv);
        if (newKv.getBuffer() != lastBuffer) {
            expectedOff = 4;
            lastBuffer = newKv.getBuffer();
        }
        assertEquals(expectedOff, newKv.getOffset());
        assertTrue("Allocation overruns buffer", newKv.getOffset() + size <= newKv.getBuffer().capacity());
        expectedOff += size;
    }
    // chunks will be put back to pool after close
    mslab.close();
    int chunkCount = chunkCreator.getPoolSize();
    assertTrue(chunkCount > 0);
    // reconstruct mslab
    mslab = new MemStoreLABImpl(conf);
    // chunk should be got from the pool, so we can reuse it.
    KeyValue kv = new KeyValue(rk, cf, q, new byte[10]);
    mslab.copyCellInto(kv);
    assertEquals(chunkCount - 1, chunkCreator.getPoolSize());
}
Also used : ByteBufferKeyValue(org.apache.hadoop.hbase.ByteBufferKeyValue) ByteBufferKeyValue(org.apache.hadoop.hbase.ByteBufferKeyValue) KeyValue(org.apache.hadoop.hbase.KeyValue) Random(java.util.Random) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 15 with ByteBufferKeyValue

use of org.apache.hadoop.hbase.ByteBufferKeyValue in project hbase by apache.

the class TestWALCellCodecWithCompression method createOffheapKV.

private ByteBufferKeyValue createOffheapKV(byte[] row, byte[] value, int noOfTags) {
    byte[] cf = Bytes.toBytes("myCF");
    byte[] q = Bytes.toBytes("myQualifier");
    List<Tag> tags = new ArrayList<>(noOfTags);
    for (int i = 1; i <= noOfTags; i++) {
        tags.add(new ArrayBackedTag((byte) i, Bytes.toBytes("tagValue" + i)));
    }
    KeyValue kv = new KeyValue(row, cf, q, HConstants.LATEST_TIMESTAMP, value, tags);
    ByteBuffer dbb = ByteBuffer.allocateDirect(kv.getBuffer().length);
    dbb.put(kv.getBuffer());
    return new ByteBufferKeyValue(dbb, 0, kv.getBuffer().length);
}
Also used : ByteBufferKeyValue(org.apache.hadoop.hbase.ByteBufferKeyValue) ByteBufferKeyValue(org.apache.hadoop.hbase.ByteBufferKeyValue) KeyValue(org.apache.hadoop.hbase.KeyValue) ArrayList(java.util.ArrayList) ArrayBackedTag(org.apache.hadoop.hbase.ArrayBackedTag) Tag(org.apache.hadoop.hbase.Tag) ArrayBackedTag(org.apache.hadoop.hbase.ArrayBackedTag) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ByteBufferKeyValue (org.apache.hadoop.hbase.ByteBufferKeyValue)26 KeyValue (org.apache.hadoop.hbase.KeyValue)22 ByteBuffer (java.nio.ByteBuffer)16 Cell (org.apache.hadoop.hbase.Cell)14 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)6 ArrayBackedTag (org.apache.hadoop.hbase.ArrayBackedTag)6 Random (java.util.Random)4 Tag (org.apache.hadoop.hbase.Tag)4 List (java.util.List)3 Configuration (org.apache.hadoop.conf.Configuration)3 Path (org.apache.hadoop.fs.Path)3 HashMap (java.util.HashMap)2 ExtendedCell (org.apache.hadoop.hbase.ExtendedCell)2 CellProtos (org.apache.hadoop.hbase.shaded.protobuf.generated.CellProtos)2 IOException (java.io.IOException)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 FileSystem (org.apache.hadoop.fs.FileSystem)1