1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.accumulo.core.client.impl;
18
19 import java.security.SecurityPermission;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.SortedMap;
23 import java.util.TreeMap;
24
25 import org.apache.accumulo.core.Constants;
26 import org.apache.accumulo.core.client.Instance;
27 import org.apache.accumulo.core.client.TableNotFoundException;
28 import org.apache.accumulo.core.master.state.tables.TableState;
29 import org.apache.accumulo.core.zookeeper.ZooUtil;
30 import org.apache.accumulo.fate.zookeeper.ZooCache;
31
32 public class Tables {
33 private static SecurityPermission TABLES_PERMISSION = new SecurityPermission("tablesPermission");
34
35 private static ZooCache getZooCache(Instance instance) {
36 SecurityManager sm = System.getSecurityManager();
37 if (sm != null) {
38 sm.checkPermission(TABLES_PERMISSION);
39 }
40 return ZooCache.getInstance(instance.getZooKeepers(), instance.getZooKeepersSessionTimeOut());
41 }
42
43 private static SortedMap<String,String> getMap(Instance instance, boolean nameAsKey) {
44 ZooCache zc = getZooCache(instance);
45
46 List<String> tableIds = zc.getChildren(ZooUtil.getRoot(instance) + Constants.ZTABLES);
47
48 TreeMap<String,String> tableMap = new TreeMap<String,String>();
49
50 for (String tableId : tableIds) {
51 byte[] tblPath = zc.get(ZooUtil.getRoot(instance) + Constants.ZTABLES + "/" + tableId + Constants.ZTABLE_NAME);
52 if (tblPath != null) {
53 if (nameAsKey)
54 tableMap.put(new String(tblPath), tableId);
55 else
56 tableMap.put(tableId, new String(tblPath));
57 }
58 }
59
60 return tableMap;
61 }
62
63 public static String getTableId(Instance instance, String tableName) throws TableNotFoundException {
64 String tableId = getNameToIdMap(instance).get(tableName);
65 if (tableId == null)
66 throw new TableNotFoundException(tableId, tableName, null);
67 return tableId;
68 }
69
70 public static String getTableName(Instance instance, String tableId) throws TableNotFoundException {
71 String tableName = getIdToNameMap(instance).get(tableId);
72 if (tableName == null)
73 throw new TableNotFoundException(tableId, tableName, null);
74 return tableName;
75 }
76
77 public static SortedMap<String,String> getNameToIdMap(Instance instance) {
78 return getMap(instance, true);
79 }
80
81 public static SortedMap<String,String> getIdToNameMap(Instance instance) {
82 return getMap(instance, false);
83 }
84
85 public static boolean exists(Instance instance, String tableId) {
86 ZooCache zc = getZooCache(instance);
87 List<String> tableIds = zc.getChildren(ZooUtil.getRoot(instance) + Constants.ZTABLES);
88 return tableIds.contains(tableId);
89 }
90
91 public static void clearCache(Instance instance) {
92 getZooCache(instance).clear(ZooUtil.getRoot(instance) + Constants.ZTABLES);
93 }
94
95 public static String getPrintableTableNameFromId(Map<String,String> tidToNameMap, String tableId) {
96 String tableName = tidToNameMap.get(tableId);
97 return tableName == null ? "(ID:" + tableId + ")" : tableName;
98 }
99
100 public static String getPrintableTableIdFromName(Map<String,String> nameToIdMap, String tableName) {
101 String tableId = nameToIdMap.get(tableName);
102 return tableId == null ? "(NAME:" + tableName + ")" : tableId;
103 }
104
105 public static TableState getTableState(Instance instance, String tableId) {
106 String statePath = ZooUtil.getRoot(instance) + Constants.ZTABLES + "/" + tableId + Constants.ZTABLE_STATE;
107 ZooCache zc = getZooCache(instance);
108 byte[] state = zc.get(statePath);
109 if (state == null)
110 return TableState.UNKNOWN;
111
112 return TableState.valueOf(new String(state));
113 }
114 }