Compaction commonly merges more than two SSTables. A key may have several historical values, but only the value from the newest segment can survive. A newest tombstone deletes the key entirely.
Design an NWaySSTableCompactor class:
NWaySSTableCompactor() creates a stateless compactor.int[][] compact(int[][] entries) returns the resolved live rows sorted by key.
Each input row is [segmentIndex, key, value]. A larger segmentIndex is newer. For each key, keep the row with the largest segment index. If its value is -1, omit the key; otherwise return [key, value].
Example 1:
Input:
Output:
Explanation: For key 3, segment 1 is newer than segment 0, so value 99 wins.
Example 2:
Input:
Output:
Explanation: Key 5 has a newer tombstone, so the older live value cannot survive.
Constraints
0 <= entries.length <= 10^5entries[i].length == 30 <= segmentIndex <= 10^9- Each
(segmentIndex, key) pair occurs at most once. - A value of
-1 is a tombstone; all other keys and values fit in signed 32-bit integers. - At most
100 calls are made to compact.