View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with this
4    * work for additional information regarding copyright ownership. The ASF
5    * licenses this file to you under the Apache License, Version 2.0 (the
6    * "License"); you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14   * License for the specific language governing permissions and limitations
15   * under the License.
16   */
17  package org.apache.hadoop.hbase.io.hfile;
18  
19  import java.io.DataOutputStream;
20  import java.io.IOException;
21  
22  import org.apache.hadoop.hbase.classification.InterfaceAudience;
23  import org.apache.hadoop.hbase.Cell;
24  import org.apache.hadoop.hbase.io.encoding.DataBlockEncoder;
25  import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
26  import org.apache.hadoop.hbase.io.encoding.HFileBlockDecodingContext;
27  import org.apache.hadoop.hbase.io.encoding.HFileBlockDefaultDecodingContext;
28  import org.apache.hadoop.hbase.io.encoding.HFileBlockDefaultEncodingContext;
29  import org.apache.hadoop.hbase.io.encoding.HFileBlockEncodingContext;
30  import org.apache.hadoop.hbase.io.hfile.HFile.FileInfo;
31  import org.apache.hadoop.hbase.util.Bytes;
32  
33  /**
34   * Do different kinds of data block encoding according to column family
35   * options.
36   */
37  @InterfaceAudience.Private
38  public class HFileDataBlockEncoderImpl implements HFileDataBlockEncoder {
39    private final DataBlockEncoding encoding;
40  
41    /**
42     * Do data block encoding with specified options.
43     * @param encoding What kind of data block encoding will be used.
44     */
45    public HFileDataBlockEncoderImpl(DataBlockEncoding encoding) {
46      this.encoding = encoding != null ? encoding : DataBlockEncoding.NONE;
47    }
48  
49    public static HFileDataBlockEncoder createFromFileInfo(
50        FileInfo fileInfo) throws IOException {
51      DataBlockEncoding encoding = DataBlockEncoding.NONE;
52      byte[] dataBlockEncodingType = fileInfo.get(DATA_BLOCK_ENCODING);
53      if (dataBlockEncodingType != null) {
54        String dataBlockEncodingStr = Bytes.toString(dataBlockEncodingType);
55        try {
56          encoding = DataBlockEncoding.valueOf(dataBlockEncodingStr);
57        } catch (IllegalArgumentException ex) {
58          throw new IOException("Invalid data block encoding type in file info: "
59            + dataBlockEncodingStr, ex);
60        }
61      }
62  
63      if (encoding == DataBlockEncoding.NONE) {
64        return NoOpDataBlockEncoder.INSTANCE;
65      }
66      return new HFileDataBlockEncoderImpl(encoding);
67    }
68  
69    @Override
70    public void saveMetadata(HFile.Writer writer) throws IOException {
71      writer.appendFileInfo(DATA_BLOCK_ENCODING, encoding.getNameInBytes());
72    }
73  
74    @Override
75    public DataBlockEncoding getDataBlockEncoding() {
76      return encoding;
77    }
78  
79    public boolean useEncodedScanner(boolean isCompaction) {
80      if (isCompaction && encoding == DataBlockEncoding.NONE) {
81        return false;
82      }
83      return encoding != DataBlockEncoding.NONE;
84    }
85  
86    @Override
87    public DataBlockEncoding getEffectiveEncodingInCache(boolean isCompaction) {
88      if (!useEncodedScanner(isCompaction)) {
89        return DataBlockEncoding.NONE;
90      }
91      return encoding;
92    }
93  
94    @Override
95    public int encode(Cell cell, HFileBlockEncodingContext encodingCtx, DataOutputStream out)
96        throws IOException {
97      return this.encoding.getEncoder().encode(cell, encodingCtx, out);
98    }
99  
100   @Override
101   public boolean useEncodedScanner() {
102     return encoding != DataBlockEncoding.NONE;
103   }
104 
105 
106   @Override
107   public String toString() {
108     return getClass().getSimpleName() + "(encoding=" + encoding + ")";
109   }
110 
111   @Override
112   public HFileBlockEncodingContext newDataBlockEncodingContext(
113       byte[] dummyHeader, HFileContext fileContext) {
114     DataBlockEncoder encoder = encoding.getEncoder();
115     if (encoder != null) {
116       return encoder.newDataBlockEncodingContext(encoding, dummyHeader, fileContext);
117     }
118     return new HFileBlockDefaultEncodingContext(null, dummyHeader, fileContext);
119   }
120 
121   @Override
122   public HFileBlockDecodingContext newDataBlockDecodingContext(HFileContext fileContext) {
123     DataBlockEncoder encoder = encoding.getEncoder();
124     if (encoder != null) {
125       return encoder.newDataBlockDecodingContext(fileContext);
126     }
127     return new HFileBlockDefaultDecodingContext(fileContext);
128   }
129 
130   @Override
131   public void startBlockEncoding(HFileBlockEncodingContext encodingCtx, DataOutputStream out)
132       throws IOException {
133     if (this.encoding != null && this.encoding != DataBlockEncoding.NONE) {
134       this.encoding.getEncoder().startBlockEncoding(encodingCtx, out);
135     }
136   }
137 
138   @Override
139   public void endBlockEncoding(HFileBlockEncodingContext encodingCtx, DataOutputStream out,
140       byte[] uncompressedBytesWithHeader, BlockType blockType) throws IOException {
141     this.encoding.getEncoder().endBlockEncoding(encodingCtx, out, uncompressedBytesWithHeader);
142   }
143 }