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  
19  package org.apache.hadoop.hbase.codec.prefixtree.decode.column;
20  
21  import org.apache.hadoop.hbase.classification.InterfaceAudience;
22  import org.apache.hadoop.hbase.codec.prefixtree.PrefixTreeBlockMeta;
23  import org.apache.hadoop.hbase.codec.prefixtree.encode.other.ColumnNodeType;
24  import org.apache.hadoop.hbase.nio.ByteBuff;
25  import org.apache.hadoop.hbase.util.vint.UFIntTool;
26  import org.apache.hadoop.hbase.util.vint.UVIntTool;
27  
28  @InterfaceAudience.Private
29  public class ColumnNodeReader {
30  
31    /**************** fields ************************/
32  
33    protected PrefixTreeBlockMeta blockMeta;
34    protected ByteBuff block;
35    protected ColumnNodeType nodeType;
36    protected byte[] columnBuffer;
37  
38    protected int offsetIntoBlock;
39  
40    protected int tokenOffsetIntoBlock;
41    protected int tokenLength;
42    protected int parentStartPosition;
43  
44  
45    /************** construct *************************/
46  
47    public ColumnNodeReader(byte[] columnBuffer, ColumnNodeType nodeType) {
48      this.columnBuffer = columnBuffer;
49      this.nodeType = nodeType;
50    }
51  
52    public void initOnBlock(PrefixTreeBlockMeta blockMeta, ByteBuff block) {
53      this.blockMeta = blockMeta;
54      this.block = block;
55    }
56  
57  
58    /************* methods *****************************/
59  
60    public void positionAt(int offsetIntoBlock) {
61      this.offsetIntoBlock = offsetIntoBlock;
62      tokenLength = UVIntTool.getInt(block, offsetIntoBlock);
63      tokenOffsetIntoBlock = offsetIntoBlock + UVIntTool.numBytes(tokenLength);
64      int parentStartPositionIndex = tokenOffsetIntoBlock + tokenLength;
65      int offsetWidth;
66      if(nodeType == ColumnNodeType.FAMILY) {
67        offsetWidth = blockMeta.getFamilyOffsetWidth();
68      } else if(nodeType == ColumnNodeType.QUALIFIER) {
69        offsetWidth = blockMeta.getQualifierOffsetWidth();
70      } else {
71        offsetWidth = blockMeta.getTagsOffsetWidth();
72      }
73      parentStartPosition = (int) UFIntTool.fromBytes(block, parentStartPositionIndex, offsetWidth);
74    }
75  
76    public void prependTokenToBuffer(int bufferStartIndex) {
77      block.get(tokenOffsetIntoBlock, columnBuffer, bufferStartIndex, tokenLength);
78    }
79  
80    public boolean isRoot() {
81      if (nodeType == ColumnNodeType.FAMILY) {
82        return offsetIntoBlock == blockMeta.getAbsoluteFamilyOffset();
83      } else if (nodeType == ColumnNodeType.QUALIFIER) {
84        return offsetIntoBlock == blockMeta.getAbsoluteQualifierOffset();
85      } else {
86        return offsetIntoBlock == blockMeta.getAbsoluteTagsOffset();
87      }
88    }
89  
90  
91    /************** standard methods *********************/
92  
93    @Override
94    public String toString() {
95      return super.toString() + "[" + offsetIntoBlock + "]";
96    }
97  
98  
99    /****************** get/set ****************************/
100 
101   public int getTokenLength() {
102     return tokenLength;
103   }
104 
105   public int getParentStartPosition() {
106     return parentStartPosition;
107   }
108 
109 }