@VisibleForTesting NameSpaceContainer deleteEntity(final NamespaceKey path, final Type type, String version, boolean deleteRoot) throws NamespaceException { final List<NameSpaceContainer> entitiesOnPath = getEntitiesOnPath(path); final NameSpaceContainer container = lastElement(entitiesOnPath); if (container == null) { throw new NamespaceNotFoundException(path, String.format("Entity %s not found", path)); } return doDeleteEntity(path, type, version, entitiesOnPath, deleteRoot); }
@GET @Produces(MediaType.APPLICATION_JSON) public Spaces getSpaces() throws Exception { final Spaces spaces = new Spaces(); for (SpaceConfig spaceConfig : namespaceService.getSpaces()) { int datasetCount = 0; try { datasetCount = namespaceService.getDatasetCount(new SpacePath(spaceConfig.getName()).toNamespaceKey(), BoundedDatasetCount.SEARCH_TIME_LIMIT_MS, BoundedDatasetCount.COUNT_LIMIT_TO_STOP_SEARCH).getCount(); } catch (IllegalArgumentException e) { logger.warn("Could not load dataset count for {} because it has a invalid name: {}", spaceConfig.getName(), e.getMessage()); } catch (NamespaceException e) { logger.warn("Could not load dataset count for {}: {}", spaceConfig.getName(), e.getMessage()); } try { // we catch exceptions here so the user can still see their other Spaces spaces.add(newSpace(spaceConfig, datasetCount)); } catch (NamespaceNotFoundException e) { logger.warn("Skipping Space {} because namespace not found: {}", spaceConfig.getName(), e.getMessage()); } } return spaces; }
@Override public List<NameSpaceContainer> list(NamespaceKey root) throws NamespaceException { final List<NameSpaceContainer> entitiesOnPath = getEntitiesOnPath(root); final NameSpaceContainer rootContainer = lastElement(entitiesOnPath); if (rootContainer == null) { throw new NamespaceNotFoundException(root, "not found"); } if (!isListable(rootContainer.getType())) { throw new NamespaceNotFoundException(root, "no listable entity found"); } return doList(root, entitiesOnPath); }
/** * Helper method that returns the list of the namespace entities on the given path. Except the last entity on the path * all other entities should return a non-null value. * * @param entityPath * @return */ protected List<NameSpaceContainer> getEntitiesOnPath(NamespaceKey entityPath) throws NamespaceException { final List<byte[]> keys = Lists.newArrayListWithExpectedSize(entityPath.getPathComponents().size()); NamespaceKey currentPath = entityPath; for (int i = 0; i < entityPath.getPathComponents().size(); i++) { keys.add(new NamespaceInternalKey(currentPath, keyNormalization).getKey()); if (currentPath.hasParent()) { currentPath = currentPath.getParent(); } } // reverse the keys so that the order of keys is from root to leaf level entity. Collections.reverse(keys); final List<NameSpaceContainer> entitiesOnPath = namespace.get(keys); for (int i = 0; i < entitiesOnPath.size() - 1; i++) { if (entitiesOnPath.get(i) == null) { throw new NamespaceNotFoundException(entityPath, "one or more elements on the path are not found in namespace"); } } return entitiesOnPath; }
/** * Helper method which retrieves the entity with given key. No authorization check done. * * @param key * @return * @throws NamespaceException */ private NameSpaceContainer getEntity(final NamespaceKey key, Type type) throws NamespaceException { final List<NameSpaceContainer> entitiesOnPath = getEntitiesOnPath(key); NameSpaceContainer container = lastElement(entitiesOnPath); if (container == null || container.getType() != type) { throw new NamespaceNotFoundException(key, "not found"); } return doGetEntity(key, type, entitiesOnPath); }
protected NameSpaceContainer getEntityByIndex(IndexKey key, String index, Type type) throws NamespaceException { NameSpaceContainer namespaceContainer = getByIndex(key, index); if (namespaceContainer == null || namespaceContainer.getType() != type) { throw new NamespaceNotFoundException(new NamespaceKey(key.toString()), "not found"); } return namespaceContainer; }