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