private static long getDirIdFromRoot(final Path rootPath, final Mode mode) {
        LOGGER.debug("getLastDirId - path: {}, mode: {}", rootPath, mode);

        final List<DirId> depthDirs = findDirs(rootPath, mode, DirUtil::isValidDepthPath);
        DirId dirId = null;
        final AtomicReference<Path> incompletePath = new AtomicReference<>();
        for (final DirId depthDirId : depthDirs) {
            final Path depthDir = depthDirId.dir;
            LOGGER.debug("Checking depthPath: '{}'", depthDir);
            dirId = getDirId(depthDir, mode, incompletePath);
            if (dirId != null) {
                // Found it
                break;
            } else {
                incompletePath.set(depthDir);
            }
        }

        if (dirId != null && incompletePath.get() != null && mode == Mode.MAX) {
            // In MAX mode we are scanning in reverse order, so we must have found
            // an incomplete path BEFORE the valid leaf path. This means we have at least one
            // DirId but an incomplete branch belonging to a higher DirId. Thus, we can't be
            // sure what the max ID is as it must have been higher than dirId.
            // If dirId == null then we have an empty root, so hopefully it is ok.
            throw new IllegalStateException(
                    LogUtil.message(
                            "Incomplete directory ID path found '{}'. This implies that " +
                            "an ID has previously been allocated with this branch so we cannot be sure what the " +
                            "maximum ID is.", LogUtil.path(incompletePath.get())));
        }

        LOGGER.debug("getDirIdFromRoot - returning dirId {}", dirId);
        return NullSafe.getOrElse(dirId, DirId::num, 0L);
    }
Get the ID for the dir matching mode in the root directory, i.e. the directory
that contains the depth paths.
stroom-state/stroom-planb-impl/src/main/java/stroom/planb/impl/data/DirUtil.java