View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.regionserver.wal;
20  
21  import static org.junit.Assert.assertEquals;
22  
23  import java.io.ByteArrayInputStream;
24  import java.io.ByteArrayOutputStream;
25  import java.io.InputStream;
26  import java.util.ArrayList;
27  import java.util.List;
28  
29  import org.apache.hadoop.conf.Configuration;
30  import org.apache.hadoop.hbase.HConstants;
31  import org.apache.hadoop.hbase.KeyValue;
32  import org.apache.hadoop.hbase.Tag;
33  import org.apache.hadoop.hbase.SmallTests;
34  import org.apache.hadoop.hbase.codec.Codec.Decoder;
35  import org.apache.hadoop.hbase.codec.Codec.Encoder;
36  import org.apache.hadoop.hbase.io.util.LRUDictionary;
37  import org.apache.hadoop.hbase.util.Bytes;
38  import org.junit.Test;
39  import org.junit.experimental.categories.Category;
40  
41  @Category(SmallTests.class)
42  public class TestWALCellCodecWithCompression {
43  
44    @Test
45    public void testEncodeDecodeKVsWithTags() throws Exception {
46      doTest(false);
47    }
48  
49    @Test
50    public void testEncodeDecodeKVsWithTagsWithTagsCompression() throws Exception {
51      doTest(true);
52    }
53  
54    private void doTest(boolean compressTags) throws Exception {
55      Configuration conf = new Configuration(false);
56      conf.setBoolean(CompressionContext.ENABLE_WAL_TAGS_COMPRESSION, compressTags);
57      WALCellCodec codec = new WALCellCodec(conf, new CompressionContext(LRUDictionary.class, false,
58          compressTags));
59      ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
60      Encoder encoder = codec.getEncoder(bos);
61      encoder.write(createKV(1));
62      encoder.write(createKV(0));
63      encoder.write(createKV(2));
64  
65      InputStream is = new ByteArrayInputStream(bos.toByteArray());
66      Decoder decoder = codec.getDecoder(is);
67      decoder.advance();
68      KeyValue kv = (KeyValue) decoder.current();
69      List<Tag> tags = kv.getTags();
70      assertEquals(1, tags.size());
71      assertEquals("tagValue1", Bytes.toString(tags.get(0).getValue()));
72      decoder.advance();
73      kv = (KeyValue) decoder.current();
74      tags = kv.getTags();
75      assertEquals(0, tags.size());
76      decoder.advance();
77      kv = (KeyValue) decoder.current();
78      tags = kv.getTags();
79      assertEquals(2, tags.size());
80      assertEquals("tagValue1", Bytes.toString(tags.get(0).getValue()));
81      assertEquals("tagValue2", Bytes.toString(tags.get(1).getValue()));
82    }
83  
84    private KeyValue createKV(int noOfTags) {
85      byte[] row = Bytes.toBytes("myRow");
86      byte[] cf = Bytes.toBytes("myCF");
87      byte[] q = Bytes.toBytes("myQualifier");
88      byte[] value = Bytes.toBytes("myValue");
89      List<Tag> tags = new ArrayList<Tag>(noOfTags);
90      for (int i = 1; i <= noOfTags; i++) {
91        tags.add(new Tag((byte) i, Bytes.toBytes("tagValue" + i)));
92      }
93      return new KeyValue(row, cf, q, HConstants.LATEST_TIMESTAMP, value, tags);
94    }
95  }