View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional information regarding
4    * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License. You may obtain a
6    * copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable
7    * law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
8    * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
9    * for the specific language governing permissions and limitations under the License.
10   */
11  
12  package org.apache.hadoop.hbase.namespace;
13  
14  import java.util.HashMap;
15  import java.util.Map;
16  import java.util.Map.Entry;
17  import java.util.Set;
18  import java.util.concurrent.atomic.AtomicInteger;
19  
20  import org.apache.hadoop.hbase.TableName;
21  
22  import com.google.common.base.Joiner;
23  import org.apache.hadoop.hbase.classification.InterfaceAudience;
24  
25  /**
26   * NamespaceTableAndRegionInfo is a helper class that contains information about current state of
27   * tables and regions in a namespace.
28   */
29  @InterfaceAudience.Private
30  class NamespaceTableAndRegionInfo {
31    private String name;
32    private Map<TableName, AtomicInteger> tableAndRegionInfo;
33  
34    public NamespaceTableAndRegionInfo(String namespace) {
35      this.name = namespace;
36      this.tableAndRegionInfo = new HashMap<TableName, AtomicInteger>();
37    }
38  
39    /**
40     * Gets the name of the namespace.
41     * @return name of the namespace.
42     */
43    String getName() {
44      return name;
45    }
46  
47    /**
48     * Gets the set of table names belonging to namespace.
49     * @return A set of table names.
50     */
51    synchronized Set<TableName> getTables() {
52      return this.tableAndRegionInfo.keySet();
53    }
54  
55    /**
56     * Gets the total number of regions in namespace.
57     * @return the region count
58     */
59    synchronized int getRegionCount() {
60      int regionCount = 0;
61      for (Entry<TableName, AtomicInteger> entry : this.tableAndRegionInfo.entrySet()) {
62        regionCount = regionCount + entry.getValue().get();
63      }
64      return regionCount;
65    }
66  
67    synchronized int getRegionCountOfTable(TableName tableName) {
68      if (tableAndRegionInfo.containsKey(tableName)) {
69        return this.tableAndRegionInfo.get(tableName).get();
70      } else {
71        return -1;
72      }
73    }
74  
75    synchronized boolean containsTable(TableName tableName) {
76      return this.tableAndRegionInfo.containsKey(tableName);
77    }
78  
79    synchronized void addTable(TableName tableName, int regionCount) {
80      if (!name.equalsIgnoreCase(tableName.getNamespaceAsString())) {
81        throw new IllegalStateException("Table : " + tableName + " does not belong to namespace "
82            + name);
83      }
84      if (!tableAndRegionInfo.containsKey(tableName)) {
85        tableAndRegionInfo.put(tableName, new AtomicInteger(regionCount));
86      } else {
87        throw new IllegalStateException("Table already in the cache " + tableName);
88      }
89    }
90  
91    synchronized void removeTable(TableName tableName) {
92      tableAndRegionInfo.remove(tableName);
93    }
94  
95    synchronized int incRegionCountForTable(TableName tableName, int count) {
96      return tableAndRegionInfo.get(tableName).addAndGet(count);
97    }
98  
99    synchronized int decrementRegionCountForTable(TableName tableName, int count) {
100     return tableAndRegionInfo.get(tableName).decrementAndGet();
101   }
102 
103   @Override
104   public String toString() {
105     Joiner.MapJoiner mapJoiner = Joiner.on(',').withKeyValueSeparator("=");
106     return "NamespaceTableAndRegionInfo [name=" + name + ", tableAndRegionInfo="
107         + mapJoiner.join(tableAndRegionInfo) + "]";
108   }
109 }