void restore() throws IOException {
    if (!directory.exists()) {
      return;
    }
    File[] ratioFiles = directory.listFiles((dir, name) -> name.startsWith(FILE_PREFIX));
    // First try to recover from the new version of the file, parse the file name, and get the file
    // with the largest disk size value
    if (ratioFiles != null && ratioFiles.length > 0) {
      int maxRatioIndex = 0;
      for (int i = 0; i < ratioFiles.length; i++) {
        String[] fileNameArray = ratioFiles[i].getName().split("-");
        // fileNameArray.length != 3 means the compression ratio may be negative, ignore it
        if (fileNameArray.length == 3) {
          try {
            long diskSize = Long.parseLong(fileNameArray[2]);
            if (diskSize > totalDiskSize) {
              totalMemorySize = new AtomicLong(Long.parseLong(fileNameArray[1]));
              totalDiskSize = diskSize;
              maxRatioIndex = i;
            }
          } catch (NumberFormatException ignore) {
            // ignore illegal compression file name
          }
        }
      }
      LOGGER.debug(
          "After restoring from compression ratio file, total memory size = {}, total disk size = {}",
          totalMemorySize,
          totalDiskSize);
      oldFileName = ratioFiles[maxRatioIndex].getName();
      deleteRedundantFilesByIndex(ratioFiles, maxRatioIndex);
    } else { // If there is no new file, try to restore from the old version file
      File[] ratioFilesBeforeV121 =
          directory.listFiles((dir, name) -> name.startsWith(FILE_PREFIX_BEFORE_V121));
      if (ratioFilesBeforeV121 != null && ratioFilesBeforeV121.length > 0) {
        int maxRatioIndex = 0;
        totalDiskSize = 1;
        for (int i = 0; i < ratioFilesBeforeV121.length; i++) {
          String[] fileNameArray = ratioFilesBeforeV121[i].getName().split("-");
          // fileNameArray.length != 3 means the compression ratio may be negative, ignore it
          if (fileNameArray.length == 3) {
            try {
              double currentCompressRatio =
                  Double.parseDouble(fileNameArray[1]) / Double.parseDouble(fileNameArray[2]);
              if (getRatio() < currentCompressRatio) {
                totalMemorySize = new AtomicLong((long) currentCompressRatio);
                maxRatioIndex = i;
              }
            } catch (NumberFormatException ignore) {
              // ignore illegal compression file name
            }
          }
        }
        deleteRedundantFilesByIndex(ratioFilesBeforeV121, maxRatioIndex);
      }
    }
  }
Restore compression ratio statistics from disk when system restart
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/flush/CompressionRatio.java