001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.io.hfile;
019
020import org.apache.hadoop.hbase.HConstants;
021import org.apache.hadoop.hbase.io.HeapSize;
022import org.apache.hadoop.hbase.io.compress.Compression;
023import org.apache.hadoop.hbase.io.crypto.Encryption;
024import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
025import org.apache.hadoop.hbase.util.Bytes;
026import org.apache.hadoop.hbase.util.ChecksumType;
027import org.apache.hadoop.hbase.util.ClassSize;
028import org.apache.yetus.audience.InterfaceAudience;
029
030/**
031 * This carries the information on some of the meta data about the HFile. This
032 * meta data is used across the HFileWriter/Readers and the HFileBlocks.
033 * This helps to add new information to the HFile.
034 */
035@InterfaceAudience.Private
036public class HFileContext implements HeapSize, Cloneable {
037
038  public static final int DEFAULT_BYTES_PER_CHECKSUM = 16 * 1024;
039
040  /** Whether checksum is enabled or not**/
041  private boolean usesHBaseChecksum = true;
042  /** Whether mvcc is to be included in the Read/Write**/
043  private boolean includesMvcc = true;
044  /**Whether tags are to be included in the Read/Write**/
045  private boolean includesTags;
046  /**Compression algorithm used**/
047  private Compression.Algorithm compressAlgo = Compression.Algorithm.NONE;
048  /** Whether tags to be compressed or not**/
049  private boolean compressTags;
050  /** the checksum type **/
051  private ChecksumType checksumType = ChecksumType.getDefaultChecksumType();
052  /** the number of bytes per checksum value **/
053  private int bytesPerChecksum = DEFAULT_BYTES_PER_CHECKSUM;
054  /** Number of uncompressed bytes we allow per block. */
055  private int blocksize = HConstants.DEFAULT_BLOCKSIZE;
056  private DataBlockEncoding encoding = DataBlockEncoding.NONE;
057  /** Encryption algorithm and key used */
058  private Encryption.Context cryptoContext = Encryption.Context.NONE;
059  private long fileCreateTime;
060  private String hfileName;
061
062  //Empty constructor.  Go with setters
063  public HFileContext() {
064  }
065
066  /**
067   * Copy constructor
068   * @param context
069   */
070  public HFileContext(HFileContext context) {
071    this.usesHBaseChecksum = context.usesHBaseChecksum;
072    this.includesMvcc = context.includesMvcc;
073    this.includesTags = context.includesTags;
074    this.compressAlgo = context.compressAlgo;
075    this.compressTags = context.compressTags;
076    this.checksumType = context.checksumType;
077    this.bytesPerChecksum = context.bytesPerChecksum;
078    this.blocksize = context.blocksize;
079    this.encoding = context.encoding;
080    this.cryptoContext = context.cryptoContext;
081    this.fileCreateTime = context.fileCreateTime;
082    this.hfileName = context.hfileName;
083  }
084
085  HFileContext(boolean useHBaseChecksum, boolean includesMvcc, boolean includesTags,
086      Compression.Algorithm compressAlgo, boolean compressTags, ChecksumType checksumType,
087      int bytesPerChecksum, int blockSize, DataBlockEncoding encoding,
088      Encryption.Context cryptoContext, long fileCreateTime, String hfileName) {
089    this.usesHBaseChecksum = useHBaseChecksum;
090    this.includesMvcc =  includesMvcc;
091    this.includesTags = includesTags;
092    this.compressAlgo = compressAlgo;
093    this.compressTags = compressTags;
094    this.checksumType = checksumType;
095    this.bytesPerChecksum = bytesPerChecksum;
096    this.blocksize = blockSize;
097    if (encoding != null) {
098      this.encoding = encoding;
099    }
100    this.cryptoContext = cryptoContext;
101    this.fileCreateTime = fileCreateTime;
102    this.hfileName = hfileName;
103  }
104
105  /**
106   * @return true when on-disk blocks from this file are compressed, and/or encrypted;
107   * false otherwise.
108   */
109  public boolean isCompressedOrEncrypted() {
110    Compression.Algorithm compressAlgo = getCompression();
111    boolean compressed =
112      compressAlgo != null
113        && compressAlgo != Compression.Algorithm.NONE;
114
115    Encryption.Context cryptoContext = getEncryptionContext();
116    boolean encrypted = cryptoContext != null
117      && cryptoContext != Encryption.Context.NONE;
118
119    return compressed || encrypted;
120  }
121
122  public Compression.Algorithm getCompression() {
123    return 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 Encryption.Context getEncryptionContext() {
179    return cryptoContext;
180  }
181
182  public void setEncryptionContext(Encryption.Context cryptoContext) {
183    this.cryptoContext = cryptoContext;
184  }
185
186  public String getHFileName() {
187    return this.hfileName;
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        5 * ClassSize.REFERENCE +
200        2 * Bytes.SIZEOF_INT +
201        // usesHBaseChecksum, includesMvcc, includesTags and compressTags
202        4 * Bytes.SIZEOF_BOOLEAN +
203        Bytes.SIZEOF_LONG);
204    if (this.hfileName != null) {
205      size += ClassSize.STRING + this.hfileName.length();
206    }
207    return size;
208  }
209
210  @Override
211  public HFileContext clone() {
212    try {
213      return (HFileContext)(super.clone());
214    } catch (CloneNotSupportedException e) {
215      throw new AssertionError(); // Won't happen
216    }
217  }
218
219  @Override
220  public String toString() {
221    StringBuilder sb = new StringBuilder();
222    sb.append("[");
223    sb.append("usesHBaseChecksum="); sb.append(usesHBaseChecksum);
224    sb.append(", checksumType=");      sb.append(checksumType);
225    sb.append(", bytesPerChecksum=");  sb.append(bytesPerChecksum);
226    sb.append(", blocksize=");         sb.append(blocksize);
227    sb.append(", encoding=");          sb.append(encoding);
228    sb.append(", includesMvcc=");      sb.append(includesMvcc);
229    sb.append(", includesTags=");      sb.append(includesTags);
230    sb.append(", compressAlgo=");      sb.append(compressAlgo);
231    sb.append(", compressTags=");      sb.append(compressTags);
232    sb.append(", cryptoContext=[");   sb.append(cryptoContext);      sb.append("]");
233    if (hfileName != null) {
234      sb.append(", name=");
235      sb.append(hfileName);
236    }
237    sb.append("]");
238    return sb.toString();
239  }
240
241}