From 8210a51fdcd70ce04f37cb5ca800db90270e0421 Mon Sep 17 00:00:00 2001 From: John Preston Date: Wed, 29 Aug 2018 22:07:47 +0300 Subject: [PATCH] Fix build for Xcode. Also guard database compactor so that it won't work after closing. --- Telegram/SourceFiles/base/concurrent_timer.h | 1 - .../storage/cache/storage_cache_compactor.cpp | 21 ++++++++-- .../storage/cache/storage_cache_compactor.h | 2 + .../cache/storage_cache_database_object.cpp | 15 ++++--- .../cache/storage_cache_database_object.h | 1 + .../storage/cache/storage_cache_types.cpp | 39 +++++++++++++++++++ .../storage/cache/storage_cache_types.h | 31 ++------------- .../SourceFiles/storage/storage_databases.cpp | 8 ++-- 8 files changed, 76 insertions(+), 42 deletions(-) diff --git a/Telegram/SourceFiles/base/concurrent_timer.h b/Telegram/SourceFiles/base/concurrent_timer.h index 1be2af1b7..b279b1d1e 100644 --- a/Telegram/SourceFiles/base/concurrent_timer.h +++ b/Telegram/SourceFiles/base/concurrent_timer.h @@ -129,7 +129,6 @@ private: base::binary_guard _running; TimeMs _next = 0; int _timeout = 0; - int _timerId = 0; Qt::TimerType _type : 2; bool _adjusted : 1; diff --git a/Telegram/SourceFiles/storage/cache/storage_cache_compactor.cpp b/Telegram/SourceFiles/storage/cache/storage_cache_compactor.cpp index c8cab2bcd..d439d9203 100644 --- a/Telegram/SourceFiles/storage/cache/storage_cache_compactor.cpp +++ b/Telegram/SourceFiles/storage/cache/storage_cache_compactor.cpp @@ -22,6 +22,7 @@ public: CompactorObject( crl::weak_on_queue weak, crl::weak_on_queue database, + base::binary_guard guard, const QString &base, const Settings &settings, EncryptionKey &&key, @@ -64,6 +65,7 @@ private: crl::weak_on_queue _weak; crl::weak_on_queue _database; + base::binary_guard _guard; QString _base; Settings _settings; EncryptionKey _key; @@ -83,12 +85,14 @@ private: CompactorObject::CompactorObject( crl::weak_on_queue weak, crl::weak_on_queue database, + base::binary_guard guard, const QString &base, const Settings &settings, EncryptionKey &&key, const Info &info) : _weak(std::move(weak)) , _database(std::move(database)) +, _guard(std::move(guard)) , _base(base) , _settings(settings) , _key(std::move(key)) @@ -170,8 +174,10 @@ void CompactorObject::fail() { void CompactorObject::done(int64 till) { const auto path = compactPath(); - _database.with([=](DatabaseObject &database) { - database.compactorDone(path, till); + _database.with([=, good = std::move(_guard)](DatabaseObject &database) { + if (good.alive()) { + database.compactorDone(path, till); + } }); } @@ -354,7 +360,7 @@ void CompactorObject::addListRecord( } auto record = RecordStore(); record.key = raw.first; - record.size = ReadTo(raw.second.size); + record.setSize(raw.second.size); record.checksum = raw.second.checksum; record.tag = raw.second.tag; record.place = raw.second.place; @@ -367,11 +373,18 @@ void CompactorObject::addListRecord( Compactor::Compactor( crl::weak_on_queue database, + base::binary_guard guard, const QString &base, const Settings &settings, EncryptionKey &&key, const Info &info) -: _wrapped(std::move(database), base, settings, std::move(key), info) { +: _wrapped( + std::move(database), + std::move(guard), + base, + settings, + std::move(key), + info) { } Compactor::~Compactor() = default; diff --git a/Telegram/SourceFiles/storage/cache/storage_cache_compactor.h b/Telegram/SourceFiles/storage/cache/storage_cache_compactor.h index 1752b4358..6b1c0a84d 100644 --- a/Telegram/SourceFiles/storage/cache/storage_cache_compactor.h +++ b/Telegram/SourceFiles/storage/cache/storage_cache_compactor.h @@ -9,6 +9,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "storage/cache/storage_cache_types.h" #include +#include namespace Storage { class EncryptionKey; @@ -28,6 +29,7 @@ public: Compactor( crl::weak_on_queue database, + base::binary_guard guard, const QString &base, const Settings &settings, EncryptionKey &&key, diff --git a/Telegram/SourceFiles/storage/cache/storage_cache_database_object.cpp b/Telegram/SourceFiles/storage/cache/storage_cache_database_object.cpp index caa2517df..df21dd4a7 100644 --- a/Telegram/SourceFiles/storage/cache/storage_cache_database_object.cpp +++ b/Telegram/SourceFiles/storage/cache/storage_cache_database_object.cpp @@ -69,8 +69,8 @@ DatabaseObject::Entry::Entry( : useTime(useTime) , size(size) , checksum(checksum) -, tag(tag) -, place(place) { +, place(place) +, tag(tag) { } DatabaseObject::DatabaseObject( @@ -488,7 +488,7 @@ template bool DatabaseObject::processRecordStoreGeneric( const Record *record, Postprocess &&postprocess) { - const auto size = ReadFrom(record->size); + const auto size = record->getSize(); if (size <= 0 || size > _settings.maxDataSize) { return false; } @@ -738,6 +738,7 @@ void DatabaseObject::clearState() { _map = {}; _removing = {}; _accessed = {}; + _stale = {}; _time = {}; _binlogExcessLength = 0; _totalSize = 0; @@ -747,7 +748,6 @@ void DatabaseObject::clearState() { _pushingStats = false; _writeBundlesTimer.cancel(); _pruneTimer.cancel(); - _cleaner = CleanerWrap(); _compactor = CompactorWrap(); } @@ -816,7 +816,7 @@ base::optional DatabaseObject::writeKeyPlaceGeneric( const auto size = size_type(value.bytes.size()); record.tag = value.tag; record.key = key; - record.size = ReadTo(size); + record.setSize(size); record.checksum = checksum; if (const auto i = _map.find(key); i != end(_map)) { const auto &already = i->second; @@ -876,7 +876,7 @@ Error DatabaseObject::writeExistingPlaceGeneric( const Entry &entry) { record.key = key; record.tag = entry.tag; - record.size = ReadTo(entry.size); + record.setSize(entry.size); record.checksum = entry.checksum; if (const auto i = _map.find(key); i != end(_map)) { const auto &already = i->second; @@ -1187,8 +1187,11 @@ void DatabaseObject::checkCompactor() { info.till = _binlog.size(); info.systemTime = _time.system; info.keysCount = _map.size(); + auto [first, second] = base::make_binary_guard(); + _compactor.guard = std::move(first); _compactor.object = std::make_unique( _weak, + std::move(second), _path, _settings, base::duplicate(_key), diff --git a/Telegram/SourceFiles/storage/cache/storage_cache_database_object.h b/Telegram/SourceFiles/storage/cache/storage_cache_database_object.h index bebed7259..75e4ed4f6 100644 --- a/Telegram/SourceFiles/storage/cache/storage_cache_database_object.h +++ b/Telegram/SourceFiles/storage/cache/storage_cache_database_object.h @@ -96,6 +96,7 @@ private: int64 excessLength = 0; crl::time_type nextAttempt = 0; crl::time_type delayAfterFailure = 10 * crl::time_type(1000); + base::binary_guard guard; }; using Map = std::unordered_map; diff --git a/Telegram/SourceFiles/storage/cache/storage_cache_types.cpp b/Telegram/SourceFiles/storage/cache/storage_cache_types.cpp index 9aba5788f..d785e4a9d 100644 --- a/Telegram/SourceFiles/storage/cache/storage_cache_types.cpp +++ b/Telegram/SourceFiles/storage/cache/storage_cache_types.cpp @@ -12,6 +12,37 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL namespace Storage { namespace Cache { namespace details { +namespace { + +template +inline Packed ReadTo(size_type count) { + Expects(count >= 0 && count < (1 << (Packed().size() * 8))); + + auto result = Packed(); + for (auto &element : result) { + element = uint8(count & 0xFF); + count >>= 8; + } + return result; +} + +template +inline size_type ReadFrom(const Packed &count) { + auto result = size_type(); + for (auto &element : (count | ranges::view::reverse)) { + result <<= 8; + result |= size_type(element); + } + return result; +} + +template +inline size_type ValidateStrictCount(const Packed &count) { + const auto result = ReadFrom(count); + return (result != 0) ? result : -1; +} + +} // namespace TaggedValue::TaggedValue(QByteArray &&bytes, uint8 tag) : bytes(std::move(bytes)), tag(tag) { @@ -61,6 +92,14 @@ BasicHeader::BasicHeader() , flags(0) { } +void Store::setSize(size_type size) { + this->size = ReadTo(size); +} + +size_type Store::getSize() const { + return ReadFrom(size); +} + MultiStore::MultiStore(size_type count) : type(kType) , count(ReadTo(count)) { diff --git a/Telegram/SourceFiles/storage/cache/storage_cache_types.h b/Telegram/SourceFiles/storage/cache/storage_cache_types.h index 582edcffe..e406ad85b 100644 --- a/Telegram/SourceFiles/storage/cache/storage_cache_types.h +++ b/Telegram/SourceFiles/storage/cache/storage_cache_types.h @@ -101,34 +101,6 @@ using PlaceId = std::array; using EntrySize = std::array; using RecordsCount = std::array; -template -inline Packed ReadTo(size_type count) { - Expects(count >= 0 && count < (1 << (Packed().size() * 8))); - - auto result = Packed(); - for (auto &element : result) { - element = uint8(count & 0xFF); - count >>= 8; - } - return result; -} - -template -inline size_type ReadFrom(const Packed &count) { - auto result = size_type(); - for (auto &element : (count | ranges::view::reverse)) { - result <<= 8; - result |= size_type(element); - } - return result; -} - -template -inline size_type ValidateStrictCount(const Packed &count) { - const auto result = ReadFrom(count); - return (result != 0) ? result : -1; -} - constexpr auto kRecordSizeUnknown = size_type(-1); constexpr auto kRecordSizeInvalid = size_type(-2); constexpr auto kBundledRecordsLimit = (1 << (RecordsCount().size() * 8)); @@ -170,6 +142,9 @@ struct EstimatedTimePoint { struct Store { static constexpr auto kType = RecordType(0x01); + void setSize(size_type size); + size_type getSize() const; + RecordType type = kType; uint8 tag = 0; EntrySize size = { { 0 } }; diff --git a/Telegram/SourceFiles/storage/storage_databases.cpp b/Telegram/SourceFiles/storage/storage_databases.cpp index 9f722f393..4fe5d6ba3 100644 --- a/Telegram/SourceFiles/storage/storage_databases.cpp +++ b/Telegram/SourceFiles/storage/storage_databases.cpp @@ -14,8 +14,8 @@ namespace Storage { DatabasePointer::DatabasePointer( not_null owner, const std::unique_ptr &value) -: _owner(owner) -, _value(value.get()) { +: _value(value.get()) +, _owner(owner) { } DatabasePointer::DatabasePointer(DatabasePointer &&other) @@ -83,7 +83,9 @@ DatabasePointer Databases::get( } void Databases::destroy(Cache::Database *database) { - for (auto &[path, kept] : _map) { + for (auto &entry : _map) { + const auto &path = entry.first; // Need to capture it in lambda. + auto &kept = entry.second; if (kept.database.get() == database) { Assert(!kept.destroying.alive()); auto [first, second] = base::make_binary_guard();