1   /**
2    * Copyright 2007 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.regionserver;
21  
22  import org.apache.hadoop.hbase.HRegionInfo;
23  import org.apache.hadoop.hbase.HTableDescriptor;
24  import org.apache.hadoop.hbase.util.Bytes;
25  import org.apache.hadoop.hbase.util.MD5Hash;
26  
27  import org.junit.Test;
28  import static org.junit.Assert.*;
29  
30  public class TestHRegionInfo {
31    @Test
32    public void testCreateHRegionInfoName() throws Exception {
33      String tableName = "tablename";
34      final byte [] tn = Bytes.toBytes(tableName);
35      String startKey = "startkey";
36      final byte [] sk = Bytes.toBytes(startKey);
37      String id = "id";
38  
39      // old format region name
40      byte [] name = HRegionInfo.createRegionName(tn, sk, id, false);
41      String nameStr = Bytes.toString(name);
42      assertEquals(tableName + "," + startKey + "," + id, nameStr);
43  
44  
45      // new format region name.
46      String md5HashInHex = MD5Hash.getMD5AsHex(name);
47      assertEquals(HRegionInfo.MD5_HEX_LENGTH, md5HashInHex.length());
48      name = HRegionInfo.createRegionName(tn, sk, id, true);
49      nameStr = Bytes.toString(name);
50      assertEquals(tableName + "," + startKey + ","
51                   + id + "." + md5HashInHex + ".",
52                   nameStr);
53    }
54    
55    @Test
56    public void testContainsRange() {
57      HTableDescriptor tableDesc = new HTableDescriptor("testtable");
58      HRegionInfo hri = new HRegionInfo(
59          tableDesc, Bytes.toBytes("a"), Bytes.toBytes("g"));
60      // Single row range at start of region
61      assertTrue(hri.containsRange(Bytes.toBytes("a"), Bytes.toBytes("a")));
62      // Fully contained range
63      assertTrue(hri.containsRange(Bytes.toBytes("b"), Bytes.toBytes("c")));
64      // Range overlapping start of region
65      assertTrue(hri.containsRange(Bytes.toBytes("a"), Bytes.toBytes("c")));
66      // Fully contained single-row range
67      assertTrue(hri.containsRange(Bytes.toBytes("c"), Bytes.toBytes("c")));
68      // Range that overlaps end key and hence doesn't fit
69      assertFalse(hri.containsRange(Bytes.toBytes("a"), Bytes.toBytes("g")));
70      // Single row range on end key
71      assertFalse(hri.containsRange(Bytes.toBytes("g"), Bytes.toBytes("g")));
72      // Single row range entirely outside
73      assertFalse(hri.containsRange(Bytes.toBytes("z"), Bytes.toBytes("z")));
74      
75      // Degenerate range
76      try {
77        hri.containsRange(Bytes.toBytes("z"), Bytes.toBytes("a"));
78        fail("Invalid range did not throw IAE");
79      } catch (IllegalArgumentException iae) {
80      }
81    }
82  }