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.namespace;
019
020import java.util.HashMap;
021import java.util.Map;
022import java.util.Map.Entry;
023import java.util.Set;
024import java.util.concurrent.atomic.AtomicInteger;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.yetus.audience.InterfaceAudience;
027
028import org.apache.hbase.thirdparty.com.google.common.base.Joiner;
029
030/**
031 * NamespaceTableAndRegionInfo is a helper class that contains information about current state of
032 * tables and regions in a namespace.
033 */
034@InterfaceAudience.Private
035class NamespaceTableAndRegionInfo {
036  private String name;
037  private Map<TableName, AtomicInteger> tableAndRegionInfo;
038
039  public NamespaceTableAndRegionInfo(String namespace) {
040    this.name = namespace;
041    this.tableAndRegionInfo = new HashMap<>();
042  }
043
044  /**
045   * Gets the name of the namespace.
046   * @return name of the namespace.
047   */
048  String getName() {
049    return name;
050  }
051
052  /**
053   * Gets the set of table names belonging to namespace.
054   * @return A set of table names.
055   */
056  synchronized Set<TableName> getTables() {
057    return this.tableAndRegionInfo.keySet();
058  }
059
060  /**
061   * Gets the total number of regions in namespace.
062   * @return the region count
063   */
064  synchronized int getRegionCount() {
065    int regionCount = 0;
066    for (Entry<TableName, AtomicInteger> entry : this.tableAndRegionInfo.entrySet()) {
067      regionCount = regionCount + entry.getValue().get();
068    }
069    return regionCount;
070  }
071
072  synchronized int getRegionCountOfTable(TableName tableName) {
073    if (tableAndRegionInfo.containsKey(tableName)) {
074      return this.tableAndRegionInfo.get(tableName).get();
075    } else {
076      return -1;
077    }
078  }
079
080  synchronized boolean containsTable(TableName tableName) {
081    return this.tableAndRegionInfo.containsKey(tableName);
082  }
083
084  synchronized void addTable(TableName tableName, int regionCount) {
085    if (!name.equalsIgnoreCase(tableName.getNamespaceAsString())) {
086      throw new IllegalStateException(
087        "Table : " + tableName + " does not belong to namespace " + name);
088    }
089    if (!tableAndRegionInfo.containsKey(tableName)) {
090      tableAndRegionInfo.put(tableName, new AtomicInteger(regionCount));
091    } else {
092      throw new IllegalStateException("Table already in the cache " + tableName);
093    }
094  }
095
096  synchronized void removeTable(TableName tableName) {
097    tableAndRegionInfo.remove(tableName);
098  }
099
100  synchronized int incRegionCountForTable(TableName tableName, int count) {
101    return tableAndRegionInfo.get(tableName).addAndGet(count);
102  }
103
104  synchronized int decrementRegionCountForTable(TableName tableName, int count) {
105    return tableAndRegionInfo.get(tableName).decrementAndGet();
106  }
107
108  @Override
109  public String toString() {
110    Joiner.MapJoiner mapJoiner = Joiner.on(',').withKeyValueSeparator("=");
111    return "NamespaceTableAndRegionInfo [name=" + name + ", tableAndRegionInfo="
112      + mapJoiner.join(tableAndRegionInfo) + "]";
113  }
114}