ThirdEyeQueryExecutor { public List<UUID> selectTreesToQuery(final Map<UUID, IndexMetadata> treeMetadataMap, final TimeRange queryTimeRange) { List<UUID> treeIds = new ArrayList<>(); for (UUID treeId : treeMetadataMap.keySet()) { IndexMetadata indexMetadata = treeMetadataMap.get(treeId); TimeRange treeTimeRange = new TimeRange(indexMetadata.getMinDataTimeMillis(), indexMetadata.getMaxDataTimeMillis()); TimeRange wallClockRange = new TimeRange(indexMetadata.getStartTimeMillis(), indexMetadata.getEndTimeMillis()); if ((!queryTimeRange.isDisjoint(treeTimeRange) || !queryTimeRange.isDisjoint(wallClockRange)) && !treeIds.contains(treeId)) { treeIds.add(treeId); } } Comparator<? super UUID> comparator = new Comparator<UUID>() { @Override public int compare(UUID treeId1, UUID treeId2) { IndexMetadata indexMetadata1 = treeMetadataMap.get(treeId1); IndexMetadata indexMetadata2 = treeMetadataMap.get(treeId2); Long startTime1 = indexMetadata1.getStartTimeMillis(); Long startTime2 = indexMetadata2.getStartTimeMillis(); int ret = startTime1.compareTo(startTime2); if (ret == 0) { Integer timeGranularity1 = timeGranularitySortOrder.get(indexMetadata1.getTimeGranularity().toUpperCase()); Integer timeGranularity2 = timeGranularitySortOrder.get(indexMetadata2.getTimeGranularity().toUpperCase()); ret = timeGranularity1.compareTo(timeGranularity2); } return ret; } }; Collections.sort(treeIds, comparator); List<UUID> treeIdsToQuery = new ArrayList<>(); TimeRange lastSelectedRange = null; for (UUID treeId : treeIds) { IndexMetadata indexMetadata = treeMetadataMap.get(treeId); TimeRange treeRange = new TimeRange(indexMetadata.getStartTimeMillis(), indexMetadata.getEndTimeMillis()); if (lastSelectedRange == null || lastSelectedRange.getEnd() <= treeRange.getStart()) { lastSelectedRange = treeRange; treeIdsToQuery.add(treeId); LOGGER.info("Selecting treeId:{} with TimeRange:{}", treeId, treeRange); } } return treeIdsToQuery; } ThirdEyeQueryExecutor(ExecutorService executorService, StarTreeManager starTreeManager); ThirdEyeQueryResult executeQuery(String sql); ThirdEyeQueryResult executeQuery(final ThirdEyeQuery query); List<UUID> selectTreesToQuery(final Map<UUID, IndexMetadata> treeMetadataMap,
                                       final TimeRange queryTimeRange);  }
@Test public void testSelectTreeForQueryTimeRange_alignedDataTime() throws Exception { Object[][] indexMetadata = { { UUID.randomUUID(), createIndexMetadata("2014-10TZ", "2014-11TZ", "2014-10TZ", "2014-11TZ", "MONTHLY") }, { UUID.randomUUID(), createIndexMetadata("2014-11TZ", "2014-12TZ", "2014-11TZ", "2014-12TZ", "MONTHLY") }, { UUID.randomUUID(), createIndexMetadata("2014-12TZ", "2015-01TZ", "2014-12TZ", "2015-01TZ", "MONTHLY") }, { UUID.randomUUID(), createIndexMetadata("2014-12-24TZ", "2014-12-25TZ", "2014-12-24TZ", "2014-12-25TZ", "DAILY") }, { UUID.randomUUID(), createIndexMetadata("2014-12-25TZ", "2014-12-26TZ", "2014-12-25TZ", "2014-12-26TZ", "DAILY") }, { UUID.randomUUID(), createIndexMetadata("2014-12-26TZ", "2014-12-27TZ", "2014-12-26TZ", "2014-12-27TZ", "DAILY") }, { UUID.randomUUID(), createIndexMetadata("2014-12-27TZ", "2014-12-28TZ", "2014-12-27TZ", "2014-12-28TZ", "DAILY") }, { UUID.randomUUID(), createIndexMetadata("2014-12-28TZ", "2014-12-29TZ", "2014-12-28TZ", "2014-12-29TZ", "DAILY") }, { UUID.randomUUID(), createIndexMetadata("2014-12-29TZ", "2014-12-30TZ", "2014-12-29TZ", "2014-12-30TZ", "DAILY") }, { UUID.randomUUID(), createIndexMetadata("2014-12-30TZ", "2014-12-31TZ", "2014-12-30TZ", "2014-12-31TZ", "DAILY") }, { UUID.randomUUID(), createIndexMetadata("2015-01-01TZ", "2015-01-02TZ", "2015-01-01TZ", "2015-01-02TZ", "DAILY") }, { UUID.randomUUID(), createIndexMetadata("2015-01-02TZ", "2015-01-03TZ", "2015-01-02TZ", "2015-01-03TZ", "DAILY") }, { UUID.randomUUID(), createIndexMetadata("2015-01-03TZ", "2015-01-04TZ", "2015-01-03TZ", "2015-01-04TZ", "DAILY") }, { UUID.randomUUID(), createIndexMetadata("2015-01-04TZ", "2015-01-05TZ", "2015-01-04TZ", "2015-01-05TZ", "DAILY") }, { UUID.randomUUID(), createIndexMetadata("2015-01-05TZ", "2015-01-06TZ", "2015-01-05TZ", "2015-01-06TZ", "DAILY") }, { UUID.randomUUID(), createIndexMetadata("2015-01-06TZ", "2015-01-07TZ", "2015-01-06TZ", "2015-01-07TZ", "DAILY") }, }; Map<UUID, IndexMetadata> indexMetadataMap = new HashMap<>(); for (Object[] datum : indexMetadata) { indexMetadataMap.put((UUID) datum[0], (IndexMetadata) datum[1]); } DateTime queryStartTime = ISODateTimeFormat.dateTimeParser().parseDateTime("2014-12-02TZ").toDateTime(DateTimeZone.UTC); DateTime queryEndTime = ISODateTimeFormat.dateTimeParser().parseDateTime("2015-01-04TZ").toDateTime(DateTimeZone.UTC); TimeRange queryTimeRange = new TimeRange(queryStartTime.getMillis(), queryEndTime.getMillis()); List<UUID> ids = queryExecutor.selectTreesToQuery(indexMetadataMap, queryTimeRange); Assert.assertEquals(ids, ImmutableList.of( (UUID) indexMetadata[2][0], (UUID) indexMetadata[10][0], (UUID) indexMetadata[11][0], (UUID) indexMetadata[12][0], (UUID) indexMetadata[13][0] )); }