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.HConstants;
20  import org.apache.hadoop.hbase.classification.InterfaceAudience;
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    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 boolean isUseHBaseChecksum() {
126     return usesHBaseChecksum;
127   }
128 
129   public boolean isIncludesMvcc() {
130     return includesMvcc;
131   }
132 
133   public void setIncludesMvcc(boolean includesMvcc) {
134     this.includesMvcc = includesMvcc;
135   }
136 
137   public boolean isIncludesTags() {
138     return includesTags;
139   }
140 
141   public void setIncludesTags(boolean includesTags) {
142     this.includesTags = includesTags;
143   }
144 
145   public void setFileCreateTime(long fileCreateTime) {
146     this.fileCreateTime = fileCreateTime;
147   }
148 
149   public boolean isCompressTags() {
150     return compressTags;
151   }
152 
153   public void setCompressTags(boolean compressTags) {
154     this.compressTags = compressTags;
155   }
156 
157   public ChecksumType getChecksumType() {
158     return checksumType;
159   }
160 
161   public int getBytesPerChecksum() {
162     return bytesPerChecksum;
163   }
164 
165   public int getBlocksize() {
166     return blocksize;
167   }
168 
169   public long getFileCreateTime() {
170     return fileCreateTime;
171   }
172 
173   public DataBlockEncoding getDataBlockEncoding() {
174     return encoding;
175   }
176 
177   public Encryption.Context getEncryptionContext() {
178     return cryptoContext;
179   }
180 
181   public void setEncryptionContext(Encryption.Context cryptoContext) {
182     this.cryptoContext = cryptoContext;
183   }
184 
185   public String getHFileName() {
186     return this.hfileName;
187   }
188 
189   /**
190    * HeapSize implementation
191    * NOTE : The heapsize should be altered as and when new state variable are added
192    * @return heap size of the HFileContext
193    */
194   @Override
195   public long heapSize() {
196     long size = ClassSize.align(ClassSize.OBJECT +
197         // Algorithm reference, encodingon, checksumtype, Encryption.Context reference
198         5 * ClassSize.REFERENCE +
199         2 * Bytes.SIZEOF_INT +
200         // usesHBaseChecksum, includesMvcc, includesTags and compressTags
201         4 * Bytes.SIZEOF_BOOLEAN +
202         Bytes.SIZEOF_LONG);
203     if (this.hfileName != null) {
204       size += ClassSize.STRING + this.hfileName.length();
205     }
206     return size;
207   }
208 
209   @Override
210   public HFileContext clone() {
211     try {
212       return (HFileContext)(super.clone());
213     } catch (CloneNotSupportedException e) {
214       throw new AssertionError(); // Won't happen
215     }
216   }
217 
218   @Override
219   public String toString() {
220     StringBuilder sb = new StringBuilder();
221     sb.append("[");
222     sb.append("usesHBaseChecksum="); sb.append(usesHBaseChecksum);
223     sb.append(", checksumType=");      sb.append(checksumType);
224     sb.append(", bytesPerChecksum=");  sb.append(bytesPerChecksum);
225     sb.append(", blocksize=");         sb.append(blocksize);
226     sb.append(", encoding=");          sb.append(encoding);
227     sb.append(", includesMvcc=");      sb.append(includesMvcc);
228     sb.append(", includesTags=");      sb.append(includesTags);
229     sb.append(", compressAlgo=");      sb.append(compressAlgo);
230     sb.append(", compressTags=");      sb.append(compressTags);
231     sb.append(", cryptoContext=[");   sb.append(cryptoContext);      sb.append("]");
232     if (hfileName != null) {
233       sb.append(", name=");
234       sb.append(hfileName);
235     }
236     sb.append("]");
237     return sb.toString();
238   }
239 
240 }