@Override
    public void appendOrOverwriteRegion(int subpartition, T newRegion) throws IOException {
        // This method will only be called when we want to eliminate a region. We can't let the
        // region be reloaded into the cache, otherwise it will lead to an infinite loop.
        long oldRegionOffset = findRegion(subpartition, newRegion.getFirstBufferIndex(), false);
        if (oldRegionOffset != -1) {
            // if region is already exists in file, overwrite it.
            writeRegionToOffset(oldRegionOffset, newRegion);
        } else {
            // otherwise, append region to region group.
            appendRegion(subpartition, newRegion);
        }
    }
@Test
    void testAppendOrOverwriteRegion() throws Exception {
        CompletableFuture<Void> cachedRegionFuture = new CompletableFuture<>();
        try (FileDataIndexSpilledRegionManager<TestingFileDataIndexRegion> spilledRegionManager =
                createSpilledRegionManager(
                        (ignore1, ignore2) -> cachedRegionFuture.complete(null))) {
            TestingFileDataIndexRegion region = createSingleTestRegion(0, 0L, 1);
            // append region to index file.
            spilledRegionManager.appendOrOverwriteRegion(0, region);
            assertThat(cachedRegionFuture).isNotCompleted();
            FileChannel indexFileChannel = FileChannel.open(indexFilePath, StandardOpenOption.READ);
            TestingFileDataIndexRegion readRegion = readRegionFromFile(indexFileChannel, 0L);
            assertRegionEquals(readRegion, region);

            // new region must have the same size of old region.
            TestingFileDataIndexRegion newRegion = createSingleTestRegion(0, 10L, 1);
            // overwrite old region.
            spilledRegionManager.appendOrOverwriteRegion(0, newRegion);
            // appendOrOverwriteRegion will not trigger cache load.
            assertThat(cachedRegionFuture).isNotCompleted();
            TestingFileDataIndexRegion readNewRegion = readRegionFromFile(indexFileChannel, 0L);
            assertRegionEquals(readNewRegion, newRegion);
        }
    }