private void testHeaderSizeInCacheWithoutChecksumInternals(boolean useTags) throws IOException { int headerSize = HConstants.HFILEBLOCK_HEADER_SIZE_NO_CHECKSUM; // Create some KVs and create the block with old-style header. List<KeyValue> kvs = generator.generateTestKeyValues(60, useTags); ByteBuffer keyValues = RedundantKVGenerator.convertKvToByteBuffer(kvs, includesMemstoreTS); int size = keyValues.limit(); ByteBuffer buf = ByteBuffer.allocate(size + headerSize); buf.position(headerSize); keyValues.rewind(); buf.put(keyValues); HFileContext hfileContext = new HFileContextBuilder().withHBaseCheckSum(false) .withIncludesMvcc(includesMemstoreTS) .withIncludesTags(useTags) .withBlockSize(0) .withChecksumType(ChecksumType.NULL) .build(); HFileBlock block = new HFileBlock(BlockType.DATA, size, size, -1, buf, HFileBlock.FILL_HEADER, 0, 0, hfileContext); HFileBlock cacheBlock = createBlockOnDisk(kvs, block, useTags); assertEquals(headerSize, cacheBlock.getDummyHeaderForVersion().length); }
private HFileBlock getSampleHFileBlock(List<KeyValue> kvs, boolean useTag) { ByteBuffer keyValues = RedundantKVGenerator.convertKvToByteBuffer(kvs, includesMemstoreTS); int size = keyValues.limit(); ByteBuffer buf = ByteBuffer.allocate(size + HConstants.HFILEBLOCK_HEADER_SIZE); buf.position(HConstants.HFILEBLOCK_HEADER_SIZE); keyValues.rewind(); buf.put(keyValues); HFileContext meta = new HFileContextBuilder() .withIncludesMvcc(includesMemstoreTS) .withIncludesTags(useTag) .withHBaseCheckSum(true) .withCompression(Algorithm.NONE) .withBlockSize(0) .withChecksumType(ChecksumType.NULL) .build(); HFileBlock b = new HFileBlock(BlockType.DATA, size, size, -1, buf, HFileBlock.FILL_HEADER, 0, 0, meta); return b; }
private void testHeaderSizeInCacheWithoutChecksumInternals(boolean useTags) throws IOException { int headerSize = HConstants.HFILEBLOCK_HEADER_SIZE_NO_CHECKSUM; // Create some KVs and create the block with old-style header. ByteBuffer keyValues = RedundantKVGenerator.convertKvToByteBuffer( generator.generateTestKeyValues(60, useTags), includesMemstoreTS); int size = keyValues.limit(); ByteBuffer buf = ByteBuffer.allocate(size + headerSize); buf.position(headerSize); keyValues.rewind(); buf.put(keyValues); HFileContext hfileContext = new HFileContextBuilder().withHBaseCheckSum(false) .withIncludesMvcc(includesMemstoreTS) .withIncludesTags(useTags) .withBlockSize(0) .withChecksumType(ChecksumType.NULL) .build(); HFileBlock block = new HFileBlock(BlockType.DATA, size, size, -1, buf, HFileBlock.FILL_HEADER, 0, 0, hfileContext); HFileBlock cacheBlock = createBlockOnDisk(block, useTags); assertEquals(headerSize, cacheBlock.getDummyHeaderForVersion().length); }
private HFileBlock getSampleHFileBlock(boolean useTag) { ByteBuffer keyValues = RedundantKVGenerator.convertKvToByteBuffer( generator.generateTestKeyValues(60, useTag), includesMemstoreTS); int size = keyValues.limit(); ByteBuffer buf = ByteBuffer.allocate(size + HConstants.HFILEBLOCK_HEADER_SIZE); buf.position(HConstants.HFILEBLOCK_HEADER_SIZE); keyValues.rewind(); buf.put(keyValues); HFileContext meta = new HFileContextBuilder() .withIncludesMvcc(includesMemstoreTS) .withIncludesTags(useTag) .withHBaseCheckSum(true) .withCompression(Algorithm.NONE) .withBlockSize(0) .withChecksumType(ChecksumType.NULL) .build(); HFileBlock b = new HFileBlock(BlockType.DATA, size, size, -1, buf, HFileBlock.FILL_HEADER, 0, 0, meta); return b; }
/** * Test data block encoding of empty KeyValue. * * @throws IOException * On test failure. */ @Test public void testEmptyKeyValues() throws IOException { List<KeyValue> kvList = new ArrayList<KeyValue>(); byte[] row = new byte[0]; byte[] family = new byte[0]; byte[] qualifier = new byte[0]; byte[] value = new byte[0]; if (!includesTags) { kvList.add(new KeyValue(row, family, qualifier, 0l, value)); kvList.add(new KeyValue(row, family, qualifier, 0l, value)); } else { byte[] metaValue1 = Bytes.toBytes("metaValue1"); byte[] metaValue2 = Bytes.toBytes("metaValue2"); kvList.add(new KeyValue(row, family, qualifier, 0l, value, new Tag[] { new Tag((byte) 1, metaValue1) })); kvList.add(new KeyValue(row, family, qualifier, 0l, value, new Tag[] { new Tag((byte) 1, metaValue2) })); } testEncodersOnDataset(RedundantKVGenerator.convertKvToByteBuffer(kvList, includesMemstoreTS), kvList); }
/** * Test KeyValues with negative timestamp. * * @throws IOException * On test failure. */ @Test public void testNegativeTimestamps() throws IOException { List<KeyValue> kvList = new ArrayList<KeyValue>(); byte[] row = new byte[0]; byte[] family = new byte[0]; byte[] qualifier = new byte[0]; byte[] value = new byte[0]; if (includesTags) { byte[] metaValue1 = Bytes.toBytes("metaValue1"); byte[] metaValue2 = Bytes.toBytes("metaValue2"); kvList.add(new KeyValue(row, family, qualifier, 0l, value, new Tag[] { new Tag((byte) 1, metaValue1) })); kvList.add(new KeyValue(row, family, qualifier, 0l, value, new Tag[] { new Tag((byte) 1, metaValue2) })); } else { kvList.add(new KeyValue(row, family, qualifier, -1l, Type.Put, value)); kvList.add(new KeyValue(row, family, qualifier, -2l, Type.Put, value)); } testEncodersOnDataset(RedundantKVGenerator.convertKvToByteBuffer(kvList, includesMemstoreTS), kvList); }
@Test public void testZeroByte() throws IOException { List<KeyValue> kvList = new ArrayList<KeyValue>(); byte[] row = Bytes.toBytes("abcd"); byte[] family = new byte[] { 'f' }; byte[] qualifier0 = new byte[] { 'b' }; byte[] qualifier1 = new byte[] { 'c' }; byte[] value0 = new byte[] { 'd' }; byte[] value1 = new byte[] { 0x00 }; if (includesTags) { kvList.add(new KeyValue(row, family, qualifier0, 0, value0, new Tag[] { new Tag((byte) 1, "value1") })); kvList.add(new KeyValue(row, family, qualifier1, 0, value1, new Tag[] { new Tag((byte) 1, "value1") })); } else { kvList.add(new KeyValue(row, family, qualifier0, 0, Type.Put, value0)); kvList.add(new KeyValue(row, family, qualifier1, 0, Type.Put, value1)); } testEncodersOnDataset(RedundantKVGenerator.convertKvToByteBuffer(kvList, includesMemstoreTS), kvList); }
/** Test for HBASE-5746. */ @Test public void testHeaderSizeInCacheWithoutChecksum() throws Exception { int headerSize = HConstants.HFILEBLOCK_HEADER_SIZE_NO_CHECKSUM; // Create some KVs and create the block with old-style header. ByteBuffer keyValues = RedundantKVGenerator.convertKvToByteBuffer( generator.generateTestKeyValues(60), includesMemstoreTS); int size = keyValues.limit(); ByteBuffer buf = ByteBuffer.allocate(size + headerSize); buf.position(headerSize); keyValues.rewind(); buf.put(keyValues); HFileBlock block = new HFileBlock(BlockType.DATA, size, size, -1, buf, HFileBlock.FILL_HEADER, 0, includesMemstoreTS, HFileBlock.MINOR_VERSION_NO_CHECKSUM, 0, ChecksumType.NULL.getCode(), 0); HFileBlock cacheBlock = createBlockOnDisk(block); assertEquals(headerSize, cacheBlock.getDummyHeaderForVersion().length); }
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, HConstants.HFILEBLOCK_DUMMY_HEADER, fileContext); ByteArrayOutputStream baos = new ByteArrayOutputStream(); baos.write(HConstants.HFILEBLOCK_DUMMY_HEADER); DataOutputStream dos = new DataOutputStream(baos); encoder.startBlockEncoding(encodingContext, dos); for (KeyValue kv : kvList) { encoder.encode(kv, encodingContext, dos); } BufferGrabbingByteArrayOutputStream stream = new BufferGrabbingByteArrayOutputStream(); baos.writeTo(stream); encoder.endBlockEncoding(encodingContext, dos, stream.getBuffer()); byte[] encodedData = baos.toByteArray(); testAlgorithm(encodedData, unencodedDataBuf, encoder); } }
/** * Test whether compression -> decompression gives the consistent results on * pseudorandom sample. * @throws IOException On test failure. */ @Test public void testExecutionOnSample() throws IOException { List<KeyValue> kvList = generator.generateTestKeyValues(NUMBER_OF_KV, includesTags); testEncodersOnDataset(RedundantKVGenerator.convertKvToByteBuffer(kvList, includesMemstoreTS), kvList); }
/** * Test whether the decompression of first key is implemented correctly. */ @Test public void testFirstKeyInBlockOnSample() { List<KeyValue> sampleKv = generator.generateTestKeyValues(NUMBER_OF_KV, includesTags); ByteBuffer originalBuffer = RedundantKVGenerator.convertKvToByteBuffer(sampleKv, includesMemstoreTS); for (DataBlockEncoding encoding : DataBlockEncoding.values()) { if (encoding.getEncoder() == null) { continue; } DataBlockEncoder encoder = encoding.getEncoder(); ByteBuffer encodedBuffer = null; try { encodedBuffer = ByteBuffer.wrap(encodeBytes(encoding, originalBuffer)); } catch (IOException e) { throw new RuntimeException(String.format("Bug while encoding using '%s'", encoder.toString()), e); } ByteBuffer keyBuffer = encoder.getFirstKeyInBlock(encodedBuffer); KeyValue firstKv = sampleKv.get(0); if (0 != Bytes.compareTo(keyBuffer.array(), keyBuffer.arrayOffset(), keyBuffer.limit(), firstKv.getBuffer(), firstKv.getKeyOffset(), firstKv.getKeyLength())) { int commonPrefix = 0; int length = Math.min(keyBuffer.limit(), firstKv.getKeyLength()); while (commonPrefix < length && keyBuffer.array()[keyBuffer.arrayOffset() + commonPrefix] == firstKv.getBuffer()[firstKv .getKeyOffset() + commonPrefix]) { commonPrefix++; } fail(String.format("Bug in '%s' commonPrefix %d", encoder.toString(), commonPrefix)); } } }
private HFileBlock getSampleHFileBlock() { ByteBuffer keyValues = RedundantKVGenerator.convertKvToByteBuffer( generator.generateTestKeyValues(60), includesMemstoreTS); int size = keyValues.limit(); ByteBuffer buf = ByteBuffer.allocate(size + HConstants.HFILEBLOCK_HEADER_SIZE); buf.position(HConstants.HFILEBLOCK_HEADER_SIZE); keyValues.rewind(); buf.put(keyValues); HFileBlock b = new HFileBlock(BlockType.DATA, size, size, -1, buf, HFileBlock.FILL_HEADER, 0, includesMemstoreTS, HFileReaderV2.MAX_MINOR_VERSION, 0, ChecksumType.NULL.getCode(), 0); return b; }
/** * Test data block encoding of empty KeyValue. * @throws IOException On test failure. */ @Test public void testEmptyKeyValues() throws IOException { List<KeyValue> kvList = new ArrayList<KeyValue>(); byte[] row = new byte[0]; byte[] family = new byte[0]; byte[] qualifier = new byte[0]; byte[] value = new byte[0]; kvList.add(new KeyValue(row, family, qualifier, 0l, Type.Put, value)); kvList.add(new KeyValue(row, family, qualifier, 0l, Type.Put, value)); testEncodersOnDataset(RedundantKVGenerator.convertKvToByteBuffer(kvList, includesMemstoreTS)); }
/** * Test KeyValues with negative timestamp. * @throws IOException On test failure. */ @Test public void testNegativeTimestamps() throws IOException { List<KeyValue> kvList = new ArrayList<KeyValue>(); byte[] row = new byte[0]; byte[] family = new byte[0]; byte[] qualifier = new byte[0]; byte[] value = new byte[0]; kvList.add(new KeyValue(row, family, qualifier, -1l, Type.Put, value)); kvList.add(new KeyValue(row, family, qualifier, -2l, Type.Put, value)); testEncodersOnDataset( RedundantKVGenerator.convertKvToByteBuffer(kvList, includesMemstoreTS)); }
/** * Test whether compression -> decompression gives the consistent results on * pseudorandom sample. * @throws IOException On test failure. */ @Test public void testExecutionOnSample() throws IOException { testEncodersOnDataset( RedundantKVGenerator.convertKvToByteBuffer( generator.generateTestKeyValues(NUMBER_OF_KV), includesMemstoreTS)); }
/** * Test whether the decompression of first key is implemented correctly. */ @Test public void testFirstKeyInBlockOnSample() { List<KeyValue> sampleKv = generator.generateTestKeyValues(NUMBER_OF_KV); ByteBuffer originalBuffer = RedundantKVGenerator.convertKvToByteBuffer(sampleKv, includesMemstoreTS); for (DataBlockEncoding encoding : DataBlockEncoding.values()) { if (encoding.getEncoder() == null) { continue; } DataBlockEncoder encoder = encoding.getEncoder(); ByteBuffer encodedBuffer = null; try { encodedBuffer = ByteBuffer.wrap(encodeBytes(encoding, originalBuffer)); } catch (IOException e) { throw new RuntimeException(String.format( "Bug while encoding using '%s'", encoder.toString()), e); } ByteBuffer keyBuffer = encoder.getFirstKeyInBlock(encodedBuffer); KeyValue firstKv = sampleKv.get(0); if (0 != Bytes.compareTo( keyBuffer.array(), keyBuffer.arrayOffset(), keyBuffer.limit(), firstKv.getBuffer(), firstKv.getKeyOffset(), firstKv.getKeyLength())) { int commonPrefix = 0; int length = Math.min(keyBuffer.limit(), firstKv.getKeyLength()); while (commonPrefix < length && keyBuffer.array()[keyBuffer.arrayOffset() + commonPrefix] == firstKv.getBuffer()[firstKv.getKeyOffset() + commonPrefix]) { commonPrefix++; } fail(String.format("Bug in '%s' commonPrefix %d", encoder.toString(), commonPrefix)); } } }
private HFileBlock getSampleHFileBlock() { ByteBuffer keyValues = RedundantKVGenerator.convertKvToByteBuffer( generator.generateTestKeyValues(60), includesMemstoreTS); int size = keyValues.limit(); ByteBuffer buf = ByteBuffer.allocate(size + HFileBlock.HEADER_SIZE); buf.position(HFileBlock.HEADER_SIZE); keyValues.rewind(); buf.put(keyValues); HFileBlock b = new HFileBlock(BlockType.DATA, size, size, -1, buf, HFileBlock.FILL_HEADER, 0, includesMemstoreTS, HFileReaderV2.MAX_MINOR_VERSION, 0, ChecksumType.NULL.getCode(), 0); return b; }
@Test public void testNextOnSample() { List<KeyValue> sampleKv = generator.generateTestKeyValues(NUMBER_OF_KV, includesTags); ByteBuffer originalBuffer = RedundantKVGenerator.convertKvToByteBuffer(sampleKv, includesMemstoreTS); for (DataBlockEncoding encoding : DataBlockEncoding.values()) { if (encoding.getEncoder() == null) { continue; } DataBlockEncoder encoder = encoding.getEncoder(); ByteBuffer encodedBuffer = null; try { encodedBuffer = ByteBuffer.wrap(encodeBytes(encoding, originalBuffer)); } catch (IOException e) { throw new RuntimeException(String.format("Bug while encoding using '%s'", encoder.toString()), e); } HFileContext meta = new HFileContextBuilder() .withHBaseCheckSum(false) .withIncludesMvcc(includesMemstoreTS) .withIncludesTags(includesTags) .withCompression(Compression.Algorithm.NONE) .build(); DataBlockEncoder.EncodedSeeker seeker = encoder.createSeeker(KeyValue.COMPARATOR, encoder.newDataBlockDecodingContext(meta)); seeker.setCurrentBuffer(encodedBuffer); int i = 0; do { KeyValue expectedKeyValue = sampleKv.get(i); ByteBuffer keyValue = seeker.getKeyValueBuffer(); if (0 != Bytes.compareTo(keyValue.array(), keyValue.arrayOffset(), keyValue.limit(), expectedKeyValue.getBuffer(), expectedKeyValue.getOffset(), expectedKeyValue.getLength())) { int commonPrefix = 0; byte[] left = keyValue.array(); byte[] right = expectedKeyValue.getBuffer(); int leftOff = keyValue.arrayOffset(); int rightOff = expectedKeyValue.getOffset(); int length = Math.min(keyValue.limit(), expectedKeyValue.getLength()); while (commonPrefix < length && left[commonPrefix + leftOff] == right[commonPrefix + rightOff]) { commonPrefix++; } fail(String.format("next() produces wrong results " + "encoder: %s i: %d commonPrefix: %d" + "\n expected %s\n actual %s", encoder .toString(), i, commonPrefix, Bytes.toStringBinary(expectedKeyValue.getBuffer(), expectedKeyValue.getOffset(), expectedKeyValue.getLength()), Bytes .toStringBinary(keyValue))); } i++; } while (seeker.next()); } }
/** * Test iterating on encoded buffers. */ @Test public void testNextOnSample() { List<KeyValue> sampleKv = generator.generateTestKeyValues(NUMBER_OF_KV); ByteBuffer originalBuffer = RedundantKVGenerator.convertKvToByteBuffer(sampleKv, includesMemstoreTS); for (DataBlockEncoding encoding : DataBlockEncoding.values()) { if (encoding.getEncoder() == null) { continue; } DataBlockEncoder encoder = encoding.getEncoder(); ByteBuffer encodedBuffer = null; try { encodedBuffer = ByteBuffer.wrap(encodeBytes(encoding, originalBuffer)); } catch (IOException e) { throw new RuntimeException(String.format( "Bug while encoding using '%s'", encoder.toString()), e); } DataBlockEncoder.EncodedSeeker seeker = encoder.createSeeker(KeyValue.COMPARATOR, includesMemstoreTS); seeker.setCurrentBuffer(encodedBuffer); int i = 0; do { KeyValue expectedKeyValue = sampleKv.get(i); ByteBuffer keyValue = seeker.getKeyValueBuffer(); if (0 != Bytes.compareTo( keyValue.array(), keyValue.arrayOffset(), keyValue.limit(), expectedKeyValue.getBuffer(), expectedKeyValue.getOffset(), expectedKeyValue.getLength())) { int commonPrefix = 0; byte[] left = keyValue.array(); byte[] right = expectedKeyValue.getBuffer(); int leftOff = keyValue.arrayOffset(); int rightOff = expectedKeyValue.getOffset(); int length = Math.min(keyValue.limit(), expectedKeyValue.getLength()); while (commonPrefix < length && left[commonPrefix + leftOff] == right[commonPrefix + rightOff]) { commonPrefix++; } fail(String.format( "next() produces wrong results " + "encoder: %s i: %d commonPrefix: %d" + "\n expected %s\n actual %s", encoder.toString(), i, commonPrefix, Bytes.toStringBinary(expectedKeyValue.getBuffer(), expectedKeyValue.getOffset(), expectedKeyValue.getLength()), Bytes.toStringBinary(keyValue))); } i++; } while (seeker.next()); } }
/** * Test iterating on encoded buffers. */ @Test public void testNextOnSample() { List<KeyValue> sampleKv = generator.generateTestKeyValues(NUMBER_OF_KV); ByteBuffer originalBuffer = RedundantKVGenerator.convertKvToByteBuffer(sampleKv, includesMemstoreTS); for (DataBlockEncoding encoding : DataBlockEncoding.values()) { if (encoding.getEncoder() == null) { continue; } DataBlockEncoder encoder = encoding.getEncoder(); ByteBuffer encodedBuffer = null; try { encodedBuffer = ByteBuffer.wrap(encodeBytes(encoding, originalBuffer)); } catch (IOException e) { throw new RuntimeException(String.format( "Bug while encoding using '%s'", encoder.toString()), e); } DataBlockEncoder.EncodedSeeker seeker = encoder.createSeeker(KeyValue.KEY_COMPARATOR, includesMemstoreTS); seeker.setCurrentBuffer(encodedBuffer); int i = 0; do { KeyValue expectedKeyValue = sampleKv.get(i); ByteBuffer keyValue = seeker.getKeyValueBuffer(); if (0 != Bytes.compareTo( keyValue.array(), keyValue.arrayOffset(), keyValue.limit(), expectedKeyValue.getBuffer(), expectedKeyValue.getOffset(), expectedKeyValue.getLength())) { int commonPrefix = 0; byte[] left = keyValue.array(); byte[] right = expectedKeyValue.getBuffer(); int leftOff = keyValue.arrayOffset(); int rightOff = expectedKeyValue.getOffset(); int length = Math.min(keyValue.limit(), expectedKeyValue.getLength()); while (commonPrefix < length && left[commonPrefix + leftOff] == right[commonPrefix + rightOff]) { commonPrefix++; } fail(String.format( "next() produces wrong results " + "encoder: %s i: %d commonPrefix: %d" + "\n expected %s\n actual %s", encoder.toString(), i, commonPrefix, Bytes.toStringBinary(expectedKeyValue.getBuffer(), expectedKeyValue.getOffset(), expectedKeyValue.getLength()), Bytes.toStringBinary(keyValue))); } i++; } while (seeker.next()); } }