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.classification.InterfaceAudience;
22  import org.apache.hadoop.hbase.codec.prefixtree.PrefixTreeBlockMeta;
23  import org.apache.hadoop.hbase.util.vint.UFIntTool;
24  import org.apache.hadoop.hbase.util.vint.UVIntTool;
25  
26  @InterfaceAudience.Private
27  public class ColumnNodeReader {
28  
29    /**************** fields ************************/
30  
31    protected PrefixTreeBlockMeta blockMeta;
32    protected byte[] block;
33  
34    protected byte[] columnBuffer;
35    protected boolean familyVsQualifier;
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, boolean familyVsQualifier) {
47      this.columnBuffer = columnBuffer;
48      this.familyVsQualifier = familyVsQualifier;
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 (familyVsQualifier) {
66        offsetWidth = blockMeta.getFamilyOffsetWidth();
67      } else {
68        offsetWidth = blockMeta.getQualifierOffsetWidth();
69      }
70      parentStartPosition = (int) UFIntTool.fromBytes(block, parentStartPositionIndex, offsetWidth);
71    }
72  
73    public void prependTokenToBuffer(int bufferStartIndex) {
74      System.arraycopy(block, tokenOffsetIntoBlock, columnBuffer, bufferStartIndex, tokenLength);
75    }
76  
77    public boolean isRoot() {
78      if (familyVsQualifier) {
79        return offsetIntoBlock == blockMeta.getAbsoluteFamilyOffset();
80      } else {
81        return offsetIntoBlock == blockMeta.getAbsoluteQualifierOffset();
82      }
83    }
84  
85  
86    /************** standard methods *********************/
87  
88    @Override
89    public String toString() {
90      return super.toString() + "[" + offsetIntoBlock + "]";
91    }
92  
93  
94    /****************** get/set ****************************/
95  
96    public int getTokenLength() {
97      return tokenLength;
98    }
99  
100   public int getParentStartPosition() {
101     return parentStartPosition;
102   }
103 
104 }