1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 }