View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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 }