001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase; 019 020import java.io.IOException; 021import java.util.ArrayList; 022import java.util.Collections; 023import java.util.List; 024import org.apache.hadoop.hbase.client.RegionInfo; 025import org.apache.hadoop.hbase.client.Result; 026import org.apache.hadoop.hbase.util.Bytes; 027 028/** 029 * Mocking utility for common hbase:meta functionality 030 */ 031public class MetaMockingUtil { 032 033 /** 034 * Returns a Result object constructed from the given region information simulating a catalog 035 * table result. 036 * @param region the HRegionInfo object or null 037 * @return A mocked up Result that fakes a Get on a row in the <code>hbase:meta</code> table. 038 */ 039 public static Result getMetaTableRowResult(final HRegionInfo region) throws IOException { 040 return getMetaTableRowResult(region, null, null, null); 041 } 042 043 /** 044 * Returns a Result object constructed from the given region information simulating a catalog 045 * table result. 046 * @param region the HRegionInfo object or null 047 * @param sn to use making startcode and server hostname:port in meta or null 048 * @return A mocked up Result that fakes a Get on a row in the <code>hbase:meta</code> table. 049 */ 050 public static Result getMetaTableRowResult(final HRegionInfo region, final ServerName sn) 051 throws IOException { 052 return getMetaTableRowResult(region, sn, null, null); 053 } 054 055 /** 056 * Returns a Result object constructed from the given region information simulating a catalog 057 * table result. 058 * @param region the HRegionInfo object or null 059 * @param sn to use making startcode and server hostname:port in meta or null 060 * @param splita daughter region or null 061 * @param splitb daughter region or null 062 * @return A mocked up Result that fakes a Get on a row in the <code>hbase:meta</code> table. 063 */ 064 public static Result getMetaTableRowResult(RegionInfo region, final ServerName sn, 065 RegionInfo splita, RegionInfo splitb) throws IOException { 066 List<Cell> kvs = new ArrayList<>(); 067 if (region != null) { 068 kvs.add(new KeyValue(region.getRegionName(), HConstants.CATALOG_FAMILY, 069 HConstants.REGIONINFO_QUALIFIER, RegionInfo.toByteArray(region))); 070 } 071 072 if (sn != null) { 073 kvs.add(new KeyValue(region.getRegionName(), HConstants.CATALOG_FAMILY, 074 HConstants.SERVER_QUALIFIER, Bytes.toBytes(sn.getAddress().toString()))); 075 kvs.add(new KeyValue(region.getRegionName(), HConstants.CATALOG_FAMILY, 076 HConstants.STARTCODE_QUALIFIER, Bytes.toBytes(sn.getStartcode()))); 077 } 078 079 if (splita != null) { 080 kvs.add(new KeyValue(region.getRegionName(), HConstants.CATALOG_FAMILY, 081 HConstants.SPLITA_QUALIFIER, RegionInfo.toByteArray(splita))); 082 } 083 084 if (splitb != null) { 085 kvs.add(new KeyValue(region.getRegionName(), HConstants.CATALOG_FAMILY, 086 HConstants.SPLITB_QUALIFIER, RegionInfo.toByteArray(splitb))); 087 } 088 089 // important: sort the kvs so that binary search work 090 Collections.sort(kvs, MetaCellComparator.META_COMPARATOR); 091 092 return Result.create(kvs); 093 } 094 095}