001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.hbase.regionserver.wal;
020
021import java.lang.reflect.Constructor;
022import java.lang.reflect.InvocationTargetException;
023import java.util.EnumMap;
024import java.util.Map;
025
026import org.apache.hadoop.hbase.HBaseInterfaceAudience;
027import org.apache.yetus.audience.InterfaceAudience;
028import org.apache.hadoop.hbase.io.TagCompressionContext;
029import org.apache.hadoop.hbase.io.util.Dictionary;
030
031/**
032 * Context that holds the various dictionaries for compression in WAL.
033 */
034@InterfaceAudience.LimitedPrivate({HBaseInterfaceAudience.COPROC, HBaseInterfaceAudience.PHOENIX})
035public class CompressionContext {
036
037  static final String ENABLE_WAL_TAGS_COMPRESSION =
038      "hbase.regionserver.wal.tags.enablecompression";
039
040  public enum DictionaryIndex {
041    REGION, TABLE, FAMILY, QUALIFIER, ROW
042  }
043
044  private final Map<DictionaryIndex, Dictionary> dictionaries =
045      new EnumMap<>(DictionaryIndex.class);
046  // Context used for compressing tags
047  TagCompressionContext tagCompressionContext = null;
048
049  public CompressionContext(Class<? extends Dictionary> dictType, boolean recoveredEdits,
050      boolean hasTagCompression) throws SecurityException, NoSuchMethodException,
051      InstantiationException, IllegalAccessException, InvocationTargetException {
052    Constructor<? extends Dictionary> dictConstructor =
053        dictType.getConstructor();
054    for (DictionaryIndex dictionaryIndex : DictionaryIndex.values()) {
055      Dictionary newDictionary = dictConstructor.newInstance();
056      dictionaries.put(dictionaryIndex, newDictionary);
057    }
058    if(recoveredEdits) {
059      getDictionary(DictionaryIndex.REGION).init(1);
060      getDictionary(DictionaryIndex.TABLE).init(1);
061    } else {
062      getDictionary(DictionaryIndex.REGION).init(Short.MAX_VALUE);
063      getDictionary(DictionaryIndex.TABLE).init(Short.MAX_VALUE);
064    }
065
066    getDictionary(DictionaryIndex.ROW).init(Short.MAX_VALUE);
067    getDictionary(DictionaryIndex.FAMILY).init(Byte.MAX_VALUE);
068    getDictionary(DictionaryIndex.QUALIFIER).init(Byte.MAX_VALUE);
069
070    if (hasTagCompression) {
071      tagCompressionContext = new TagCompressionContext(dictType, Short.MAX_VALUE);
072    }
073  }
074
075  public Dictionary getDictionary(Enum dictIndex) {
076    return dictionaries.get(dictIndex);
077  }
078
079  void clear() {
080    for(Dictionary dictionary : dictionaries.values()){
081      dictionary.clear();
082    }
083    if (tagCompressionContext != null) {
084      tagCompressionContext.clear();
085    }
086  }
087}