/** * Look for namespace ID in ZK. Fail quietly by logging and returning null. */ public static Namespace.ID lookupNamespaceId(ClientContext context, String namespaceName) { Namespace.ID id = null; try { id = getNamespaceId(context, namespaceName); } catch (NamespaceNotFoundException e) { if (log.isDebugEnabled()) log.debug("Failed to find namespace ID from name: " + namespaceName, e); } return id; }
/** * Return true if namespace name exists */ public static boolean namespaceNameExists(ClientContext context, String namespaceName) { return lookupNamespaceId(context, namespaceName) != null; }
public static List<Table.ID> getTableIds(ClientContext context, Namespace.ID namespaceId) throws NamespaceNotFoundException { String namespace = getNamespaceName(context, namespaceId); List<Table.ID> tableIds = new LinkedList<>(); for (Entry<String,Table.ID> nameToId : Tables.getNameToIdMap(context).entrySet()) if (namespace.equals(Tables.qualify(nameToId.getKey()).getFirst())) tableIds.add(nameToId.getValue()); return tableIds; }
@Override public void delete(String namespace) throws AccumuloException, AccumuloSecurityException, NamespaceNotFoundException, NamespaceNotEmptyException { checkArgument(namespace != null, "namespace is null"); Namespace.ID namespaceId = Namespaces.getNamespaceId(context, namespace); if (namespaceId.equals(Namespace.ID.ACCUMULO) || namespaceId.equals(Namespace.ID.DEFAULT)) { Credentials credentials = context.getCredentials(); log.debug("{} attempted to delete the {} namespace", credentials.getPrincipal(), namespaceId); throw new AccumuloSecurityException(credentials.getPrincipal(), SecurityErrorCode.UNSUPPORTED_OPERATION); } if (Namespaces.getTableIds(context, namespaceId).size() > 0) { throw new NamespaceNotEmptyException(namespaceId.canonicalID(), namespace, null); } List<ByteBuffer> args = Arrays.asList(ByteBuffer.wrap(namespace.getBytes(UTF_8))); Map<String,String> opts = new HashMap<>(); try { doNamespaceFateOperation(FateOperation.NAMESPACE_DELETE, args, opts, namespace); } catch (NamespaceExistsException e) { // should not happen throw new AssertionError(e); } }
Namespace.ID namespaceId = Namespaces.getNamespaceId(shellState.getContext(), cl.getOptionValue(optNamespace.getOpt())); tables.addAll(Namespaces.getTableNames(shellState.getContext(), namespaceId));
@Override public Map<String,String> namespaceIdMap() { return Namespaces.getNameToIdMap(context).entrySet().stream() .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().canonicalID(), (v1, v2) -> { throw new RuntimeException(String.format("Duplicate key for values %s and %s", v1, v2)); }, TreeMap::new)); }
/** * Return sorted map with key = ID, value = namespaceName */ public static SortedMap<Namespace.ID,String> getIdToNameMap(ClientContext context) { SortedMap<Namespace.ID,String> idMap = new TreeMap<>(); getAllNamespaces(context, (id, name) -> idMap.put(Namespace.ID.of(id), name)); return idMap; }
Namespace.ID id = namespace.getSecond(); log.debug("Upgrade creating namespace \"{}\" (ID: {})", ns, id); if (!Namespaces.exists(context, id)) TableManager.prepareNewNamespaceState(zoo, getInstanceID(), id, ns, NodeExistsPolicy.SKIP);
tableSet.add(cl.getOptionValue(optTableName.getOpt())); } else if (cl.hasOption(optNamespace.getOpt())) { Namespace.ID namespaceId = Namespaces.getNamespaceId(shellState.getContext(), cl.getOptionValue(optNamespace.getOpt())); for (Table.ID tableId : Namespaces.getTableIds(shellState.getContext(), namespaceId)) { tableSet.add(Tables.getTableName(shellState.getContext(), tableId));
protected void doTableOp(final Shell shellState, final String namespace, boolean force) throws Exception { boolean resetContext = false; String currentTable = shellState.getTableName(); Namespace.ID namespaceId = Namespaces.getNamespaceId(shellState.getContext(), namespace); List<String> tables = Namespaces.getTableNames(shellState.getContext(), namespaceId); resetContext = tables.contains(currentTable); if (force) for (String table : shellState.getAccumuloClient().tableOperations().list()) if (table.startsWith(namespace + ".")) shellState.getAccumuloClient().tableOperations().delete(table); shellState.getAccumuloClient().namespaceOperations().delete(namespace); if (resetContext) { shellState.setTableName(""); } }
@Override public SortedSet<String> list() { OpTimer timer = null; if (log.isTraceEnabled()) { log.trace("tid={} Fetching list of namespaces...", Thread.currentThread().getId()); timer = new OpTimer().start(); } TreeSet<String> namespaces = new TreeSet<>(Namespaces.getNameToIdMap(context).keySet()); if (timer != null) { timer.stop(); log.trace("tid={} Fetched {} namespaces in {}", Thread.currentThread().getId(), namespaces.size(), String.format("%.3f secs", timer.scale(TimeUnit.SECONDS))); } return namespaces; }
/** * Return sorted map with key = namespaceName, value = ID */ public static SortedMap<String,Namespace.ID> getNameToIdMap(ClientContext context) { SortedMap<String,Namespace.ID> nameMap = new TreeMap<>(); getAllNamespaces(context, (id, name) -> nameMap.put(name, Namespace.ID.of(id))); return nameMap; }
@Override public int execute(final String fullCommand, final CommandLine cl, final Shell shellState) throws AccumuloException, AccumuloSecurityException, TableNotFoundException, TableExistsException, NamespaceNotFoundException, NamespaceExistsException { String old = cl.getArgs()[0]; String newer = cl.getArgs()[1]; boolean resetContext = false; Table.ID currentTableId = null; if (!(shellState.getTableName() == null) && !shellState.getTableName().isEmpty()) { Namespace.ID namespaceId = Namespaces.getNamespaceId(shellState.getContext(), old); List<Table.ID> tableIds = Namespaces.getTableIds(shellState.getContext(), namespaceId); currentTableId = Tables.getTableId(shellState.getContext(), shellState.getTableName()); resetContext = tableIds.contains(currentTableId); } shellState.getAccumuloClient().namespaceOperations().rename(old, newer); if (resetContext) { shellState.setTableName(Tables.getTableName(shellState.getContext(), currentTableId)); } return 0; }
public CloneZookeeper(CloneInfo cloneInfo, ClientContext context) throws NamespaceNotFoundException { this.cloneInfo = cloneInfo; this.cloneInfo.namespaceId = Namespaces.getNamespaceId(context, Tables.qualify(this.cloneInfo.tableName).getFirst()); }
public static Namespace.ID checkNamespaceId(ClientContext context, String namespaceName, TableOperation operation) throws ThriftTableOperationException { Namespace.ID namespaceId = Namespaces.lookupNamespaceId(context, namespaceName); if (namespaceId == null) { // maybe the namespace exists, but the cache was not updated yet... so try to clear the cache // and check again Tables.clearCache(context); namespaceId = Namespaces.lookupNamespaceId(context, namespaceName); if (namespaceId == null) throw new ThriftTableOperationException(null, namespaceName, operation, TableOperationExceptionType.NAMESPACE_NOTFOUND, null); } return namespaceId; }
public static List<String> getTableNames(ClientContext context, Namespace.ID namespaceId) throws NamespaceNotFoundException { String namespace = getNamespaceName(context, namespaceId); List<String> names = new LinkedList<>(); for (String name : Tables.getNameToIdMap(context).keySet()) if (namespace.equals(Tables.qualify(name).getFirst())) names.add(name); return names; }
/** * Lookup table ID in ZK. If not found, clears cache and tries again. */ public static Table.ID _getTableId(ClientContext context, String tableName) throws NamespaceNotFoundException, TableNotFoundException { Table.ID tableId = getNameToIdMap(context).get(tableName); if (tableId == null) { // maybe the table exist, but the cache was not updated yet... so try to clear the cache and // check again clearCache(context); tableId = getNameToIdMap(context).get(tableName); if (tableId == null) { String namespace = qualify(tableName).getFirst(); if (Namespaces.getNameToIdMap(context).containsKey(namespace)) throw new TableNotFoundException(null, tableName, null); else throw new NamespaceNotFoundException(null, namespace, null); } } return tableId; }
/** * Look for namespace ID in ZK. Throw NamespaceNotFoundException if not found. */ public static Namespace.ID getNamespaceId(ClientContext context, String namespaceName) throws NamespaceNotFoundException { final ArrayList<Namespace.ID> singleId = new ArrayList<>(1); getAllNamespaces(context, (id, name) -> { if (name.equals(namespaceName)) singleId.add(Namespace.ID.of(id)); }); if (singleId.isEmpty()) throw new NamespaceNotFoundException(null, namespaceName, "getNamespaceId() failed to find namespace"); return singleId.get(0); }
@Override public Map<String,String> getNamespaceConfiguration(TInfo tinfo, TCredentials credentials, String ns) throws ThriftTableOperationException, TException { Namespace.ID namespaceId; try { namespaceId = Namespaces.getNamespaceId(context, ns); } catch (NamespaceNotFoundException e) { String why = "Could not find namespace while getting configuration."; throw new ThriftTableOperationException(null, ns, null, TableOperationExceptionType.NAMESPACE_NOTFOUND, why); } AccumuloConfiguration config = context.getServerConfFactory() .getNamespaceConfiguration(namespaceId); return conf(credentials, config); }
public static void checkNamespaceDoesNotExist(ServerContext context, String namespace, Namespace.ID namespaceId, TableOperation operation) throws AcceptableThriftTableOperationException { Namespace.ID n = Namespaces.lookupNamespaceId(context, namespace); if (n != null && !n.equals(namespaceId)) throw new AcceptableThriftTableOperationException(null, namespace, operation, TableOperationExceptionType.NAMESPACE_EXISTS, null); }