View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.io.hfile;
19  
20  import org.apache.hadoop.hbase.classification.InterfaceAudience;
21  import org.apache.hadoop.hbase.HConstants;
22  import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;
23  import org.apache.hadoop.hbase.io.crypto.Encryption;
24  import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
25  import org.apache.hadoop.hbase.util.ChecksumType;
26  
27  /**
28   * A builder that helps in building up the HFileContext
29   */
30  @InterfaceAudience.Private
31  public class HFileContextBuilder {
32  
33    public static final int DEFAULT_BYTES_PER_CHECKSUM = 16 * 1024;
34  
35    /** Whether checksum is enabled or not **/
36    private boolean usesHBaseChecksum = true;
37    /** Whether mvcc is to be included in the Read/Write **/
38    private boolean includesMvcc = true;
39    /** Whether tags are to be included in the Read/Write **/
40    private boolean includesTags = false;
41    /** Compression algorithm used **/
42    private Algorithm compression = Algorithm.NONE;
43    /** Whether tags to be compressed or not **/
44    private boolean compressTags = false;
45    /** the checksum type **/
46    private ChecksumType checksumType = ChecksumType.getDefaultChecksumType();
47    /** the number of bytes per checksum value **/
48    private int bytesPerChecksum = DEFAULT_BYTES_PER_CHECKSUM;
49    /** Number of uncompressed bytes we allow per block. */
50    private int blocksize = HConstants.DEFAULT_BLOCKSIZE;
51    private DataBlockEncoding encoding = DataBlockEncoding.NONE;
52    /** Crypto context */
53    private Encryption.Context cryptoContext = Encryption.Context.NONE;
54    private long fileCreateTime = 0;
55  
56    private String hfileName = null;
57    private byte[] columnFamily = null;
58    private byte[] tableName = null;
59  
60    public HFileContextBuilder() {}
61  
62    /**
63     * Use this constructor if you want to change a few settings only in another context.
64     */
65    public HFileContextBuilder(final HFileContext hfc) {
66      this.usesHBaseChecksum = hfc.isUseHBaseChecksum();
67      this.includesMvcc = hfc.isIncludesMvcc();
68      this.includesTags = hfc.isIncludesTags();
69      this.compression = hfc.getCompression();
70      this.compressTags = hfc.isCompressTags();
71      this.checksumType = hfc.getChecksumType();
72      this.bytesPerChecksum = hfc.getBytesPerChecksum();
73      this.blocksize = hfc.getBlocksize();
74      this.encoding = hfc.getDataBlockEncoding();
75      this.cryptoContext = hfc.getEncryptionContext();
76      this.fileCreateTime = hfc.getFileCreateTime();
77      this.hfileName = hfc.getHFileName();
78      this.columnFamily = hfc.getColumnFamily();
79      this.tableName = hfc.getTableName();
80    }
81  
82    public HFileContextBuilder withHBaseCheckSum(boolean useHBaseCheckSum) {
83      this.usesHBaseChecksum = useHBaseCheckSum;
84      return this;
85    }
86  
87    public HFileContextBuilder withIncludesMvcc(boolean includesMvcc) {
88      this.includesMvcc = includesMvcc;
89      return this;
90    }
91  
92    public HFileContextBuilder withIncludesTags(boolean includesTags) {
93      this.includesTags = includesTags;
94      return this;
95    }
96  
97    public HFileContextBuilder withCompression(Algorithm compression) {
98      this.compression = compression;
99      return this;
100   }
101 
102   public HFileContextBuilder withCompressTags(boolean compressTags) {
103     this.compressTags = compressTags;
104     return this;
105   }
106 
107   public HFileContextBuilder withChecksumType(ChecksumType checkSumType) {
108     this.checksumType = checkSumType;
109     return this;
110   }
111 
112   public HFileContextBuilder withBytesPerCheckSum(int bytesPerChecksum) {
113     this.bytesPerChecksum = bytesPerChecksum;
114     return this;
115   }
116 
117   public HFileContextBuilder withBlockSize(int blockSize) {
118     this.blocksize = blockSize;
119     return this;
120   }
121 
122   public HFileContextBuilder withDataBlockEncoding(DataBlockEncoding encoding) {
123     this.encoding = encoding;
124     return this;
125   }
126 
127   public HFileContextBuilder withEncryptionContext(Encryption.Context cryptoContext) {
128     this.cryptoContext = cryptoContext;
129     return this;
130   }
131 
132   public HFileContextBuilder withCreateTime(long fileCreateTime) {
133     this.fileCreateTime = fileCreateTime;
134     return this;
135   }
136 
137   public HFileContextBuilder withHFileName(String name) {
138     this.hfileName = name;
139     return this;
140   }
141 
142   public HFileContextBuilder withColumnFamily(byte[] columnFamily){
143     this.columnFamily = columnFamily;
144     return this;
145   }
146 
147   public HFileContextBuilder withTableName(byte[] tableName){
148     this.tableName = tableName;
149     return this;
150   }
151 
152   public HFileContext build() {
153     return new HFileContext(usesHBaseChecksum, includesMvcc, includesTags, compression,
154         compressTags, checksumType, bytesPerChecksum, blocksize, encoding, cryptoContext,
155         fileCreateTime, hfileName, columnFamily, tableName);
156   }
157 }