View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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  }