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 */ 018package org.apache.hadoop.hbase.io.encoding; 019 020import java.io.IOException; 021import java.io.OutputStream; 022import org.apache.hadoop.hbase.util.Bytes; 023import org.apache.yetus.audience.InterfaceAudience; 024 025/** 026 * Provide access to all data block encoding algorithms. All of the algorithms are required to have 027 * unique id which should <b>NEVER</b> be changed. If you want to add a new algorithm/version, 028 * assign it a new id. Announce the new id in the HBase mailing list to prevent collisions. 029 */ 030@InterfaceAudience.Public 031public enum DataBlockEncoding { 032 033 /** Disable data block encoding. */ 034 NONE(0, null), 035 // id 1 is reserved for the BITSET algorithm to be added later 036 PREFIX(2, "org.apache.hadoop.hbase.io.encoding.PrefixKeyDeltaEncoder"), 037 DIFF(3, "org.apache.hadoop.hbase.io.encoding.DiffKeyDeltaEncoder"), 038 FAST_DIFF(4, "org.apache.hadoop.hbase.io.encoding.FastDiffDeltaEncoder"), 039 // id 5 is reserved for the COPY_KEY algorithm for benchmarking 040 // COPY_KEY(5, "org.apache.hadoop.hbase.io.encoding.CopyKeyDataBlockEncoder"), 041 // PREFIX_TREE(6, "org.apache.hadoop.hbase.codec.prefixtree.PrefixTreeCodec"), 042 ROW_INDEX_V1(7, "org.apache.hadoop.hbase.io.encoding.RowIndexCodecV1"); 043 044 private final short id; 045 private final byte[] idInBytes; 046 private DataBlockEncoder encoder; 047 private final String encoderCls; 048 049 public static final int ID_SIZE = Bytes.SIZEOF_SHORT; 050 051 /** Maps data block encoding ids to enum instances. */ 052 private static DataBlockEncoding[] idArray = new DataBlockEncoding[Byte.MAX_VALUE + 1]; 053 054 static { 055 for (DataBlockEncoding algo : values()) { 056 if (idArray[algo.id] != null) { 057 throw new RuntimeException( 058 String.format("Two data block encoder algorithms '%s' and '%s' have " + "the same id %d", 059 idArray[algo.id].toString(), algo.toString(), (int) algo.id)); 060 } 061 idArray[algo.id] = algo; 062 } 063 } 064 065 private DataBlockEncoding(int id, String encoderClsName) { 066 if (id < 0 || id > Byte.MAX_VALUE) { 067 throw new AssertionError("Data block encoding algorithm id is out of range: " + id); 068 } 069 this.id = (short) id; 070 this.idInBytes = Bytes.toBytes(this.id); 071 if (idInBytes.length != ID_SIZE) { 072 // White this may seem redundant, if we accidentally serialize 073 // the id as e.g. an int instead of a short, all encoders will break. 074 throw new RuntimeException("Unexpected length of encoder ID byte " + "representation: " 075 + Bytes.toStringBinary(idInBytes)); 076 } 077 this.encoderCls = encoderClsName; 078 } 079 080 /** Returns name converted to bytes. */ 081 public byte[] getNameInBytes() { 082 return Bytes.toBytes(toString()); 083 } 084 085 /** Returns The id of a data block encoder. */ 086 public short getId() { 087 return id; 088 } 089 090 /** 091 * Writes id in bytes. 092 * @param stream where the id should be written. 093 */ 094 public void writeIdInBytes(OutputStream stream) throws IOException { 095 stream.write(idInBytes); 096 } 097 098 /** 099 * Writes id bytes to the given array starting from offset. 100 * @param dest output array 101 * @param offset starting offset of the output array 102 */ 103 public void writeIdInBytes(byte[] dest, int offset) throws IOException { 104 System.arraycopy(idInBytes, 0, dest, offset, ID_SIZE); 105 } 106 107 /** 108 * Return new data block encoder for given algorithm type. 109 * @return data block encoder if algorithm is specified, null if none is selected. 110 */ 111 public DataBlockEncoder getEncoder() { 112 if (encoder == null && id != 0) { 113 // lazily create the encoder 114 encoder = createEncoder(encoderCls); 115 } 116 return encoder; 117 } 118 119 /** 120 * Find and create data block encoder for given id; 121 * @param encoderId id of data block encoder. 122 * @return Newly created data block encoder. 123 */ 124 public static DataBlockEncoder getDataBlockEncoderById(short encoderId) { 125 return getEncodingById(encoderId).getEncoder(); 126 } 127 128 /** 129 * Find and return the name of data block encoder for the given id. 130 * @param encoderId id of data block encoder 131 * @return name, same as used in options in column family 132 */ 133 public static String getNameFromId(short encoderId) { 134 return getEncodingById(encoderId).toString(); 135 } 136 137 /** 138 * Check if given encoder has this id. 139 * @param encoder encoder which id will be checked 140 * @param encoderId id which we except 141 * @return true if id is right for given encoder, false otherwise 142 * @exception IllegalArgumentException thrown when there is no matching data block encoder 143 */ 144 public static boolean isCorrectEncoder(DataBlockEncoder encoder, short encoderId) { 145 DataBlockEncoding algorithm = getEncodingById(encoderId); 146 String encoderCls = encoder.getClass().getName(); 147 return encoderCls.equals(algorithm.encoderCls); 148 } 149 150 public static DataBlockEncoding getEncodingById(short dataBlockEncodingId) { 151 DataBlockEncoding algorithm = null; 152 if (dataBlockEncodingId >= 0 && dataBlockEncodingId <= Byte.MAX_VALUE) { 153 algorithm = idArray[dataBlockEncodingId]; 154 } 155 if (algorithm == null) { 156 throw new IllegalArgumentException(String 157 .format("There is no data block encoder for given id '%d'", (int) dataBlockEncodingId)); 158 } 159 return algorithm; 160 } 161 162 protected static DataBlockEncoder createEncoder(String fullyQualifiedClassName) { 163 try { 164 return (DataBlockEncoder) Class.forName(fullyQualifiedClassName).getDeclaredConstructor() 165 .newInstance(); 166 } catch (Exception e) { 167 throw new RuntimeException(e); 168 } 169 } 170 171}