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;
20  
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import org.apache.hadoop.hbase.client.Result;
27  import org.apache.hadoop.hbase.util.Bytes;
28  
29  /**
30   * Mocking utility for common hbase:meta functionality
31   */
32  public class MetaMockingUtil {
33  
34    /**
35     * Returns a Result object constructed from the given region information simulating
36     * a catalog table result.
37     * @param region the HRegionInfo object or null
38     * @return A mocked up Result that fakes a Get on a row in the <code>hbase:meta</code> table.
39     * @throws IOException
40     */
41    public static Result getMetaTableRowResult(final HRegionInfo region)
42        throws IOException {
43      return getMetaTableRowResult(region, null, null, null);
44    }
45  
46    /**
47     * Returns a Result object constructed from the given region information simulating
48     * a catalog table result.
49     * @param region the HRegionInfo object or null
50     * @param sn to use making startcode and server hostname:port in meta or null
51     * @return A mocked up Result that fakes a Get on a row in the <code>hbase:meta</code> table.
52     * @throws IOException
53     */
54    public static Result getMetaTableRowResult(final HRegionInfo region, final ServerName sn)
55        throws IOException {
56      return getMetaTableRowResult(region, sn, null, null);
57    }
58  
59    /**
60     * Returns a Result object constructed from the given region information simulating
61     * a catalog table result.
62     * @param region the HRegionInfo object or null
63     * @param sn to use making startcode and server hostname:port in meta or null
64     * @param splita daughter region or null
65     * @param splitb  daughter region or null
66     * @return A mocked up Result that fakes a Get on a row in the <code>hbase:meta</code> table.
67     * @throws IOException
68     */
69    public static Result getMetaTableRowResult(HRegionInfo region, final ServerName sn,
70        HRegionInfo splita, HRegionInfo splitb) throws IOException {
71      List<Cell> kvs = new ArrayList<Cell>();
72      if (region != null) {
73        kvs.add(new KeyValue(
74          region.getRegionName(),
75          HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER,
76          region.toByteArray()));
77      }
78  
79      if (sn != null) {
80        kvs.add(new KeyValue(region.getRegionName(),
81          HConstants.CATALOG_FAMILY, HConstants.SERVER_QUALIFIER,
82          Bytes.toBytes(sn.getHostAndPort())));
83        kvs.add(new KeyValue(region.getRegionName(),
84          HConstants.CATALOG_FAMILY, HConstants.STARTCODE_QUALIFIER,
85          Bytes.toBytes(sn.getStartcode())));
86      }
87  
88      if (splita != null) {
89        kvs.add(new KeyValue(
90            region.getRegionName(),
91            HConstants.CATALOG_FAMILY, HConstants.SPLITA_QUALIFIER,
92            splita.toByteArray()));
93      }
94  
95      if (splitb != null) {
96        kvs.add(new KeyValue(
97            region.getRegionName(),
98            HConstants.CATALOG_FAMILY, HConstants.SPLITB_QUALIFIER,
99            splitb.toByteArray()));
100     }
101 
102     //important: sort the kvs so that binary search work
103     Collections.sort(kvs, KeyValue.META_COMPARATOR);
104 
105     return Result.create(kvs);
106   }
107 
108   /**
109    * @param sn  ServerName to use making startcode and server in meta
110    * @param hri Region to serialize into HRegionInfo
111    * @return A mocked up Result that fakes a Get on a row in the <code>hbase:meta</code> table.
112    * @throws IOException
113    */
114   public static Result getMetaTableRowResultAsSplitRegion(final HRegionInfo hri,
115       final ServerName sn) throws IOException {
116     hri.setOffline(true);
117     hri.setSplit(true);
118     return getMetaTableRowResult(hri, sn);
119   }
120 
121 }