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.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    public Compression.Algorithm getCompression() {
100     return compressAlgo;
101   }
102 
103   public void setCompression(Compression.Algorithm compressAlgo) {
104     this.compressAlgo = compressAlgo;
105   }
106 
107   public boolean isUseHBaseChecksum() {
108     return usesHBaseChecksum;
109   }
110 
111   public boolean isIncludesMvcc() {
112     return includesMvcc;
113   }
114 
115   public void setIncludesMvcc(boolean includesMvcc) {
116     this.includesMvcc = includesMvcc;
117   }
118 
119   public boolean isIncludesTags() {
120     return includesTags;
121   }
122 
123   public void setIncludesTags(boolean includesTags) {
124     this.includesTags = includesTags;
125   }
126 
127   public boolean isCompressTags() {
128     return compressTags;
129   }
130 
131   public void setCompressTags(boolean compressTags) {
132     this.compressTags = compressTags;
133   }
134 
135   public ChecksumType getChecksumType() {
136     return checksumType;
137   }
138 
139   public int getBytesPerChecksum() {
140     return bytesPerChecksum;
141   }
142 
143   public int getBlocksize() {
144     return blocksize;
145   }
146 
147   public DataBlockEncoding getDataBlockEncoding() {
148     return encoding;
149   }
150 
151   public void setDataBlockEncoding(DataBlockEncoding encoding) {
152     this.encoding = encoding;
153   }
154 
155   public Encryption.Context getEncryptionContext() {
156     return cryptoContext;
157   }
158 
159   public void setEncryptionContext(Encryption.Context cryptoContext) {
160     this.cryptoContext = cryptoContext;
161   }
162 
163   /**
164    * HeapSize implementation
165    * NOTE : The heapsize should be altered as and when new state variable are added
166    * @return heap size of the HFileContext
167    */
168   @Override
169   public long heapSize() {
170     long size = ClassSize.align(ClassSize.OBJECT +
171         // Algorithm reference, encodingon, checksumtype, Encryption.Context reference
172         4 * ClassSize.REFERENCE +
173         2 * Bytes.SIZEOF_INT +
174         // usesHBaseChecksum, includesMvcc, includesTags and compressTags
175         4 * Bytes.SIZEOF_BOOLEAN);
176     return size;
177   }
178 
179   @Override
180   public HFileContext clone() {
181     try {
182       return (HFileContext)(super.clone());
183     } catch (CloneNotSupportedException e) {
184       throw new AssertionError(); // Won't happen
185     }
186   }
187 
188   @Override
189   public String toString() {
190     StringBuilder sb = new StringBuilder();
191     sb.append("HFileContext [");
192     sb.append(" usesHBaseChecksum="); sb.append(usesHBaseChecksum);
193     sb.append(" checksumType=");      sb.append(checksumType);
194     sb.append(" bytesPerChecksum=");  sb.append(bytesPerChecksum);
195     sb.append(" blocksize=");         sb.append(blocksize);
196     sb.append(" encoding=");          sb.append(encoding);
197     sb.append(" includesMvcc=");      sb.append(includesMvcc);
198     sb.append(" includesTags=");      sb.append(includesTags);
199     sb.append(" compressAlgo=");      sb.append(compressAlgo);
200     sb.append(" compressTags=");      sb.append(compressTags);
201     sb.append(" cryptoContext=[ ");   sb.append(cryptoContext);      sb.append(" ]");
202     sb.append(" ]");
203     return sb.toString();
204   }
205 
206 }