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    private byte[] columnFamily;
61    private byte[] tableName;
62  
63    //Empty constructor.  Go with setters
64    public HFileContext() {
65    }
66  
67    /**
68     * Copy constructor
69     * @param context
70     */
71    public HFileContext(HFileContext context) {
72      this.usesHBaseChecksum = context.usesHBaseChecksum;
73      this.includesMvcc = context.includesMvcc;
74      this.includesTags = context.includesTags;
75      this.compressAlgo = context.compressAlgo;
76      this.compressTags = context.compressTags;
77      this.checksumType = context.checksumType;
78      this.bytesPerChecksum = context.bytesPerChecksum;
79      this.blocksize = context.blocksize;
80      this.encoding = context.encoding;
81      this.cryptoContext = context.cryptoContext;
82      this.fileCreateTime = context.fileCreateTime;
83      this.hfileName = context.hfileName;
84      this.columnFamily = context.columnFamily;
85      this.tableName = context.tableName;
86    }
87  
88    public HFileContext(boolean useHBaseChecksum, boolean includesMvcc, boolean includesTags,
89                 Compression.Algorithm compressAlgo, boolean compressTags, ChecksumType checksumType,
90                 int bytesPerChecksum, int blockSize, DataBlockEncoding encoding,
91                 Encryption.Context cryptoContext, long fileCreateTime, String hfileName,
92                 byte[] columnFamily, byte[] tableName) {
93      this.usesHBaseChecksum = useHBaseChecksum;
94      this.includesMvcc =  includesMvcc;
95      this.includesTags = includesTags;
96      this.compressAlgo = compressAlgo;
97      this.compressTags = compressTags;
98      this.checksumType = checksumType;
99      this.bytesPerChecksum = bytesPerChecksum;
100     this.blocksize = blockSize;
101     if (encoding != null) {
102       this.encoding = encoding;
103     }
104     this.cryptoContext = cryptoContext;
105     this.fileCreateTime = fileCreateTime;
106     this.hfileName = hfileName;
107     this.columnFamily = columnFamily;
108     this.tableName = tableName;
109   }
110 
111   /**
112    * @return true when on-disk blocks from this file are compressed, and/or encrypted;
113    * false otherwise.
114    */
115   public boolean isCompressedOrEncrypted() {
116     Compression.Algorithm compressAlgo = getCompression();
117     boolean compressed =
118       compressAlgo != null
119         && compressAlgo != Compression.Algorithm.NONE;
120 
121     Encryption.Context cryptoContext = getEncryptionContext();
122     boolean encrypted = cryptoContext != null
123       && cryptoContext != Encryption.Context.NONE;
124 
125     return compressed || encrypted;
126   }
127 
128   public Compression.Algorithm getCompression() {
129     return compressAlgo;
130   }
131 
132   public void setCompression(Compression.Algorithm compressAlgo) {
133     this.compressAlgo = compressAlgo;
134   }
135 
136   public boolean isUseHBaseChecksum() {
137     return usesHBaseChecksum;
138   }
139 
140   public boolean isIncludesMvcc() {
141     return includesMvcc;
142   }
143 
144   public void setIncludesMvcc(boolean includesMvcc) {
145     this.includesMvcc = includesMvcc;
146   }
147 
148   public boolean isIncludesTags() {
149     return includesTags;
150   }
151 
152   public void setIncludesTags(boolean includesTags) {
153     this.includesTags = includesTags;
154   }
155 
156   public void setFileCreateTime(long fileCreateTime) {
157     this.fileCreateTime = fileCreateTime;
158   }
159 
160   public boolean isCompressTags() {
161     return compressTags;
162   }
163 
164   public void setCompressTags(boolean compressTags) {
165     this.compressTags = compressTags;
166   }
167 
168   public ChecksumType getChecksumType() {
169     return checksumType;
170   }
171 
172   public int getBytesPerChecksum() {
173     return bytesPerChecksum;
174   }
175 
176   public int getBlocksize() {
177     return blocksize;
178   }
179 
180   public long getFileCreateTime() {
181     return fileCreateTime;
182   }
183 
184   public DataBlockEncoding getDataBlockEncoding() {
185     return encoding;
186   }
187 
188   public void setDataBlockEncoding(DataBlockEncoding encoding) {
189     this.encoding = encoding;
190   }
191 
192   public Encryption.Context getEncryptionContext() {
193     return cryptoContext;
194   }
195 
196   public void setEncryptionContext(Encryption.Context cryptoContext) {
197     this.cryptoContext = cryptoContext;
198   }
199 
200   public String getHFileName() {
201     return this.hfileName;
202   }
203 
204   public byte[] getColumnFamily() {
205     return this.columnFamily;
206   }
207 
208   public byte[] getTableName() {
209     return this.tableName;
210   }
211 
212   /**
213    * HeapSize implementation
214    * NOTE : The heapsize should be altered as and when new state variable are added
215    * @return heap size of the HFileContext
216    */
217   @Override
218   public long heapSize() {
219     long size = ClassSize.align(ClassSize.OBJECT +
220         // Algorithm reference, encodingon, checksumtype, Encryption.Context reference
221         5 * ClassSize.REFERENCE +
222         2 * Bytes.SIZEOF_INT +
223         // usesHBaseChecksum, includesMvcc, includesTags and compressTags
224         4 * Bytes.SIZEOF_BOOLEAN +
225         //column family, table name byte arrays
226         2 * ClassSize.ARRAY + 2 * ClassSize.REFERENCE +
227         Bytes.SIZEOF_LONG);
228     if (this.columnFamily != null){
229       size += ClassSize.sizeOf(this.columnFamily, this.columnFamily.length);
230     }
231     if (this.tableName != null){
232       size += ClassSize.sizeOf(this.tableName, this.tableName.length);
233     }
234     return size;
235   }
236 
237   @Override
238   public HFileContext clone() {
239     try {
240       return (HFileContext)(super.clone());
241     } catch (CloneNotSupportedException e) {
242       throw new AssertionError(); // Won't happen
243     }
244   }
245 
246   @Override
247   public String toString() {
248     StringBuilder sb = new StringBuilder();
249     sb.append("HFileContext [");
250     sb.append(" usesHBaseChecksum="); sb.append(usesHBaseChecksum);
251     sb.append(" checksumType=");      sb.append(checksumType);
252     sb.append(" bytesPerChecksum=");  sb.append(bytesPerChecksum);
253     sb.append(" blocksize=");         sb.append(blocksize);
254     sb.append(" encoding=");          sb.append(encoding);
255     sb.append(" includesMvcc=");      sb.append(includesMvcc);
256     sb.append(" includesTags=");      sb.append(includesTags);
257     sb.append(" compressAlgo=");      sb.append(compressAlgo);
258     sb.append(" compressTags=");      sb.append(compressTags);
259     sb.append(" cryptoContext=[ ");   sb.append(cryptoContext);
260     sb.append(" ]");
261     if (tableName != null) {
262       sb.append(", tableName=");
263       sb.append(Bytes.toString(tableName));
264     }
265     if (columnFamily != null) {
266       sb.append(", columnFamily=");
267       sb.append(Bytes.toString(columnFamily));
268     }
269     sb.append(" ]");
270     return sb.toString();
271   }
272 }