@VisibleForTesting
  static List<IndexEntry> copyIndices(File srcFile, File destFile, TreeMap<IndexKey, IndexEntry> indicesToCopy)
      throws IOException {
    // Copy index from original index file and append to temp file.
    // Keep track of the index entry pointing to the temp index file.
    List<IndexEntry> retained = new ArrayList<>();
    long nextOffset = 0;
    // With FileChannel, we can seek to the data flexibly.
    try (FileChannel srcCh = new RandomAccessFile(srcFile, "r").getChannel();
        FileChannel dstCh = new RandomAccessFile(destFile, "rw").getChannel()) {
      for (IndexEntry index : indicesToCopy.values()) {
        org.apache.pinot.common.utils.FileUtils.transferBytes(srcCh, index._startOffset, index._size, dstCh);
        retained.add(new IndexEntry(index._key, nextOffset, index._size));
        nextOffset += index._size;
      }
    }
    return retained;
  }
@Test
  public void testCopyIndices()
      throws IOException {
    File srcTmp = new File(TEMP_DIR, UUID.randomUUID().toString());
    if (!srcTmp.exists()) {
      FileUtils.touch(srcTmp);
    }
    File dstTmp = new File(TEMP_DIR, UUID.randomUUID().toString());
    TreeMap<IndexKey, IndexEntry> indicesToCopy = new TreeMap<>(ImmutableMap
        .of(new IndexKey("foo", StandardIndexes.inverted()),
            new IndexEntry(new IndexKey("foo", StandardIndexes.inverted()), 0, 0),
            new IndexKey("foo", StandardIndexes.forward()),
            new IndexEntry(new IndexKey("foo", StandardIndexes.forward()), 0, 0),
            new IndexKey("bar", StandardIndexes.forward()),
            new IndexEntry(new IndexKey("bar", StandardIndexes.forward()), 0, 0),
            new IndexKey("bar", StandardIndexes.dictionary()),
            new IndexEntry(new IndexKey("bar", StandardIndexes.dictionary()), 0, 0),
            new IndexKey("bar", StandardIndexes.json()),
            new IndexEntry(new IndexKey("bar", StandardIndexes.json()), 0, 0)));
    List<IndexEntry> retained = SingleFileIndexDirectory.copyIndices(srcTmp, dstTmp, indicesToCopy);
    List<IndexKey> retainedKeys = retained.stream().map(e -> e._key).collect(Collectors.toList());
    // The returned entries are sorted.
    assertEquals(retainedKeys, Arrays
        .asList(new IndexKey("bar", StandardIndexes.dictionary()), new IndexKey("bar", StandardIndexes.forward()),
            new IndexKey("bar", StandardIndexes.json()), new IndexKey("foo", StandardIndexes.forward()),
            new IndexKey("foo", StandardIndexes.inverted())));
  }