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