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
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2021-06-30 19:10:53 +00:00
|
|
|
#include "data/data_abstract_sparse_ids.h"
|
2018-01-09 17:08:31 +00:00
|
|
|
#include "data/data_messages.h"
|
|
|
|
|
2017-10-29 15:32:01 +00:00
|
|
|
namespace Storage {
|
|
|
|
struct SparseIdsListResult;
|
|
|
|
struct SparseIdsSliceUpdate;
|
|
|
|
} // namespace Storage
|
|
|
|
|
2021-06-30 19:10:53 +00:00
|
|
|
class SparseIdsSlice final : public AbstractSparseIds<base::flat_set<MsgId>> {
|
2017-10-29 15:32:01 +00:00
|
|
|
public:
|
|
|
|
using Key = MsgId;
|
2021-06-30 19:10:53 +00:00
|
|
|
using AbstractSparseIds<base::flat_set<MsgId>>::AbstractSparseIds;
|
2017-10-29 15:32:01 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2021-06-29 14:40:55 +00:00
|
|
|
using SparseUnsortedIdsSlice = AbstractSparseIds<std::vector<MsgId>>;
|
|
|
|
|
2017-10-29 15:32:01 +00:00
|
|
|
class SparseIdsMergedSlice {
|
|
|
|
public:
|
|
|
|
using UniversalMsgId = MsgId;
|
|
|
|
struct Key {
|
|
|
|
Key(
|
|
|
|
PeerId peerId,
|
|
|
|
PeerId migratedPeerId,
|
2021-06-29 14:40:55 +00:00
|
|
|
UniversalMsgId universalId,
|
|
|
|
bool scheduled = false)
|
2017-10-29 15:32:01 +00:00
|
|
|
: peerId(peerId)
|
2021-06-29 14:40:55 +00:00
|
|
|
, scheduled(scheduled)
|
|
|
|
, migratedPeerId(scheduled ? 0 : migratedPeerId)
|
2017-10-29 15:32:01 +00:00
|
|
|
, universalId(universalId) {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const Key &other) const {
|
|
|
|
return (peerId == other.peerId)
|
|
|
|
&& (migratedPeerId == other.migratedPeerId)
|
|
|
|
&& (universalId == other.universalId);
|
|
|
|
}
|
2017-12-09 10:02:51 +00:00
|
|
|
bool operator!=(const Key &other) const {
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
2017-10-29 15:32:01 +00:00
|
|
|
|
|
|
|
PeerId peerId = 0;
|
2021-06-29 14:40:55 +00:00
|
|
|
bool scheduled = false;
|
2017-10-29 15:32:01 +00:00
|
|
|
PeerId migratedPeerId = 0;
|
|
|
|
UniversalMsgId universalId = 0;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
SparseIdsMergedSlice(Key key);
|
|
|
|
SparseIdsMergedSlice(
|
|
|
|
Key key,
|
|
|
|
SparseIdsSlice part,
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<SparseIdsSlice> migrated);
|
2021-06-29 14:40:55 +00:00
|
|
|
SparseIdsMergedSlice(
|
|
|
|
Key key,
|
|
|
|
SparseUnsortedIdsSlice scheduled);
|
2017-10-29 15:32:01 +00:00
|
|
|
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<int> fullCount() const;
|
|
|
|
std::optional<int> skippedBefore() const;
|
|
|
|
std::optional<int> skippedAfter() const;
|
|
|
|
std::optional<int> indexOf(FullMsgId fullId) const;
|
2017-10-29 15:32:01 +00:00
|
|
|
int size() const;
|
|
|
|
FullMsgId operator[](int index) const;
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<int> distance(const Key &a, const Key &b) const;
|
|
|
|
std::optional<FullMsgId> nearest(UniversalMsgId id) const;
|
2017-10-29 15:32:01 +00:00
|
|
|
|
2017-10-30 19:24:20 +00:00
|
|
|
using SimpleViewerFunction = rpl::producer<SparseIdsSlice>(
|
|
|
|
PeerId peerId,
|
|
|
|
SparseIdsSlice::Key simpleKey,
|
|
|
|
int limitBefore,
|
|
|
|
int limitAfter);
|
|
|
|
static rpl::producer<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
|
|
|
|
|
|
|
private:
|
2017-10-29 15:32:01 +00:00
|
|
|
static SparseIdsSlice::Key PartKey(const Key &key) {
|
|
|
|
return (key.universalId < 0) ? 1 : key.universalId;
|
|
|
|
}
|
|
|
|
static SparseIdsSlice::Key MigratedKey(const Key &key) {
|
|
|
|
return (key.universalId < 0)
|
2017-10-30 19:24:20 +00:00
|
|
|
? (ServerMaxMsgId + key.universalId)
|
|
|
|
: (key.universalId > 0) ? (ServerMaxMsgId - 1) : 0;
|
2017-10-29 15:32:01 +00:00
|
|
|
}
|
2018-09-21 16:28:46 +00:00
|
|
|
static std::optional<SparseIdsSlice> MigratedSlice(const Key &key) {
|
2017-10-29 15:32:01 +00:00
|
|
|
return key.migratedPeerId
|
|
|
|
? base::make_optional(SparseIdsSlice())
|
2018-09-21 16:28:46 +00:00
|
|
|
: std::nullopt;
|
2017-10-29 15:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool IsFromSlice(PeerId peerId, FullMsgId fullId) {
|
|
|
|
return peerIsChannel(peerId)
|
|
|
|
? (peerId == peerFromChannel(fullId.channel))
|
|
|
|
: !fullId.channel;
|
|
|
|
}
|
|
|
|
static FullMsgId ComputeId(PeerId peerId, MsgId msgId) {
|
2021-04-01 21:04:10 +00:00
|
|
|
return FullMsgId(peerToChannel(peerId), msgId);
|
2017-10-29 15:32:01 +00:00
|
|
|
}
|
|
|
|
static FullMsgId ComputeId(const Key &key) {
|
|
|
|
return (key.universalId >= 0)
|
|
|
|
? ComputeId(key.peerId, key.universalId)
|
|
|
|
: ComputeId(key.migratedPeerId, ServerMaxMsgId + key.universalId);
|
|
|
|
}
|
2018-09-21 16:28:46 +00:00
|
|
|
static std::optional<int> Add(
|
|
|
|
const std::optional<int> &a,
|
|
|
|
const std::optional<int> &b) {
|
|
|
|
return (a && b) ? base::make_optional(*a + *b) : std::nullopt;
|
2017-10-29 15:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isFromPart(FullMsgId fullId) const {
|
|
|
|
return IsFromSlice(_key.peerId, fullId);
|
|
|
|
}
|
|
|
|
bool isFromMigrated(FullMsgId fullId) const {
|
|
|
|
return _migrated
|
|
|
|
? IsFromSlice(_key.migratedPeerId, fullId)
|
|
|
|
: false;
|
|
|
|
}
|
|
|
|
int migratedSize() const {
|
|
|
|
return isolatedInPart() ? 0 : _migrated->size();
|
|
|
|
}
|
|
|
|
bool isolatedInPart() const {
|
|
|
|
return IsServerMsgId(_key.universalId)
|
|
|
|
&& (!_migrated || _part.skippedBefore() != 0);
|
|
|
|
}
|
|
|
|
bool isolatedInMigrated() const {
|
|
|
|
return IsServerMsgId(ServerMaxMsgId + _key.universalId)
|
|
|
|
&& (_migrated->skippedAfter() != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Key _key;
|
|
|
|
SparseIdsSlice _part;
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<SparseIdsSlice> _migrated;
|
2021-06-29 14:40:55 +00:00
|
|
|
std::optional<SparseUnsortedIdsSlice> _scheduled;
|
2017-10-29 15:32:01 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class SparseIdsSliceBuilder {
|
|
|
|
public:
|
|
|
|
using Key = SparseIdsSlice::Key;
|
|
|
|
|
|
|
|
SparseIdsSliceBuilder(Key key, int limitBefore, int limitAfter);
|
|
|
|
|
|
|
|
bool applyInitial(const Storage::SparseIdsListResult &result);
|
|
|
|
bool applyUpdate(const Storage::SparseIdsSliceUpdate &update);
|
|
|
|
bool removeOne(MsgId messageId);
|
|
|
|
bool removeAll();
|
2018-02-05 14:37:22 +00:00
|
|
|
bool invalidateBottom();
|
2017-10-29 15:32:01 +00:00
|
|
|
|
|
|
|
void checkInsufficient();
|
2017-10-30 19:24:20 +00:00
|
|
|
struct AroundData {
|
|
|
|
MsgId aroundId = 0;
|
2018-01-09 17:08:31 +00:00
|
|
|
Data::LoadDirection direction = Data::LoadDirection::Around;
|
2017-10-30 19:24:20 +00:00
|
|
|
|
|
|
|
inline bool operator<(const AroundData &other) const {
|
|
|
|
return (aroundId < other.aroundId)
|
|
|
|
|| ((aroundId == other.aroundId)
|
|
|
|
&& (direction < other.direction));
|
|
|
|
}
|
|
|
|
};
|
2017-10-29 15:32:01 +00:00
|
|
|
auto insufficientAround() const {
|
|
|
|
return _insufficientAround.events();
|
|
|
|
}
|
|
|
|
|
|
|
|
SparseIdsSlice snapshot() const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum class RequestDirection {
|
|
|
|
Before,
|
|
|
|
After,
|
|
|
|
};
|
|
|
|
void requestMessages(RequestDirection direction);
|
|
|
|
void requestMessagesCount();
|
|
|
|
void fillSkippedAndSliceToLimits();
|
|
|
|
void sliceToLimits();
|
|
|
|
|
|
|
|
void 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::nullopt,
|
|
|
|
std::optional<int> skippedAfter = std::nullopt);
|
2017-10-29 15:32:01 +00:00
|
|
|
|
|
|
|
Key _key;
|
|
|
|
base::flat_set<MsgId> _ids;
|
2018-09-21 16:28:46 +00:00
|
|
|
std::optional<int> _fullCount;
|
|
|
|
std::optional<int> _skippedBefore;
|
|
|
|
std::optional<int> _skippedAfter;
|
2017-10-29 15:32:01 +00:00
|
|
|
int _limitBefore = 0;
|
|
|
|
int _limitAfter = 0;
|
|
|
|
|
|
|
|
rpl::event_stream<AroundData> _insufficientAround;
|
|
|
|
|
|
|
|
};
|