in iotdb-collector/collector-core/src/main/java/org/apache/iotdb/collector/utils/sorter/PipeTableModelTabletEventSorter.java [58:139]
public void sortAndDeduplicateByDevIdTimestamp() {
if (tablet == null || tablet.getRowSize() < 1) {
return;
}
HashMap<IDeviceID, List<Pair<Integer, Integer>>> deviceIDToIndexMap = new HashMap<>();
final long[] timestamps = tablet.getTimestamps();
IDeviceID lastDevice = tablet.getDeviceID(0);
long previousTimestamp = tablet.getTimestamp(0);
int lasIndex = 0;
for (int i = 1, size = tablet.getRowSize(); i < size; ++i) {
final IDeviceID deviceID = tablet.getDeviceID(i);
final long currentTimestamp = timestamps[i];
final int deviceComparison = deviceID.compareTo(lastDevice);
if (deviceComparison == 0) {
if (previousTimestamp == currentTimestamp) {
hasDuplicates = true;
continue;
}
if (previousTimestamp > currentTimestamp) {
isUnSorted = true;
}
previousTimestamp = currentTimestamp;
continue;
}
if (deviceComparison < 0) {
isUnSorted = true;
}
List<Pair<Integer, Integer>> list =
deviceIDToIndexMap.computeIfAbsent(lastDevice, k -> new ArrayList<>());
if (!list.isEmpty()) {
isUnSorted = true;
}
list.add(new Pair<>(lasIndex, i));
lastDevice = deviceID;
lasIndex = i;
previousTimestamp = currentTimestamp;
}
List<Pair<Integer, Integer>> list =
deviceIDToIndexMap.computeIfAbsent(lastDevice, k -> new ArrayList<>());
if (!list.isEmpty()) {
isUnSorted = true;
}
list.add(new Pair<>(lasIndex, tablet.getRowSize()));
if (!isUnSorted && !hasDuplicates) {
return;
}
initIndexSize = 0;
deduplicatedSize = 0;
index = new Integer[tablet.getRowSize()];
deviceIDToIndexMap.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.forEach(
entry -> {
final int start = initIndexSize;
int i = initIndexSize;
for (Pair<Integer, Integer> pair : entry.getValue()) {
for (int j = pair.left; j < pair.right; j++) {
index[i++] = j;
}
}
if (isUnSorted) {
sortTimestamps(start, i);
deduplicateTimestamps(start, i);
initIndexSize = i;
return;
}
if (hasDuplicates) {
deduplicateTimestamps(start, i);
}
initIndexSize = i;
});
sortAndDeduplicateValuesAndBitMaps();
}