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.regionserver.wal;
20  
21  import java.lang.reflect.Constructor;
22  import java.lang.reflect.InvocationTargetException;
23  
24  import org.apache.hadoop.hbase.classification.InterfaceAudience;
25  import org.apache.hadoop.hbase.io.TagCompressionContext;
26  import org.apache.hadoop.hbase.io.util.Dictionary;
27  
28  /**
29   * Context that holds the various dictionaries for compression in WAL.
30   */
31  @InterfaceAudience.Private
32  public class CompressionContext {
33  
34    static final String ENABLE_WAL_TAGS_COMPRESSION = 
35        "hbase.regionserver.wal.tags.enablecompression";
36  
37    // visible only for WALKey, until we move everything into o.a.h.h.wal
38    public final Dictionary regionDict;
39    public final Dictionary tableDict;
40    public final Dictionary familyDict;
41    final Dictionary qualifierDict;
42    final Dictionary rowDict;
43    // Context used for compressing tags
44    TagCompressionContext tagCompressionContext = null;
45  
46    public CompressionContext(Class<? extends Dictionary> dictType, boolean recoveredEdits,
47        boolean hasTagCompression) throws SecurityException, NoSuchMethodException,
48        InstantiationException, IllegalAccessException, InvocationTargetException {
49      Constructor<? extends Dictionary> dictConstructor =
50          dictType.getConstructor();
51      regionDict = dictConstructor.newInstance();
52      tableDict = dictConstructor.newInstance();
53      familyDict = dictConstructor.newInstance();
54      qualifierDict = dictConstructor.newInstance();
55      rowDict = dictConstructor.newInstance();
56      if (recoveredEdits) {
57        // This will never change
58        regionDict.init(1);
59        tableDict.init(1);
60      } else {
61        regionDict.init(Short.MAX_VALUE);
62        tableDict.init(Short.MAX_VALUE);
63      }
64      rowDict.init(Short.MAX_VALUE);
65      familyDict.init(Byte.MAX_VALUE);
66      qualifierDict.init(Byte.MAX_VALUE);
67      if (hasTagCompression) {
68        tagCompressionContext = new TagCompressionContext(dictType, Short.MAX_VALUE);
69      }
70    }
71  
72    void clear() {
73      regionDict.clear();
74      tableDict.clear();
75      familyDict.clear();
76      qualifierDict.clear();
77      rowDict.clear();
78      if (tagCompressionContext != null) {
79        tagCompressionContext.clear();
80      }
81    }
82  }