1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.regionserver.wal;
20
21 import static org.junit.Assert.assertEquals;
22
23 import java.io.ByteArrayInputStream;
24 import java.io.ByteArrayOutputStream;
25 import java.io.InputStream;
26 import java.util.ArrayList;
27 import java.util.List;
28
29 import org.apache.hadoop.conf.Configuration;
30 import org.apache.hadoop.hbase.HConstants;
31 import org.apache.hadoop.hbase.KeyValue;
32 import org.apache.hadoop.hbase.Tag;
33 import org.apache.hadoop.hbase.codec.Codec.Decoder;
34 import org.apache.hadoop.hbase.codec.Codec.Encoder;
35 import org.apache.hadoop.hbase.io.util.LRUDictionary;
36 import org.apache.hadoop.hbase.testclassification.SmallTests;
37 import org.apache.hadoop.hbase.util.Bytes;
38 import org.junit.Test;
39 import org.junit.experimental.categories.Category;
40
41 @Category(SmallTests.class)
42 public class TestWALCellCodecWithCompression {
43
44 @Test
45 public void testEncodeDecodeKVsWithTags() throws Exception {
46 doTest(false);
47 }
48
49 @Test
50 public void testEncodeDecodeKVsWithTagsWithTagsCompression() throws Exception {
51 doTest(true);
52 }
53
54 private void doTest(boolean compressTags) throws Exception {
55 Configuration conf = new Configuration(false);
56 conf.setBoolean(CompressionContext.ENABLE_WAL_TAGS_COMPRESSION, compressTags);
57 WALCellCodec codec = new WALCellCodec(conf, new CompressionContext(LRUDictionary.class, false,
58 compressTags));
59 ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
60 Encoder encoder = codec.getEncoder(bos);
61 encoder.write(createKV(1));
62 encoder.write(createKV(0));
63 encoder.write(createKV(2));
64
65 InputStream is = new ByteArrayInputStream(bos.toByteArray());
66 Decoder decoder = codec.getDecoder(is);
67 decoder.advance();
68 KeyValue kv = (KeyValue) decoder.current();
69 List<Tag> tags = kv.getTags();
70 assertEquals(1, tags.size());
71 assertEquals("tagValue1", Bytes.toString(tags.get(0).getValue()));
72 decoder.advance();
73 kv = (KeyValue) decoder.current();
74 tags = kv.getTags();
75 assertEquals(0, tags.size());
76 decoder.advance();
77 kv = (KeyValue) decoder.current();
78 tags = kv.getTags();
79 assertEquals(2, tags.size());
80 assertEquals("tagValue1", Bytes.toString(tags.get(0).getValue()));
81 assertEquals("tagValue2", Bytes.toString(tags.get(1).getValue()));
82 }
83
84 private KeyValue createKV(int noOfTags) {
85 byte[] row = Bytes.toBytes("myRow");
86 byte[] cf = Bytes.toBytes("myCF");
87 byte[] q = Bytes.toBytes("myQualifier");
88 byte[] value = Bytes.toBytes("myValue");
89 List<Tag> tags = new ArrayList<Tag>(noOfTags);
90 for (int i = 1; i <= noOfTags; i++) {
91 tags.add(new Tag((byte) i, Bytes.toBytes("tagValue" + i)));
92 }
93 return new KeyValue(row, cf, q, HConstants.LATEST_TIMESTAMP, value, tags);
94 }
95 }