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