private String findLatestDBPath()

in hugegraph-store/hg-store-rocksdb/src/main/java/org/apache/hugegraph/rocksdb/access/RocksDBSession.java [295:384]


    private String findLatestDBPath(String path, long version) {
        File file = new File(path);
        int strIndex = file.getName().indexOf("_");

        String defaultName;
        if (strIndex < 0) {
            defaultName = file.getName();
        } else {
            defaultName = file.getName().substring(0, strIndex);
        }
        String prefix = defaultName + "_";
        File parentFile = new File(file.getParent());
        ArrayList<HgPair<Long, Long>> dbs = new ArrayList<>();
        File[] files = parentFile.listFiles();
        if (files != null) {
            dbs.ensureCapacity(files.length);
            // search all db path
            for (final File sFile : files) {
                final String name = sFile.getName();
                if (!name.startsWith(prefix) && !name.equals(defaultName)) {
                    continue;
                }
                if (name.endsWith(tempSuffix)) {
                    continue;
                }
                long v1 = -1L;
                long v2 = -1L;
                if (name.length() > defaultName.length()) {
                    String[] versions = name.substring(prefix.length()).split("_");
                    if (versions.length == 1) {
                        v1 = Long.parseLong(versions[0]);
                    } else if (versions.length == 2) {
                        v1 = Long.parseLong(versions[0]);
                        v2 = Long.parseLong(versions[1]);
                    } else {
                        continue;
                    }
                }
                dbs.add(new HgPair<>(v1, v2));
            }
        }

        RocksDBFactory factory = RocksDBFactory.getInstance();
        // get last index db path
        String latestDBPath = "";
        if (!dbs.isEmpty()) {

            dbs.sort((o1, o2) -> o1.getKey().equals(o2.getKey()) ?
                                 o1.getValue().compareTo(o2.getValue()) :
                                 o1.getKey().compareTo(o2.getKey()));
            final int dbCount = dbs.size();
            for (int i = 0; i < dbCount; i++) {
                final HgPair<Long, Long> pair = dbs.get(i);
                String curDBName;
                if (pair.getKey() == -1L) {
                    curDBName = defaultName;
                } else if (pair.getValue() == -1L) {
                    curDBName = String.format("%s_%d", defaultName, pair.getKey());
                } else {
                    curDBName =
                            String.format("%s_%d_%d", defaultName, pair.getKey(), pair.getValue());
                }
                String curDBPath = Paths.get(parentFile.getPath(), curDBName).toString();
                if (i == dbCount - 1) {
                    latestDBPath = curDBPath;
                } else {
                    // delete old db, do not delete files in the deletion queue
                    if (!factory.findPathInRemovedList(curDBPath)) {
                        try {
                            FileUtils.deleteDirectory(new File(curDBPath));
                            log.info("delete old dbpath {}", curDBPath);
                        } catch (IOException e) {
                            log.error("fail to delete old dbpath {}", curDBPath, e);
                        }
                    }
                }
            }
        } else {
            latestDBPath = Paths.get(parentFile.getPath(), defaultName).toString();
        }
        if (factory.findPathInRemovedList(latestDBPath)) {
            // Has been deleted, create a new directory
            latestDBPath =
                    Paths.get(parentFile.getPath(), String.format("%s_%d", defaultName, version))
                         .toString();
        }
        //
        log.info("{} latest db path {}", this.graphName, latestDBPath);
        return latestDBPath;
    }