presto-hive/src/main/java/io/prestosql/plugin/hive/util/SortBuffer.java/SortBuffer/flushTo
class SortBuffer:    public void flushTo(Consumer<Page> consumer)
    {
        checkState(!pages.isEmpty(), "page buffer is empty");

        long[] addresses = pageSorter.sort(types, pages, sortFields, sortOrders, rowCount);

        int[] pageIndex = new int[addresses.length];
        int[] positionIndex = new int[addresses.length];
        for (int i = 0; i < addresses.length; i++) {
            pageIndex[i] = pageSorter.decodePageIndex(addresses[i]);
            positionIndex[i] = pageSorter.decodePositionIndex(addresses[i]);
        }

        verify(pageBuilder.isEmpty());

        for (int i = 0; i < pageIndex.length; i++) {
            Page page = pages.get(pageIndex[i]);
            int position = positionIndex[i];
            appendPositionTo(page, position, pageBuilder);

            if (pageBuilder.isFull()) {
                consumer.accept(pageBuilder.build());
                pageBuilder.reset();
            }
        }

        if (!pageBuilder.isEmpty()) {
            consumer.accept(pageBuilder.build());
            pageBuilder.reset();
        }

        pages.clear();
        rowCount = 0;
        usedMemoryBytes = 0;
    }