HBASE-26416 Implement a new method for region replication instead of using replay...
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / MetaMockingUtil.java
blob68935ffe5c8c3c5d92c04394486804123ad5d9cc
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;
25 import org.apache.hadoop.hbase.client.RegionInfo;
26 import org.apache.hadoop.hbase.client.Result;
27 import org.apache.hadoop.hbase.util.Bytes;
29 /**
30 * Mocking utility for common hbase:meta functionality
32 public class MetaMockingUtil {
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
41 public static Result getMetaTableRowResult(final RegionInfo region)
42 throws IOException {
43 return getMetaTableRowResult(region, null, null, null);
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
54 public static Result getMetaTableRowResult(final RegionInfo region, final ServerName sn)
55 throws IOException {
56 return getMetaTableRowResult(region, sn, null, null);
59 /**
60 * Returns a Result object constructed from the given region information simulating
61 * a catalog table result.
62 * @param region the RegionInfo 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
69 public static Result getMetaTableRowResult(RegionInfo region, final ServerName sn,
70 RegionInfo splita, RegionInfo splitb) throws IOException {
71 List<Cell> kvs = new ArrayList<>();
72 if (region != null) {
73 kvs.add(new KeyValue(
74 region.getRegionName(),
75 HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER,
76 RegionInfo.toByteArray(region)));
79 if (sn != null) {
80 kvs.add(new KeyValue(region.getRegionName(),
81 HConstants.CATALOG_FAMILY, HConstants.SERVER_QUALIFIER,
82 Bytes.toBytes(sn.getAddress().toString())));
83 kvs.add(new KeyValue(region.getRegionName(),
84 HConstants.CATALOG_FAMILY, HConstants.STARTCODE_QUALIFIER,
85 Bytes.toBytes(sn.getStartcode())));
88 if (splita != null) {
89 kvs.add(new KeyValue(
90 region.getRegionName(),
91 HConstants.CATALOG_FAMILY, HConstants.SPLITA_QUALIFIER,
92 RegionInfo.toByteArray(splita)));
95 if (splitb != null) {
96 kvs.add(new KeyValue(
97 region.getRegionName(),
98 HConstants.CATALOG_FAMILY, HConstants.SPLITB_QUALIFIER,
99 RegionInfo.toByteArray(splitb)));
102 //important: sort the kvs so that binary search work
103 Collections.sort(kvs, MetaCellComparator.META_COMPARATOR);
105 return Result.create(kvs);