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