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