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;
20  
21  import java.nio.ByteBuffer;
22  
23  import org.apache.hadoop.hbase.Cell;
24  import org.apache.hadoop.hbase.CellUtil;
25  import org.apache.hadoop.hbase.KeyValue;
26  import org.apache.hadoop.hbase.KeyValue.KVComparator;
27  import org.apache.hadoop.hbase.KeyValue.Type;
28  import org.apache.hadoop.hbase.KeyValueUtil;
29  import org.apache.hadoop.hbase.SettableSequenceId;
30  import org.apache.hadoop.hbase.classification.InterfaceAudience;
31  import org.apache.hadoop.hbase.codec.prefixtree.decode.DecoderFactory;
32  import org.apache.hadoop.hbase.codec.prefixtree.decode.PrefixTreeArraySearcher;
33  import org.apache.hadoop.hbase.codec.prefixtree.scanner.CellScannerPosition;
34  import org.apache.hadoop.hbase.io.HeapSize;
35  import org.apache.hadoop.hbase.io.encoding.DataBlockEncoder.EncodedSeeker;
36  import org.apache.hadoop.hbase.util.Bytes;
37  import org.apache.hadoop.hbase.util.ClassSize;
38  
39  /**
40   * These methods have the same definition as any implementation of the EncodedSeeker.
41   *
42   * In the future, the EncodedSeeker could be modified to work with the Cell interface directly.  It
43   * currently returns a new KeyValue object each time getKeyValue is called.  This is not horrible,
44   * but in order to create a new KeyValue object, we must first allocate a new byte[] and copy in
45   * the data from the PrefixTreeCell.  It is somewhat heavyweight right now.
46   */
47  @InterfaceAudience.Private
48  public class PrefixTreeSeeker implements EncodedSeeker {
49  
50    protected ByteBuffer block;
51    protected boolean includeMvccVersion;
52    protected PrefixTreeArraySearcher ptSearcher;
53  
54    public PrefixTreeSeeker(boolean includeMvccVersion) {
55      this.includeMvccVersion = includeMvccVersion;
56    }
57  
58    @Override
59    public void setCurrentBuffer(ByteBuffer fullBlockBuffer) {
60      block = fullBlockBuffer;
61      ptSearcher = DecoderFactory.checkOut(block, includeMvccVersion);
62      rewind();
63    }
64  
65    /**
66     * Currently unused.
67     * <p/>
68     * TODO performance leak. should reuse the searchers. hbase does not currently have a hook where
69     * this can be called
70     */
71    public void releaseCurrentSearcher(){
72      DecoderFactory.checkIn(ptSearcher);
73    }
74  
75  
76    @Override
77    public ByteBuffer getKeyDeepCopy() {
78      return KeyValueUtil.copyKeyToNewByteBuffer(ptSearcher.current());
79    }
80  
81  
82    @Override
83    public ByteBuffer getValueShallowCopy() {
84      return CellUtil.getValueBufferShallowCopy(ptSearcher.current());
85    }
86  
87    /**
88     * currently must do deep copy into new array
89     */
90    @Override
91    public ByteBuffer getKeyValueBuffer() {
92      return KeyValueUtil.copyToNewByteBuffer(ptSearcher.current());
93    }
94  
95    /**
96     * currently must do deep copy into new array
97     */
98    @Override
99    public Cell getKeyValue() {
100     Cell cell = ptSearcher.current();
101     if (cell == null) {
102       return null;
103     }
104     return new ClonedPrefixTreeCell(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength(),
105         cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength(),
106         cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength(),
107         cell.getValueArray(), cell.getValueOffset(), cell.getValueLength(), cell.getTagsArray(),
108         cell.getTagsOffset(), cell.getTagsLength(), cell.getTimestamp(), cell.getTypeByte(),
109         cell.getSequenceId());
110   }
111 
112   /**
113    * Currently unused.
114    * <p/>
115    * A nice, lightweight reference, though the underlying cell is transient. This method may return
116    * the same reference to the backing PrefixTreeCell repeatedly, while other implementations may
117    * return a different reference for each Cell.
118    * <p/>
119    * The goal will be to transition the upper layers of HBase, like Filters and KeyValueHeap, to
120    * use this method instead of the getKeyValue() methods above.
121    */
122   public Cell get() {
123     return ptSearcher.current();
124   }
125 
126   @Override
127   public void rewind() {
128     ptSearcher.positionAtFirstCell();
129   }
130 
131   @Override
132   public boolean next() {
133     return ptSearcher.advance();
134   }
135 
136   public boolean advance() {
137     return ptSearcher.advance();
138   }
139 
140 
141   private static final boolean USE_POSITION_BEFORE = false;
142 
143   /*
144    * Support both of these options since the underlying PrefixTree supports both.  Possibly
145    * expand the EncodedSeeker to utilize them both.
146    */
147 
148   protected int seekToOrBeforeUsingPositionAtOrBefore(byte[] keyOnlyBytes, int offset, int length,
149       boolean seekBefore){
150     // this does a deep copy of the key byte[] because the CellSearcher interface wants a Cell
151     KeyValue kv = new KeyValue.KeyOnlyKeyValue(keyOnlyBytes, offset, length);
152 
153     return seekToOrBeforeUsingPositionAtOrBefore(kv, seekBefore);
154   }
155 
156   /*
157    * Support both of these options since the underlying PrefixTree supports
158    * both. Possibly expand the EncodedSeeker to utilize them both.
159    */
160 
161   protected int seekToOrBeforeUsingPositionAtOrBefore(Cell kv, boolean seekBefore) {
162     // this does a deep copy of the key byte[] because the CellSearcher
163     // interface wants a Cell
164     CellScannerPosition position = ptSearcher.seekForwardToOrBefore(kv);
165 
166     if (CellScannerPosition.AT == position) {
167       if (seekBefore) {
168         ptSearcher.previous();
169         return 1;
170       }
171       return 0;
172     }
173 
174     return 1;
175   }
176 
177   protected int seekToOrBeforeUsingPositionAtOrAfter(byte[] keyOnlyBytes, int offset, int length,
178       boolean seekBefore) {
179     // this does a deep copy of the key byte[] because the CellSearcher
180     // interface wants a Cell
181     KeyValue kv = new KeyValue.KeyOnlyKeyValue(keyOnlyBytes, offset, length);
182     return seekToOrBeforeUsingPositionAtOrAfter(kv, seekBefore);
183   }
184 
185   protected int seekToOrBeforeUsingPositionAtOrAfter(Cell kv, boolean seekBefore) {
186     // should probably switch this to use the seekForwardToOrBefore method
187     CellScannerPosition position = ptSearcher.seekForwardToOrAfter(kv);
188 
189     if (CellScannerPosition.AT == position) {
190       if (seekBefore) {
191         ptSearcher.previous();
192         return 1;
193       }
194       return 0;
195 
196     }
197 
198     if (CellScannerPosition.AFTER == position) {
199       if (!ptSearcher.isBeforeFirst()) {
200         ptSearcher.previous();
201       }
202       return 1;
203     }
204 
205     if (position == CellScannerPosition.AFTER_LAST) {
206       if (seekBefore) {
207         ptSearcher.previous();
208       }
209       return 1;
210     }
211 
212     throw new RuntimeException("unexpected CellScannerPosition:" + position);
213   }
214 
215   @Override
216   public int compareKey(KVComparator comparator, byte[] key, int offset, int length) {
217     // can't optimize this, make a copy of the key
218     ByteBuffer bb = getKeyDeepCopy();
219     return comparator.compareFlatKey(key, offset, length, bb.array(), bb.arrayOffset(), bb.limit());
220   }
221 
222   @Override
223   public int seekToKeyInBlock(Cell key, boolean forceBeforeOnExactMatch) {
224     if (USE_POSITION_BEFORE) {
225       return seekToOrBeforeUsingPositionAtOrBefore(key, forceBeforeOnExactMatch);
226     } else {
227       return seekToOrBeforeUsingPositionAtOrAfter(key, forceBeforeOnExactMatch);
228     }
229   }
230 
231   @Override
232   public int compareKey(KVComparator comparator, Cell key) {
233     ByteBuffer bb = getKeyDeepCopy();
234     return comparator.compare(key,
235         new KeyValue.KeyOnlyKeyValue(bb.array(), bb.arrayOffset(), bb.limit()));
236   }
237   /**
238    * Cloned version of the PrefixTreeCell where except the value part, the rest
239    * of the key part is deep copied
240    *
241    */
242   private static class ClonedPrefixTreeCell implements Cell, SettableSequenceId, HeapSize {
243     private static final long FIXED_OVERHEAD = ClassSize.align(ClassSize.OBJECT
244         + (5 * ClassSize.REFERENCE) + (2 * Bytes.SIZEOF_LONG) + (4 * Bytes.SIZEOF_INT)
245         + (Bytes.SIZEOF_SHORT) + (2 * Bytes.SIZEOF_BYTE) + (5 * ClassSize.ARRAY));
246     private byte[] row;
247     private short rowLength;
248     private byte[] fam;
249     private byte famLength;
250     private byte[] qual;
251     private int qualLength;
252     private byte[] val;
253     private int valOffset;
254     private int valLength;
255     private byte[] tag;
256     private int tagsLength;
257     private long ts;
258     private long seqId;
259     private byte type;
260 
261     public ClonedPrefixTreeCell(byte[] row, int rowOffset, short rowLength, byte[] fam,
262         int famOffset, byte famLength, byte[] qual, int qualOffset, int qualLength, byte[] val,
263         int valOffset, int valLength, byte[] tag, int tagOffset, int tagLength, long ts, byte type,
264         long seqId) {
265       this.row = new byte[rowLength];
266       System.arraycopy(row, rowOffset, this.row, 0, rowLength);
267       this.rowLength = rowLength;
268       this.fam = new byte[famLength];
269       System.arraycopy(fam, famOffset, this.fam, 0, famLength);
270       this.famLength = famLength;
271       this.qual = new byte[qualLength];
272       System.arraycopy(qual, qualOffset, this.qual, 0, qualLength);
273       this.qualLength = qualLength;
274       this.tag = new byte[tagLength];
275       System.arraycopy(tag, tagOffset, this.tag, 0, tagLength);
276       this.tagsLength = tagLength;
277       this.val = val;
278       this.valLength = valLength;
279       this.valOffset = valOffset;
280       this.ts = ts;
281       this.seqId = seqId;
282       this.type = type;
283     }
284 
285     @Override
286     public void setSequenceId(long seqId) {
287       this.seqId = seqId;
288     }
289 
290     @Override
291     public byte[] getRowArray() {
292       return this.row;
293     }
294 
295     @Override
296     public int getRowOffset() {
297       return 0;
298     }
299 
300     @Override
301     public short getRowLength() {
302       return this.rowLength;
303     }
304 
305     @Override
306     public byte[] getFamilyArray() {
307       return this.fam;
308     }
309 
310     @Override
311     public int getFamilyOffset() {
312       return 0;
313     }
314 
315     @Override
316     public byte getFamilyLength() {
317       return this.famLength;
318     }
319 
320     @Override
321     public byte[] getQualifierArray() {
322       return this.qual;
323     }
324 
325     @Override
326     public int getQualifierOffset() {
327       return 0;
328     }
329 
330     @Override
331     public int getQualifierLength() {
332       return this.qualLength;
333     }
334 
335     @Override
336     public long getTimestamp() {
337       return ts;
338     }
339 
340     @Override
341     public byte getTypeByte() {
342       return type;
343     }
344 
345     @Override
346     @Deprecated
347     public long getMvccVersion() {
348       return getSequenceId();
349     }
350 
351     @Override
352     public long getSequenceId() {
353       return seqId;
354     }
355 
356     @Override
357     public byte[] getValueArray() {
358       return val;
359     }
360 
361     @Override
362     public int getValueOffset() {
363       return this.valOffset;
364     }
365 
366     @Override
367     public int getValueLength() {
368       return this.valLength;
369     }
370 
371     @Override
372     public byte[] getTagsArray() {
373       return this.tag;
374     }
375 
376     @Override
377     public int getTagsOffset() {
378       return 0;
379     }
380 
381     @Override
382     public int getTagsLength() {
383       return this.tagsLength;
384     }
385 
386     @Override
387     @Deprecated
388     public byte[] getValue() {
389       return this.val;
390     }
391 
392     @Override
393     @Deprecated
394     public byte[] getFamily() {
395       return this.fam;
396     }
397 
398     @Override
399     @Deprecated
400     public byte[] getQualifier() {
401       return this.qual;
402     }
403 
404     @Override
405     @Deprecated
406     public byte[] getRow() {
407       return this.row;
408     }
409 
410     @Override
411     public String toString() {
412       String row = Bytes.toStringBinary(getRowArray(), getRowOffset(), getRowLength());
413       String family = Bytes.toStringBinary(getFamilyArray(), getFamilyOffset(), getFamilyLength());
414       String qualifier = Bytes.toStringBinary(getQualifierArray(), getQualifierOffset(),
415           getQualifierLength());
416       String timestamp = String.valueOf((getTimestamp()));
417       return row + "/" + family + (family != null && family.length() > 0 ? ":" : "") + qualifier
418           + "/" + timestamp + "/" + Type.codeToType(type);
419     }
420 
421     @Override
422     public long heapSize() {
423       return FIXED_OVERHEAD + rowLength + famLength + qualLength + valLength + tagsLength;
424     }
425   }
426 }