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 java.nio.ByteBuffer;
22  
23  import org.apache.hadoop.classification.InterfaceAudience;
24  import org.apache.hadoop.hbase.codec.prefixtree.PrefixTreeBlockMeta;
25  import org.apache.hadoop.hbase.codec.prefixtree.scanner.CellSearcher;
26  
27  /**
28   * Static wrapper class for the ArraySearcherPool.
29   */
30  @InterfaceAudience.Private
31  public class DecoderFactory {
32  
33    private static final ArraySearcherPool POOL = new ArraySearcherPool();
34  
35    //TODO will need a PrefixTreeSearcher on top of CellSearcher
36    public static PrefixTreeArraySearcher checkOut(final ByteBuffer buffer, 
37        boolean includeMvccVersion) {
38      if (buffer.isDirect()) {
39        throw new IllegalArgumentException("DirectByteBuffers not supported yet");
40        // TODO implement PtByteBufferBlockScanner
41      }
42  
43      PrefixTreeArraySearcher searcher = POOL.checkOut(buffer,
44        includeMvccVersion);
45      return searcher;
46    }
47  
48    public static void checkIn(CellSearcher pSearcher) {
49      if (pSearcher == null) {
50        return;
51      }
52      if (! (pSearcher instanceof PrefixTreeArraySearcher)) {
53        throw new IllegalArgumentException("Cannot return "+pSearcher.getClass()+" to "
54            +DecoderFactory.class);
55      }
56      PrefixTreeArraySearcher searcher = (PrefixTreeArraySearcher) pSearcher;
57      POOL.checkIn(searcher);
58    }
59  
60  
61    /**************************** helper ******************************/
62    public static PrefixTreeArraySearcher ensureArraySearcherValid(ByteBuffer buffer,
63        PrefixTreeArraySearcher searcher, boolean includeMvccVersion) {
64      if (searcher == null) {
65        PrefixTreeBlockMeta blockMeta = new PrefixTreeBlockMeta(buffer);
66        searcher = new PrefixTreeArraySearcher(blockMeta, blockMeta.getRowTreeDepth(),
67            blockMeta.getMaxRowLength(), blockMeta.getMaxQualifierLength(),
68            blockMeta.getMaxTagsLength());
69        searcher.initOnBlock(blockMeta, buffer.array(), includeMvccVersion);
70        return searcher;
71      }
72  
73      PrefixTreeBlockMeta blockMeta = searcher.getBlockMeta();
74      blockMeta.initOnBlock(buffer);
75      if (!searcher.areBuffersBigEnough()) {
76        int maxRowTreeStackNodes = Math.max(blockMeta.getRowTreeDepth(),
77          searcher.getMaxRowTreeStackNodes());
78        int rowBufferLength = Math.max(blockMeta.getMaxRowLength(), searcher.getRowBufferLength());
79        int qualifierBufferLength = Math.max(blockMeta.getMaxQualifierLength(),
80          searcher.getQualifierBufferLength());
81        int tagBufferLength = Math.max(blockMeta.getMaxTagsLength(), searcher.getTagBufferLength());
82        searcher = new PrefixTreeArraySearcher(blockMeta, maxRowTreeStackNodes, rowBufferLength,
83            qualifierBufferLength, tagBufferLength);
84      }
85      //this is where we parse the BlockMeta
86      searcher.initOnBlock(blockMeta, buffer.array(), includeMvccVersion);
87      return searcher;
88    }
89  
90  }