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