1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.codec.prefixtree.encode.column;
20
21 import java.io.IOException;
22 import java.io.OutputStream;
23
24 import org.apache.hadoop.classification.InterfaceAudience;
25 import org.apache.hadoop.hbase.codec.prefixtree.PrefixTreeBlockMeta;
26 import org.apache.hadoop.hbase.codec.prefixtree.encode.tokenize.TokenizerNode;
27 import org.apache.hadoop.hbase.util.ByteRange;
28 import org.apache.hadoop.hbase.util.Bytes;
29 import org.apache.hadoop.hbase.util.Strings;
30 import org.apache.hadoop.hbase.util.vint.UFIntTool;
31 import org.apache.hadoop.hbase.util.vint.UVIntTool;
32
33
34
35
36
37
38
39
40
41
42
43 @InterfaceAudience.Private
44 public class ColumnNodeWriter{
45
46
47
48 protected TokenizerNode builderNode;
49 protected PrefixTreeBlockMeta blockMeta;
50
51 protected boolean familyVsQualifier;
52
53 protected int tokenLength;
54 protected byte[] token;
55 protected int parentStartPosition;
56
57
58
59
60 public ColumnNodeWriter(PrefixTreeBlockMeta blockMeta, TokenizerNode builderNode,
61 boolean familyVsQualifier) {
62 this.blockMeta = blockMeta;
63 this.builderNode = builderNode;
64 this.familyVsQualifier = familyVsQualifier;
65 calculateTokenLength();
66 }
67
68
69
70
71 public boolean isRoot() {
72 return parentStartPosition == 0;
73 }
74
75 private void calculateTokenLength() {
76 tokenLength = builderNode.getTokenLength();
77 token = new byte[tokenLength];
78 }
79
80
81
82
83
84
85
86 public int getWidthUsingPlaceholderForOffsetWidth(int offsetWidthPlaceholder) {
87 int width = 0;
88 width += UVIntTool.numBytes(tokenLength);
89 width += token.length;
90 width += offsetWidthPlaceholder;
91 return width;
92 }
93
94 public void writeBytes(OutputStream os) throws IOException {
95 int parentOffsetWidth;
96 if (familyVsQualifier) {
97 parentOffsetWidth = blockMeta.getFamilyOffsetWidth();
98 } else {
99 parentOffsetWidth = blockMeta.getQualifierOffsetWidth();
100 }
101 UVIntTool.writeBytes(tokenLength, os);
102 os.write(token);
103 UFIntTool.writeBytes(parentOffsetWidth, parentStartPosition, os);
104 }
105
106 public void setTokenBytes(ByteRange source) {
107 source.deepCopySubRangeTo(0, tokenLength, token, 0);
108 }
109
110
111
112
113 @Override
114 public String toString() {
115 StringBuilder sb = new StringBuilder();
116 sb.append(Strings.padFront(builderNode.getOutputArrayOffset() + "", ' ', 3) + ",");
117 sb.append("[");
118 sb.append(Bytes.toString(token));
119 sb.append("]->");
120 sb.append(parentStartPosition);
121 return sb.toString();
122 }
123
124
125
126
127 public void setParentStartPosition(int parentStartPosition) {
128 this.parentStartPosition = parentStartPosition;
129 }
130
131 }