Github user ctubbsii commented on a diff in the pull request:
https://github.com/apache/accumulo/pull/279#discussion_r126759594
--- Diff:
core/src/main/java/org/apache/accumulo/core/client/impl/Namespaces.java ---
@@ -100,50 +106,150 @@ private static ZooCache getZooCache(Instance
instance) {
return namespaceMap;
}
- public static boolean exists(Instance instance, String namespaceId) {
+ public static boolean exists(Instance instance, Namespace.ID
namespaceId) {
ZooCache zc = getZooCache(instance);
List<String> namespaceIds = zc.getChildren(ZooUtil.getRoot(instance) +
Constants.ZNAMESPACES);
- return namespaceIds.contains(namespaceId);
- }
-
- public static String getNamespaceId(Instance instance, String namespace)
throws NamespaceNotFoundException {
- String id = getNameToIdMap(instance).get(namespace);
- if (id == null)
- throw new NamespaceNotFoundException(null, namespace,
"getNamespaceId() failed to find namespace");
- return id;
+ return namespaceIds.contains(namespaceId.canonicalID());
}
+ /**
+ * @deprecated Do not use - String for namespace ID is not type safe.
Use {@link #getNamespaceName(Instance, Namespace.ID)}
+ */
+ @Deprecated
public static String getNamespaceName(Instance instance, String
namespaceId) throws NamespaceNotFoundException {
String namespaceName = getIdToNameMap(instance).get(namespaceId);
if (namespaceName == null)
throw new NamespaceNotFoundException(namespaceId, null,
"getNamespaceName() failed to find namespace");
return namespaceName;
}
+ /**
+ * @deprecated Do not use - Not type safe, ambiguous String-String map.
Use {@link #getNameMap(Instance)}
+ */
+ @Deprecated
public static SortedMap<String,String> getNameToIdMap(Instance instance)
{
return getMap(instance, true);
}
+ /**
+ * @deprecated Do not use - Not type safe, ambiguous String-String map.
Use {@link #getIdMap(Instance)}
+ */
+ @Deprecated
public static SortedMap<String,String> getIdToNameMap(Instance instance)
{
return getMap(instance, false);
}
- public static List<String> getTableIds(Instance instance, String
namespaceId) throws NamespaceNotFoundException {
+ public static List<Table.ID> getTableIds(Instance instance, Namespace.ID
namespaceId) throws NamespaceNotFoundException {
String namespace = getNamespaceName(instance, namespaceId);
- List<String> names = new LinkedList<>();
- for (Entry<String,String> nameToId :
Tables.getNameToIdMap(instance).entrySet())
+ List<Table.ID> tableIds = new LinkedList<>();
+ for (Entry<String,Table.ID> nameToId :
Tables.getNameMap(instance).entrySet())
if (namespace.equals(Tables.qualify(nameToId.getKey()).getFirst()))
- names.add(nameToId.getValue());
- return names;
+ tableIds.add(nameToId.getValue());
+ return tableIds;
}
- public static List<String> getTableNames(Instance instance, String
namespaceId) throws NamespaceNotFoundException {
+ public static List<String> getTableNames(Instance instance, Namespace.ID
namespaceId) throws NamespaceNotFoundException {
String namespace = getNamespaceName(instance, namespaceId);
List<String> names = new LinkedList<>();
- for (String name : Tables.getNameToIdMap(instance).keySet())
+ for (String name : Tables.getNameMap(instance).keySet())
if (namespace.equals(Tables.qualify(name).getFirst()))
names.add(name);
return names;
}
+ /**
+ * Populate map passed in as the BiConsumer. key = ID, value =
namespaceName
+ */
+ private static void populateMap(Instance instance,
BiConsumer<String,String> biConsumer) {
+ final ZooCache zc = getZooCache(instance);
+ List<String> namespaceIds = zc.getChildren(ZooUtil.getRoot(instance) +
Constants.ZNAMESPACES);
+ for (String id : namespaceIds) {
+ byte[] path = zc.get(ZooUtil.getRoot(instance) +
Constants.ZNAMESPACES + "/" + id + Constants.ZNAMESPACE_NAME);
+ if (path != null) {
+ biConsumer.accept(id, new String(path, UTF_8));
+ }
+ }
+ }
+
+ /**
+ * Return sorted map with key = ID, value = namespaceName
+ */
+ public static SortedMap<Namespace.ID,String> getIdMap(Instance instance)
{
+ SortedMap<Namespace.ID,String> idMap = new TreeMap<>();
+ populateMap(instance, (id, name) -> idMap.put(new Namespace.ID(id),
name));
+ return idMap;
+ }
+
+ /**
+ * Return sorted map with key = namespaceName, value = ID
+ */
+ public static SortedMap<String,Namespace.ID> getNameMap(Instance
instance) {
+ SortedMap<String,Namespace.ID> nameMap = new TreeMap<>();
+ populateMap(instance, (id, name) -> nameMap.put(name, new
Namespace.ID(id)));
+ return nameMap;
+ }
+
+ /**
+ * Look for namespace ID in ZK. Throw NamespaceNotFoundException if not
found.
+ */
+ public static Namespace.ID getNamespaceId(Instance instance, String
namespaceName) throws NamespaceNotFoundException {
+ final ArrayList<Namespace.ID> singleId = new ArrayList<>(1);
+ populateMap(instance, (id, name) -> {
+ if (name.equals(namespaceName))
+ singleId.add(new Namespace.ID(id));
+ });
+ if (singleId.isEmpty())
+ throw new NamespaceNotFoundException(null, namespaceName,
"getNamespaceId() failed to find namespace");
+ return singleId.get(0);
+ }
+
+ /**
+ * Look for namespace ID in ZK. Fail quietly by logging and returning
null.
+ */
+ public static Namespace.ID lookupNamespaceId(Instance instance, String
namespaceName) {
+ Namespace.ID id = null;
+ try {
+ id = getNamespaceId(instance, 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(Instance instance, String
namespaceName) {
+ return lookupNamespaceId(instance, namespaceName) != null;
+ }
+
+ /**
+ * Look for namespace name in ZK. Throw NamespaceNotFoundException if
not found.
+ */
+ public static String getNamespaceName(Instance instance, Namespace.ID
namespaceId) throws NamespaceNotFoundException {
+ final ArrayList<String> singleName = new ArrayList<>(1);
+ populateMap(instance, (id, name) -> {
+ if (id.equals(namespaceId.canonicalID()))
+ singleName.add(name);
+ });
+ if (singleName.isEmpty())
+ throw new NamespaceNotFoundException(namespaceId.canonicalID(),
null, "getNamespaceName() failed to find namespace");
+ return singleName.get(0);
+ }
+
+ /**
+ * Look for namespace name in ZK. Fail quietly by logging and returning
null.
+ */
+ public static String lookupNamespaceName(Instance instance, Namespace.ID
namespaceId) {
+ String name = null;
+ try {
+ getNamespaceName(instance, namespaceId);
--- End diff --
I don't think this is something @milleruntime added. It probably already
existed. I think this kind of thing plagues the whole of `Tables` and
`Namespaces` util classes. These were intended for very specific internal use
cases, with very specific handling behaviors, and in some cases, warnings were
suppressed from common code because the caller had its own handling (possibly
with better messages).
Over time, these util methods got used for things they were not originally
intended to be used for. It's possible some of the callers depend on the null
return value, and others would benefit from the exception being thrown. Without
actually tracing all the callers, it's anyone's guess as to how many of these
util classes are being used with incorrect assumptions about exception
handling. I seem to recall at least a few bugs being fixed because of this.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---