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.List;
23
24 import org.apache.hadoop.hbase.KeyValue;
25 import org.apache.hadoop.hbase.SmallTests;
26 import org.apache.hadoop.hbase.util.Bytes;
27 import org.apache.hadoop.io.DataOutputBuffer;
28 import org.junit.Test;
29 import org.junit.experimental.categories.Category;
30
31 import static org.junit.Assert.*;
32
33 import com.google.common.collect.Lists;
34
35 @Category(SmallTests.class)
36 public class TestKeyValueCompression {
37 private static final byte[] VALUE = Bytes.toBytes("fake value");
38 private static final int BUF_SIZE = 256*1024;
39
40 @Test
41 public void testCountingKVs() throws Exception {
42 List<KeyValue> kvs = Lists.newArrayList();
43 for (int i = 0; i < 400; i++) {
44 byte[] row = Bytes.toBytes("row" + i);
45 byte[] fam = Bytes.toBytes("fam" + i);
46 byte[] qual = Bytes.toBytes("qual" + i);
47 kvs.add(new KeyValue(row, fam, qual, 12345L, VALUE));
48 }
49
50 runTestCycle(kvs);
51 }
52
53 @Test
54 public void testRepeatingKVs() throws Exception {
55 List<KeyValue> kvs = Lists.newArrayList();
56 for (int i = 0; i < 400; i++) {
57 byte[] row = Bytes.toBytes("row" + (i % 10));
58 byte[] fam = Bytes.toBytes("fam" + (i % 127));
59 byte[] qual = Bytes.toBytes("qual" + (i % 128));
60 kvs.add(new KeyValue(row, fam, qual, 12345L, VALUE));
61 }
62
63 runTestCycle(kvs);
64 }
65
66 private void runTestCycle(List<KeyValue> kvs) throws Exception {
67 CompressionContext ctx = new CompressionContext(LRUDictionary.class);
68 DataOutputBuffer buf = new DataOutputBuffer(BUF_SIZE);
69 for (KeyValue kv : kvs) {
70 KeyValueCompression.writeKV(buf, kv, ctx);
71 }
72
73 ctx.clear();
74 DataInputStream in = new DataInputStream(new ByteArrayInputStream(
75 buf.getData(), 0, buf.getLength()));
76 for (KeyValue kv : kvs) {
77 KeyValue readBack = KeyValueCompression.readKV(in, ctx);
78 assertEquals(kv, readBack);
79 }
80 }
81 }