1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License
17   */
18  package org.apache.hadoop.hbase.regionserver.wal;
19  
20  import java.io.ByteArrayInputStream;
21  import java.io.DataInputStream;
22  import java.util.List;
23  
24  import org.apache.hadoop.hbase.KeyValue;
25  import org.apache.hadoop.hbase.SmallTests;
26  import org.apache.hadoop.hbase.util.Bytes;
27  import org.apache.hadoop.io.DataOutputBuffer;
28  import org.junit.Test;
29  import org.junit.experimental.categories.Category;
30  
31  import static org.junit.Assert.*;
32  
33  import com.google.common.collect.Lists;
34  
35  @Category(SmallTests.class)
36  public class TestKeyValueCompression {
37    private static final byte[] VALUE = Bytes.toBytes("fake value");
38    private static final int BUF_SIZE = 256*1024;
39    
40    @Test
41    public void testCountingKVs() throws Exception {
42      List<KeyValue> kvs = Lists.newArrayList();
43      for (int i = 0; i < 400; i++) {
44        byte[] row = Bytes.toBytes("row" + i);
45        byte[] fam = Bytes.toBytes("fam" + i);
46        byte[] qual = Bytes.toBytes("qual" + i);
47        kvs.add(new KeyValue(row, fam, qual, 12345L, VALUE));
48      }
49      
50      runTestCycle(kvs);
51    }
52    
53    @Test
54    public void testRepeatingKVs() throws Exception {
55      List<KeyValue> kvs = Lists.newArrayList();
56      for (int i = 0; i < 400; i++) {
57        byte[] row = Bytes.toBytes("row" + (i % 10));
58        byte[] fam = Bytes.toBytes("fam" + (i % 127));
59        byte[] qual = Bytes.toBytes("qual" + (i % 128));
60        kvs.add(new KeyValue(row, fam, qual, 12345L, VALUE));
61      }
62      
63      runTestCycle(kvs);
64    }
65  
66    private void runTestCycle(List<KeyValue> kvs) throws Exception {
67      CompressionContext ctx = new CompressionContext(LRUDictionary.class);
68      DataOutputBuffer buf = new DataOutputBuffer(BUF_SIZE);
69      for (KeyValue kv : kvs) {
70        KeyValueCompression.writeKV(buf, kv, ctx);
71      }
72  
73      ctx.clear();
74      DataInputStream in = new DataInputStream(new ByteArrayInputStream(
75          buf.getData(), 0, buf.getLength()));
76      for (KeyValue kv : kvs) {
77        KeyValue readBack = KeyValueCompression.readKV(in, ctx);
78        assertEquals(kv, readBack);
79      }
80    }
81  }