2017-10-29 15:32:01 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2017-10-29 15:32:01 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2017-10-29 15:32:01 +00:00
|
|
|
*/
|
2017-11-06 07:31:18 +00:00
|
|
|
#include "data/data_sparse_ids.h"
|
2017-10-29 15:32:01 +00:00
|
|
|
|
2017-10-30 19:24:20 +00:00
|
|
|
#include <rpl/combine.h>
|
2017-10-29 15:32:01 +00:00
|
|
|
#include "storage/storage_sparse_ids_list.h"
|
|
|
|
|
|
|
|
SparseIdsMergedSlice::SparseIdsMergedSlice(Key key)
|
|
|
|
: SparseIdsMergedSlice(
|
|
|
|
key,
|
|
|
|
SparseIdsSlice(),
|
|
|
|
MigratedSlice(key)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
SparseIdsMergedSlice::SparseIdsMergedSlice(
|
|
|
|
Key key,
|
|
|
|
SparseIdsSlice part,
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<SparseIdsSlice> migrated)
|
2017-10-29 15:32:01 +00:00
|
|
|
: _key(key)
|
|
|
|
, _part(std::move(part))
|
|
|
|
, _migrated(std::move(migrated)) {
|
|
|
|
}
|
|
|
|
|
2021-06-29 14:40:55 +00:00
|
|
|
SparseIdsMergedSlice::SparseIdsMergedSlice(
|
|
|
|
Key key,
|
|
|
|
SparseUnsortedIdsSlice scheduled)
|
|
|
|
: _key(key)
|
|
|
|
, _scheduled(std::move(scheduled)) {
|
|
|
|
}
|
|
|
|
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<int> SparseIdsMergedSlice::fullCount() const {
|
2021-06-29 14:40:55 +00:00
|
|
|
return _scheduled
|
|
|
|
? _scheduled->fullCount()
|
|
|
|
: Add(
|
|
|
|
_part.fullCount(),
|
|
|
|
_migrated ? _migrated->fullCount() : 0);
|
2017-10-29 15:32:01 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<int> SparseIdsMergedSlice::skippedBefore() const {
|
2021-06-29 14:40:55 +00:00
|
|
|
return _scheduled
|
|
|
|
? _scheduled->skippedBefore()
|
|
|
|
: Add(
|
|
|
|
isolatedInMigrated() ? 0 : _part.skippedBefore(),
|
|
|
|
_migrated
|
|
|
|
? (isolatedInPart()
|
|
|
|
? _migrated->fullCount()
|
|
|
|
: _migrated->skippedBefore())
|
|
|
|
: 0
|
|
|
|
);
|
2017-10-29 15:32:01 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<int> SparseIdsMergedSlice::skippedAfter() const {
|
2021-06-29 14:40:55 +00:00
|
|
|
return _scheduled
|
|
|
|
? _scheduled->skippedAfter()
|
|
|
|
: Add(
|
|
|
|
isolatedInMigrated() ? _part.fullCount() : _part.skippedAfter(),
|
|
|
|
isolatedInPart() ? 0 : _migrated->skippedAfter()
|
|
|
|
);
|
2017-10-29 15:32:01 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<int> SparseIdsMergedSlice::indexOf(
|
2017-10-29 15:32:01 +00:00
|
|
|
FullMsgId fullId) const {
|
2021-06-29 14:40:55 +00:00
|
|
|
return _scheduled
|
|
|
|
? _scheduled->indexOf(fullId.msg)
|
|
|
|
: isFromPart(fullId)
|
2017-10-29 15:32:01 +00:00
|
|
|
? (_part.indexOf(fullId.msg) | func::add(migratedSize()))
|
|
|
|
: isolatedInPart()
|
2018-09-21 16:28:46 +00:00
|
|
|
? std::nullopt
|
2017-10-29 15:32:01 +00:00
|
|
|
: isFromMigrated(fullId)
|
|
|
|
? _migrated->indexOf(fullId.msg)
|
2018-09-21 16:28:46 +00:00
|
|
|
: std::nullopt;
|
2017-10-29 15:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int SparseIdsMergedSlice::size() const {
|
2021-06-29 14:40:55 +00:00
|
|
|
return _scheduled
|
|
|
|
? _scheduled->size()
|
|
|
|
: (isolatedInPart() ? 0 : migratedSize())
|
|
|
|
+ (isolatedInMigrated() ? 0 : _part.size());
|
2017-10-29 15:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FullMsgId SparseIdsMergedSlice::operator[](int index) const {
|
|
|
|
Expects(index >= 0 && index < size());
|
|
|
|
|
2021-06-29 14:40:55 +00:00
|
|
|
if (_scheduled) {
|
|
|
|
return ComputeId(_key.peerId, (*_scheduled)[index]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const auto size = migratedSize()) {
|
2017-10-29 15:32:01 +00:00
|
|
|
if (index < size) {
|
|
|
|
return ComputeId(_key.migratedPeerId, (*_migrated)[index]);
|
|
|
|
}
|
|
|
|
index -= size;
|
|
|
|
}
|
|
|
|
return ComputeId(_key.peerId, _part[index]);
|
|
|
|
}
|
|
|
|
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<int> SparseIdsMergedSlice::distance(
|
2017-10-29 15:32:01 +00:00
|
|
|
const Key &a,
|
|
|
|
const Key &b) const {
|
2021-06-29 14:40:55 +00:00
|
|
|
if (const auto i = indexOf(ComputeId(a))) {
|
|
|
|
if (const auto j = indexOf(ComputeId(b))) {
|
2017-10-29 15:32:01 +00:00
|
|
|
return *j - *i;
|
|
|
|
}
|
|
|
|
}
|
2018-09-21 16:28:46 +00:00
|
|
|
return std::nullopt;
|
2017-10-29 15:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto SparseIdsMergedSlice::nearest(
|
2018-09-21 16:28:46 +00:00
|
|
|
UniversalMsgId id) const -> std::optional<FullMsgId> {
|
2021-06-29 14:40:55 +00:00
|
|
|
if (_scheduled) {
|
|
|
|
if (const auto nearestId = _scheduled->nearest(id)) {
|
|
|
|
return ComputeId(_key.peerId, *nearestId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const auto convertFromPartNearest = [&](MsgId result) {
|
2017-11-12 14:35:20 +00:00
|
|
|
return ComputeId(_key.peerId, result);
|
|
|
|
};
|
2021-06-29 14:40:55 +00:00
|
|
|
const auto convertFromMigratedNearest = [&](MsgId result) {
|
2017-11-12 14:35:20 +00:00
|
|
|
return ComputeId(_key.migratedPeerId, result);
|
2017-10-29 15:32:01 +00:00
|
|
|
};
|
|
|
|
if (IsServerMsgId(id)) {
|
|
|
|
if (auto partNearestId = _part.nearest(id)) {
|
2017-11-12 14:35:20 +00:00
|
|
|
return partNearestId
|
|
|
|
| convertFromPartNearest;
|
2017-10-29 15:32:01 +00:00
|
|
|
} else if (isolatedInPart()) {
|
2018-09-21 16:28:46 +00:00
|
|
|
return std::nullopt;
|
2017-10-29 15:32:01 +00:00
|
|
|
}
|
|
|
|
return _migrated->nearest(ServerMaxMsgId - 1)
|
|
|
|
| convertFromMigratedNearest;
|
|
|
|
}
|
|
|
|
if (auto migratedNearestId = _migrated
|
|
|
|
? _migrated->nearest(id + ServerMaxMsgId)
|
2018-09-21 16:28:46 +00:00
|
|
|
: std::nullopt) {
|
2017-10-29 15:32:01 +00:00
|
|
|
return migratedNearestId
|
|
|
|
| convertFromMigratedNearest;
|
|
|
|
} else if (isolatedInMigrated()) {
|
2018-09-21 16:28:46 +00:00
|
|
|
return std::nullopt;
|
2017-10-29 15:32:01 +00:00
|
|
|
}
|
2017-11-12 14:35:20 +00:00
|
|
|
return _part.nearest(0)
|
|
|
|
| convertFromPartNearest;
|
2017-10-29 15:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SparseIdsSliceBuilder::SparseIdsSliceBuilder(
|
|
|
|
Key key,
|
|
|
|
int limitBefore,
|
|
|
|
int limitAfter)
|
|
|
|
: _key(key)
|
|
|
|
, _limitBefore(limitBefore)
|
|
|
|
, _limitAfter(limitAfter) {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SparseIdsSliceBuilder::applyInitial(
|
|
|
|
const Storage::SparseIdsListResult &result) {
|
|
|
|
mergeSliceData(
|
|
|
|
result.count,
|
|
|
|
result.messageIds,
|
|
|
|
result.skippedBefore,
|
|
|
|
result.skippedAfter);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SparseIdsSliceBuilder::applyUpdate(
|
|
|
|
const Storage::SparseIdsSliceUpdate &update) {
|
|
|
|
auto intersects = [](MsgRange range1, MsgRange range2) {
|
|
|
|
return (range1.from <= range2.till)
|
|
|
|
&& (range2.from <= range1.till);
|
|
|
|
};
|
|
|
|
auto needMergeMessages = (update.messages != nullptr)
|
|
|
|
&& intersects(update.range, {
|
|
|
|
_ids.empty() ? _key : _ids.front(),
|
|
|
|
_ids.empty() ? _key : _ids.back()
|
|
|
|
});
|
|
|
|
if (!needMergeMessages && !update.count) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto skippedBefore = (update.range.from == 0)
|
|
|
|
? 0
|
2018-09-21 16:28:46 +00:00
|
|
|
: std::optional<int> {};
|
2017-10-29 15:32:01 +00:00
|
|
|
auto skippedAfter = (update.range.till == ServerMaxMsgId)
|
|
|
|
? 0
|
2018-09-21 16:28:46 +00:00
|
|
|
: std::optional<int> {};
|
2017-10-29 15:32:01 +00:00
|
|
|
mergeSliceData(
|
|
|
|
update.count,
|
|
|
|
needMergeMessages
|
|
|
|
? *update.messages
|
|
|
|
: base::flat_set<MsgId> {},
|
|
|
|
skippedBefore,
|
|
|
|
skippedAfter);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SparseIdsSliceBuilder::removeOne(MsgId messageId) {
|
|
|
|
auto changed = false;
|
|
|
|
if (_fullCount && *_fullCount > 0) {
|
|
|
|
--*_fullCount;
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
if (_ids.contains(messageId)) {
|
|
|
|
_ids.remove(messageId);
|
|
|
|
changed = true;
|
|
|
|
} else if (!_ids.empty()) {
|
|
|
|
if (_ids.front() > messageId
|
|
|
|
&& _skippedBefore
|
|
|
|
&& *_skippedBefore > 0) {
|
|
|
|
--*_skippedBefore;
|
|
|
|
changed = true;
|
|
|
|
} else if (_ids.back() < messageId
|
|
|
|
&& _skippedAfter
|
|
|
|
&& *_skippedAfter > 0) {
|
|
|
|
--*_skippedAfter;
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
2018-02-05 20:19:51 +00:00
|
|
|
if (changed) {
|
|
|
|
checkInsufficient();
|
|
|
|
}
|
2017-10-29 15:32:01 +00:00
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SparseIdsSliceBuilder::removeAll() {
|
|
|
|
_ids = {};
|
|
|
|
_fullCount = 0;
|
|
|
|
_skippedBefore = 0;
|
|
|
|
_skippedAfter = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-05 14:37:22 +00:00
|
|
|
bool SparseIdsSliceBuilder::invalidateBottom() {
|
2018-09-21 16:28:46 +00:00
|
|
|
_fullCount = _skippedAfter = std::nullopt;
|
2018-02-05 14:37:22 +00:00
|
|
|
checkInsufficient();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-29 15:32:01 +00:00
|
|
|
void SparseIdsSliceBuilder::checkInsufficient() {
|
|
|
|
sliceToLimits();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SparseIdsSliceBuilder::mergeSliceData(
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<int> count,
|
2017-10-29 15:32:01 +00:00
|
|
|
const base::flat_set<MsgId> &messageIds,
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<int> skippedBefore,
|
|
|
|
std::optional<int> skippedAfter) {
|
2017-10-29 15:32:01 +00:00
|
|
|
if (messageIds.empty()) {
|
|
|
|
if (count && _fullCount != count) {
|
|
|
|
_fullCount = count;
|
|
|
|
if (*_fullCount <= _ids.size()) {
|
|
|
|
_fullCount = _ids.size();
|
|
|
|
_skippedBefore = _skippedAfter = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fillSkippedAndSliceToLimits();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (count) {
|
|
|
|
_fullCount = count;
|
|
|
|
}
|
|
|
|
auto wasMinId = _ids.empty() ? -1 : _ids.front();
|
|
|
|
auto wasMaxId = _ids.empty() ? -1 : _ids.back();
|
|
|
|
_ids.merge(messageIds.begin(), messageIds.end());
|
|
|
|
|
|
|
|
auto adjustSkippedBefore = [&](MsgId oldId, int oldSkippedBefore) {
|
|
|
|
auto it = _ids.find(oldId);
|
|
|
|
Assert(it != _ids.end());
|
|
|
|
_skippedBefore = oldSkippedBefore - (it - _ids.begin());
|
|
|
|
accumulate_max(*_skippedBefore, 0);
|
|
|
|
};
|
|
|
|
if (skippedBefore) {
|
|
|
|
adjustSkippedBefore(messageIds.front(), *skippedBefore);
|
|
|
|
} else if (wasMinId >= 0 && _skippedBefore) {
|
|
|
|
adjustSkippedBefore(wasMinId, *_skippedBefore);
|
|
|
|
} else {
|
2018-09-21 16:28:46 +00:00
|
|
|
_skippedBefore = std::nullopt;
|
2017-10-29 15:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto adjustSkippedAfter = [&](MsgId oldId, int oldSkippedAfter) {
|
|
|
|
auto it = _ids.find(oldId);
|
|
|
|
Assert(it != _ids.end());
|
|
|
|
_skippedAfter = oldSkippedAfter - (_ids.end() - it - 1);
|
|
|
|
accumulate_max(*_skippedAfter, 0);
|
|
|
|
};
|
|
|
|
if (skippedAfter) {
|
|
|
|
adjustSkippedAfter(messageIds.back(), *skippedAfter);
|
|
|
|
} else if (wasMaxId >= 0 && _skippedAfter) {
|
|
|
|
adjustSkippedAfter(wasMaxId, *_skippedAfter);
|
|
|
|
} else {
|
2018-09-21 16:28:46 +00:00
|
|
|
_skippedAfter = std::nullopt;
|
2017-10-29 15:32:01 +00:00
|
|
|
}
|
|
|
|
fillSkippedAndSliceToLimits();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SparseIdsSliceBuilder::fillSkippedAndSliceToLimits() {
|
|
|
|
if (_fullCount) {
|
|
|
|
if (_skippedBefore && !_skippedAfter) {
|
|
|
|
_skippedAfter = *_fullCount
|
|
|
|
- *_skippedBefore
|
|
|
|
- int(_ids.size());
|
|
|
|
} else if (_skippedAfter && !_skippedBefore) {
|
|
|
|
_skippedBefore = *_fullCount
|
|
|
|
- *_skippedAfter
|
|
|
|
- int(_ids.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sliceToLimits();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SparseIdsSliceBuilder::sliceToLimits() {
|
|
|
|
if (!_key) {
|
|
|
|
if (!_fullCount) {
|
|
|
|
requestMessagesCount();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto requestedSomething = false;
|
2017-11-20 12:23:20 +00:00
|
|
|
auto aroundIt = ranges::lower_bound(_ids, _key);
|
2017-10-29 15:32:01 +00:00
|
|
|
auto removeFromBegin = (aroundIt - _ids.begin() - _limitBefore);
|
|
|
|
auto removeFromEnd = (_ids.end() - aroundIt - _limitAfter - 1);
|
|
|
|
if (removeFromBegin > 0) {
|
|
|
|
_ids.erase(_ids.begin(), _ids.begin() + removeFromBegin);
|
|
|
|
if (_skippedBefore) {
|
|
|
|
*_skippedBefore += removeFromBegin;
|
|
|
|
}
|
|
|
|
} else if (removeFromBegin < 0
|
|
|
|
&& (!_skippedBefore || *_skippedBefore > 0)) {
|
|
|
|
requestedSomething = true;
|
|
|
|
requestMessages(RequestDirection::Before);
|
|
|
|
}
|
|
|
|
if (removeFromEnd > 0) {
|
|
|
|
_ids.erase(_ids.end() - removeFromEnd, _ids.end());
|
|
|
|
if (_skippedAfter) {
|
|
|
|
*_skippedAfter += removeFromEnd;
|
|
|
|
}
|
|
|
|
} else if (removeFromEnd < 0
|
|
|
|
&& (!_skippedAfter || *_skippedAfter > 0)) {
|
|
|
|
requestedSomething = true;
|
|
|
|
requestMessages(RequestDirection::After);
|
|
|
|
}
|
|
|
|
if (!_fullCount && !requestedSomething) {
|
|
|
|
requestMessagesCount();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SparseIdsSliceBuilder::requestMessages(
|
|
|
|
RequestDirection direction) {
|
|
|
|
auto requestAroundData = [&]() -> AroundData {
|
|
|
|
if (_ids.empty()) {
|
2018-01-09 17:08:31 +00:00
|
|
|
return { _key, Data::LoadDirection::Around };
|
2017-10-29 15:32:01 +00:00
|
|
|
} else if (direction == RequestDirection::Before) {
|
2018-01-09 17:08:31 +00:00
|
|
|
return { _ids.front(), Data::LoadDirection::Before };
|
2017-10-29 15:32:01 +00:00
|
|
|
}
|
2018-01-09 17:08:31 +00:00
|
|
|
return { _ids.back(), Data::LoadDirection::After };
|
2017-10-29 15:32:01 +00:00
|
|
|
};
|
|
|
|
_insufficientAround.fire(requestAroundData());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SparseIdsSliceBuilder::requestMessagesCount() {
|
2018-01-09 17:08:31 +00:00
|
|
|
_insufficientAround.fire({ 0, Data::LoadDirection::Around });
|
2017-10-29 15:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SparseIdsSlice SparseIdsSliceBuilder::snapshot() const {
|
|
|
|
return SparseIdsSlice(
|
|
|
|
_ids,
|
|
|
|
_fullCount,
|
|
|
|
_skippedBefore,
|
|
|
|
_skippedAfter);
|
|
|
|
}
|
2017-10-30 19:24:20 +00:00
|
|
|
|
|
|
|
rpl::producer<SparseIdsMergedSlice> SparseIdsMergedSlice::CreateViewer(
|
|
|
|
SparseIdsMergedSlice::Key key,
|
|
|
|
int limitBefore,
|
|
|
|
int limitAfter,
|
2018-06-04 15:35:11 +00:00
|
|
|
Fn<SimpleViewerFunction> simpleViewer) {
|
2017-10-30 19:24:20 +00:00
|
|
|
Expects(IsServerMsgId(key.universalId)
|
|
|
|
|| (key.universalId == 0)
|
|
|
|
|| (IsServerMsgId(ServerMaxMsgId + key.universalId) && key.migratedPeerId != 0));
|
|
|
|
Expects((key.universalId != 0)
|
|
|
|
|| (limitBefore == 0 && limitAfter == 0));
|
|
|
|
|
|
|
|
return [=](auto consumer) {
|
|
|
|
auto partViewer = simpleViewer(
|
|
|
|
key.peerId,
|
|
|
|
SparseIdsMergedSlice::PartKey(key),
|
|
|
|
limitBefore,
|
|
|
|
limitAfter
|
|
|
|
);
|
|
|
|
if (!key.migratedPeerId) {
|
2017-12-22 07:05:20 +00:00
|
|
|
return std::move(
|
|
|
|
partViewer
|
|
|
|
) | rpl::start_with_next([=](SparseIdsSlice &&part) {
|
|
|
|
consumer.put_next(SparseIdsMergedSlice(
|
|
|
|
key,
|
|
|
|
std::move(part),
|
2018-09-21 16:28:46 +00:00
|
|
|
std::nullopt));
|
2017-12-22 07:05:20 +00:00
|
|
|
});
|
2017-10-30 19:24:20 +00:00
|
|
|
}
|
|
|
|
auto migratedViewer = simpleViewer(
|
|
|
|
key.migratedPeerId,
|
|
|
|
SparseIdsMergedSlice::MigratedKey(key),
|
|
|
|
limitBefore,
|
|
|
|
limitAfter);
|
|
|
|
return rpl::combine(
|
|
|
|
std::move(partViewer),
|
|
|
|
std::move(migratedViewer)
|
|
|
|
) | rpl::start_with_next([=](
|
|
|
|
SparseIdsSlice &&part,
|
|
|
|
SparseIdsSlice &&migrated) {
|
|
|
|
consumer.put_next(SparseIdsMergedSlice(
|
|
|
|
key,
|
|
|
|
std::move(part),
|
|
|
|
std::move(migrated)));
|
|
|
|
});
|
|
|
|
};
|
2021-06-29 14:40:55 +00:00
|
|
|
}
|