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.compress.Compression.Algorithm;
022import org.apache.hadoop.hbase.io.crypto.Encryption;
023import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
024import org.apache.hadoop.hbase.util.ChecksumType;
025import org.apache.yetus.audience.InterfaceAudience;
026
027/**
028 * A builder that helps in building up the HFileContext 
029 */
030@InterfaceAudience.Private
031public class HFileContextBuilder {
032
033  public static final int DEFAULT_BYTES_PER_CHECKSUM = 16 * 1024;
034
035  /** Whether checksum is enabled or not **/
036  private boolean usesHBaseChecksum = true;
037  /** Whether mvcc is to be included in the Read/Write **/
038  private boolean includesMvcc = true;
039  /** Whether tags are to be included in the Read/Write **/
040  private boolean includesTags = false;
041  /** Compression algorithm used **/
042  private Algorithm compression = Algorithm.NONE;
043  /** Whether tags to be compressed or not **/
044  private boolean compressTags = false;
045  /** the checksum type **/
046  private ChecksumType checksumType = ChecksumType.getDefaultChecksumType();
047  /** the number of bytes per checksum value **/
048  private int bytesPerChecksum = DEFAULT_BYTES_PER_CHECKSUM;
049  /** Number of uncompressed bytes we allow per block. */
050  private int blocksize = HConstants.DEFAULT_BLOCKSIZE;
051  private DataBlockEncoding encoding = DataBlockEncoding.NONE;
052  /** Crypto context */
053  private Encryption.Context cryptoContext = Encryption.Context.NONE;
054  private long fileCreateTime = 0;
055
056  private String hfileName = null;
057
058  public HFileContextBuilder() {}
059
060  /**
061   * Use this constructor if you want to change a few settings only in another context.
062   */
063  public HFileContextBuilder(final HFileContext hfc) {
064    this.usesHBaseChecksum = hfc.isUseHBaseChecksum();
065    this.includesMvcc = hfc.isIncludesMvcc();
066    this.includesTags = hfc.isIncludesTags();
067    this.compression = hfc.getCompression();
068    this.compressTags = hfc.isCompressTags();
069    this.checksumType = hfc.getChecksumType();
070    this.bytesPerChecksum = hfc.getBytesPerChecksum();
071    this.blocksize = hfc.getBlocksize();
072    this.encoding = hfc.getDataBlockEncoding();
073    this.cryptoContext = hfc.getEncryptionContext();
074    this.fileCreateTime = hfc.getFileCreateTime();
075    this.hfileName = hfc.getHFileName();
076  }
077
078  public HFileContextBuilder withHBaseCheckSum(boolean useHBaseCheckSum) {
079    this.usesHBaseChecksum = useHBaseCheckSum;
080    return this;
081  }
082
083  public HFileContextBuilder withIncludesMvcc(boolean includesMvcc) {
084    this.includesMvcc = includesMvcc;
085    return this;
086  }
087
088  public HFileContextBuilder withIncludesTags(boolean includesTags) {
089    this.includesTags = includesTags;
090    return this;
091  }
092
093  public HFileContextBuilder withCompression(Algorithm compression) {
094    this.compression = compression;
095    return this;
096  }
097
098  public HFileContextBuilder withCompressTags(boolean compressTags) {
099    this.compressTags = compressTags;
100    return this;
101  }
102
103  public HFileContextBuilder withChecksumType(ChecksumType checkSumType) {
104    this.checksumType = checkSumType;
105    return this;
106  }
107
108  public HFileContextBuilder withBytesPerCheckSum(int bytesPerChecksum) {
109    this.bytesPerChecksum = bytesPerChecksum;
110    return this;
111  }
112
113  public HFileContextBuilder withBlockSize(int blockSize) {
114    this.blocksize = blockSize;
115    return this;
116  }
117
118  public HFileContextBuilder withDataBlockEncoding(DataBlockEncoding encoding) {
119    this.encoding = encoding;
120    return this;
121  }
122
123  public HFileContextBuilder withEncryptionContext(Encryption.Context cryptoContext) {
124    this.cryptoContext = cryptoContext;
125    return this;
126  }
127
128  public HFileContextBuilder withCreateTime(long fileCreateTime) {
129    this.fileCreateTime = fileCreateTime;
130    return this;
131  }
132
133  public HFileContextBuilder withHFileName(String name) {
134    this.hfileName = name;
135    return this;
136  }
137
138  public HFileContext build() {
139    return new HFileContext(usesHBaseChecksum, includesMvcc, includesTags, compression,
140        compressTags, checksumType, bytesPerChecksum, blocksize, encoding, cryptoContext,
141        fileCreateTime, hfileName);
142  }
143}