dart-sdk
dart-sdk copied to clipboard
Explain method _updateDeletedRanges()
Trying to understand this method but have no clue. Could you explain the purpose of the method?
/// Calculate ranges of missing messages
void _updateDeletedRanges() {
var ranges = <DataMessage>[];
DataMessage prev;
// Check for gap in the beginning, before the first message.
var first = _messages.length > 0 ? _messages.getAt(0) : null;
if (first != null && _minSeq > 1 && !_noEarlierMsgs) {
// Some messages are missing in the beginning.
if (first.hi != null && (first.hi ?? 0) > 0) {
// The first message already represents a gap.
if ((first.seq ?? 0) > 1) {
first.seq = 1;
}
if ((first.hi ?? 0) < _minSeq - 1) {
first.hi = _minSeq - 1;
}
prev = first;
} else {
// Create new gap.
prev = DataMessage(seq: 1, hi: _minSeq - 1);
ranges.add(prev);
}
} else {
// No gap in the beginning.
prev = DataMessage(seq: 0, hi: 0);
}
// Find gaps in the list of received messages. The list contains messages-proper as well
// as placeholders for deleted ranges.
// The messages are iterated by seq ID in ascending order.
_messages.forEach((data, i) {
// Do not create a gap between the last sent message and the first unsent.
if (data.seq! >= _configService.appSettings.localSeqId) {
return;
}
// New message is reducing the existing gap
if (data.seq == ((prev.hi != null && prev.hi! > 0) ? prev.hi : prev.seq)! + 1) {
// No new gap. Replace previous with current.
prev = data;
return;
}
// Found a new gap.
if (prev.hi != null && prev.hi != 0) {
// Previous is also a gap, alter it.
prev.hi = data.hi! > 0 ? data.hi : data.seq;
return;
}
// Previous is not a gap. Create a new gap.
prev = DataMessage(
seq: (data.hi! > 0 ? data.hi! : data.seq)! + 1,
hi: data.hi! > 0 ? data.hi : data.seq,
);
ranges.add(prev);
}, null, null);
// Check for missing messages at the end.
// All messages could be missing or it could be a new topic with no messages.
var last = _messages.length > 0 ? _messages.getLast() : null;
var maxSeq = max(seq!, _maxSeq);
if ((maxSeq > 0 && last == null) || (last != null && (((last.hi != null && last.hi! > 0) ? last.hi : last.seq)! < maxSeq))) {
if (last != null && (last.hi != null && last.hi! > 0)) {
// Extend existing gap
last.hi = maxSeq;
} else {
// Create new gap.
ranges.add(DataMessage(seq: last != null ? last.seq! + 1 : 1, hi: maxSeq));
}
}
// Insert new gaps into cache.
ranges.map((gap) {
_messages.put([gap]);
});
}