use of com.netflix.titus.grpc.protogen.Pagination in project titus-control-plane by Netflix.
the class PageAggregationUtil method takeCombinedPage.
static <T> Pair<List<T>, Pagination> takeCombinedPage(Page requested, List<T> combinedItems, Pagination combinedPagination, Comparator<T> cursorComparator, Function<T, String> cursorFactory) {
List<T> sorted = combinedItems.stream().sorted(cursorComparator).collect(Collectors.toList());
int lastItemOffset = Math.min(sorted.size(), requested.getPageSize());
List<T> pageItems = sorted.subList(0, lastItemOffset);
String cursor = sorted.isEmpty() ? "" : cursorFactory.apply(pageItems.get(pageItems.size() - 1));
// first item position relative to totalItems from all Cells
int firstItemPosition = Math.max(0, combinedPagination.getCursorPosition() - (sorted.size() - 1));
int pageNumber = firstItemPosition / requested.getPageSize();
Pagination finalPagination = Pagination.newBuilder(combinedPagination).setCurrentPage(Page.newBuilder(requested).setPageNumber(pageNumber)).setCursor(cursor).setCursorPosition(firstItemPosition + lastItemOffset - 1).setHasMore(combinedPagination.getHasMore() || lastItemOffset < sorted.size()).build();
return Pair.of(pageItems, finalPagination);
}
use of com.netflix.titus.grpc.protogen.Pagination in project titus-control-plane by Netflix.
the class GatewayJobServiceGatewayTest method testCombineActiveAndArchiveTaskResultSet.
private void testCombineActiveAndArchiveTaskResultSet(Function<Pagination, TaskQuery> queryFunction, Function<Integer, TaskQueryResult> activePageResultFunction) {
// First page
TaskQueryResult combinedResult = testCombineForFirstPage();
// Iterate using page numbers
// Archive tasks first.
Pagination lastPagination = combinedResult.getPagination();
for (int p = 1; p < ARCHIVED_TASKS_COUNT / PAGE_SIZE; p++) {
TaskQuery cursorQuery = queryFunction.apply(lastPagination);
TaskQueryResult cursorResult = GatewayJobServiceGateway.combineTaskResults(cursorQuery, activePageResultFunction.apply(0), ARCHIVED_TASKS);
checkCombinedResult(cursorResult, ARCHIVED_TASKS.subList(p * PAGE_SIZE, (p + 1) * PAGE_SIZE));
lastPagination = cursorResult.getPagination();
}
// Now fetch the active data
for (int p = 0; p < JOB_SIZE / PAGE_SIZE; p++) {
TaskQuery cursorQuery = queryFunction.apply(lastPagination);
TaskQueryResult cursorResult = GatewayJobServiceGateway.combineTaskResults(cursorQuery, activePageResultFunction.apply(p), ARCHIVED_TASKS);
checkCombinedResult(cursorResult, ACTIVE_TASKS.subList(p * PAGE_SIZE, (p + 1) * PAGE_SIZE));
lastPagination = cursorResult.getPagination();
}
}
Aggregations