1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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.Tag;
28 import org.apache.hadoop.hbase.io.util.LRUDictionary;
29 import org.apache.hadoop.hbase.testclassification.SmallTests;
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 }