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.hbase.cell;
20  
21  import java.nio.ByteBuffer;
22  
23  import org.apache.hadoop.classification.InterfaceAudience;
24  import org.apache.hadoop.classification.InterfaceStability;
25  import org.apache.hadoop.hbase.util.ByteRange;
26  import org.apache.hbase.Cell;
27  
28  @InterfaceAudience.Private
29  @InterfaceStability.Evolving
30  public final class CellTool {
31  
32    /******************* ByteRange *******************************/
33  
34    public static ByteRange fillRowRange(Cell cell, ByteRange range) {
35      return range.set(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
36    }
37  
38    public static ByteRange fillFamilyRange(Cell cell, ByteRange range) {
39      return range.set(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
40    }
41  
42    public static ByteRange fillQualifierRange(Cell cell, ByteRange range) {
43      return range.set(cell.getQualifierArray(), cell.getQualifierOffset(),
44        cell.getQualifierLength());
45    }
46  
47  
48    /***************** get individual arrays for tests ************/
49  
50    public static byte[] getRowArray(Cell cell){
51      byte[] output = new byte[cell.getRowLength()];
52      copyRowTo(cell, output, 0);
53      return output;
54    }
55  
56    public static byte[] getFamilyArray(Cell cell){
57      byte[] output = new byte[cell.getFamilyLength()];
58      copyFamilyTo(cell, output, 0);
59      return output;
60    }
61  
62    public static byte[] getQualifierArray(Cell cell){
63      byte[] output = new byte[cell.getQualifierLength()];
64      copyQualifierTo(cell, output, 0);
65      return output;
66    }
67  
68    public static byte[] getValueArray(Cell cell){
69      byte[] output = new byte[cell.getValueLength()];
70      copyValueTo(cell, output, 0);
71      return output;
72    }
73  
74  
75    /******************** copyTo **********************************/
76  
77    public static int copyRowTo(Cell cell, byte[] destination, int destinationOffset) {
78      System.arraycopy(cell.getRowArray(), cell.getRowOffset(), destination, destinationOffset,
79        cell.getRowLength());
80      return destinationOffset + cell.getRowLength();
81    }
82  
83    public static int copyFamilyTo(Cell cell, byte[] destination, int destinationOffset) {
84      System.arraycopy(cell.getFamilyArray(), cell.getFamilyOffset(), destination, destinationOffset,
85        cell.getFamilyLength());
86      return destinationOffset + cell.getFamilyLength();
87    }
88  
89    public static int copyQualifierTo(Cell cell, byte[] destination, int destinationOffset) {
90      System.arraycopy(cell.getQualifierArray(), cell.getQualifierOffset(), destination,
91        destinationOffset, cell.getQualifierLength());
92      return destinationOffset + cell.getQualifierLength();
93    }
94  
95    public static int copyValueTo(Cell cell, byte[] destination, int destinationOffset) {
96      System.arraycopy(cell.getValueArray(), cell.getValueOffset(), destination, destinationOffset,
97        cell.getValueLength());
98      return destinationOffset + cell.getValueLength();
99    }
100 
101 
102   /********************* misc *************************************/
103 
104   public static byte getRowByte(Cell cell, int index) {
105     return cell.getRowArray()[cell.getRowOffset() + index];
106   }
107 
108 
109   /********************** KeyValue (move to KeyValueUtils) *********************/
110 
111   public static ByteBuffer getValueBufferShallowCopy(Cell cell) {
112     ByteBuffer buffer = ByteBuffer.wrap(cell.getValueArray(), cell.getValueOffset(),
113       cell.getValueLength());
114 //    buffer.position(buffer.limit());//make it look as if value was appended
115     return buffer;
116   }
117 
118 }