2019-02-13 18:10:18 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "media/streaming/media_streaming_reader.h"
|
|
|
|
|
2019-03-05 13:56:27 +00:00
|
|
|
#include "media/streaming/media_streaming_common.h"
|
2019-02-13 18:10:18 +00:00
|
|
|
#include "media/streaming/media_streaming_loader.h"
|
|
|
|
#include "storage/cache/storage_cache_database.h"
|
|
|
|
|
|
|
|
namespace Media {
|
|
|
|
namespace Streaming {
|
|
|
|
namespace {
|
|
|
|
|
2019-02-25 17:26:08 +00:00
|
|
|
constexpr auto kPartSize = Loader::kPartSize;
|
|
|
|
constexpr auto kPartsInSlice = 64;
|
|
|
|
constexpr auto kInSlice = kPartsInSlice * kPartSize;
|
2019-03-13 16:55:57 +00:00
|
|
|
constexpr auto kMaxPartsInHeader = 64;
|
2019-02-25 17:26:08 +00:00
|
|
|
constexpr auto kMaxOnlyInHeader = 80 * kPartSize;
|
2019-03-08 11:23:34 +00:00
|
|
|
constexpr auto kPartsOutsideFirstSliceGood = 8;
|
2019-02-26 13:57:47 +00:00
|
|
|
constexpr auto kSlicesInMemory = 2;
|
2019-02-25 17:26:08 +00:00
|
|
|
|
|
|
|
// 1 MB of parts are requested from cloud ahead of reading demand.
|
|
|
|
constexpr auto kPreloadPartsAhead = 8;
|
2019-04-12 10:50:41 +00:00
|
|
|
constexpr auto kDownloaderRequestsLimit = 4;
|
2019-02-25 17:26:08 +00:00
|
|
|
|
2019-04-11 11:56:26 +00:00
|
|
|
using PartsMap = base::flat_map<int, QByteArray>;
|
|
|
|
|
|
|
|
struct ParsedCacheEntry {
|
|
|
|
PartsMap parts;
|
|
|
|
std::optional<PartsMap> included;
|
|
|
|
};
|
|
|
|
|
2019-03-08 11:23:34 +00:00
|
|
|
bool IsContiguousSerialization(int serializedSize, int maxSliceSize) {
|
|
|
|
return !(serializedSize % kPartSize) || (serializedSize == maxSliceSize);
|
|
|
|
}
|
|
|
|
|
2019-04-11 11:56:26 +00:00
|
|
|
bool IsFullInHeader(int size) {
|
|
|
|
return (size <= kMaxOnlyInHeader);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ComputeIsGoodHeader(int size, const PartsMap &header) {
|
|
|
|
if (IsFullInHeader(size)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const auto outsideFirstSliceIt = ranges::lower_bound(
|
|
|
|
header,
|
|
|
|
kInSlice,
|
|
|
|
ranges::less(),
|
|
|
|
&PartsMap::value_type::first);
|
|
|
|
const auto outsideFirstSlice = end(header) - outsideFirstSliceIt;
|
|
|
|
return (outsideFirstSlice <= kPartsOutsideFirstSliceGood);
|
|
|
|
}
|
|
|
|
|
|
|
|
int SlicesCount(int size) {
|
|
|
|
return (size + kInSlice - 1) / kInSlice;
|
|
|
|
}
|
|
|
|
|
|
|
|
int MaxSliceSize(int sliceNumber, int size) {
|
|
|
|
return !sliceNumber
|
|
|
|
? size
|
|
|
|
: (sliceNumber == SlicesCount(size))
|
|
|
|
? (size - (sliceNumber - 1) * kInSlice)
|
|
|
|
: kInSlice;
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes::const_span ParseComplexCachedMap(
|
|
|
|
PartsMap &result,
|
|
|
|
bytes::const_span data,
|
|
|
|
int maxSize) {
|
|
|
|
const auto takeInt = [&]() -> std::optional<int> {
|
|
|
|
if (data.size() < sizeof(int32)) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
const auto bytes = data.data();
|
|
|
|
const auto result = *reinterpret_cast<const int32*>(bytes);
|
|
|
|
data = data.subspan(sizeof(int32));
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
const auto takeBytes = [&](int count) {
|
|
|
|
if (count <= 0 || data.size() < count) {
|
|
|
|
return bytes::const_span();
|
|
|
|
}
|
|
|
|
const auto result = data.subspan(0, count);
|
|
|
|
data = data.subspan(count);
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
const auto maybeCount = takeInt();
|
|
|
|
if (!maybeCount) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
const auto count = *maybeCount;
|
|
|
|
if (count < 0) {
|
|
|
|
return {};
|
|
|
|
} else if (!count) {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
for (auto i = 0; i != count; ++i) {
|
|
|
|
const auto offset = takeInt().value_or(0);
|
|
|
|
const auto size = takeInt().value_or(0);
|
|
|
|
const auto bytes = takeBytes(size);
|
|
|
|
if (offset < 0
|
|
|
|
|| offset >= maxSize
|
|
|
|
|| size <= 0
|
|
|
|
|| size > maxSize
|
|
|
|
|| offset + size > maxSize
|
|
|
|
|| bytes.size() != size) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
result.try_emplace(
|
|
|
|
offset,
|
|
|
|
reinterpret_cast<const char*>(bytes.data()),
|
|
|
|
bytes.size());
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes::const_span ParseCachedMap(
|
|
|
|
PartsMap &result,
|
|
|
|
bytes::const_span data,
|
|
|
|
int maxSize) {
|
|
|
|
const auto size = int(data.size());
|
|
|
|
if (IsContiguousSerialization(size, maxSize)) {
|
|
|
|
if (size > maxSize) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
for (auto offset = 0; offset < size; offset += kPartSize) {
|
|
|
|
const auto part = data.subspan(
|
|
|
|
offset,
|
|
|
|
std::min(kPartSize, size - offset));
|
|
|
|
result.try_emplace(
|
|
|
|
offset,
|
|
|
|
reinterpret_cast<const char*>(part.data()),
|
|
|
|
part.size());
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return ParseComplexCachedMap(result, data, maxSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
ParsedCacheEntry ParseCacheEntry(
|
|
|
|
bytes::const_span data,
|
|
|
|
int sliceNumber,
|
|
|
|
int size) {
|
|
|
|
auto result = ParsedCacheEntry();
|
|
|
|
const auto remaining = ParseCachedMap(
|
|
|
|
result.parts,
|
|
|
|
data,
|
|
|
|
MaxSliceSize(sliceNumber, size));
|
|
|
|
if (!sliceNumber && ComputeIsGoodHeader(size, result.parts)) {
|
|
|
|
result.included = PartsMap();
|
|
|
|
ParseCachedMap(*result.included, remaining, MaxSliceSize(1, size));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-02-13 18:10:18 +00:00
|
|
|
template <typename Range> // Range::value_type is Pair<int, QByteArray>
|
|
|
|
int FindNotLoadedStart(Range &&parts, int offset) {
|
|
|
|
auto result = offset;
|
|
|
|
for (const auto &part : parts) {
|
|
|
|
const auto partStart = part.first;
|
|
|
|
const auto partEnd = partStart + part.second.size();
|
|
|
|
if (partStart <= result && partEnd >= result) {
|
|
|
|
result = partEnd;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Range> // Range::value_type is Pair<int, QByteArray>
|
|
|
|
void CopyLoaded(bytes::span buffer, Range &&parts, int offset, int till) {
|
|
|
|
auto filled = offset;
|
|
|
|
for (const auto &part : parts) {
|
|
|
|
const auto bytes = bytes::make_span(part.second);
|
|
|
|
const auto partStart = part.first;
|
|
|
|
const auto partEnd = int(partStart + bytes.size());
|
|
|
|
const auto copyTill = std::min(partEnd, till);
|
|
|
|
Assert(partStart <= filled && filled < copyTill);
|
|
|
|
|
|
|
|
const auto from = filled - partStart;
|
|
|
|
const auto copy = copyTill - filled;
|
|
|
|
bytes::copy(buffer, bytes.subspan(from, copy));
|
|
|
|
buffer = buffer.subspan(copy);
|
|
|
|
filled += copy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2019-02-25 17:26:08 +00:00
|
|
|
template <int Size>
|
|
|
|
bool Reader::StackIntVector<Size>::add(int value) {
|
|
|
|
using namespace rpl::mappers;
|
|
|
|
|
|
|
|
const auto i = ranges::find_if(_storage, _1 < 0);
|
|
|
|
if (i == end(_storage)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*i = value;
|
|
|
|
const auto next = i + 1;
|
|
|
|
if (next != end(_storage)) {
|
|
|
|
*next = -1;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <int Size>
|
|
|
|
auto Reader::StackIntVector<Size>::values() const {
|
|
|
|
using namespace rpl::mappers;
|
|
|
|
|
|
|
|
return ranges::view::all(_storage) | ranges::view::take_while(_1 >= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Reader::CacheHelper {
|
|
|
|
explicit CacheHelper(Storage::Cache::Key baseKey);
|
|
|
|
|
|
|
|
Storage::Cache::Key key(int sliceNumber) const;
|
|
|
|
|
|
|
|
const Storage::Cache::Key baseKey;
|
|
|
|
|
|
|
|
QMutex mutex;
|
2019-04-11 11:56:26 +00:00
|
|
|
base::flat_map<int, PartsMap> results;
|
2019-05-31 16:45:03 +00:00
|
|
|
std::vector<int> sizes;
|
2019-02-25 17:26:08 +00:00
|
|
|
std::atomic<crl::semaphore*> waiting = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
Reader::CacheHelper::CacheHelper(Storage::Cache::Key baseKey)
|
|
|
|
: baseKey(baseKey) {
|
|
|
|
}
|
|
|
|
|
|
|
|
Storage::Cache::Key Reader::CacheHelper::key(int sliceNumber) const {
|
|
|
|
return Storage::Cache::Key{ baseKey.high, baseKey.low + sliceNumber };
|
|
|
|
}
|
|
|
|
|
2019-04-11 11:56:26 +00:00
|
|
|
void Reader::Slice::processCacheData(PartsMap &&data) {
|
2019-02-25 17:26:08 +00:00
|
|
|
Expects((flags & Flag::LoadingFromCache) != 0);
|
|
|
|
Expects(!(flags & Flag::LoadedFromCache));
|
|
|
|
|
|
|
|
const auto guard = gsl::finally([&] {
|
|
|
|
flags |= Flag::LoadedFromCache;
|
|
|
|
flags &= ~Flag::LoadingFromCache;
|
|
|
|
});
|
2019-04-11 11:56:26 +00:00
|
|
|
if (parts.empty()) {
|
|
|
|
parts = std::move(data);
|
|
|
|
} else {
|
|
|
|
for (auto &[offset, bytes] : data) {
|
|
|
|
parts.emplace(offset, std::move(bytes));
|
2019-02-25 17:26:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reader::Slice::addPart(int offset, QByteArray bytes) {
|
|
|
|
Expects(!parts.contains(offset));
|
|
|
|
|
|
|
|
parts.emplace(offset, std::move(bytes));
|
|
|
|
if (flags & Flag::LoadedFromCache) {
|
|
|
|
flags |= Flag::ChangedSinceCache;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Reader::Slice::prepareFill(int from, int till) -> PrepareFillResult {
|
|
|
|
auto result = PrepareFillResult();
|
|
|
|
|
|
|
|
result.ready = false;
|
|
|
|
const auto fromOffset = (from / kPartSize) * kPartSize;
|
|
|
|
const auto tillPart = (till + kPartSize - 1) / kPartSize;
|
2019-03-06 14:05:11 +00:00
|
|
|
const auto preloadTillOffset = (tillPart + kPreloadPartsAhead)
|
|
|
|
* kPartSize;
|
2019-02-25 17:26:08 +00:00
|
|
|
|
|
|
|
const auto after = ranges::upper_bound(
|
|
|
|
parts,
|
|
|
|
from,
|
|
|
|
ranges::less(),
|
2019-04-11 11:56:26 +00:00
|
|
|
&PartsMap::value_type::first);
|
2019-02-25 17:26:08 +00:00
|
|
|
if (after == begin(parts)) {
|
|
|
|
result.offsetsFromLoader = offsetsFromLoader(
|
|
|
|
fromOffset,
|
|
|
|
preloadTillOffset);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto start = after - 1;
|
|
|
|
const auto finish = ranges::lower_bound(
|
|
|
|
start,
|
|
|
|
end(parts),
|
|
|
|
till,
|
|
|
|
ranges::less(),
|
2019-04-11 11:56:26 +00:00
|
|
|
&PartsMap::value_type::first);
|
2019-02-25 17:26:08 +00:00
|
|
|
const auto haveTill = FindNotLoadedStart(
|
2019-10-02 09:54:29 +00:00
|
|
|
ranges::make_subrange(start, finish),
|
2019-02-25 17:26:08 +00:00
|
|
|
fromOffset);
|
|
|
|
if (haveTill < till) {
|
|
|
|
result.offsetsFromLoader = offsetsFromLoader(
|
|
|
|
haveTill,
|
|
|
|
preloadTillOffset);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
result.ready = true;
|
|
|
|
result.start = start;
|
|
|
|
result.finish = finish;
|
|
|
|
result.offsetsFromLoader = offsetsFromLoader(
|
|
|
|
tillPart * kPartSize,
|
|
|
|
preloadTillOffset);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Reader::Slice::offsetsFromLoader(int from, int till) const
|
|
|
|
-> StackIntVector<Reader::kLoadFromRemoteMax> {
|
|
|
|
auto result = StackIntVector<kLoadFromRemoteMax>();
|
|
|
|
|
|
|
|
const auto after = ranges::upper_bound(
|
|
|
|
parts,
|
|
|
|
from,
|
|
|
|
ranges::less(),
|
2019-04-11 11:56:26 +00:00
|
|
|
&PartsMap::value_type::first);
|
2019-02-25 17:26:08 +00:00
|
|
|
auto check = (after == begin(parts)) ? after : (after - 1);
|
|
|
|
const auto end = parts.end();
|
|
|
|
for (auto offset = from; offset != till; offset += kPartSize) {
|
|
|
|
while (check != end && check->first < offset) {
|
|
|
|
++check;
|
|
|
|
}
|
|
|
|
if (check != end && check->first == offset) {
|
|
|
|
continue;
|
|
|
|
} else if (!result.add(offset)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Reader::Slices::Slices(int size, bool useCache)
|
|
|
|
: _size(size) {
|
|
|
|
Expects(size > 0);
|
|
|
|
|
|
|
|
if (useCache) {
|
|
|
|
_header.flags |= Slice::Flag::LoadingFromCache;
|
|
|
|
} else {
|
|
|
|
_headerMode = HeaderMode::NoCache;
|
|
|
|
}
|
2019-03-08 11:23:34 +00:00
|
|
|
if (!isFullInHeader()) {
|
2019-04-11 11:56:26 +00:00
|
|
|
_data.resize(SlicesCount(_size));
|
2019-03-06 14:05:11 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-25 17:26:08 +00:00
|
|
|
|
2019-03-08 11:23:34 +00:00
|
|
|
bool Reader::Slices::headerModeUnknown() const {
|
|
|
|
return (_headerMode == HeaderMode::Unknown);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Reader::Slices::isFullInHeader() const {
|
2019-04-11 11:56:26 +00:00
|
|
|
return IsFullInHeader(_size);
|
2019-02-25 17:26:08 +00:00
|
|
|
}
|
|
|
|
|
2019-03-08 11:23:34 +00:00
|
|
|
bool Reader::Slices::isGoodHeader() const {
|
|
|
|
return (_headerMode == HeaderMode::Good);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Reader::Slices::computeIsGoodHeader() const {
|
2019-04-11 11:56:26 +00:00
|
|
|
return ComputeIsGoodHeader(_size, _header.parts);
|
2019-03-08 11:23:34 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 17:26:08 +00:00
|
|
|
void Reader::Slices::headerDone(bool fromCache) {
|
|
|
|
if (_headerMode != HeaderMode::Unknown) {
|
|
|
|
return;
|
|
|
|
}
|
2019-03-08 11:23:34 +00:00
|
|
|
_headerMode = isFullInHeader()
|
|
|
|
? HeaderMode::Full
|
|
|
|
: computeIsGoodHeader()
|
|
|
|
? HeaderMode::Good
|
|
|
|
: HeaderMode::Small;
|
2019-02-25 17:26:08 +00:00
|
|
|
if (!fromCache) {
|
|
|
|
for (auto &slice : _data) {
|
|
|
|
using Flag = Slice::Flag;
|
|
|
|
Assert(!(slice.flags
|
|
|
|
& (Flag::LoadingFromCache | Flag::LoadedFromCache)));
|
|
|
|
slice.flags |= Slice::Flag::LoadedFromCache;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-31 11:45:35 +00:00
|
|
|
int Reader::Slices::headerSize() const {
|
|
|
|
return _header.parts.size() * kPartSize;
|
|
|
|
}
|
|
|
|
|
2019-05-31 16:45:03 +00:00
|
|
|
bool Reader::Slices::fullInCache() const {
|
|
|
|
return _fullInCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Reader::Slices::requestSliceSizesCount() const {
|
|
|
|
if (!headerModeUnknown() || isFullInHeader()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return _data.size();
|
|
|
|
}
|
|
|
|
|
2019-03-05 13:56:27 +00:00
|
|
|
bool Reader::Slices::headerWontBeFilled() const {
|
2019-03-08 11:23:34 +00:00
|
|
|
return headerModeUnknown()
|
2019-03-06 14:05:11 +00:00
|
|
|
&& (_header.parts.size() >= kMaxPartsInHeader);
|
2019-03-05 13:56:27 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 17:26:08 +00:00
|
|
|
void Reader::Slices::applyHeaderCacheData() {
|
2019-03-18 06:59:51 +00:00
|
|
|
using namespace rpl::mappers;
|
|
|
|
|
|
|
|
const auto applyWhile = [&](auto &&predicate) {
|
|
|
|
for (const auto &[offset, part] : _header.parts) {
|
|
|
|
const auto index = offset / kInSlice;
|
|
|
|
if (!predicate(index)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_data[index].addPart(
|
|
|
|
offset - index * kInSlice,
|
|
|
|
base::duplicate(part));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if (_header.parts.empty()) {
|
|
|
|
return;
|
|
|
|
} else if (_headerMode == HeaderMode::Good) {
|
|
|
|
// Always apply data to first block if it is cached in the header.
|
|
|
|
applyWhile(_1 == 0);
|
|
|
|
} else if (_headerMode != HeaderMode::Unknown) {
|
2019-02-25 17:26:08 +00:00
|
|
|
return;
|
2019-03-08 11:23:34 +00:00
|
|
|
} else if (isFullInHeader()) {
|
2019-03-06 14:05:11 +00:00
|
|
|
headerDone(true);
|
2019-03-18 06:59:51 +00:00
|
|
|
} else {
|
|
|
|
applyWhile(_1 < int(_data.size()));
|
|
|
|
headerDone(true);
|
2019-02-25 17:26:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-11 11:56:26 +00:00
|
|
|
void Reader::Slices::processCacheResult(int sliceNumber, PartsMap &&result) {
|
2019-02-25 17:26:08 +00:00
|
|
|
Expects(sliceNumber >= 0 && sliceNumber <= _data.size());
|
|
|
|
|
|
|
|
auto &slice = (sliceNumber ? _data[sliceNumber - 1] : _header);
|
2019-03-08 11:23:34 +00:00
|
|
|
if (!sliceNumber && isGoodHeader()) {
|
|
|
|
// We've loaded header slice because really we wanted first slice.
|
|
|
|
if (!(_data[0].flags & Slice::Flag::LoadingFromCache)) {
|
|
|
|
// We could've already unloaded this slice using LRU _usedSlices.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// So just process whole result even if we didn't want header really.
|
|
|
|
slice.flags |= Slice::Flag::LoadingFromCache;
|
2019-03-10 08:01:12 +00:00
|
|
|
slice.flags &= ~Slice::Flag::LoadedFromCache;
|
2019-03-08 11:23:34 +00:00
|
|
|
}
|
|
|
|
if (!(slice.flags & Slice::Flag::LoadingFromCache)) {
|
2019-02-26 13:57:47 +00:00
|
|
|
// We could've already unloaded this slice using LRU _usedSlices.
|
2019-03-06 14:05:11 +00:00
|
|
|
return;
|
2019-02-26 13:57:47 +00:00
|
|
|
}
|
2019-04-11 11:56:26 +00:00
|
|
|
slice.processCacheData(std::move(result));
|
2019-05-31 16:45:03 +00:00
|
|
|
checkSliceFullLoaded(sliceNumber);
|
2019-02-25 17:26:08 +00:00
|
|
|
if (!sliceNumber) {
|
|
|
|
applyHeaderCacheData();
|
2019-03-08 11:23:34 +00:00
|
|
|
if (isGoodHeader()) {
|
|
|
|
// When we first read header we don't request the first slice.
|
|
|
|
// But we get it, so let's apply it anyway.
|
|
|
|
_data[0].flags |= Slice::Flag::LoadingFromCache;
|
|
|
|
}
|
2019-02-25 17:26:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-31 16:45:03 +00:00
|
|
|
void Reader::Slices::processCachedSizes(const std::vector<int> &sizes) {
|
|
|
|
Expects(sizes.size() == _data.size());
|
|
|
|
|
|
|
|
using Flag = Slice::Flag;
|
|
|
|
const auto count = int(sizes.size());
|
|
|
|
auto loadedCount = 0;
|
|
|
|
for (auto i = 0; i != count; ++i) {
|
|
|
|
const auto sliceNumber = (i + 1);
|
|
|
|
const auto sliceSize = (sliceNumber < _data.size())
|
|
|
|
? kInSlice
|
|
|
|
: (_size - (sliceNumber - 1) * kInSlice);
|
|
|
|
const auto loaded = (sizes[i] == sliceSize);
|
|
|
|
|
|
|
|
if (_data[i].flags & Flag::FullInCache) {
|
|
|
|
++loadedCount;
|
|
|
|
} else if (loaded) {
|
|
|
|
_data[i].flags |= Flag::FullInCache;
|
|
|
|
++loadedCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_fullInCache = (loadedCount == count);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reader::Slices::checkSliceFullLoaded(int sliceNumber) {
|
|
|
|
if (!sliceNumber && !isFullInHeader()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto partsCount = [&] {
|
|
|
|
if (!sliceNumber) {
|
|
|
|
return (_size + kPartSize - 1) / kPartSize;
|
|
|
|
}
|
|
|
|
return (sliceNumber < _data.size())
|
|
|
|
? kPartsInSlice
|
|
|
|
: ((_size - (sliceNumber - 1) * kInSlice + kPartSize - 1)
|
|
|
|
/ kPartSize);
|
|
|
|
}();
|
|
|
|
auto &slice = (sliceNumber ? _data[sliceNumber - 1] : _header);
|
|
|
|
const auto loaded = (slice.parts.size() == partsCount);
|
|
|
|
|
|
|
|
using Flag = Slice::Flag;
|
|
|
|
if ((slice.flags & Flag::FullInCache) && !loaded) {
|
|
|
|
slice.flags &= ~Flag::FullInCache;
|
|
|
|
_fullInCache = false;
|
|
|
|
} else if (!(slice.flags & Flag::FullInCache) && loaded) {
|
|
|
|
slice.flags |= Flag::FullInCache;
|
|
|
|
_fullInCache = checkFullInCache();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Reader::Slices::checkFullInCache() const {
|
|
|
|
using Flag = Slice::Flag;
|
|
|
|
if (isFullInHeader()) {
|
|
|
|
return (_header.flags & Flag::FullInCache);
|
|
|
|
}
|
|
|
|
const auto i = ranges::find_if(_data, [](const Slice &slice) {
|
|
|
|
return !(slice.flags & Flag::FullInCache);
|
|
|
|
});
|
|
|
|
return (i == end(_data));
|
|
|
|
}
|
|
|
|
|
2019-03-06 14:05:11 +00:00
|
|
|
void Reader::Slices::processPart(
|
2019-03-05 13:56:27 +00:00
|
|
|
int offset,
|
|
|
|
QByteArray &&bytes) {
|
2019-03-08 11:23:34 +00:00
|
|
|
Expects(isFullInHeader() || (offset / kInSlice < _data.size()));
|
2019-02-25 17:26:08 +00:00
|
|
|
|
2019-03-08 11:23:34 +00:00
|
|
|
if (isFullInHeader()) {
|
2019-03-06 14:05:11 +00:00
|
|
|
_header.addPart(offset, bytes);
|
2019-05-31 16:45:03 +00:00
|
|
|
checkSliceFullLoaded(0);
|
2019-03-06 14:05:11 +00:00
|
|
|
return;
|
|
|
|
} else if (_headerMode == HeaderMode::Unknown) {
|
2019-02-25 17:26:08 +00:00
|
|
|
if (_header.parts.contains(offset)) {
|
2019-03-06 14:05:11 +00:00
|
|
|
return;
|
2019-03-05 13:56:27 +00:00
|
|
|
} else if (_header.parts.size() < kMaxPartsInHeader) {
|
|
|
|
_header.addPart(offset, bytes);
|
2019-02-25 17:26:08 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-06 14:05:11 +00:00
|
|
|
const auto index = offset / kInSlice;
|
2019-02-25 17:26:08 +00:00
|
|
|
_data[index].addPart(offset - index * kInSlice, std::move(bytes));
|
2019-05-31 16:45:03 +00:00
|
|
|
checkSliceFullLoaded(index + 1);
|
2019-02-25 17:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto Reader::Slices::fill(int offset, bytes::span buffer) -> FillResult {
|
|
|
|
Expects(!buffer.empty());
|
|
|
|
Expects(offset >= 0 && offset < _size);
|
|
|
|
Expects(offset + buffer.size() <= _size);
|
|
|
|
Expects(buffer.size() <= kInSlice);
|
|
|
|
|
2019-02-26 13:57:47 +00:00
|
|
|
using Flag = Slice::Flag;
|
|
|
|
|
2019-02-25 17:26:08 +00:00
|
|
|
if (_headerMode != HeaderMode::NoCache
|
2019-02-26 13:57:47 +00:00
|
|
|
&& !(_header.flags & Flag::LoadedFromCache)) {
|
2019-02-25 17:26:08 +00:00
|
|
|
// Waiting for initial cache query.
|
2019-04-12 08:00:06 +00:00
|
|
|
Assert(waitingForHeaderCache());
|
2019-02-25 17:26:08 +00:00
|
|
|
return {};
|
2019-03-08 11:23:34 +00:00
|
|
|
} else if (isFullInHeader()) {
|
2019-03-06 14:05:11 +00:00
|
|
|
return fillFromHeader(offset, buffer);
|
2019-02-25 17:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto result = FillResult();
|
|
|
|
const auto till = int(offset + buffer.size());
|
|
|
|
const auto fromSlice = offset / kInSlice;
|
|
|
|
const auto tillSlice = (till + kInSlice - 1) / kInSlice;
|
|
|
|
Assert(fromSlice >= 0
|
|
|
|
&& (fromSlice + 1 == tillSlice || fromSlice + 2 == tillSlice)
|
|
|
|
&& tillSlice <= _data.size());
|
|
|
|
|
2019-02-26 15:31:33 +00:00
|
|
|
const auto cacheNotLoaded = [&](int sliceIndex) {
|
|
|
|
return (_headerMode != HeaderMode::NoCache)
|
|
|
|
&& (_headerMode != HeaderMode::Unknown)
|
|
|
|
&& !(_data[sliceIndex].flags & Flag::LoadedFromCache);
|
|
|
|
};
|
2019-02-25 17:26:08 +00:00
|
|
|
const auto handlePrepareResult = [&](
|
|
|
|
int sliceIndex,
|
|
|
|
const Slice::PrepareFillResult &prepared) {
|
2019-02-26 15:31:33 +00:00
|
|
|
if (cacheNotLoaded(sliceIndex)) {
|
2019-02-25 17:26:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (const auto offset : prepared.offsetsFromLoader.values()) {
|
|
|
|
const auto full = offset + sliceIndex * kInSlice;
|
2019-03-06 14:05:11 +00:00
|
|
|
if (offset < kInSlice && full < _size) {
|
2019-02-25 17:26:08 +00:00
|
|
|
result.offsetsFromLoader.add(full);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-02-26 15:31:33 +00:00
|
|
|
const auto handleReadFromCache = [&](int sliceIndex) {
|
|
|
|
if (cacheNotLoaded(sliceIndex)
|
|
|
|
&& !(_data[sliceIndex].flags & Flag::LoadingFromCache)) {
|
|
|
|
_data[sliceIndex].flags |= Flag::LoadingFromCache;
|
|
|
|
result.sliceNumbersFromCache.add(sliceIndex + 1);
|
|
|
|
}
|
|
|
|
};
|
2019-02-25 17:26:08 +00:00
|
|
|
const auto firstFrom = offset - fromSlice * kInSlice;
|
|
|
|
const auto firstTill = std::min(kInSlice, till - fromSlice * kInSlice);
|
|
|
|
const auto secondFrom = 0;
|
|
|
|
const auto secondTill = till - (fromSlice + 1) * kInSlice;
|
|
|
|
const auto first = _data[fromSlice].prepareFill(firstFrom, firstTill);
|
|
|
|
const auto second = (fromSlice + 1 < tillSlice)
|
|
|
|
? _data[fromSlice + 1].prepareFill(secondFrom, secondTill)
|
|
|
|
: Slice::PrepareFillResult();
|
|
|
|
handlePrepareResult(fromSlice, first);
|
2019-02-26 15:31:33 +00:00
|
|
|
if (fromSlice + 1 < tillSlice) {
|
|
|
|
handlePrepareResult(fromSlice + 1, second);
|
|
|
|
}
|
2019-02-25 17:26:08 +00:00
|
|
|
if (first.ready && second.ready) {
|
2019-02-26 13:57:47 +00:00
|
|
|
markSliceUsed(fromSlice);
|
2019-02-25 17:26:08 +00:00
|
|
|
CopyLoaded(
|
|
|
|
buffer,
|
2019-10-02 09:54:29 +00:00
|
|
|
ranges::make_subrange(first.start, first.finish),
|
2019-02-25 17:26:08 +00:00
|
|
|
firstFrom,
|
|
|
|
firstTill);
|
|
|
|
if (fromSlice + 1 < tillSlice) {
|
2019-02-26 13:57:47 +00:00
|
|
|
markSliceUsed(fromSlice + 1);
|
2019-02-25 17:26:08 +00:00
|
|
|
CopyLoaded(
|
|
|
|
buffer.subspan(firstTill - firstFrom),
|
2019-10-02 09:54:29 +00:00
|
|
|
ranges::make_subrange(second.start, second.finish),
|
2019-02-25 17:26:08 +00:00
|
|
|
secondFrom,
|
|
|
|
secondTill);
|
|
|
|
}
|
2019-02-26 15:31:33 +00:00
|
|
|
result.toCache = serializeAndUnloadUnused();
|
2019-02-25 17:26:08 +00:00
|
|
|
result.filled = true;
|
2019-02-26 15:31:33 +00:00
|
|
|
} else {
|
|
|
|
handleReadFromCache(fromSlice);
|
|
|
|
if (fromSlice + 1 < tillSlice) {
|
|
|
|
handleReadFromCache(fromSlice + 1);
|
|
|
|
}
|
2019-02-25 17:26:08 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-03-06 14:05:11 +00:00
|
|
|
auto Reader::Slices::fillFromHeader(int offset, bytes::span buffer)
|
|
|
|
-> FillResult {
|
|
|
|
auto result = FillResult();
|
|
|
|
const auto from = offset;
|
|
|
|
const auto till = int(offset + buffer.size());
|
|
|
|
|
|
|
|
const auto prepared = _header.prepareFill(from, till);
|
|
|
|
for (const auto full : prepared.offsetsFromLoader.values()) {
|
|
|
|
if (full < _size) {
|
|
|
|
result.offsetsFromLoader.add(full);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (prepared.ready) {
|
|
|
|
CopyLoaded(
|
|
|
|
buffer,
|
2019-10-02 09:54:29 +00:00
|
|
|
ranges::make_subrange(prepared.start, prepared.finish),
|
2019-03-06 14:05:11 +00:00
|
|
|
from,
|
|
|
|
till);
|
|
|
|
result.filled = true;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-04-11 13:07:48 +00:00
|
|
|
QByteArray Reader::Slices::partForDownloader(int offset) const {
|
|
|
|
Expects(offset < _size);
|
|
|
|
|
|
|
|
if (const auto i = _header.parts.find(offset); i != end(_header.parts)) {
|
|
|
|
return i->second;
|
|
|
|
} else if (isFullInHeader()) {
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
const auto index = offset / kInSlice;
|
|
|
|
const auto &slice = _data[index];
|
|
|
|
const auto i = slice.parts.find(offset - index * kInSlice);
|
|
|
|
return (i != end(slice.parts)) ? i->second : QByteArray();
|
|
|
|
}
|
|
|
|
|
2019-04-12 08:00:06 +00:00
|
|
|
bool Reader::Slices::waitingForHeaderCache() const {
|
|
|
|
return (_header.flags & Slice::Flag::LoadingFromCache);
|
|
|
|
}
|
|
|
|
|
2019-04-12 10:50:41 +00:00
|
|
|
bool Reader::Slices::readCacheForDownloaderRequired(int offset) {
|
2019-04-11 13:07:48 +00:00
|
|
|
Expects(offset < _size);
|
2019-04-12 08:00:06 +00:00
|
|
|
Expects(!waitingForHeaderCache());
|
2019-04-11 13:07:48 +00:00
|
|
|
|
2019-04-12 08:00:06 +00:00
|
|
|
if (isFullInHeader()) {
|
2019-04-12 10:50:41 +00:00
|
|
|
return false;
|
2019-04-11 13:07:48 +00:00
|
|
|
}
|
|
|
|
const auto index = offset / kInSlice;
|
|
|
|
auto &slice = _data[index];
|
2019-04-12 10:50:41 +00:00
|
|
|
return !(slice.flags & Slice::Flag::LoadedFromCache);
|
2019-04-11 13:07:48 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 13:57:47 +00:00
|
|
|
void Reader::Slices::markSliceUsed(int sliceIndex) {
|
|
|
|
const auto i = ranges::find(_usedSlices, sliceIndex);
|
|
|
|
const auto end = _usedSlices.end();
|
|
|
|
if (i == end) {
|
|
|
|
_usedSlices.push_back(sliceIndex);
|
|
|
|
} else {
|
|
|
|
const auto next = i + 1;
|
|
|
|
if (next != end) {
|
|
|
|
std::rotate(i, next, end);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-25 17:26:08 +00:00
|
|
|
int Reader::Slices::maxSliceSize(int sliceNumber) const {
|
2019-04-11 11:56:26 +00:00
|
|
|
return MaxSliceSize(sliceNumber, _size);
|
2019-02-25 17:26:08 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 15:31:33 +00:00
|
|
|
Reader::SerializedSlice Reader::Slices::serializeAndUnloadUnused() {
|
2019-04-12 14:49:03 +00:00
|
|
|
using Flag = Slice::Flag;
|
|
|
|
|
2019-02-26 15:31:33 +00:00
|
|
|
if (_headerMode == HeaderMode::Unknown
|
|
|
|
|| _usedSlices.size() <= kSlicesInMemory) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
const auto purgeSlice = _usedSlices.front();
|
|
|
|
_usedSlices.pop_front();
|
2019-04-12 14:49:03 +00:00
|
|
|
if (!(_data[purgeSlice].flags & Flag::LoadedFromCache)) {
|
2019-02-26 15:31:33 +00:00
|
|
|
// If the only data in this slice was from _header, just leave it.
|
|
|
|
return {};
|
2019-04-12 14:49:03 +00:00
|
|
|
}
|
|
|
|
const auto noNeedToSaveToCache = [&] {
|
|
|
|
if (_headerMode == HeaderMode::NoCache) {
|
|
|
|
// Cache is not used.
|
|
|
|
return true;
|
|
|
|
} else if (!(_data[purgeSlice].flags & Flag::ChangedSinceCache)) {
|
|
|
|
// If no data was changed we should still save first slice,
|
|
|
|
// if header data was changed since loading from cache.
|
|
|
|
// Otherwise in destructor we won't be able to unload header.
|
|
|
|
if (!isGoodHeader()
|
|
|
|
|| (purgeSlice > 0)
|
|
|
|
|| (!(_header.flags & Flag::ChangedSinceCache))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}();
|
|
|
|
if (noNeedToSaveToCache) {
|
2019-05-31 16:45:03 +00:00
|
|
|
unloadSlice(_data[purgeSlice]);
|
2019-02-26 15:31:33 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return serializeAndUnloadSlice(purgeSlice + 1);
|
|
|
|
}
|
|
|
|
|
2019-02-25 17:26:08 +00:00
|
|
|
Reader::SerializedSlice Reader::Slices::serializeAndUnloadSlice(
|
2019-03-20 10:17:40 +00:00
|
|
|
int sliceNumber) {
|
|
|
|
Expects(_headerMode != HeaderMode::Unknown);
|
|
|
|
Expects(_headerMode != HeaderMode::NoCache);
|
2019-02-26 13:57:47 +00:00
|
|
|
Expects(sliceNumber >= 0 && sliceNumber <= _data.size());
|
2019-02-25 17:26:08 +00:00
|
|
|
|
2019-03-08 11:23:34 +00:00
|
|
|
if (isGoodHeader() && (sliceNumber == 1)) {
|
|
|
|
return serializeAndUnloadSlice(0);
|
|
|
|
}
|
|
|
|
const auto writeHeaderAndSlice = isGoodHeader() && !sliceNumber;
|
|
|
|
|
2019-02-26 13:57:47 +00:00
|
|
|
auto &slice = sliceNumber ? _data[sliceNumber - 1] : _header;
|
2019-02-25 17:26:08 +00:00
|
|
|
const auto count = slice.parts.size();
|
2019-02-26 13:57:47 +00:00
|
|
|
Assert(count > 0);
|
|
|
|
|
2019-02-25 17:26:08 +00:00
|
|
|
auto result = SerializedSlice();
|
|
|
|
result.number = sliceNumber;
|
2019-03-08 11:23:34 +00:00
|
|
|
|
|
|
|
// We always use complex serialization for header + first slice.
|
|
|
|
const auto continuousTill = writeHeaderAndSlice
|
|
|
|
? 0
|
|
|
|
: FindNotLoadedStart(slice.parts, 0);
|
|
|
|
const auto continuous = (continuousTill > slice.parts.back().first);
|
|
|
|
if (continuous) {
|
2019-02-25 17:26:08 +00:00
|
|
|
// All data is continuous.
|
|
|
|
result.data.reserve(count * kPartSize);
|
|
|
|
for (const auto &[offset, part] : slice.parts) {
|
|
|
|
result.data.append(part);
|
|
|
|
}
|
|
|
|
} else {
|
2019-03-08 11:23:34 +00:00
|
|
|
result.data = serializeComplexSlice(slice);
|
|
|
|
if (writeHeaderAndSlice) {
|
|
|
|
result.data.append(serializeAndUnloadFirstSliceNoHeader());
|
2019-02-25 17:26:08 +00:00
|
|
|
}
|
2019-03-08 11:23:34 +00:00
|
|
|
|
|
|
|
// Make sure this data won't be taken for full continuous data.
|
|
|
|
const auto maxSize = maxSliceSize(sliceNumber);
|
|
|
|
while (IsContiguousSerialization(result.data.size(), maxSize)) {
|
|
|
|
result.data.push_back(char(0));
|
2019-02-25 17:26:08 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-08 11:23:34 +00:00
|
|
|
|
|
|
|
// We may serialize header in the middle of streaming, if we use
|
|
|
|
// HeaderMode::Good and we unload first slice. We still require
|
|
|
|
// header data to continue working, so don't really unload the header.
|
|
|
|
if (sliceNumber) {
|
2019-05-31 16:45:03 +00:00
|
|
|
unloadSlice(slice);
|
2019-03-08 11:23:34 +00:00
|
|
|
} else {
|
|
|
|
slice.flags &= ~Slice::Flag::ChangedSinceCache;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-05-31 16:45:03 +00:00
|
|
|
void Reader::Slices::unloadSlice(Slice &slice) const {
|
|
|
|
const auto full = (slice.flags & Slice::Flag::FullInCache);
|
|
|
|
slice = Slice();
|
|
|
|
if (full) {
|
|
|
|
slice.flags |= Slice::Flag::FullInCache;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-08 11:23:34 +00:00
|
|
|
QByteArray Reader::Slices::serializeComplexSlice(const Slice &slice) const {
|
|
|
|
auto result = QByteArray();
|
|
|
|
const auto count = slice.parts.size();
|
|
|
|
const auto intSize = sizeof(int32);
|
|
|
|
result.reserve(count * kPartSize + 2 * intSize * (count + 1));
|
|
|
|
const auto appendInt = [&](int value) {
|
|
|
|
auto serialized = int32(value);
|
|
|
|
result.append(
|
|
|
|
reinterpret_cast<const char*>(&serialized),
|
|
|
|
intSize);
|
|
|
|
};
|
|
|
|
appendInt(count);
|
|
|
|
for (const auto &[offset, part] : slice.parts) {
|
|
|
|
appendInt(offset);
|
|
|
|
appendInt(part.size());
|
|
|
|
result.append(part);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray Reader::Slices::serializeAndUnloadFirstSliceNoHeader() {
|
|
|
|
Expects(_data[0].flags & Slice::Flag::LoadedFromCache);
|
|
|
|
|
|
|
|
auto &slice = _data[0];
|
|
|
|
for (const auto &[offset, part] : _header.parts) {
|
|
|
|
slice.parts.erase(offset);
|
|
|
|
}
|
|
|
|
auto result = serializeComplexSlice(slice);
|
2019-05-31 16:45:03 +00:00
|
|
|
unloadSlice(slice);
|
2019-02-25 17:26:08 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Reader::SerializedSlice Reader::Slices::unloadToCache() {
|
|
|
|
if (_headerMode == HeaderMode::Unknown
|
|
|
|
|| _headerMode == HeaderMode::NoCache) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
if (_header.flags & Slice::Flag::ChangedSinceCache) {
|
2019-02-26 13:57:47 +00:00
|
|
|
return serializeAndUnloadSlice(0);
|
2019-02-25 17:26:08 +00:00
|
|
|
}
|
2019-02-26 13:57:47 +00:00
|
|
|
for (auto i = 0, count = int(_data.size()); i != count; ++i) {
|
|
|
|
if (_data[i].flags & Slice::Flag::ChangedSinceCache) {
|
|
|
|
return serializeAndUnloadSlice(i + 1);
|
2019-02-25 17:26:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-02-13 18:10:18 +00:00
|
|
|
Reader::Reader(
|
2019-06-26 10:01:04 +00:00
|
|
|
not_null<Storage::Cache::Database*> cache,
|
2019-02-13 18:10:18 +00:00
|
|
|
std::unique_ptr<Loader> loader)
|
2019-06-26 10:01:04 +00:00
|
|
|
: _cache(cache)
|
2019-02-25 17:26:08 +00:00
|
|
|
, _loader(std::move(loader))
|
|
|
|
, _cacheHelper(InitCacheHelper(_loader->baseCacheKey()))
|
|
|
|
, _slices(_loader->size(), _cacheHelper != nullptr) {
|
2019-02-13 18:10:18 +00:00
|
|
|
_loader->parts(
|
|
|
|
) | rpl::start_with_next([=](LoadedPart &&part) {
|
2019-04-12 09:25:00 +00:00
|
|
|
if (_attachedDownloader) {
|
2019-04-11 07:59:18 +00:00
|
|
|
_partsForDownloader.fire_copy(part);
|
|
|
|
}
|
2019-04-12 09:25:00 +00:00
|
|
|
if (_streamingActive) {
|
|
|
|
_loadedParts.emplace(std::move(part));
|
|
|
|
}
|
2019-04-11 10:52:49 +00:00
|
|
|
if (const auto waiting = _waiting.load(std::memory_order_acquire)) {
|
|
|
|
_waiting.store(nullptr, std::memory_order_release);
|
2019-02-13 18:10:18 +00:00
|
|
|
waiting->release();
|
|
|
|
}
|
|
|
|
}, _lifetime);
|
2019-02-25 17:26:08 +00:00
|
|
|
|
|
|
|
if (_cacheHelper) {
|
|
|
|
readFromCache(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-11 10:52:49 +00:00
|
|
|
void Reader::startSleep(not_null<crl::semaphore*> wake) {
|
|
|
|
_sleeping.store(wake, std::memory_order_release);
|
|
|
|
processDownloaderRequests();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reader::wakeFromSleep() {
|
|
|
|
if (const auto sleeping = _sleeping.load(std::memory_order_acquire)) {
|
|
|
|
_sleeping.store(nullptr, std::memory_order_release);
|
|
|
|
sleeping->release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reader::stopSleep() {
|
|
|
|
_sleeping.store(nullptr, std::memory_order_release);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reader::startStreaming() {
|
|
|
|
_streamingActive = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reader::stopStreaming(bool stillActive) {
|
|
|
|
Expects(_sleeping == nullptr);
|
|
|
|
|
|
|
|
_waiting.store(nullptr, std::memory_order_release);
|
|
|
|
if (!stillActive) {
|
|
|
|
_streamingActive = false;
|
2019-04-12 09:25:00 +00:00
|
|
|
_loadingOffsets.clear();
|
2019-04-11 10:52:49 +00:00
|
|
|
processDownloaderRequests();
|
|
|
|
}
|
2019-03-04 11:28:52 +00:00
|
|
|
}
|
|
|
|
|
2019-04-11 07:59:18 +00:00
|
|
|
rpl::producer<LoadedPart> Reader::partsForDownloader() const {
|
|
|
|
return _partsForDownloader.events();
|
|
|
|
}
|
|
|
|
|
2019-04-12 09:25:00 +00:00
|
|
|
void Reader::loadForDownloader(
|
2019-12-04 06:51:21 +00:00
|
|
|
not_null<Storage::StreamedFileDownloader*> downloader,
|
2019-04-12 09:25:00 +00:00
|
|
|
int offset) {
|
|
|
|
if (_attachedDownloader != downloader) {
|
|
|
|
if (_attachedDownloader) {
|
|
|
|
cancelForDownloader(_attachedDownloader);
|
|
|
|
}
|
|
|
|
_attachedDownloader = downloader;
|
|
|
|
_loader->attachDownloader(downloader);
|
|
|
|
}
|
2019-04-11 07:59:18 +00:00
|
|
|
_downloaderOffsetRequests.emplace(offset);
|
2019-04-11 10:52:49 +00:00
|
|
|
if (_streamingActive) {
|
|
|
|
wakeFromSleep();
|
|
|
|
} else {
|
|
|
|
processDownloaderRequests();
|
|
|
|
}
|
2019-04-11 07:59:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-12 07:34:39 +00:00
|
|
|
void Reader::doneForDownloader(int offset) {
|
2019-04-12 09:25:00 +00:00
|
|
|
_downloaderOffsetAcks.emplace(offset);
|
|
|
|
if (!_streamingActive) {
|
2019-04-12 07:34:39 +00:00
|
|
|
processDownloaderRequests();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 09:25:00 +00:00
|
|
|
void Reader::cancelForDownloader(
|
2019-12-04 06:51:21 +00:00
|
|
|
not_null<Storage::StreamedFileDownloader*> downloader) {
|
2019-04-12 09:25:00 +00:00
|
|
|
if (_attachedDownloader == downloader) {
|
2019-04-11 07:59:18 +00:00
|
|
|
_downloaderOffsetRequests.take();
|
2019-04-12 09:25:00 +00:00
|
|
|
_attachedDownloader = nullptr;
|
|
|
|
_loader->clearAttachedDownloader();
|
2019-04-11 07:59:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-11 13:07:48 +00:00
|
|
|
void Reader::enqueueDownloaderOffsets() {
|
|
|
|
auto offsets = _downloaderOffsetRequests.take();
|
|
|
|
if (!empty(offsets)) {
|
|
|
|
if (!empty(_offsetsForDownloader)) {
|
|
|
|
_offsetsForDownloader.insert(
|
|
|
|
end(_offsetsForDownloader),
|
|
|
|
std::make_move_iterator(begin(offsets)),
|
|
|
|
std::make_move_iterator(end(offsets)));
|
|
|
|
checkForDownloaderChange(offsets.size() + 1);
|
|
|
|
} else {
|
|
|
|
_offsetsForDownloader = std::move(offsets);
|
|
|
|
checkForDownloaderChange(offsets.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reader::checkForDownloaderChange(int checkItemsCount) {
|
|
|
|
Expects(checkItemsCount <= _offsetsForDownloader.size());
|
|
|
|
|
|
|
|
// If a requested offset is less-or-equal of some previously requested
|
|
|
|
// offset, it means that the downloader was changed, ignore old offsets.
|
|
|
|
const auto end = _offsetsForDownloader.end();
|
|
|
|
const auto changed = std::adjacent_find(
|
|
|
|
end - checkItemsCount,
|
|
|
|
end,
|
|
|
|
[](int first, int second) { return (second <= first); });
|
|
|
|
if (changed != end) {
|
|
|
|
_offsetsForDownloader.erase(
|
|
|
|
begin(_offsetsForDownloader),
|
|
|
|
changed + 1);
|
2019-04-12 10:50:41 +00:00
|
|
|
_downloaderReadCache.clear();
|
|
|
|
_downloaderOffsetAcks.take();
|
2019-04-11 13:07:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reader::checkForDownloaderReadyOffsets() {
|
|
|
|
// If a requested part is available right now we simply fire it on the
|
|
|
|
// main thread, until the first not-available-right-now offset is found.
|
2019-04-12 10:50:41 +00:00
|
|
|
const auto unavailableInBytes = [&](int offset, QByteArray &&bytes) {
|
|
|
|
if (bytes.isEmpty()) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-04-12 07:34:39 +00:00
|
|
|
crl::on_main(this, [=, bytes = std::move(bytes)]() mutable {
|
|
|
|
_partsForDownloader.fire({ offset, std::move(bytes) });
|
|
|
|
});
|
2019-04-12 10:50:41 +00:00
|
|
|
return false;
|
2019-04-12 07:34:39 +00:00
|
|
|
};
|
2019-04-12 10:50:41 +00:00
|
|
|
const auto unavailableInCache = [&](int offset) {
|
|
|
|
const auto index = (offset / kInSlice);
|
|
|
|
const auto sliceNumber = index + 1;
|
|
|
|
const auto i = _downloaderReadCache.find(sliceNumber);
|
|
|
|
if (i == end(_downloaderReadCache) || !i->second) {
|
|
|
|
return true;
|
2019-04-12 07:34:39 +00:00
|
|
|
}
|
2019-04-12 10:50:41 +00:00
|
|
|
const auto j = i->second->find(offset - index * kInSlice);
|
|
|
|
if (j == end(*i->second)) {
|
|
|
|
return true;
|
2019-04-11 13:07:48 +00:00
|
|
|
}
|
2019-04-12 10:50:41 +00:00
|
|
|
return unavailableInBytes(offset, std::move(j->second));
|
|
|
|
};
|
|
|
|
const auto unavailable = [&](int offset) {
|
|
|
|
return unavailableInBytes(offset, _slices.partForDownloader(offset))
|
|
|
|
&& unavailableInCache(offset);
|
2019-04-11 13:07:48 +00:00
|
|
|
};
|
|
|
|
_offsetsForDownloader.erase(
|
|
|
|
begin(_offsetsForDownloader),
|
|
|
|
ranges::find_if(_offsetsForDownloader, unavailable));
|
|
|
|
}
|
|
|
|
|
2019-04-11 10:52:49 +00:00
|
|
|
void Reader::processDownloaderRequests() {
|
2019-04-12 07:34:39 +00:00
|
|
|
processCacheResults();
|
2019-04-11 13:07:48 +00:00
|
|
|
enqueueDownloaderOffsets();
|
|
|
|
checkForDownloaderReadyOffsets();
|
2019-04-12 10:50:41 +00:00
|
|
|
pruneDoneDownloaderRequests();
|
|
|
|
if (!empty(_offsetsForDownloader)) {
|
|
|
|
pruneDownloaderCache(_offsetsForDownloader.front());
|
|
|
|
sendDownloaderRequests();
|
2019-04-11 13:07:48 +00:00
|
|
|
}
|
2019-04-12 10:50:41 +00:00
|
|
|
}
|
2019-04-11 13:07:48 +00:00
|
|
|
|
2019-04-12 10:50:41 +00:00
|
|
|
void Reader::pruneDownloaderCache(int minimalOffset) {
|
|
|
|
const auto minimalSliceNumber = (minimalOffset / kInSlice) + 1;
|
|
|
|
const auto removeTill = ranges::lower_bound(
|
|
|
|
_downloaderReadCache,
|
|
|
|
minimalSliceNumber,
|
|
|
|
ranges::less(),
|
|
|
|
&base::flat_map<int, std::optional<PartsMap>>::value_type::first);
|
|
|
|
_downloaderReadCache.erase(_downloaderReadCache.begin(), removeTill);
|
|
|
|
}
|
2019-04-11 10:52:49 +00:00
|
|
|
|
2019-04-12 10:50:41 +00:00
|
|
|
void Reader::pruneDoneDownloaderRequests() {
|
2019-04-12 09:25:00 +00:00
|
|
|
for (const auto done : _downloaderOffsetAcks.take()) {
|
|
|
|
_downloaderOffsetsRequested.remove(done);
|
2019-04-12 10:50:41 +00:00
|
|
|
const auto i = ranges::find(_offsetsForDownloader, done);
|
|
|
|
if (i != end(_offsetsForDownloader)) {
|
|
|
|
_offsetsForDownloader.erase(i);
|
|
|
|
}
|
2019-04-12 09:25:00 +00:00
|
|
|
}
|
2019-04-12 10:50:41 +00:00
|
|
|
}
|
2019-04-12 09:25:00 +00:00
|
|
|
|
2019-04-12 10:50:41 +00:00
|
|
|
void Reader::sendDownloaderRequests() {
|
|
|
|
auto &&offsets = ranges::view::all(
|
|
|
|
_offsetsForDownloader
|
|
|
|
) | ranges::view::take(kDownloaderRequestsLimit);
|
|
|
|
for (const auto offset : offsets) {
|
|
|
|
if ((!_cacheHelper || !downloaderWaitForCachedSlice(offset))
|
|
|
|
&& _downloaderOffsetsRequested.emplace(offset).second) {
|
|
|
|
_loader->load(offset);
|
|
|
|
}
|
2019-04-12 07:34:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Reader::downloaderWaitForCachedSlice(int offset) {
|
2019-04-12 08:00:06 +00:00
|
|
|
if (_slices.waitingForHeaderCache()) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-04-12 10:50:41 +00:00
|
|
|
if (!_slices.readCacheForDownloaderRequired(offset)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const auto sliceNumber = (offset / kInSlice) + 1;
|
|
|
|
auto i = _downloaderReadCache.find(sliceNumber);
|
|
|
|
if (i == _downloaderReadCache.end()) {
|
|
|
|
// If we didn't request that slice yet, try requesting it.
|
|
|
|
// If there is no need to (header mode is unknown) - place empty map.
|
|
|
|
// Otherwise place std::nullopt and wait for the cache result.
|
|
|
|
i = _downloaderReadCache.emplace(
|
|
|
|
sliceNumber,
|
|
|
|
(readFromCacheForDownloader(sliceNumber)
|
|
|
|
? std::nullopt
|
|
|
|
: std::make_optional(PartsMap()))).first;
|
2019-04-12 07:34:39 +00:00
|
|
|
}
|
2019-04-12 10:50:41 +00:00
|
|
|
return !i->second;
|
2019-04-11 10:52:49 +00:00
|
|
|
}
|
|
|
|
|
2019-04-11 11:56:26 +00:00
|
|
|
void Reader::checkCacheResultsForDownloader() {
|
2019-04-11 13:07:48 +00:00
|
|
|
if (_streamingActive) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
processDownloaderRequests();
|
2019-04-11 11:56:26 +00:00
|
|
|
}
|
|
|
|
|
2019-03-05 13:56:27 +00:00
|
|
|
bool Reader::isRemoteLoader() const {
|
|
|
|
return _loader->baseCacheKey().has_value();
|
|
|
|
}
|
|
|
|
|
2019-02-25 17:26:08 +00:00
|
|
|
std::shared_ptr<Reader::CacheHelper> Reader::InitCacheHelper(
|
|
|
|
std::optional<Storage::Cache::Key> baseKey) {
|
|
|
|
if (!baseKey) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return std::make_shared<Reader::CacheHelper>(*baseKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 0 is for headerData, slice index = sliceNumber - 1.
|
|
|
|
void Reader::readFromCache(int sliceNumber) {
|
|
|
|
Expects(_cacheHelper != nullptr);
|
2019-03-08 11:23:34 +00:00
|
|
|
Expects(!sliceNumber || !_slices.headerModeUnknown());
|
2019-02-25 17:26:08 +00:00
|
|
|
|
2019-03-08 11:23:34 +00:00
|
|
|
if (sliceNumber == 1 && _slices.isGoodHeader()) {
|
2019-03-10 07:37:49 +00:00
|
|
|
return readFromCache(0);
|
2019-03-08 11:23:34 +00:00
|
|
|
}
|
2019-04-11 11:56:26 +00:00
|
|
|
const auto size = _loader->size();
|
2019-02-25 17:26:08 +00:00
|
|
|
const auto key = _cacheHelper->key(sliceNumber);
|
2019-04-11 11:56:26 +00:00
|
|
|
const auto cache = std::weak_ptr<CacheHelper>(_cacheHelper);
|
|
|
|
const auto weak = base::make_weak(this);
|
2019-05-31 16:45:03 +00:00
|
|
|
const auto ready = [=](
|
|
|
|
QByteArray &&result,
|
|
|
|
std::vector<int> &&sizes = {}) {
|
|
|
|
crl::async([
|
|
|
|
=,
|
|
|
|
result = std::move(result),
|
|
|
|
sizes = std::move(sizes)
|
|
|
|
]() mutable{
|
2019-04-11 11:56:26 +00:00
|
|
|
auto entry = ParseCacheEntry(
|
|
|
|
bytes::make_span(result),
|
|
|
|
sliceNumber,
|
|
|
|
size);
|
|
|
|
if (const auto strong = cache.lock()) {
|
|
|
|
QMutexLocker lock(&strong->mutex);
|
|
|
|
strong->results.emplace(sliceNumber, std::move(entry.parts));
|
|
|
|
if (!sliceNumber && entry.included) {
|
|
|
|
strong->results.emplace(1, std::move(*entry.included));
|
|
|
|
}
|
2019-05-31 16:45:03 +00:00
|
|
|
strong->sizes = std::move(sizes);
|
2019-04-11 11:56:26 +00:00
|
|
|
if (const auto waiting = strong->waiting.load()) {
|
|
|
|
strong->waiting.store(nullptr, std::memory_order_release);
|
|
|
|
waiting->release();
|
|
|
|
} else {
|
|
|
|
crl::on_main(weak, [=] {
|
|
|
|
checkCacheResultsForDownloader();
|
|
|
|
});
|
|
|
|
}
|
2019-02-25 17:26:08 +00:00
|
|
|
}
|
2019-04-11 11:56:26 +00:00
|
|
|
});
|
2019-05-31 16:45:03 +00:00
|
|
|
};
|
|
|
|
auto keys = std::vector<Storage::Cache::Key>();
|
|
|
|
const auto count = _slices.requestSliceSizesCount();
|
|
|
|
for (auto i = 0; i != count; ++i) {
|
|
|
|
keys.push_back(_cacheHelper->key(i + 1));
|
|
|
|
}
|
2019-06-26 10:01:04 +00:00
|
|
|
_cache->getWithSizes(key, std::move(keys), ready);
|
2019-02-25 17:26:08 +00:00
|
|
|
}
|
|
|
|
|
2019-04-12 10:50:41 +00:00
|
|
|
bool Reader::readFromCacheForDownloader(int sliceNumber) {
|
2019-04-12 07:34:39 +00:00
|
|
|
Expects(_cacheHelper != nullptr);
|
2019-04-12 10:50:41 +00:00
|
|
|
Expects(sliceNumber > 0);
|
2019-04-12 07:34:39 +00:00
|
|
|
|
|
|
|
if (_slices.headerModeUnknown()) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-12 10:50:41 +00:00
|
|
|
readFromCache(sliceNumber);
|
2019-04-12 07:34:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-25 17:26:08 +00:00
|
|
|
void Reader::putToCache(SerializedSlice &&slice) {
|
|
|
|
Expects(_cacheHelper != nullptr);
|
|
|
|
Expects(slice.number >= 0);
|
|
|
|
|
2019-06-26 10:01:04 +00:00
|
|
|
_cache->put(_cacheHelper->key(slice.number), std::move(slice.data));
|
2019-02-13 18:10:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Reader::size() const {
|
|
|
|
return _loader->size();
|
|
|
|
}
|
|
|
|
|
2019-04-12 07:34:39 +00:00
|
|
|
std::optional<Error> Reader::streamingError() const {
|
|
|
|
return _streamingError;
|
2019-02-13 18:10:18 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 17:26:08 +00:00
|
|
|
void Reader::headerDone() {
|
|
|
|
_slices.headerDone(false);
|
|
|
|
}
|
2019-03-12 14:56:35 +00:00
|
|
|
|
2019-05-31 11:45:35 +00:00
|
|
|
int Reader::headerSize() const {
|
|
|
|
return _slices.headerSize();
|
|
|
|
}
|
|
|
|
|
2019-05-31 16:45:03 +00:00
|
|
|
bool Reader::fullInCache() const {
|
|
|
|
return _slices.fullInCache();
|
|
|
|
}
|
|
|
|
|
2019-02-13 18:10:18 +00:00
|
|
|
bool Reader::fill(
|
|
|
|
int offset,
|
2019-02-25 17:26:08 +00:00
|
|
|
bytes::span buffer,
|
|
|
|
not_null<crl::semaphore*> notify) {
|
2019-02-13 18:10:18 +00:00
|
|
|
Expects(offset + buffer.size() <= size());
|
|
|
|
|
2019-02-25 17:26:08 +00:00
|
|
|
const auto startWaiting = [&] {
|
|
|
|
if (_cacheHelper) {
|
|
|
|
_cacheHelper->waiting = notify.get();
|
|
|
|
}
|
2019-04-11 10:52:49 +00:00
|
|
|
_waiting.store(notify.get(), std::memory_order_release);
|
2019-02-13 18:10:18 +00:00
|
|
|
};
|
2019-02-25 17:26:08 +00:00
|
|
|
const auto clearWaiting = [&] {
|
2019-04-11 10:52:49 +00:00
|
|
|
_waiting.store(nullptr, std::memory_order_release);
|
2019-02-25 17:26:08 +00:00
|
|
|
if (_cacheHelper) {
|
2019-04-11 10:52:49 +00:00
|
|
|
_cacheHelper->waiting.store(nullptr, std::memory_order_release);
|
2019-02-25 17:26:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
const auto done = [&] {
|
|
|
|
clearWaiting();
|
2019-02-13 18:10:18 +00:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
const auto failed = [&] {
|
2019-02-25 17:26:08 +00:00
|
|
|
clearWaiting();
|
|
|
|
notify->release();
|
2019-02-13 18:10:18 +00:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2019-04-11 13:07:48 +00:00
|
|
|
checkForSomethingMoreReceived();
|
2019-04-12 07:34:39 +00:00
|
|
|
if (_streamingError) {
|
2019-02-13 18:10:18 +00:00
|
|
|
return failed();
|
|
|
|
}
|
|
|
|
|
2019-02-25 17:26:08 +00:00
|
|
|
do {
|
|
|
|
if (fillFromSlices(offset, buffer)) {
|
|
|
|
clearWaiting();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
startWaiting();
|
2019-04-11 13:07:48 +00:00
|
|
|
} while (checkForSomethingMoreReceived());
|
2019-02-25 17:26:08 +00:00
|
|
|
|
2019-04-12 07:34:39 +00:00
|
|
|
return _streamingError ? failed() : false;
|
2019-02-25 17:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Reader::fillFromSlices(int offset, bytes::span buffer) {
|
|
|
|
using namespace rpl::mappers;
|
|
|
|
|
|
|
|
auto result = _slices.fill(offset, buffer);
|
2019-03-05 13:56:27 +00:00
|
|
|
if (!result.filled && _slices.headerWontBeFilled()) {
|
2019-04-12 07:34:39 +00:00
|
|
|
_streamingError = Error::NotStreamable;
|
2019-03-05 13:56:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-02-25 17:26:08 +00:00
|
|
|
|
|
|
|
for (const auto sliceNumber : result.sliceNumbersFromCache.values()) {
|
|
|
|
readFromCache(sliceNumber);
|
2019-02-13 18:10:18 +00:00
|
|
|
}
|
|
|
|
|
2019-03-08 11:23:34 +00:00
|
|
|
if (_cacheHelper && result.toCache.number >= 0) {
|
|
|
|
// If we put to cache the header (number == 0) that means we're in
|
|
|
|
// HeaderMode::Good and really are putting the first slice to cache.
|
|
|
|
Assert(result.toCache.number > 0 || _slices.isGoodHeader());
|
|
|
|
|
2019-03-20 09:41:13 +00:00
|
|
|
const auto index = std::max(result.toCache.number, 1) - 1;
|
2019-02-25 17:26:08 +00:00
|
|
|
cancelLoadInRange(index * kInSlice, (index + 1) * kInSlice);
|
|
|
|
putToCache(std::move(result.toCache));
|
2019-02-13 18:10:18 +00:00
|
|
|
}
|
2019-02-25 17:26:08 +00:00
|
|
|
auto checkPriority = true;
|
|
|
|
for (const auto offset : result.offsetsFromLoader.values()) {
|
|
|
|
if (checkPriority) {
|
|
|
|
checkLoadWillBeFirst(offset);
|
|
|
|
checkPriority = false;
|
|
|
|
}
|
|
|
|
loadAtOffset(offset);
|
|
|
|
}
|
|
|
|
return result.filled;
|
2019-02-13 18:10:18 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 17:26:08 +00:00
|
|
|
void Reader::cancelLoadInRange(int from, int till) {
|
|
|
|
Expects(from < till);
|
|
|
|
|
|
|
|
for (const auto offset : _loadingOffsets.takeInRange(from, till)) {
|
2019-04-12 07:34:39 +00:00
|
|
|
if (!_downloaderOffsetsRequested.contains(offset)) {
|
|
|
|
_loader->cancel(offset);
|
|
|
|
}
|
2019-02-25 17:26:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reader::checkLoadWillBeFirst(int offset) {
|
|
|
|
if (_loadingOffsets.front().value_or(offset) != offset) {
|
|
|
|
_loadingOffsets.increasePriority();
|
|
|
|
_loader->increasePriority();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Reader::processCacheResults() {
|
|
|
|
if (!_cacheHelper) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QMutexLocker lock(&_cacheHelper->mutex);
|
2019-04-11 11:56:26 +00:00
|
|
|
auto loaded = base::take(_cacheHelper->results);
|
2019-05-31 16:45:03 +00:00
|
|
|
auto sizes = base::take(_cacheHelper->sizes);
|
2019-02-13 18:10:18 +00:00
|
|
|
lock.unlock();
|
|
|
|
|
2019-04-12 10:50:41 +00:00
|
|
|
for (auto &[sliceNumber, cachedParts] : _downloaderReadCache) {
|
|
|
|
if (!cachedParts) {
|
|
|
|
const auto i = loaded.find(sliceNumber);
|
|
|
|
if (i != end(loaded)) {
|
|
|
|
cachedParts = i->second;
|
|
|
|
}
|
2019-04-12 07:34:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_streamingError) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-11 11:56:26 +00:00
|
|
|
for (auto &[sliceNumber, result] : loaded) {
|
|
|
|
_slices.processCacheResult(sliceNumber, std::move(result));
|
|
|
|
}
|
2019-05-31 16:45:03 +00:00
|
|
|
if (!sizes.empty()) {
|
|
|
|
_slices.processCachedSizes(sizes);
|
|
|
|
}
|
2019-04-11 11:56:26 +00:00
|
|
|
if (!loaded.empty()
|
|
|
|
&& (loaded.front().first == 0)
|
|
|
|
&& _slices.isGoodHeader()) {
|
|
|
|
Assert(loaded.size() > 1);
|
|
|
|
Assert((loaded.begin() + 1)->first == 1);
|
2019-02-25 17:26:08 +00:00
|
|
|
}
|
|
|
|
return !loaded.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Reader::processLoadedParts() {
|
2019-04-12 07:34:39 +00:00
|
|
|
if (_streamingError) {
|
2019-02-25 17:26:08 +00:00
|
|
|
return false;
|
2019-02-13 18:10:18 +00:00
|
|
|
}
|
2019-02-25 17:26:08 +00:00
|
|
|
|
2019-04-11 07:59:18 +00:00
|
|
|
auto loaded = _loadedParts.take();
|
2019-02-13 18:10:18 +00:00
|
|
|
for (auto &part : loaded) {
|
2019-04-11 13:07:48 +00:00
|
|
|
if (!part.valid(size())) {
|
2019-04-12 07:34:39 +00:00
|
|
|
_streamingError = Error::LoadFailed;
|
2019-02-25 17:26:08 +00:00
|
|
|
return false;
|
|
|
|
} else if (!_loadingOffsets.remove(part.offset)) {
|
|
|
|
continue;
|
2019-03-05 13:56:27 +00:00
|
|
|
}
|
2019-03-06 14:05:11 +00:00
|
|
|
_slices.processPart(
|
2019-03-05 13:56:27 +00:00
|
|
|
part.offset,
|
|
|
|
std::move(part.bytes));
|
2019-02-25 17:26:08 +00:00
|
|
|
}
|
|
|
|
return !loaded.empty();
|
|
|
|
}
|
|
|
|
|
2019-04-11 13:07:48 +00:00
|
|
|
bool Reader::checkForSomethingMoreReceived() {
|
|
|
|
const auto result1 = processCacheResults();
|
|
|
|
const auto result2 = processLoadedParts();
|
|
|
|
return result1 || result2;
|
|
|
|
}
|
|
|
|
|
2019-02-25 17:26:08 +00:00
|
|
|
void Reader::loadAtOffset(int offset) {
|
|
|
|
if (_loadingOffsets.add(offset)) {
|
|
|
|
_loader->load(offset);
|
2019-02-13 18:10:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-25 17:26:08 +00:00
|
|
|
void Reader::finalizeCache() {
|
|
|
|
if (!_cacheHelper) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (_cacheHelper->waiting != nullptr) {
|
|
|
|
QMutexLocker lock(&_cacheHelper->mutex);
|
2019-04-11 10:52:49 +00:00
|
|
|
_cacheHelper->waiting.store(nullptr, std::memory_order_release);
|
2019-02-25 17:26:08 +00:00
|
|
|
}
|
|
|
|
auto toCache = _slices.unloadToCache();
|
|
|
|
while (toCache.number >= 0) {
|
|
|
|
putToCache(std::move(toCache));
|
|
|
|
toCache = _slices.unloadToCache();
|
|
|
|
}
|
2019-06-26 10:01:04 +00:00
|
|
|
_cache->sync();
|
2019-02-13 18:10:18 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 17:26:08 +00:00
|
|
|
Reader::~Reader() {
|
|
|
|
finalizeCache();
|
|
|
|
}
|
2019-02-13 18:10:18 +00:00
|
|
|
|
|
|
|
} // namespace Streaming
|
|
|
|
} // namespace Media
|