View Javadoc

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.ArrayList;
23  import java.util.List;
24  
25  import org.apache.hadoop.hbase.HConstants;
26  import org.apache.hadoop.hbase.KeyValue;
27  import org.apache.hadoop.hbase.testclassification.SmallTests;
28  import org.apache.hadoop.hbase.Tag;
29  import org.apache.hadoop.hbase.io.util.LRUDictionary;
30  import org.apache.hadoop.hbase.util.Bytes;
31  import org.apache.hadoop.io.DataOutputBuffer;
32  import org.junit.Test;
33  import org.junit.experimental.categories.Category;
34  
35  import static org.junit.Assert.*;
36  
37  import com.google.common.collect.Lists;
38  
39  @Category(SmallTests.class)
40  public class TestKeyValueCompression {
41    private static final byte[] VALUE = Bytes.toBytes("fake value");
42    private static final int BUF_SIZE = 256*1024;
43    
44    @Test
45    public void testCountingKVs() throws Exception {
46      List<KeyValue> kvs = Lists.newArrayList();
47      for (int i = 0; i < 400; i++) {
48        byte[] row = Bytes.toBytes("row" + i);
49        byte[] fam = Bytes.toBytes("fam" + i);
50        byte[] qual = Bytes.toBytes("qual" + i);
51        kvs.add(new KeyValue(row, fam, qual, 12345L, VALUE));
52      }
53      
54      runTestCycle(kvs);
55    }
56    
57    @Test
58    public void testRepeatingKVs() throws Exception {
59      List<KeyValue> kvs = Lists.newArrayList();
60      for (int i = 0; i < 400; i++) {
61        byte[] row = Bytes.toBytes("row" + (i % 10));
62        byte[] fam = Bytes.toBytes("fam" + (i % 127));
63        byte[] qual = Bytes.toBytes("qual" + (i % 128));
64        kvs.add(new KeyValue(row, fam, qual, 12345L, VALUE));
65      }
66      
67      runTestCycle(kvs);
68    }
69  
70    private void runTestCycle(List<KeyValue> kvs) throws Exception {
71      CompressionContext ctx = new CompressionContext(LRUDictionary.class, false, false);
72      DataOutputBuffer buf = new DataOutputBuffer(BUF_SIZE);
73      for (KeyValue kv : kvs) {
74        KeyValueCompression.writeKV(buf, kv, ctx);
75      }
76  
77      ctx.clear();
78      DataInputStream in = new DataInputStream(new ByteArrayInputStream(
79          buf.getData(), 0, buf.getLength()));
80      for (KeyValue kv : kvs) {
81        KeyValue readBack = KeyValueCompression.readKV(in, ctx);
82        assertEquals(kv, readBack);
83      }
84    }
85  
86    @Test
87    public void testKVWithTags() throws Exception {
88      CompressionContext ctx = new CompressionContext(LRUDictionary.class, false, false);
89      DataOutputBuffer buf = new DataOutputBuffer(BUF_SIZE);
90      KeyValueCompression.writeKV(buf, createKV(1), ctx);
91      KeyValueCompression.writeKV(buf, createKV(0), ctx);
92      KeyValueCompression.writeKV(buf, createKV(2), ctx);
93      
94      ctx.clear();
95      DataInputStream in = new DataInputStream(new ByteArrayInputStream(
96          buf.getData(), 0, buf.getLength()));
97      
98      KeyValue readBack = KeyValueCompression.readKV(in, ctx);
99      List<Tag> tags = readBack.getTags();
100     assertEquals(1, tags.size());
101   }
102   
103   private KeyValue createKV(int noOfTags) {
104     byte[] row = Bytes.toBytes("myRow");
105     byte[] cf = Bytes.toBytes("myCF");
106     byte[] q = Bytes.toBytes("myQualifier");
107     byte[] value = Bytes.toBytes("myValue");
108     List<Tag> tags = new ArrayList<Tag>(noOfTags);
109     for (int i = 1; i <= noOfTags; i++) {
110       tags.add(new Tag((byte) i, Bytes.toBytes("tagValue" + i)));
111     }
112     return new KeyValue(row, cf, q, HConstants.LATEST_TIMESTAMP, value, tags);
113   }
114 }