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