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  
26  /**
27   * Position one of these appropriately in the data block and you can call its methods to retrieve
28   * the family or qualifier at the current position.
29   */
30  @InterfaceAudience.Private
31  public class ColumnReader {
32  
33    /****************** fields *************************/
34  
35    protected PrefixTreeBlockMeta blockMeta;
36  
37    protected byte[] columnBuffer;
38    protected int columnOffset;
39    protected int columnLength;
40    protected ColumnNodeType nodeType;  
41  
42    protected ColumnNodeReader columnNodeReader;
43  
44  
45    /******************** construct *******************/
46  
47    public ColumnReader(byte[] columnBuffer, ColumnNodeType nodeType) {
48      this.columnBuffer = columnBuffer;
49      this.nodeType = nodeType;
50      this.columnNodeReader = new ColumnNodeReader(columnBuffer, nodeType);
51    }
52  
53    public void initOnBlock(PrefixTreeBlockMeta blockMeta, ByteBuff block) {
54      this.blockMeta = blockMeta;
55      clearColumnBuffer();
56      columnNodeReader.initOnBlock(blockMeta, block);
57    }
58  
59  
60    /********************* methods *******************/
61  
62    public ColumnReader populateBuffer(int offsetIntoColumnData) {
63      clearColumnBuffer();
64      int nextRelativeOffset = offsetIntoColumnData;
65      while (true) {
66        int absoluteOffset = 0;
67        if (nodeType == ColumnNodeType.FAMILY) {
68          absoluteOffset = blockMeta.getAbsoluteFamilyOffset() + nextRelativeOffset;
69        } else if (nodeType == ColumnNodeType.QUALIFIER) {
70          absoluteOffset = blockMeta.getAbsoluteQualifierOffset() + nextRelativeOffset;
71        } else {
72          absoluteOffset = blockMeta.getAbsoluteTagsOffset() + nextRelativeOffset;
73        }
74        columnNodeReader.positionAt(absoluteOffset);
75        columnOffset -= columnNodeReader.getTokenLength();
76        columnLength += columnNodeReader.getTokenLength();
77        columnNodeReader.prependTokenToBuffer(columnOffset);
78        if (columnNodeReader.isRoot()) {
79          return this;
80        }
81        nextRelativeOffset = columnNodeReader.getParentStartPosition();
82      }
83    }
84  
85    public byte[] copyBufferToNewArray() {// for testing
86      byte[] out = new byte[columnLength];
87      System.arraycopy(columnBuffer, columnOffset, out, 0, out.length);
88      return out;
89    }
90  
91    public int getColumnLength() {
92      return columnLength;
93    }
94  
95    public void clearColumnBuffer() {
96      columnOffset = columnBuffer.length;
97      columnLength = 0;
98    }
99  
100 
101   /****************************** get/set *************************************/
102 
103   public int getColumnOffset() {
104     return columnOffset;
105   }
106 
107 }
108