class IndexDirectoryManager {

    public synchronized List<File> getDirectoriesBefore(final long timestamp) {
        final List<File> selected = new ArrayList<>();

        // An index cannot be expired if it is the latest index in the storage directory. As a result, we need to
        // separate the indexes by Storage Directory so that we can easily determine if this is the case.
        final Map<String, List<IndexLocation>> startTimeWithFileByStorageDirectory = flattenDirectoriesByTimestamp().stream()
            .collect(Collectors.groupingBy(indexLoc -> indexLoc.getPartitionName()));

        // Scan through the index directories and the associated index event start time.
        // If looking at index N, we can determine the index end time by assuming that it is the same as the
        // start time of index N+1. So we determine the time range of each index and select an index only if
        // its start time is before the given timestamp and its end time is <= the given timestamp.
        for (final List<IndexLocation> locationList : startTimeWithFileByStorageDirectory.values()) {
            for (int i = 0; i < locationList.size(); i++) {
                final IndexLocation indexLoc = locationList.get(i);

                final String partition = indexLoc.getPartitionName();
                final IndexLocation activeLocation = activeIndices.get(partition);
                if (indexLoc.equals(activeLocation)) {
                    continue;
                }

                final Long indexStartTime = indexLoc.getIndexStartTimestamp();
                if (indexStartTime > timestamp) {
                    // If the first timestamp in the index is later than the desired timestamp,
                    // then we are done. We can do this because the list is ordered by monotonically
                    // increasing timestamp as the Tuple key.
                    break;
                }

                final long indexEndTime = indexLoc.getIndexEndTimestamp();
                if (indexEndTime <= timestamp) {
                    logger.debug("Considering Index Location {} older than {} ({}) because its events have an EventTime "
                        + "ranging from {} ({}) to {} ({}) based on the following IndexLocations: {}", indexLoc, timestamp, new Date(timestamp),
                        indexStartTime, new Date(indexStartTime), indexEndTime, new Date(indexEndTime), locationList);

                    selected.add(indexLoc.getIndexDirectory());
                }
            }
        }

        logger.debug("Returning the following list of index locations because they were finished being written to before {}: {}", timestamp, selected);
        return selected;
    }

    public  IndexDirectoryManager(final RepositoryConfiguration repoConfig);

}