1
2
3
4
5
6
7
8
9
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
27
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
41
42
43 String getName() {
44 return name;
45 }
46
47
48
49
50
51 synchronized Set<TableName> getTables() {
52 return this.tableAndRegionInfo.keySet();
53 }
54
55
56
57
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 }