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  
74    /********************** Cell methods ******************/
75  
76    /**
77     * For debugging.  Currently creates new KeyValue to utilize its toString() method.
78     */
79    @Override
80    public String toString() {
81      return getKeyValueString();
82    }
83  
84    @Override
85    public boolean equals(Object obj) {
86      if (!(obj instanceof Cell)) {
87        return false;
88      }
89      //Temporary hack to maintain backwards compatibility with KeyValue.equals
90      return CellComparator.equalsIgnoreMvccVersion(this, (Cell)obj);
91  
92      //TODO return CellComparator.equals(this, (Cell)obj);//see HBASE-6907
93    }
94  
95    @Override
96    public int hashCode(){
97      //Temporary hack to maintain backwards compatibility with KeyValue.hashCode
98      //I don't think this is used in any hot code paths
99      return KeyValueUtil.copyToNewKeyValue(this).hashCode();
100 
101     //TODO return CellComparator.hashCode(this);//see HBASE-6907
102   }
103 
104   @Override
105   public int compareTo(Cell other) {
106     return CellComparator.compareStatic(this, other);
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 int getValueLength() {
124     return valueLength;
125   }
126 
127   @Override
128   public byte[] getRowArray() {
129     return rowBuffer;
130   }
131 
132   @Override
133   public int getRowOffset() {
134     return 0;
135   }
136 
137   @Override
138   public short getRowLength() {
139     return (short) rowLength;
140   }
141 
142   @Override
143   public byte[] getFamilyArray() {
144     return familyBuffer;
145   }
146 
147   @Override
148   public int getFamilyOffset() {
149     return familyOffset;
150   }
151 
152   @Override
153   public byte getFamilyLength() {
154     return (byte) familyLength;
155   }
156 
157   @Override
158   public byte[] getQualifierArray() {
159     return qualifierBuffer;
160   }
161 
162   @Override
163   public int getQualifierOffset() {
164     return qualifierOffset;
165   }
166 
167   @Override
168   public int getQualifierLength() {
169     return qualifierLength;
170   }
171 
172   @Override
173   public byte[] getValueArray() {
174     return block;
175   }
176 
177   @Override
178   public int getValueOffset() {
179     return absoluteValueOffset;
180   }
181 
182   @Override
183   public byte getTypeByte() {
184     return type.getCode();
185   }
186 
187   /* Deprecated methods pushed into the Cell interface */
188   @Override
189   public byte[] getValue() {
190     return CellUtil.cloneValue(this);
191   }
192 
193   @Override
194   public byte[] getFamily() {
195     return CellUtil.cloneFamily(this);
196   }
197 
198   @Override
199   public byte[] getQualifier() {
200     return CellUtil.cloneQualifier(this);
201   }
202 
203   @Override
204   public byte[] getRow() {
205     return CellUtil.cloneRow(this);
206   }
207 
208   /************************* helper methods *************************/
209 
210   /**
211    * Need this separate method so we can call it from subclasses' toString() methods
212    */
213   protected String getKeyValueString(){
214     KeyValue kv = KeyValueUtil.copyToNewKeyValue(this);
215     return kv.toString();
216   }
217 
218   @Override
219   public int getTagsOffset() {
220     throw new UnsupportedOperationException("Not implemented");
221   }
222 
223   @Override
224   public short getTagsLength() {
225     throw new UnsupportedOperationException("Not implemented");
226   }
227 
228   @Override
229   public byte[] getTagsArray() {
230     throw new UnsupportedOperationException("Not implemented");
231   }
232 
233 }