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.SmallTests;
25  import org.apache.hadoop.hbase.codec.prefixtree.PrefixTreeBlockMeta;
26  import org.apache.hadoop.hbase.codec.prefixtree.decode.timestamp.TimestampDecoder;
27  import org.apache.hadoop.hbase.codec.prefixtree.encode.other.LongEncoder;
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  }