@SuppressWarnings("squid:S1135")
  private boolean canAddToTaskResource(
      CrossCompactionTaskResource taskResource,
      TsFileResource unseqFile,
      List<TsFileResource> seqFiles,
      long memoryCost)
      throws IOException {
    if (memoryCost == -1) {
      // there is file been deleted during selection
      return false;
    }
    TsFileNameGenerator.TsFileName unseqFileName =
        TsFileNameGenerator.getTsFileName(unseqFile.getTsFile().getName());
    long targetCompactionFileSize = config.getTargetCompactionFileSize();
    // we add a hard limit for cross compaction that selected unseqFile should reach a certain size
    // or be compacted in inner
    // space at least once. This is used to make to improve the priority of inner compaction and
    // avoid too much cross compaction with small files.
    if (unseqFile.getTsFileSize() < targetCompactionFileSize
        && unseqFileName.getInnerCompactionCnt() < config.getMinCrossCompactionUnseqFileLevel()) {
      return false;
    }

    long totalFileSize = unseqFile.getTsFileSize();
    for (TsFileResource f : seqFiles) {
      if (f.getTsFileSize() >= targetCompactionFileSize * 1.5) {
        // to avoid serious write amplification caused by cross space compaction, we restrict that
        // seq files are no longer be compacted when the size reaches the threshold.
        return false;
      }
      totalFileSize += f.getTsFileSize();
    }

    // currently, we must allow at least one unseqFile be selected to handle the situation that
    // an unseqFile has huge time range but few data points.
    // IMPORTANT: this logic is opposite to previous level control
    if (taskResource.getUnseqFiles().isEmpty()) {
      return true;
    }

    return taskResource.getTotalFileNums() + 1 + seqFiles.size() <= maxCrossCompactionFileNum
        && taskResource.getTotalFileSize() + totalFileSize <= maxCrossCompactionFileSize
        && memoryCost < memoryBudget;
  }
In a preset time (30 seconds), for each unseqFile, find the list of seqFiles that overlap with
it and have not been selected by the file selector of this compaction task. After finding each
unseqFile and its corresponding overlap seqFile list, estimate the additional memory overhead
that may be added by compacting them (preferably using the loop estimate), and if it does not
exceed the memory overhead preset by the system for the compaction thread, put them into the
selectedSeqFiles and selectedUnseqFiles.

@throws IOException in prepare next split
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/compaction/selector/impl/RewriteCrossSpaceCompactionSelector.java