HBASE-24033 Add ut for loading the corrupt recovered hfiles (#1322)
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / MetaMockingUtil.java
blob723b9daad091e855078b17b3777686d389d7bf3d
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 package org.apache.hadoop.hbase;
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
26 import org.apache.hadoop.hbase.client.RegionInfo;
27 import org.apache.hadoop.hbase.client.Result;
28 import org.apache.hadoop.hbase.util.Bytes;
30 /**
31 * Mocking utility for common hbase:meta functionality
33 public class MetaMockingUtil {
35 /**
36 * Returns a Result object constructed from the given region information simulating
37 * a catalog table result.
38 * @param region the HRegionInfo object or null
39 * @return A mocked up Result that fakes a Get on a row in the <code>hbase:meta</code> table.
40 * @throws IOException
42 public static Result getMetaTableRowResult(final HRegionInfo region)
43 throws IOException {
44 return getMetaTableRowResult(region, null, null, null);
47 /**
48 * Returns a Result object constructed from the given region information simulating
49 * a catalog table result.
50 * @param region the HRegionInfo object or null
51 * @param sn to use making startcode and server hostname:port in meta or null
52 * @return A mocked up Result that fakes a Get on a row in the <code>hbase:meta</code> table.
53 * @throws IOException
55 public static Result getMetaTableRowResult(final HRegionInfo region, final ServerName sn)
56 throws IOException {
57 return getMetaTableRowResult(region, sn, null, null);
60 /**
61 * Returns a Result object constructed from the given region information simulating
62 * a catalog table result.
63 * @param region the RegionInfo object or null
64 * @param sn to use making startcode and server hostname:port in meta or null
65 * @param splita daughter region or null
66 * @param splitb daughter region or null
67 * @return A mocked up Result that fakes a Get on a row in the <code>hbase:meta</code> table.
68 * @throws IOException
70 public static Result getMetaTableRowResult(RegionInfo region, final ServerName sn,
71 RegionInfo splita, RegionInfo splitb) throws IOException {
72 List<Cell> kvs = new ArrayList<>();
73 if (region != null) {
74 kvs.add(new KeyValue(
75 region.getRegionName(),
76 HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER,
77 RegionInfo.toByteArray(region)));
80 if (sn != null) {
81 kvs.add(new KeyValue(region.getRegionName(),
82 HConstants.CATALOG_FAMILY, HConstants.SERVER_QUALIFIER,
83 Bytes.toBytes(sn.getHostAndPort())));
84 kvs.add(new KeyValue(region.getRegionName(),
85 HConstants.CATALOG_FAMILY, HConstants.STARTCODE_QUALIFIER,
86 Bytes.toBytes(sn.getStartcode())));
89 if (splita != null) {
90 kvs.add(new KeyValue(
91 region.getRegionName(),
92 HConstants.CATALOG_FAMILY, HConstants.SPLITA_QUALIFIER,
93 RegionInfo.toByteArray(splita)));
96 if (splitb != null) {
97 kvs.add(new KeyValue(
98 region.getRegionName(),
99 HConstants.CATALOG_FAMILY, HConstants.SPLITB_QUALIFIER,
100 RegionInfo.toByteArray(splitb)));
103 //important: sort the kvs so that binary search work
104 Collections.sort(kvs, CellComparatorImpl.META_COMPARATOR);
106 return Result.create(kvs);