001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.io.compress.zstd; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertFalse; 022import static org.junit.Assert.assertTrue; 023 024import java.io.IOException; 025import java.nio.ByteBuffer; 026import org.apache.hadoop.hbase.HBaseClassTestRule; 027import org.apache.hadoop.hbase.nio.ByteBuff; 028import org.apache.hadoop.hbase.nio.MultiByteBuff; 029import org.apache.hadoop.hbase.nio.SingleByteBuff; 030import org.apache.hadoop.hbase.testclassification.SmallTests; 031import org.apache.hadoop.hbase.util.Bytes; 032import org.junit.ClassRule; 033import org.junit.Test; 034import org.junit.experimental.categories.Category; 035 036@Category(SmallTests.class) 037public class TestZstdByteBuffDecompressor { 038 039 @ClassRule 040 public static final HBaseClassTestRule CLASS_RULE = 041 HBaseClassTestRule.forClass(TestZstdByteBuffDecompressor.class); 042 043 // "HBase is awesome" compressed with zstd, and then prepended with metadata as a 044 // BlockCompressorStream would 045 private static final byte[] COMPRESSED_PAYLOAD = 046 Bytes.fromHex("000000100000001928b52ffd2010810000484261736520697320617765736f6d65"); 047 048 @Test 049 public void testCapabilities() { 050 ByteBuff emptySingleHeapBuff = new SingleByteBuff(ByteBuffer.allocate(0)); 051 ByteBuff emptyMultiHeapBuff = new MultiByteBuff(ByteBuffer.allocate(0), ByteBuffer.allocate(0)); 052 ByteBuff emptySingleDirectBuff = new SingleByteBuff(ByteBuffer.allocateDirect(0)); 053 ByteBuff emptyMultiDirectBuff = 054 new MultiByteBuff(ByteBuffer.allocateDirect(0), ByteBuffer.allocateDirect(0)); 055 056 try (ZstdByteBuffDecompressor decompressor = new ZstdByteBuffDecompressor(null)) { 057 assertTrue(decompressor.canDecompress(emptySingleHeapBuff, emptySingleHeapBuff)); 058 assertTrue(decompressor.canDecompress(emptySingleDirectBuff, emptySingleDirectBuff)); 059 assertFalse(decompressor.canDecompress(emptySingleHeapBuff, emptySingleDirectBuff)); 060 assertFalse(decompressor.canDecompress(emptySingleDirectBuff, emptySingleHeapBuff)); 061 assertFalse(decompressor.canDecompress(emptyMultiHeapBuff, emptyMultiHeapBuff)); 062 assertFalse(decompressor.canDecompress(emptyMultiDirectBuff, emptyMultiDirectBuff)); 063 assertFalse(decompressor.canDecompress(emptySingleHeapBuff, emptyMultiHeapBuff)); 064 assertFalse(decompressor.canDecompress(emptySingleDirectBuff, emptyMultiDirectBuff)); 065 } 066 } 067 068 @Test 069 public void testDecompressHeap() throws IOException { 070 try (ZstdByteBuffDecompressor decompressor = new ZstdByteBuffDecompressor(null)) { 071 ByteBuff output = new SingleByteBuff(ByteBuffer.allocate(64)); 072 ByteBuff input = new SingleByteBuff(ByteBuffer.wrap(COMPRESSED_PAYLOAD)); 073 int decompressedSize = decompressor.decompress(output, input, COMPRESSED_PAYLOAD.length); 074 assertEquals("HBase is awesome", Bytes.toString(output.toBytes(0, decompressedSize))); 075 } 076 } 077 078 @Test 079 public void testDecompressDirect() throws IOException { 080 try (ZstdByteBuffDecompressor decompressor = new ZstdByteBuffDecompressor(null)) { 081 ByteBuff output = new SingleByteBuff(ByteBuffer.allocateDirect(64)); 082 ByteBuff input = new SingleByteBuff(ByteBuffer.allocateDirect(COMPRESSED_PAYLOAD.length)); 083 input.put(COMPRESSED_PAYLOAD); 084 input.rewind(); 085 int decompressedSize = decompressor.decompress(output, input, COMPRESSED_PAYLOAD.length); 086 assertEquals("HBase is awesome", Bytes.toString(output.toBytes(0, decompressedSize))); 087 } 088 } 089 090}