private static int getGreatestLowerBound(RetentionSet set, long value, Function<StreamCutReferenceRecord, Long> func) { return CollectionHelpers.findGreatestLowerBound(set.retentionRecords, x -> Long.compare(value, func.apply(x))); }
private CompletableFuture<Integer> searchEpochAtTime(int lowest, int highest, Predicate<Integer> ignoreCached, long timestamp) { final int middle = (lowest + highest) / 2; if (lowest > highest) { // either return epoch 0 or latest epoch return CompletableFuture.completedFuture(-1); } return getHistoryTimeSeriesChunk(middle, ignoreCached.test(middle)) .thenCompose(chunk -> { List<HistoryTimeSeriesRecord> historyRecords = chunk.getHistoryRecords(); long rangeLow = historyRecords.get(0).getScaleTime(); long rangeHigh = historyRecords.get(historyRecords.size() - 1).getScaleTime(); if (timestamp >= rangeLow && timestamp <= rangeHigh) { // found int index = CollectionHelpers.findGreatestLowerBound(historyRecords, x -> Long.compare(timestamp, x.getScaleTime())); assert index >= 0; return CompletableFuture.completedFuture(historyRecords.get(index).getEpoch()); } else if (timestamp < rangeLow) { return searchEpochAtTime(lowest, middle - 1, ignoreCached, timestamp); } else { return searchEpochAtTime(middle + 1, highest, ignoreCached, timestamp); } }); }
@Test public void testSearchGLB() { List<TestElement> list = Lists.newArrayList(new TestElement(10L), new TestElement(30L), new TestElement(75L), new TestElement(100L), new TestElement(152L), new TestElement(200L), new TestElement(400L), new TestElement(700L)); int index = CollectionHelpers.findGreatestLowerBound(list, x -> Long.compare(0L, x.getElement())); assertEquals(index, -1); index = CollectionHelpers.findGreatestLowerBound(list, x -> Long.compare(29, x.getElement())); assertEquals(index, 0); index = CollectionHelpers.findGreatestLowerBound(list, x -> Long.compare(100L, x.getElement())); assertEquals(index, 3); index = CollectionHelpers.findGreatestLowerBound(list, x -> Long.compare(Long.MAX_VALUE, x.getElement())); assertEquals(index, 7); }