@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 testWriteMoreThanOneRegionGroup() throws Exception {
        List<TestingFileDataIndexRegion> regions = createTestRegions(0, 0L, 2, 2);
        int regionGroupSize =
                regions.stream().mapToInt(TestingFileDataIndexRegion::getSize).sum() + 1;
        try (FileDataIndexSpilledRegionManager<TestingFileDataIndexRegion> spilledRegionManager =
                createSpilledRegionManager(regionGroupSize, (ignore1, ignore2) -> {})) {
            spilledRegionManager.appendOrOverwriteRegion(0, regions.get(0));
            spilledRegionManager.appendOrOverwriteRegion(0, regions.get(1));
            // region group has no enough space, will start new region group.
            TestingFileDataIndexRegion regionInNewRegionGroup = createSingleTestRegion(4, 4L, 2);
            spilledRegionManager.appendOrOverwriteRegion(0, regionInNewRegionGroup);
            FileChannel indexFileChannel = FileChannel.open(indexFilePath, StandardOpenOption.READ);
            TestingFileDataIndexRegion readRegion =
                    readRegionFromFile(
                            indexFileChannel,
                            // offset is region group size instead of two regions size to prove that
                            // new region group is started.
                            regionGroupSize);
            assertRegionEquals(readRegion, regionInNewRegionGroup);
        }
    }