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  
19  package org.apache.hadoop.hbase.io;
20  
21  import static org.junit.Assert.assertTrue;
22  
23  import java.io.ByteArrayInputStream;
24  import java.io.ByteArrayOutputStream;
25  import java.nio.ByteBuffer;
26  import java.util.ArrayList;
27  import java.util.List;
28  
29  import org.apache.hadoop.hbase.KeyValue;
30  import org.apache.hadoop.hbase.testclassification.SmallTests;
31  import org.apache.hadoop.hbase.Tag;
32  import org.apache.hadoop.hbase.io.util.LRUDictionary;
33  import org.apache.hadoop.hbase.util.Bytes;
34  import org.junit.Test;
35  import org.junit.experimental.categories.Category;
36  
37  @Category(SmallTests.class)
38  public class TestTagCompressionContext {
39  
40    private static final byte[] ROW = Bytes.toBytes("r1");
41    private static final byte[] CF = Bytes.toBytes("f");
42    private static final byte[] Q = Bytes.toBytes("q");
43    private static final byte[] V = Bytes.toBytes("v");
44  
45    @Test
46    public void testCompressUncompressTags1() throws Exception {
47      ByteArrayOutputStream baos = new ByteArrayOutputStream();
48      TagCompressionContext context = new TagCompressionContext(LRUDictionary.class, Byte.MAX_VALUE);
49      KeyValue kv1 = createKVWithTags(2);
50      int tagsLength1 = kv1.getTagsLength();
51      ByteBuffer ib = ByteBuffer.wrap(kv1.getTagsArray(), kv1.getTagsOffset(), tagsLength1);
52      context.compressTags(baos, ib, tagsLength1);
53      KeyValue kv2 = createKVWithTags(3);
54      int tagsLength2 = kv2.getTagsLength();
55      ib = ByteBuffer.wrap(kv2.getTagsArray(), kv2.getTagsOffset(), tagsLength2);
56      context.compressTags(baos, ib, tagsLength2);
57  
58      context.clear();
59  
60      byte[] dest = new byte[tagsLength1];
61      ByteBuffer ob = ByteBuffer.wrap(baos.toByteArray());
62      context.uncompressTags(ob, dest, 0, tagsLength1);
63      assertTrue(Bytes.equals(kv1.getTagsArray(), kv1.getTagsOffset(), tagsLength1, dest, 0,
64          tagsLength1));
65      dest = new byte[tagsLength2];
66      context.uncompressTags(ob, dest, 0, tagsLength2);
67      assertTrue(Bytes.equals(kv2.getTagsArray(), kv2.getTagsOffset(), tagsLength2, dest, 0,
68          tagsLength2));
69    }
70  
71    @Test
72    public void testCompressUncompressTags2() throws Exception {
73      ByteArrayOutputStream baos = new ByteArrayOutputStream();
74      TagCompressionContext context = new TagCompressionContext(LRUDictionary.class, Byte.MAX_VALUE);
75      KeyValue kv1 = createKVWithTags(1);
76      int tagsLength1 = kv1.getTagsLength();
77      context.compressTags(baos, kv1.getTagsArray(), kv1.getTagsOffset(), tagsLength1);
78      KeyValue kv2 = createKVWithTags(3);
79      int tagsLength2 = kv2.getTagsLength();
80      context.compressTags(baos, kv2.getTagsArray(), kv2.getTagsOffset(), tagsLength2);
81  
82      context.clear();
83  
84      ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
85      byte[] dest = new byte[tagsLength1];
86      context.uncompressTags(bais, dest, 0, tagsLength1);
87      assertTrue(Bytes.equals(kv1.getTagsArray(), kv1.getTagsOffset(), tagsLength1, dest, 0,
88          tagsLength1));
89      dest = new byte[tagsLength2];
90      context.uncompressTags(bais, dest, 0, tagsLength2);
91      assertTrue(Bytes.equals(kv2.getTagsArray(), kv2.getTagsOffset(), tagsLength2, dest, 0,
92          tagsLength2));
93    }
94  
95    private KeyValue createKVWithTags(int noOfTags) {
96      List<Tag> tags = new ArrayList<Tag>();
97      for (int i = 0; i < noOfTags; i++) {
98        tags.add(new Tag((byte) i, "tagValue" + i));
99      }
100     KeyValue kv = new KeyValue(ROW, CF, Q, 1234L, V, tags);
101     return kv;
102   }
103 }