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.junit.Assert;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.junit.runners.Parameterized;
31 import org.junit.runners.Parameterized.Parameters;
32
33 @RunWith(Parameterized.class)
34 public class TestTimestampEncoder {
35
36 @Parameters
37 public static Collection<Object[]> parameters() {
38 return new TestTimestampData.InMemory().getAllAsObjectArray();
39 }
40
41 private TestTimestampData timestamps;
42 private PrefixTreeBlockMeta blockMeta;
43 private LongEncoder encoder;
44 private byte[] bytes;
45 private TimestampDecoder decoder;
46
47 public TestTimestampEncoder(TestTimestampData testTimestamps) throws IOException {
48 this.timestamps = testTimestamps;
49 this.blockMeta = new PrefixTreeBlockMeta();
50 this.blockMeta.setNumMetaBytes(0);
51 this.blockMeta.setNumRowBytes(0);
52 this.blockMeta.setNumQualifierBytes(0);
53 this.encoder = new LongEncoder();
54 for (Long ts : testTimestamps.getInputs()) {
55 encoder.add(ts);
56 }
57 encoder.compile();
58 blockMeta.setTimestampFields(encoder);
59 bytes = encoder.getByteArray();
60 decoder = new TimestampDecoder();
61 decoder.initOnBlock(blockMeta, bytes);
62 }
63
64 @Test
65 public void testCompressorMinimum() {
66 Assert.assertEquals(timestamps.getMinimum(), encoder.getMin());
67 }
68
69 @Test
70 public void testCompressorRoundTrip() {
71 long[] outputs = encoder.getSortedUniqueTimestamps();
72 for (int i = 0; i < timestamps.getOutputs().size(); ++i) {
73 long input = timestamps.getOutputs().get(i);
74 long output = outputs[i];
75 Assert.assertEquals(input, output);
76 }
77 }
78
79 @Test
80 public void testReaderMinimum() {
81 Assert.assertEquals(timestamps.getMinimum(), decoder.getLong(0));
82 }
83
84 @Test
85 public void testReaderRoundTrip() {
86 for (int i = 0; i < timestamps.getOutputs().size(); ++i) {
87 long input = timestamps.getOutputs().get(i);
88 long output = decoder.getLong(i);
89 Assert.assertEquals(input, output);
90 }
91 }
92 }