private void cleanupRemovedIndices()
      throws IOException {
    File tmpIdxFile = new File(_segmentDirectory, V1Constants.INDEX_FILE_NAME + ".tmp");
    // Sort indices by column name and index type while copying, so that the
    // new index_map file is easy to inspect for troubleshooting.
    List<IndexEntry> retained = copyIndices(_indexFile, tmpIdxFile, _columnEntries);

    FileUtils.deleteQuietly(_indexFile);
    Preconditions
        .checkState(tmpIdxFile.renameTo(_indexFile), "Failed to rename temp index file: %s to original index file: %s",
            tmpIdxFile, _indexFile);

    File mapFile = new File(_segmentDirectory, V1Constants.INDEX_MAP_FILE_NAME);
    FileUtils.deleteQuietly(mapFile);
    try (PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(mapFile)))) {
      persistIndexMaps(retained, writer);
    }
  }
@Test
  public void testCleanupRemovedIndices()
      throws IOException, ConfigurationException {
    try (SingleFileIndexDirectory sfd = new SingleFileIndexDirectory(TEMP_DIR, _segmentMetadata, ReadMode.mmap)) {
      PinotDataBuffer buf = sfd.newBuffer("col1", StandardIndexes.forward(), 1024);
      buf.putInt(0, 1); // from begin position.

      buf = sfd.newBuffer("col1", StandardIndexes.dictionary(), 1024);
      buf.putChar(111, 'h');

      buf = sfd.newBuffer("col2", StandardIndexes.forward(), 1024);
      buf.putChar(222, 'w');

      buf = sfd.newBuffer("col1", StandardIndexes.json(), 1024);
      buf.putLong(333, 111111L);

      buf = sfd.newBuffer("col2", StandardIndexes.h3(), 1024);
      buf.putDouble(1016, 222.222); // touch end position.
    }

    // Remove the JSON index to trigger cleanup, but keep H3 index.
    try (SingleFileIndexDirectory sfd = new SingleFileIndexDirectory(TEMP_DIR, _segmentMetadata, ReadMode.mmap)) {
      assertTrue(sfd.hasIndexFor("col1", StandardIndexes.json()));
      sfd.removeIndex("col1", StandardIndexes.json());
    }

    // Read indices back and check the content.
    try (SingleFileIndexDirectory sfd = new SingleFileIndexDirectory(TEMP_DIR, _segmentMetadata, ReadMode.mmap)) {
      assertFalse(sfd.hasIndexFor("col1", StandardIndexes.json()));

      assertTrue(sfd.hasIndexFor("col1", StandardIndexes.forward()));
      PinotDataBuffer buf = sfd.getBuffer("col1", StandardIndexes.forward());
      assertEquals(buf.getInt(0), 1);

      assertTrue(sfd.hasIndexFor("col1", StandardIndexes.dictionary()));
      buf = sfd.getBuffer("col1", StandardIndexes.dictionary());
      assertEquals(buf.getChar(111), 'h');

      assertTrue(sfd.hasIndexFor("col2", StandardIndexes.forward()));
      buf = sfd.getBuffer("col2", StandardIndexes.forward());
      assertEquals(buf.getChar(222), 'w');

      assertTrue(sfd.hasIndexFor("col2", StandardIndexes.h3()));
      buf = sfd.getBuffer("col2", StandardIndexes.h3());
      assertEquals(buf.getDouble(1016), 222.222);
    }
  }