1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.io.hfile;
19
20 import java.io.IOException;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.hadoop.hbase.classification.InterfaceAudience;
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.fs.FSDataOutputStream;
27 import org.apache.hadoop.fs.FileSystem;
28 import org.apache.hadoop.fs.Path;
29 import org.apache.hadoop.hbase.Cell;
30 import org.apache.hadoop.hbase.HConstants;
31 import org.apache.hadoop.hbase.KeyValue.KVComparator;
32 import org.apache.hadoop.hbase.io.crypto.Encryption;
33 import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
34 import org.apache.hadoop.hbase.io.hfile.HFile.FileInfo;
35 import org.apache.hadoop.hbase.io.hfile.HFile.Writer;
36 import org.apache.hadoop.hbase.security.EncryptionUtil;
37 import org.apache.hadoop.hbase.security.User;
38 import org.apache.hadoop.hbase.util.Bytes;
39
40
41
42
43 @InterfaceAudience.Private
44 public class HFileWriterV3 extends HFileWriterV2 {
45
46 private static final Log LOG = LogFactory.getLog(HFileWriterV3.class);
47
48 private int maxTagsLength = 0;
49
50 static class WriterFactoryV3 extends HFile.WriterFactory {
51 WriterFactoryV3(Configuration conf, CacheConfig cacheConf) {
52 super(conf, cacheConf);
53 }
54
55 @Override
56 public Writer createWriter(FileSystem fs, Path path, FSDataOutputStream ostream,
57 final KVComparator comparator, HFileContext fileContext)
58 throws IOException {
59 return new HFileWriterV3(conf, cacheConf, fs, path, ostream, comparator, fileContext);
60 }
61 }
62
63
64 public HFileWriterV3(Configuration conf, CacheConfig cacheConf, FileSystem fs, Path path,
65 FSDataOutputStream ostream, final KVComparator comparator,
66 final HFileContext fileContext) throws IOException {
67 super(conf, cacheConf, fs, path, ostream, comparator, fileContext);
68 if (LOG.isTraceEnabled()) {
69 LOG.trace("Writer" + (path != null ? " for " + path : "") +
70 " initialized with cacheConf: " + cacheConf +
71 " comparator: " + comparator.getClass().getSimpleName() +
72 " fileContext: " + fileContext);
73 }
74 }
75
76
77
78
79
80
81
82
83
84 @Override
85 public void append(final Cell cell) throws IOException {
86
87 super.append(cell);
88 int tagsLength = cell.getTagsLength();
89 if (tagsLength > this.maxTagsLength) {
90 this.maxTagsLength = tagsLength;
91 }
92 }
93
94 @Override
95 protected void finishFileInfo() throws IOException {
96 super.finishFileInfo();
97 if (hFileContext.getDataBlockEncoding() == DataBlockEncoding.PREFIX_TREE) {
98
99
100 fileInfo.append(FileInfo.MAX_TAGS_LEN, Bytes.toBytes(this.maxTagsLength), false);
101 } else if (hFileContext.isIncludesTags()) {
102
103
104 fileInfo.append(FileInfo.MAX_TAGS_LEN, Bytes.toBytes(this.maxTagsLength), false);
105 boolean tagsCompressed = (hFileContext.getDataBlockEncoding() != DataBlockEncoding.NONE)
106 && hFileContext.isCompressTags();
107 fileInfo.append(FileInfo.TAGS_COMPRESSED, Bytes.toBytes(tagsCompressed), false);
108 }
109 }
110
111 @Override
112 protected int getMajorVersion() {
113 return 3;
114 }
115
116 @Override
117 protected int getMinorVersion() {
118 return HFileReaderV3.MAX_MINOR_VERSION;
119 }
120
121 @Override
122 protected void finishClose(FixedFileTrailer trailer) throws IOException {
123
124 Encryption.Context cryptoContext = hFileContext.getEncryptionContext();
125 if (cryptoContext != Encryption.Context.NONE) {
126
127
128 trailer.setEncryptionKey(EncryptionUtil.wrapKey(cryptoContext.getConf(),
129 cryptoContext.getConf().get(HConstants.CRYPTO_MASTERKEY_NAME_CONF_KEY,
130 User.getCurrent().getShortName()),
131 cryptoContext.getKey()));
132 }
133
134 super.finishClose(trailer);
135 }
136
137 }