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