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.hfile;
019
020import java.io.DataOutput;
021import java.io.IOException;
022import org.apache.hadoop.hbase.io.encoding.IndexBlockEncoding;
023import org.apache.hadoop.hbase.util.Bytes;
024import org.apache.yetus.audience.InterfaceAudience;
025
026/**
027 * Do different kinds of index block encoding according to column family options.
028 */
029@InterfaceAudience.Private
030public class HFileIndexBlockEncoderImpl implements HFileIndexBlockEncoder {
031  private final IndexBlockEncoding indexBlockEncoding;
032
033  /**
034   * Do index block encoding with specified options.
035   * @param encoding What kind of data block encoding will be used.
036   */
037  public HFileIndexBlockEncoderImpl(IndexBlockEncoding encoding) {
038    this.indexBlockEncoding = encoding != null ? encoding : IndexBlockEncoding.NONE;
039  }
040
041  public static HFileIndexBlockEncoder createFromFileInfo(HFileInfo fileInfo) throws IOException {
042    IndexBlockEncoding encoding = IndexBlockEncoding.NONE;
043    byte[] dataBlockEncodingType = fileInfo.get(INDEX_BLOCK_ENCODING);
044    if (dataBlockEncodingType != null) {
045      String dataBlockEncodingStr = Bytes.toString(dataBlockEncodingType);
046      try {
047        encoding = IndexBlockEncoding.valueOf(dataBlockEncodingStr);
048      } catch (IllegalArgumentException ex) {
049        throw new IOException(
050          "Invalid data block encoding type in file info: " + dataBlockEncodingStr, ex);
051      }
052    }
053
054    if (encoding == IndexBlockEncoding.NONE) {
055      return NoOpIndexBlockEncoder.INSTANCE;
056    }
057    return new HFileIndexBlockEncoderImpl(encoding);
058  }
059
060  @Override
061  public void saveMetadata(HFile.Writer writer) throws IOException {
062    writer.appendFileInfo(INDEX_BLOCK_ENCODING, indexBlockEncoding.getNameInBytes());
063  }
064
065  @Override
066  public IndexBlockEncoding getIndexBlockEncoding() {
067    return indexBlockEncoding;
068  }
069
070  @Override
071  public void encode(BlockIndexChunk blockIndexChunk, boolean rootIndexBlock, DataOutput out)
072    throws IOException {
073    // TODO
074  }
075
076  @Override
077  public EncodedSeeker createSeeker() {
078    return null;
079  }
080
081  @Override
082  public String toString() {
083    return getClass().getSimpleName() + "(indexBlockEncoding=" + indexBlockEncoding + ")";
084  }
085}