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