1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.codec.prefixtree.timestamp;
20
21 import java.io.IOException;
22 import java.util.Collection;
23
24 import org.apache.hadoop.hbase.codec.prefixtree.PrefixTreeBlockMeta;
25 import org.apache.hadoop.hbase.codec.prefixtree.decode.timestamp.TimestampDecoder;
26 import org.apache.hadoop.hbase.codec.prefixtree.encode.other.LongEncoder;
27 import org.apache.hadoop.hbase.testclassification.SmallTests;
28 import org.junit.Assert;
29 import org.junit.Test;
30 import org.junit.experimental.categories.Category;
31 import org.junit.runner.RunWith;
32 import org.junit.runners.Parameterized;
33 import org.junit.runners.Parameterized.Parameters;
34
35 @Category(SmallTests.class)
36 @RunWith(Parameterized.class)
37 public class TestTimestampEncoder {
38
39 @Parameters
40 public static Collection<Object[]> parameters() {
41 return new TestTimestampData.InMemory().getAllAsObjectArray();
42 }
43
44 private TestTimestampData timestamps;
45 private PrefixTreeBlockMeta blockMeta;
46 private LongEncoder encoder;
47 private byte[] bytes;
48 private TimestampDecoder decoder;
49
50 public TestTimestampEncoder(TestTimestampData testTimestamps) throws IOException {
51 this.timestamps = testTimestamps;
52 this.blockMeta = new PrefixTreeBlockMeta();
53 this.blockMeta.setNumMetaBytes(0);
54 this.blockMeta.setNumRowBytes(0);
55 this.blockMeta.setNumQualifierBytes(0);
56 this.encoder = new LongEncoder();
57 for (Long ts : testTimestamps.getInputs()) {
58 encoder.add(ts);
59 }
60 encoder.compile();
61 blockMeta.setTimestampFields(encoder);
62 bytes = encoder.getByteArray();
63 decoder = new TimestampDecoder();
64 decoder.initOnBlock(blockMeta, bytes);
65 }
66
67 @Test
68 public void testCompressorMinimum() {
69 Assert.assertEquals(timestamps.getMinimum(), encoder.getMin());
70 }
71
72 @Test
73 public void testCompressorRoundTrip() {
74 long[] outputs = encoder.getSortedUniqueTimestamps();
75 for (int i = 0; i < timestamps.getOutputs().size(); ++i) {
76 long input = timestamps.getOutputs().get(i);
77 long output = outputs[i];
78 Assert.assertEquals(input, output);
79 }
80 }
81
82 @Test
83 public void testReaderMinimum() {
84 Assert.assertEquals(timestamps.getMinimum(), decoder.getLong(0));
85 }
86
87 @Test
88 public void testReaderRoundTrip() {
89 for (int i = 0; i < timestamps.getOutputs().size(); ++i) {
90 long input = timestamps.getOutputs().get(i);
91 long output = decoder.getLong(i);
92 Assert.assertEquals(input, output);
93 }
94 }
95 }