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  package org.apache.hadoop.hbase.io.hfile;
19  import org.apache.hadoop.hbase.classification.InterfaceAudience;
20  import org.apache.hadoop.hbase.HConstants;
21  import org.apache.hadoop.hbase.io.HeapSize;
22  import org.apache.hadoop.hbase.io.compress.Compression;
23  import org.apache.hadoop.hbase.io.crypto.Encryption;
24  import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
25  import org.apache.hadoop.hbase.util.Bytes;
26  import org.apache.hadoop.hbase.util.ChecksumType;
27  import org.apache.hadoop.hbase.util.ClassSize;
28  
29  /**
30   * This carries the information on some of the meta data about the HFile. This
31   * meta data is used across the HFileWriter/Readers and the HFileBlocks.
32   * This helps to add new information to the HFile.
33   */
34  @InterfaceAudience.Private
35  public class HFileContext implements HeapSize, Cloneable {
36  
37    public static final int DEFAULT_BYTES_PER_CHECKSUM = 16 * 1024;
38  
39    /** Whether checksum is enabled or not**/
40    private boolean usesHBaseChecksum = true;
41    /** Whether mvcc is to be included in the Read/Write**/
42    private boolean includesMvcc = true;
43    /**Whether tags are to be included in the Read/Write**/
44    private boolean includesTags;
45    /**Compression algorithm used**/
46    private Compression.Algorithm compressAlgo = Compression.Algorithm.NONE;
47    /** Whether tags to be compressed or not**/
48    private boolean compressTags;
49    /** the checksum type **/
50    private ChecksumType checksumType = ChecksumType.getDefaultChecksumType();
51    /** the number of bytes per checksum value **/
52    private int bytesPerChecksum = DEFAULT_BYTES_PER_CHECKSUM;
53    /** Number of uncompressed bytes we allow per block. */
54    private int blocksize = HConstants.DEFAULT_BLOCKSIZE;
55    private DataBlockEncoding encoding = DataBlockEncoding.NONE;
56    /** Encryption algorithm and key used */
57    private Encryption.Context cryptoContext = Encryption.Context.NONE;
58    private long fileCreateTime;
59    private String hfileName;
60  
61    //Empty constructor.  Go with setters
62    public HFileContext() {
63    }
64  
65    /**
66     * Copy constructor
67     * @param context
68     */
69    public HFileContext(HFileContext context) {
70      this.usesHBaseChecksum = context.usesHBaseChecksum;
71      this.includesMvcc = context.includesMvcc;
72      this.includesTags = context.includesTags;
73      this.compressAlgo = context.compressAlgo;
74      this.compressTags = context.compressTags;
75      this.checksumType = context.checksumType;
76      this.bytesPerChecksum = context.bytesPerChecksum;
77      this.blocksize = context.blocksize;
78      this.encoding = context.encoding;
79      this.cryptoContext = context.cryptoContext;
80      this.fileCreateTime = context.fileCreateTime;
81      this.hfileName = context.hfileName;
82    }
83  
84    public HFileContext(boolean useHBaseChecksum, boolean includesMvcc, boolean includesTags,
85        Compression.Algorithm compressAlgo, boolean compressTags, ChecksumType checksumType,
86        int bytesPerChecksum, int blockSize, DataBlockEncoding encoding,
87        Encryption.Context cryptoContext, long fileCreateTime, String hfileName) {
88      this.usesHBaseChecksum = useHBaseChecksum;
89      this.includesMvcc =  includesMvcc;
90      this.includesTags = includesTags;
91      this.compressAlgo = compressAlgo;
92      this.compressTags = compressTags;
93      this.checksumType = checksumType;
94      this.bytesPerChecksum = bytesPerChecksum;
95      this.blocksize = blockSize;
96      if (encoding != null) {
97        this.encoding = encoding;
98      }
99      this.cryptoContext = cryptoContext;
100     this.fileCreateTime = fileCreateTime;
101     this.hfileName = hfileName;
102   }
103 
104   /**
105    * @return true when on-disk blocks from this file are compressed, and/or encrypted;
106    * false otherwise.
107    */
108   public boolean isCompressedOrEncrypted() {
109     Compression.Algorithm compressAlgo = getCompression();
110     boolean compressed =
111       compressAlgo != null
112         && compressAlgo != Compression.Algorithm.NONE;
113 
114     Encryption.Context cryptoContext = getEncryptionContext();
115     boolean encrypted = cryptoContext != null
116       && cryptoContext != Encryption.Context.NONE;
117 
118     return compressed || encrypted;
119   }
120 
121   public Compression.Algorithm getCompression() {
122     return compressAlgo;
123   }
124 
125   public void setCompression(Compression.Algorithm compressAlgo) {
126     this.compressAlgo = compressAlgo;
127   }
128 
129   public boolean isUseHBaseChecksum() {
130     return usesHBaseChecksum;
131   }
132 
133   public boolean isIncludesMvcc() {
134     return includesMvcc;
135   }
136 
137   public void setIncludesMvcc(boolean includesMvcc) {
138     this.includesMvcc = includesMvcc;
139   }
140 
141   public boolean isIncludesTags() {
142     return includesTags;
143   }
144 
145   public void setIncludesTags(boolean includesTags) {
146     this.includesTags = includesTags;
147   }
148 
149   public void setFileCreateTime(long fileCreateTime) {
150     this.fileCreateTime = fileCreateTime;
151   }
152 
153   public boolean isCompressTags() {
154     return compressTags;
155   }
156 
157   public void setCompressTags(boolean compressTags) {
158     this.compressTags = compressTags;
159   }
160 
161   public ChecksumType getChecksumType() {
162     return checksumType;
163   }
164 
165   public int getBytesPerChecksum() {
166     return bytesPerChecksum;
167   }
168 
169   public int getBlocksize() {
170     return blocksize;
171   }
172 
173   public long getFileCreateTime() {
174     return fileCreateTime;
175   }
176 
177   public DataBlockEncoding getDataBlockEncoding() {
178     return encoding;
179   }
180 
181   public void setDataBlockEncoding(DataBlockEncoding encoding) {
182     this.encoding = encoding;
183   }
184 
185   public Encryption.Context getEncryptionContext() {
186     return cryptoContext;
187   }
188 
189   public void setEncryptionContext(Encryption.Context cryptoContext) {
190     this.cryptoContext = cryptoContext;
191   }
192 
193   public String getHFileName() {
194     return this.hfileName;
195   }
196 
197   /**
198    * HeapSize implementation
199    * NOTE : The heapsize should be altered as and when new state variable are added
200    * @return heap size of the HFileContext
201    */
202   @Override
203   public long heapSize() {
204     long size = ClassSize.align(ClassSize.OBJECT +
205         // Algorithm reference, encodingon, checksumtype, Encryption.Context reference
206         5 * ClassSize.REFERENCE +
207         2 * Bytes.SIZEOF_INT +
208         // usesHBaseChecksum, includesMvcc, includesTags and compressTags
209         4 * Bytes.SIZEOF_BOOLEAN +
210         Bytes.SIZEOF_LONG);
211     return size;
212   }
213 
214   @Override
215   public HFileContext clone() {
216     try {
217       return (HFileContext)(super.clone());
218     } catch (CloneNotSupportedException e) {
219       throw new AssertionError(); // Won't happen
220     }
221   }
222 
223   @Override
224   public String toString() {
225     StringBuilder sb = new StringBuilder();
226     sb.append("HFileContext [");
227     sb.append(" usesHBaseChecksum="); sb.append(usesHBaseChecksum);
228     sb.append(" checksumType=");      sb.append(checksumType);
229     sb.append(" bytesPerChecksum=");  sb.append(bytesPerChecksum);
230     sb.append(" blocksize=");         sb.append(blocksize);
231     sb.append(" encoding=");          sb.append(encoding);
232     sb.append(" includesMvcc=");      sb.append(includesMvcc);
233     sb.append(" includesTags=");      sb.append(includesTags);
234     sb.append(" compressAlgo=");      sb.append(compressAlgo);
235     sb.append(" compressTags=");      sb.append(compressTags);
236     sb.append(" cryptoContext=[ ");   sb.append(cryptoContext);      sb.append(" ]");
237     sb.append(" ]");
238     return sb.toString();
239   }
240 
241 }