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