public static void sortedMerge(UTF8Row.Creator creator,
                                   DPSegment mergeSegment,
                                   List<? extends StorageSegment> segments) throws IOException {
        if (segments.isEmpty()) {
            return;
        }

        Iterator<Row>[] rowItrs = new Iterator[segments.size()];
        long totalRowCount = 0;
        for (int i = 0; i < segments.size(); i++) {
            Segment segment = segments.get(i);
            rowItrs[i] = segment.rowTraversal().iterator();
            totalRowCount += segment.rowCount();
        }
        SortedMergeUTF8RowItr sortedMergeItr = new SortedMergeUTF8RowItr(creator, rowItrs);
        UTF8Row last = null;
        long rowCount = 0;
        while (sortedMergeItr.hasNext()) {
            UTF8Row curRow = sortedMergeItr.next();
            rowCount++;
            if (last == null) {
                last = curRow;
            } else {
                assert comparator.compare(curRow, last) >= 0;
                if (comparator.compare(curRow, last) == 0) {
                    last.merge(curRow);
                    curRow.free();
                } else {
                    mergeSegment.add(last);
                    last.free();
                    last = curRow;
                }
            }
        }
        if (last != null) {
            mergeSegment.add(last);
            last.free();
            last = null;
        }

        long itrRowCount = sortedMergeItr.checkDone();
        Preconditions.checkState(rowCount == totalRowCount && itrRowCount == totalRowCount,
                "Row valueCount: %s, itr rowCount: %s, expected: %s not match",
                rowCount, itrRowCount, totalRowCount);
    }
Merge a list of dumped realtime segments.
indexr-segment/src/main/java/io/indexr/segment/rt/RTSMerge.java