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