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 */ 018 019package org.apache.hadoop.hbase; 020 021import java.io.IOException; 022import java.util.ArrayList; 023import java.util.Collections; 024import java.util.List; 025import org.apache.hadoop.hbase.client.RegionInfo; 026import org.apache.hadoop.hbase.client.Result; 027import org.apache.hadoop.hbase.util.Bytes; 028 029/** 030 * Mocking utility for common hbase:meta functionality 031 */ 032public class MetaMockingUtil { 033 034 /** 035 * Returns a Result object constructed from the given region information simulating 036 * a catalog table result. 037 * @param region the HRegionInfo object or null 038 * @return A mocked up Result that fakes a Get on a row in the <code>hbase:meta</code> table. 039 * @throws IOException 040 */ 041 public static Result getMetaTableRowResult(final HRegionInfo region) 042 throws IOException { 043 return getMetaTableRowResult(region, null, null, null); 044 } 045 046 /** 047 * Returns a Result object constructed from the given region information simulating 048 * a catalog table result. 049 * @param region the HRegionInfo object or null 050 * @param sn to use making startcode and server hostname:port in meta or null 051 * @return A mocked up Result that fakes a Get on a row in the <code>hbase:meta</code> table. 052 * @throws IOException 053 */ 054 public static Result getMetaTableRowResult(final HRegionInfo region, final ServerName sn) 055 throws IOException { 056 return getMetaTableRowResult(region, sn, null, null); 057 } 058 059 /** 060 * Returns a Result object constructed from the given region information simulating 061 * a catalog table result. 062 * @param region the HRegionInfo object or null 063 * @param sn to use making startcode and server hostname:port in meta or null 064 * @param splita daughter region or null 065 * @param splitb daughter region or null 066 * @return A mocked up Result that fakes a Get on a row in the <code>hbase:meta</code> table. 067 * @throws IOException 068 */ 069 public static Result getMetaTableRowResult(RegionInfo region, final ServerName sn, 070 RegionInfo splita, RegionInfo splitb) throws IOException { 071 List<Cell> kvs = new ArrayList<>(); 072 if (region != null) { 073 kvs.add(new KeyValue( 074 region.getRegionName(), 075 HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER, 076 RegionInfo.toByteArray(region))); 077 } 078 079 if (sn != null) { 080 kvs.add(new KeyValue(region.getRegionName(), 081 HConstants.CATALOG_FAMILY, HConstants.SERVER_QUALIFIER, 082 Bytes.toBytes(sn.getHostAndPort()))); 083 kvs.add(new KeyValue(region.getRegionName(), 084 HConstants.CATALOG_FAMILY, HConstants.STARTCODE_QUALIFIER, 085 Bytes.toBytes(sn.getStartcode()))); 086 } 087 088 if (splita != null) { 089 kvs.add(new KeyValue( 090 region.getRegionName(), 091 HConstants.CATALOG_FAMILY, HConstants.SPLITA_QUALIFIER, 092 RegionInfo.toByteArray(splita))); 093 } 094 095 if (splitb != null) { 096 kvs.add(new KeyValue( 097 region.getRegionName(), 098 HConstants.CATALOG_FAMILY, HConstants.SPLITB_QUALIFIER, 099 RegionInfo.toByteArray(splitb))); 100 } 101 102 //important: sort the kvs so that binary search work 103 Collections.sort(kvs, MetaCellComparator.META_COMPARATOR); 104 105 return Result.create(kvs); 106 } 107 108}