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    public static final ChecksumType DEFAULT_CHECKSUM_TYPE = ChecksumType.CRC32;
39  
40    /** Whether checksum is enabled or not**/
41    private boolean usesHBaseChecksum = true;
42    /** Whether mvcc is to be included in the Read/Write**/
43    private boolean includesMvcc = true;
44    /**Whether tags are to be included in the Read/Write**/
45    private boolean includesTags;
46    /**Compression algorithm used**/
47    private Compression.Algorithm compressAlgo = Compression.Algorithm.NONE;
48    /** Whether tags to be compressed or not**/
49    private boolean compressTags;
50    /** the checksum type **/
51    private ChecksumType checksumType = DEFAULT_CHECKSUM_TYPE;
52    /** the number of bytes per checksum value **/
53    private int bytesPerChecksum = DEFAULT_BYTES_PER_CHECKSUM;
54    /** Number of uncompressed bytes we allow per block. */
55    private int blocksize = HConstants.DEFAULT_BLOCKSIZE;
56    private DataBlockEncoding encoding = DataBlockEncoding.NONE;
57    /** Encryption algorithm and key used */
58    private Encryption.Context cryptoContext = Encryption.Context.NONE;
59    private long fileCreateTime;
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    }
82  
83    public HFileContext(boolean useHBaseChecksum, boolean includesMvcc, boolean includesTags,
84        Compression.Algorithm compressAlgo, boolean compressTags, ChecksumType checksumType,
85        int bytesPerChecksum, int blockSize, DataBlockEncoding encoding,
86        Encryption.Context cryptoContext, long fileCreateTime) {
87      this.usesHBaseChecksum = useHBaseChecksum;
88      this.includesMvcc =  includesMvcc;
89      this.includesTags = includesTags;
90      this.compressAlgo = compressAlgo;
91      this.compressTags = compressTags;
92      this.checksumType = checksumType;
93      this.bytesPerChecksum = bytesPerChecksum;
94      this.blocksize = blockSize;
95      if (encoding != null) {
96        this.encoding = encoding;
97      }
98      this.cryptoContext = cryptoContext;
99      this.fileCreateTime = fileCreateTime;
100   }
101 
102   /**
103    * @return true when on-disk blocks from this file are compressed, and/or encrypted;
104    * false otherwise.
105    */
106   public boolean isCompressedOrEncrypted() {
107     Compression.Algorithm compressAlgo = getCompression();
108     boolean compressed =
109       compressAlgo != null
110         && compressAlgo != Compression.Algorithm.NONE;
111 
112     Encryption.Context cryptoContext = getEncryptionContext();
113     boolean encrypted = cryptoContext != null
114       && cryptoContext != Encryption.Context.NONE;
115 
116     return compressed || encrypted;
117   }
118 
119   public Compression.Algorithm getCompression() {
120     return compressAlgo;
121   }
122 
123   public void setCompression(Compression.Algorithm compressAlgo) {
124     this.compressAlgo = compressAlgo;
125   }
126 
127   public boolean isUseHBaseChecksum() {
128     return usesHBaseChecksum;
129   }
130 
131   public boolean isIncludesMvcc() {
132     return includesMvcc;
133   }
134 
135   public void setIncludesMvcc(boolean includesMvcc) {
136     this.includesMvcc = includesMvcc;
137   }
138 
139   public boolean isIncludesTags() {
140     return includesTags;
141   }
142 
143   public void setIncludesTags(boolean includesTags) {
144     this.includesTags = includesTags;
145   }
146 
147   public void setFileCreateTime(long fileCreateTime) {
148     this.fileCreateTime = fileCreateTime;
149   }
150 
151   public boolean isCompressTags() {
152     return compressTags;
153   }
154 
155   public void setCompressTags(boolean compressTags) {
156     this.compressTags = compressTags;
157   }
158 
159   public ChecksumType getChecksumType() {
160     return checksumType;
161   }
162 
163   public int getBytesPerChecksum() {
164     return bytesPerChecksum;
165   }
166 
167   public int getBlocksize() {
168     return blocksize;
169   }
170 
171   public long getFileCreateTime() {
172     return fileCreateTime;
173   }
174 
175   public DataBlockEncoding getDataBlockEncoding() {
176     return encoding;
177   }
178 
179   public void setDataBlockEncoding(DataBlockEncoding encoding) {
180     this.encoding = encoding;
181   }
182 
183   public Encryption.Context getEncryptionContext() {
184     return cryptoContext;
185   }
186 
187   public void setEncryptionContext(Encryption.Context cryptoContext) {
188     this.cryptoContext = cryptoContext;
189   }
190 
191   /**
192    * HeapSize implementation
193    * NOTE : The heapsize should be altered as and when new state variable are added
194    * @return heap size of the HFileContext
195    */
196   @Override
197   public long heapSize() {
198     long size = ClassSize.align(ClassSize.OBJECT +
199         // Algorithm reference, encodingon, checksumtype, Encryption.Context reference
200         4 * ClassSize.REFERENCE +
201         2 * Bytes.SIZEOF_INT +
202         // usesHBaseChecksum, includesMvcc, includesTags and compressTags
203         4 * Bytes.SIZEOF_BOOLEAN +
204         Bytes.SIZEOF_LONG);
205     return size;
206   }
207 
208   @Override
209   public HFileContext clone() {
210     try {
211       return (HFileContext)(super.clone());
212     } catch (CloneNotSupportedException e) {
213       throw new AssertionError(); // Won't happen
214     }
215   }
216 
217   @Override
218   public String toString() {
219     StringBuilder sb = new StringBuilder();
220     sb.append("HFileContext [");
221     sb.append(" usesHBaseChecksum="); sb.append(usesHBaseChecksum);
222     sb.append(" checksumType=");      sb.append(checksumType);
223     sb.append(" bytesPerChecksum=");  sb.append(bytesPerChecksum);
224     sb.append(" blocksize=");         sb.append(blocksize);
225     sb.append(" encoding=");          sb.append(encoding);
226     sb.append(" includesMvcc=");      sb.append(includesMvcc);
227     sb.append(" includesTags=");      sb.append(includesTags);
228     sb.append(" compressAlgo=");      sb.append(compressAlgo);
229     sb.append(" compressTags=");      sb.append(compressTags);
230     sb.append(" cryptoContext=[ ");   sb.append(cryptoContext);      sb.append(" ]");
231     sb.append(" ]");
232     return sb.toString();
233   }
234 
235 }