2018-08-21 18:53:04 +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 "storage/cache/storage_cache_binlog_reader.h"
|
|
|
|
|
|
|
|
namespace Storage {
|
|
|
|
namespace Cache {
|
|
|
|
namespace details {
|
|
|
|
|
2018-08-21 18:53:11 +00:00
|
|
|
BinlogWrapper::BinlogWrapper(
|
|
|
|
File &binlog,
|
|
|
|
const Settings &settings,
|
|
|
|
int64 till)
|
2018-08-21 18:53:04 +00:00
|
|
|
: _binlog(binlog)
|
|
|
|
, _settings(settings)
|
2018-08-21 18:53:11 +00:00
|
|
|
, _till(till ? till : _binlog.size())
|
2018-08-21 18:53:04 +00:00
|
|
|
, _data(_settings.readBlockSize)
|
|
|
|
, _full(_data) {
|
|
|
|
}
|
|
|
|
|
2018-08-21 18:53:11 +00:00
|
|
|
bool BinlogWrapper::finished() const {
|
|
|
|
return _finished;
|
|
|
|
}
|
|
|
|
|
2018-08-21 18:53:04 +00:00
|
|
|
bool BinlogWrapper::failed() const {
|
|
|
|
return _failed;
|
|
|
|
}
|
|
|
|
|
2018-08-25 10:06:54 +00:00
|
|
|
base::optional<BasicHeader> BinlogWrapper::ReadHeader(
|
|
|
|
File &binlog,
|
|
|
|
const Settings &settings) {
|
|
|
|
auto result = BasicHeader();
|
|
|
|
if (binlog.offset() != 0) {
|
|
|
|
return {};
|
|
|
|
} else if (binlog.read(bytes::object_as_span(&result)) != sizeof(result)) {
|
|
|
|
return {};
|
|
|
|
} else if (result.format != Format::Format_0) {
|
|
|
|
return {};
|
|
|
|
} else if (settings.trackEstimatedTime
|
|
|
|
!= !!(result.flags & result.kTrackEstimatedTime)) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-08-21 18:53:04 +00:00
|
|
|
bool BinlogWrapper::readPart() {
|
|
|
|
if (_finished) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const auto no = [&] {
|
|
|
|
finish();
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
const auto offset = _binlog.offset();
|
|
|
|
const auto left = (_till - offset);
|
|
|
|
if (!left) {
|
|
|
|
return no();
|
|
|
|
}
|
|
|
|
|
2018-08-25 10:06:54 +00:00
|
|
|
if (!_part.empty() && _full.data() != _part.data()) {
|
|
|
|
bytes::move(_full, _part);
|
|
|
|
_part = _full.subspan(0, _part.size());
|
2018-08-21 18:53:04 +00:00
|
|
|
}
|
|
|
|
const auto amount = std::min(
|
|
|
|
left,
|
2018-08-25 10:06:54 +00:00
|
|
|
int64(_full.size() - _part.size()));
|
2018-08-21 18:53:04 +00:00
|
|
|
Assert(amount > 0);
|
|
|
|
const auto readBytes = _binlog.read(
|
2018-08-25 10:06:54 +00:00
|
|
|
_full.subspan(_part.size(), amount));
|
2018-08-21 18:53:04 +00:00
|
|
|
if (!readBytes) {
|
|
|
|
return no();
|
|
|
|
}
|
2018-08-25 10:06:54 +00:00
|
|
|
_part = _full.subspan(0, _part.size() + readBytes);
|
2018-08-21 18:53:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes::const_span BinlogWrapper::readRecord(ReadRecordSize readRecordSize) {
|
|
|
|
if (_finished) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
const auto size = readRecordSize(*this, _part);
|
|
|
|
if (size == kRecordSizeUnknown || size > _part.size()) {
|
|
|
|
return {};
|
|
|
|
} else if (size == kRecordSizeInvalid) {
|
|
|
|
finish();
|
|
|
|
_finished = _failed = true;
|
|
|
|
return {};
|
|
|
|
}
|
2018-08-25 10:06:54 +00:00
|
|
|
Assert(size >= 0);
|
2018-08-21 18:53:04 +00:00
|
|
|
const auto result = _part.subspan(0, size);
|
|
|
|
_part = _part.subspan(size);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BinlogWrapper::finish(size_type rollback) {
|
|
|
|
Expects(rollback >= 0);
|
|
|
|
|
|
|
|
if (rollback > 0) {
|
|
|
|
_failed = true;
|
|
|
|
}
|
2018-08-25 10:06:54 +00:00
|
|
|
rollback += _part.size();
|
|
|
|
_binlog.seek(_binlog.offset() - rollback);
|
2018-08-21 18:53:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace details
|
|
|
|
} // namespace Cache
|
|
|
|
} // namespace Storage
|