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;
20  
21  import org.apache.hadoop.classification.InterfaceAudience;
22  import org.apache.hadoop.hbase.Cell;
23  import org.apache.hadoop.hbase.CellComparator;
24  import org.apache.hadoop.hbase.CellUtil;
25  import org.apache.hadoop.hbase.KeyValue;
26  import org.apache.hadoop.hbase.KeyValueUtil;
27  
28  /**
29   * As the PrefixTreeArrayScanner moves through the tree bytes, it changes the values in the fields
30   * of this class so that Cell logic can be applied, but without allocating new memory for every Cell
31   * iterated through.
32   */
33  @InterfaceAudience.Private
34  public class PrefixTreeCell implements Cell, Comparable<Cell> {
35  
36    /********************** static **********************/
37  
38    public static final KeyValue.Type[] TYPES = new KeyValue.Type[256];
39    static {
40      for (KeyValue.Type type : KeyValue.Type.values()) {
41        TYPES[type.getCode() & 0xff] = type;
42      }
43    }
44  
45    //Same as KeyValue constructor.  Only used to avoid NPE's when full cell hasn't been initialized.
46    public static final KeyValue.Type DEFAULT_TYPE = KeyValue.Type.Put;
47  
48    /******************** fields ************************/
49  
50    protected byte[] block;
51    //we could also avoid setting the mvccVersion in the scanner/searcher, but this is simpler
52    protected boolean includeMvccVersion;
53  
54    protected byte[] rowBuffer;
55    protected int rowLength;
56  
57    protected byte[] familyBuffer;
58    protected int familyOffset;
59    protected int familyLength;
60  
61    protected byte[] qualifierBuffer;// aligned to the end of the array
62    protected int qualifierOffset;
63    protected int qualifierLength;
64  
65    protected Long timestamp;
66    protected Long mvccVersion;
67  
68    protected KeyValue.Type type;
69  
70    protected int absoluteValueOffset;
71    protected int valueLength;
72  
73    protected byte[] tagsBuffer;
74    protected int tagsOffset;
75    protected short tagsLength;
76  
77    /********************** Cell methods ******************/
78  
79    /**
80     * For debugging.  Currently creates new KeyValue to utilize its toString() method.
81     */
82    @Override
83    public String toString() {
84      return getKeyValueString();
85    }
86  
87    @Override
88    public boolean equals(Object obj) {
89      if (!(obj instanceof Cell)) {
90        return false;
91      }
92      //Temporary hack to maintain backwards compatibility with KeyValue.equals
93      return CellComparator.equalsIgnoreMvccVersion(this, (Cell)obj);
94  
95      //TODO return CellComparator.equals(this, (Cell)obj);//see HBASE-6907
96    }
97  
98    @Override
99    public int hashCode(){
100     //Temporary hack to maintain backwards compatibility with KeyValue.hashCode
101     //I don't think this is used in any hot code paths
102     return KeyValueUtil.copyToNewKeyValue(this).hashCode();
103 
104     //TODO return CellComparator.hashCode(this);//see HBASE-6907
105   }
106 
107   @Override
108   public int compareTo(Cell other) {
109     return CellComparator.compareStatic(this, other, false);
110   }
111 
112   @Override
113   public long getTimestamp() {
114     return timestamp;
115   }
116 
117   @Override
118   public long getMvccVersion() {
119     if (!includeMvccVersion) {
120       return 0L;
121     }
122     return mvccVersion;
123   }
124 
125   @Override
126   public int getValueLength() {
127     return valueLength;
128   }
129 
130   @Override
131   public byte[] getRowArray() {
132     return rowBuffer;
133   }
134 
135   @Override
136   public int getRowOffset() {
137     return 0;
138   }
139 
140   @Override
141   public short getRowLength() {
142     return (short) rowLength;
143   }
144 
145   @Override
146   public byte[] getFamilyArray() {
147     return familyBuffer;
148   }
149 
150   @Override
151   public int getFamilyOffset() {
152     return familyOffset;
153   }
154 
155   @Override
156   public byte getFamilyLength() {
157     return (byte) familyLength;
158   }
159 
160   @Override
161   public byte[] getQualifierArray() {
162     return qualifierBuffer;
163   }
164 
165   @Override
166   public int getQualifierOffset() {
167     return qualifierOffset;
168   }
169 
170   @Override
171   public int getQualifierLength() {
172     return qualifierLength;
173   }
174 
175   @Override
176   public byte[] getValueArray() {
177     return block;
178   }
179 
180   @Override
181   public int getValueOffset() {
182     return absoluteValueOffset;
183   }
184 
185   @Override
186   public byte getTypeByte() {
187     return type.getCode();
188   }
189 
190   /* Deprecated methods pushed into the Cell interface */
191   @Override
192   public byte[] getValue() {
193     return CellUtil.cloneValue(this);
194   }
195 
196   @Override
197   public byte[] getFamily() {
198     return CellUtil.cloneFamily(this);
199   }
200 
201   @Override
202   public byte[] getQualifier() {
203     return CellUtil.cloneQualifier(this);
204   }
205 
206   @Override
207   public byte[] getRow() {
208     return CellUtil.cloneRow(this);
209   }
210 
211   /************************* helper methods *************************/
212 
213   /**
214    * Need this separate method so we can call it from subclasses' toString() methods
215    */
216   protected String getKeyValueString(){
217     KeyValue kv = KeyValueUtil.copyToNewKeyValue(this);
218     return kv.toString();
219   }
220 
221   @Override
222   public int getTagsOffset() {
223     return tagsOffset;
224   }
225 
226   @Override
227   public short getTagsLength() {
228     return tagsLength;
229   }
230 
231   @Override
232   public byte[] getTagsArray() {
233     return this.tagsBuffer;
234   }
235 
236 }