From fb322b5fc5389ff5e7123d0af1bf0a06a7c20584 Mon Sep 17 00:00:00 2001 From: John Preston Date: Wed, 20 May 2020 16:28:18 +0400 Subject: [PATCH] Use empty Storage::Cache::Key as nullopt. --- .../SourceFiles/chat_helpers/stickers.cpp | 6 ++-- .../chat_helpers/stickers_emoji_pack.cpp | 6 ++-- Telegram/SourceFiles/data/data_document.cpp | 32 ++++++------------- Telegram/SourceFiles/data/data_document.h | 4 +-- Telegram/SourceFiles/data/data_photo.cpp | 2 +- .../media/streaming/media_streaming_loader.h | 3 +- .../media_streaming_loader_local.cpp | 4 +-- .../streaming/media_streaming_loader_local.h | 3 +- .../media_streaming_loader_mtproto.cpp | 2 +- .../media_streaming_loader_mtproto.h | 3 +- .../streaming/media_streaming_reader.cpp | 6 ++-- .../media/streaming/media_streaming_reader.h | 2 +- Telegram/SourceFiles/storage/localstorage.cpp | 1 - Telegram/SourceFiles/ui/image/image.cpp | 2 +- Telegram/SourceFiles/ui/image/image.h | 4 +-- .../SourceFiles/ui/image/image_location.cpp | 20 ++++++++++++ .../SourceFiles/ui/image/image_location.h | 1 + .../SourceFiles/ui/image/image_source.cpp | 24 +++++++------- Telegram/SourceFiles/ui/image/image_source.h | 12 +++---- Telegram/lib_storage | 2 +- 20 files changed, 71 insertions(+), 68 deletions(-) diff --git a/Telegram/SourceFiles/chat_helpers/stickers.cpp b/Telegram/SourceFiles/chat_helpers/stickers.cpp index 14ceae895..584c6460a 100644 --- a/Telegram/SourceFiles/chat_helpers/stickers.cpp +++ b/Telegram/SourceFiles/chat_helpers/stickers.cpp @@ -1162,7 +1162,7 @@ auto LottieFromDocument( if (const auto baseKey = document->bigFileBaseCacheKey()) { return LottieCachedFromContent( std::forward(method), - *baseKey, + baseKey, keyShift, &document->session(), Lottie::ReadContent(data, filepath), @@ -1239,7 +1239,7 @@ bool HasLottieThumbnail( if (!media->loaded()) { return false; } - return document->bigFileBaseCacheKey().has_value(); + return document->bigFileBaseCacheKey().valid(); } return false; } @@ -1269,7 +1269,7 @@ std::unique_ptr LottieThumbnail( }; return LottieCachedFromContent( method, - *baseKey, + baseKey, uint8(sizeTag), &document->session(), content, diff --git a/Telegram/SourceFiles/chat_helpers/stickers_emoji_pack.cpp b/Telegram/SourceFiles/chat_helpers/stickers_emoji_pack.cpp index b583c61bf..4a60341f4 100644 --- a/Telegram/SourceFiles/chat_helpers/stickers_emoji_pack.cpp +++ b/Telegram/SourceFiles/chat_helpers/stickers_emoji_pack.cpp @@ -141,7 +141,7 @@ public: const StorageImageLocation &location() override; void refreshFileReference(const QByteArray &data) override; - std::optional cacheKey() override; + Storage::Cache::Key cacheKey() override; void setDelayedStorageLocation( const StorageImageLocation &location) override; void performDelayedLoad(Data::FileOrigin origin) override; @@ -256,8 +256,8 @@ const StorageImageLocation &ImageSource::location() { void ImageSource::refreshFileReference(const QByteArray &data) { } -std::optional ImageSource::cacheKey() { - return std::nullopt; +Storage::Cache::Key ImageSource::cacheKey() { + return {}; } void ImageSource::setDelayedStorageLocation( diff --git a/Telegram/SourceFiles/data/data_document.cpp b/Telegram/SourceFiles/data/data_document.cpp index 35b13096e..71c1a422f 100644 --- a/Telegram/SourceFiles/data/data_document.cpp +++ b/Telegram/SourceFiles/data/data_document.cpp @@ -627,7 +627,11 @@ void DocumentData::updateThumbnails( loadThumbnail(origin); } if (!thumbnail.bytes.isEmpty()) { - // #TODO optimize put to cache + owner().cache().putIfEmpty( + _thumbnailLocation.file().cacheKey(), + Storage::Cache::Database::TaggedValue( + base::duplicate(thumbnail.bytes), + Data::kImageCacheTag)); } } } @@ -761,19 +765,17 @@ PhotoData *DocumentData::goodThumbnailPhoto() const { return _goodThumbnailPhoto; } -auto DocumentData::bigFileBaseCacheKey() const --> std::optional { - if (hasRemoteLocation()) { - return StorageFileLocation( +Storage::Cache::Key DocumentData::bigFileBaseCacheKey() const { + return hasRemoteLocation() + ? StorageFileLocation( _dc, session().userId(), MTP_inputDocumentFileLocation( MTP_long(id), MTP_long(_access), MTP_bytes(_fileReference), - MTP_string())).bigFileBaseCacheKey(); - } - return std::nullopt; + MTP_string())).bigFileBaseCacheKey() + : Storage::Cache::Key(); } bool DocumentData::saveToCache() const { @@ -786,12 +788,6 @@ bool DocumentData::saveToCache() const { } void DocumentData::unload() { - // Forget thumb only when image cache limit exceeds. - // - // Also, you can't unload() images that you don't own - // from the destructor, because they're already destroyed. - // - //_thumbnail->unload(); _replyPreview = nullptr; } @@ -1333,14 +1329,6 @@ void DocumentData::refreshFileReference(const QByteArray &value) { _thumbnailLocation.refreshFileReference(value); } -void DocumentData::refreshStickerThumbFileReference() { - // #TODO optimize - //if (_thumbnailLoader) { - // _thumbnailLocation.refreshFileReference( - // _thumbnailLoader->fileReference()); - //} -} - QString DocumentData::filename() const { return _filename; } diff --git a/Telegram/SourceFiles/data/data_document.h b/Telegram/SourceFiles/data/data_document.h index 16dab1872..75d7a8f1d 100644 --- a/Telegram/SourceFiles/data/data_document.h +++ b/Telegram/SourceFiles/data/data_document.h @@ -182,8 +182,7 @@ public: void setGoodThumbnailPhoto(not_null photo); [[nodiscard]] PhotoData *goodThumbnailPhoto() const; - [[nodiscard]] auto bigFileBaseCacheKey() const - -> std::optional; + [[nodiscard]] Storage::Cache::Key bigFileBaseCacheKey() const; void setRemoteLocation( int32 dc, @@ -197,7 +196,6 @@ public: [[nodiscard]] MTPInputDocument mtpInput() const; [[nodiscard]] QByteArray fileReference() const; void refreshFileReference(const QByteArray &value); - void refreshStickerThumbFileReference(); // When we have some client-side generated document // (for example for displaying an external inline bot result) diff --git a/Telegram/SourceFiles/data/data_photo.cpp b/Telegram/SourceFiles/data/data_photo.cpp index 0452337d5..4905b2d39 100644 --- a/Telegram/SourceFiles/data/data_photo.cpp +++ b/Telegram/SourceFiles/data/data_photo.cpp @@ -155,7 +155,7 @@ void PhotoData::collectLocalData(not_null local) { const auto copyImage = [&](const ImagePtr &src, const ImagePtr &dst) { if (const auto from = src->cacheKey()) { if (const auto to = dst->cacheKey()) { - _owner->cache().copyIfEmpty(*from, *to); + _owner->cache().copyIfEmpty(from, to); } } }; diff --git a/Telegram/SourceFiles/media/streaming/media_streaming_loader.h b/Telegram/SourceFiles/media/streaming/media_streaming_loader.h index 5ccdae833..544a1d6b2 100644 --- a/Telegram/SourceFiles/media/streaming/media_streaming_loader.h +++ b/Telegram/SourceFiles/media/streaming/media_streaming_loader.h @@ -27,8 +27,7 @@ class Loader { public: static constexpr auto kPartSize = 128 * 1024; - [[nodiscard]] virtual auto baseCacheKey() const - -> std::optional = 0; + [[nodiscard]] virtual Storage::Cache::Key baseCacheKey() const = 0; [[nodiscard]] virtual int size() const = 0; virtual void load(int offset) = 0; diff --git a/Telegram/SourceFiles/media/streaming/media_streaming_loader_local.cpp b/Telegram/SourceFiles/media/streaming/media_streaming_loader_local.cpp index dd8d937b3..d3791830f 100644 --- a/Telegram/SourceFiles/media/streaming/media_streaming_loader_local.cpp +++ b/Telegram/SourceFiles/media/streaming/media_streaming_loader_local.cpp @@ -34,8 +34,8 @@ LoaderLocal::LoaderLocal(std::unique_ptr device) } } -std::optional LoaderLocal::baseCacheKey() const { - return std::nullopt; +Storage::Cache::Key LoaderLocal::baseCacheKey() const { + return {}; } int LoaderLocal::size() const { diff --git a/Telegram/SourceFiles/media/streaming/media_streaming_loader_local.h b/Telegram/SourceFiles/media/streaming/media_streaming_loader_local.h index fccd53495..2b0667a1a 100644 --- a/Telegram/SourceFiles/media/streaming/media_streaming_loader_local.h +++ b/Telegram/SourceFiles/media/streaming/media_streaming_loader_local.h @@ -20,8 +20,7 @@ class LoaderLocal : public Loader, public base::has_weak_ptr { public: LoaderLocal(std::unique_ptr device); - [[nodiscard]] auto baseCacheKey() const - ->std::optional override; + [[nodiscard]] Storage::Cache::Key baseCacheKey() const override; [[nodiscard]] int size() const override; void load(int offset) override; diff --git a/Telegram/SourceFiles/media/streaming/media_streaming_loader_mtproto.cpp b/Telegram/SourceFiles/media/streaming/media_streaming_loader_mtproto.cpp index 555665643..d4b810fe8 100644 --- a/Telegram/SourceFiles/media/streaming/media_streaming_loader_mtproto.cpp +++ b/Telegram/SourceFiles/media/streaming/media_streaming_loader_mtproto.cpp @@ -30,7 +30,7 @@ LoaderMtproto::LoaderMtproto( , _api(api().instance()) { } -std::optional LoaderMtproto::baseCacheKey() const { +Storage::Cache::Key LoaderMtproto::baseCacheKey() const { return location().data.get().bigFileBaseCacheKey(); } diff --git a/Telegram/SourceFiles/media/streaming/media_streaming_loader_mtproto.h b/Telegram/SourceFiles/media/streaming/media_streaming_loader_mtproto.h index 4d4c33fa3..525075b70 100644 --- a/Telegram/SourceFiles/media/streaming/media_streaming_loader_mtproto.h +++ b/Telegram/SourceFiles/media/streaming/media_streaming_loader_mtproto.h @@ -23,8 +23,7 @@ public: int size, Data::FileOrigin origin); - [[nodiscard]] auto baseCacheKey() const - -> std::optional override; + [[nodiscard]] Storage::Cache::Key baseCacheKey() const override; [[nodiscard]] int size() const override; void load(int offset) override; diff --git a/Telegram/SourceFiles/media/streaming/media_streaming_reader.cpp b/Telegram/SourceFiles/media/streaming/media_streaming_reader.cpp index 0f955d754..fba6d6239 100644 --- a/Telegram/SourceFiles/media/streaming/media_streaming_reader.cpp +++ b/Telegram/SourceFiles/media/streaming/media_streaming_reader.cpp @@ -1109,15 +1109,15 @@ void Reader::refreshLoaderPriority() { } bool Reader::isRemoteLoader() const { - return _loader->baseCacheKey().has_value(); + return _loader->baseCacheKey().valid(); } std::shared_ptr Reader::InitCacheHelper( - std::optional baseKey) { + Storage::Cache::Key baseKey) { if (!baseKey) { return nullptr; } - return std::make_shared(*baseKey); + return std::make_shared(baseKey); } // 0 is for headerData, slice index = sliceNumber - 1. diff --git a/Telegram/SourceFiles/media/streaming/media_streaming_reader.h b/Telegram/SourceFiles/media/streaming/media_streaming_reader.h index f7c173c44..b8717a825 100644 --- a/Telegram/SourceFiles/media/streaming/media_streaming_reader.h +++ b/Telegram/SourceFiles/media/streaming/media_streaming_reader.h @@ -226,7 +226,7 @@ private: void refreshLoaderPriority(); static std::shared_ptr InitCacheHelper( - std::optional baseKey); + Storage::Cache::Key baseKey); const std::unique_ptr _loader; Storage::Cache::Database * const _cache = nullptr; diff --git a/Telegram/SourceFiles/storage/localstorage.cpp b/Telegram/SourceFiles/storage/localstorage.cpp index b4421ae6e..8eb501100 100644 --- a/Telegram/SourceFiles/storage/localstorage.cpp +++ b/Telegram/SourceFiles/storage/localstorage.cpp @@ -3518,7 +3518,6 @@ void _writeStickerSets(FileKey &stickersKey, CheckSet checkSet, const Stickers:: } for (const auto sticker : set.stickers) { - sticker->refreshStickerThumbFileReference(); size += Serialize::Document::sizeInStream(sticker); } diff --git a/Telegram/SourceFiles/ui/image/image.cpp b/Telegram/SourceFiles/ui/image/image.cpp index 406bd9537..8e5ed92e6 100644 --- a/Telegram/SourceFiles/ui/image/image.cpp +++ b/Telegram/SourceFiles/ui/image/image.cpp @@ -966,7 +966,7 @@ void Image::loadEvenCancelled(Data::FileOrigin origin) { } } -std::optional Image::cacheKey() const { +Storage::Cache::Key Image::cacheKey() const { return _source->cacheKey(); } diff --git a/Telegram/SourceFiles/ui/image/image.h b/Telegram/SourceFiles/ui/image/image.h index 2d49bd33b..a19fcb0a6 100644 --- a/Telegram/SourceFiles/ui/image/image.h +++ b/Telegram/SourceFiles/ui/image/image.h @@ -80,7 +80,7 @@ public: virtual const StorageImageLocation &location() = 0; virtual void refreshFileReference(const QByteArray &data) = 0; - virtual std::optional cacheKey() = 0; + virtual Storage::Cache::Key cacheKey() = 0; virtual void setDelayedStorageLocation( const StorageImageLocation &location) = 0; virtual void performDelayedLoad(Data::FileOrigin origin) = 0; @@ -220,7 +220,7 @@ public: void refreshFileReference(const QByteArray &data) { _source->refreshFileReference(data); } - std::optional cacheKey() const; + Storage::Cache::Key cacheKey() const; QByteArray bytesForCache() const { return _source->bytesForCache(); } diff --git a/Telegram/SourceFiles/ui/image/image_location.cpp b/Telegram/SourceFiles/ui/image/image_location.cpp index 725b5a439..d91061cba 100644 --- a/Telegram/SourceFiles/ui/image/image_location.cpp +++ b/Telegram/SourceFiles/ui/image/image_location.cpp @@ -776,6 +776,26 @@ DownloadLocation DownloadLocation::convertToModern( return DownloadLocation{ file.convertToModern(type, id, accessHash) }; } +Storage::Cache::Key DownloadLocation::cacheKey() const { + return data.match([](const GeoPointLocation &data) { + return Data::GeoPointCacheKey(data); + }, [](const StorageFileLocation &data) { + return data.valid() + ? data.cacheKey() + : Storage::Cache::Key(); + }, [](const WebFileLocation &data) { + return data.isNull() + ? Storage::Cache::Key() + : Data::WebDocumentCacheKey(data); + }, [](const PlainUrlLocation &data) { + return data.url.isEmpty() + ? Storage::Cache::Key() + : Data::UrlCacheKey(data.url); + }, [](const InMemoryLocation &data) { + return Storage::Cache::Key(); + }); +} + bool DownloadLocation::valid() const { return data.match([](const GeoPointLocation &data) { return true; diff --git a/Telegram/SourceFiles/ui/image/image_location.h b/Telegram/SourceFiles/ui/image/image_location.h index 1eeac47d4..a0421c684 100644 --- a/Telegram/SourceFiles/ui/image/image_location.h +++ b/Telegram/SourceFiles/ui/image/image_location.h @@ -411,6 +411,7 @@ public: uint64 id, uint64 accessHash) const; + [[nodiscard]] Storage::Cache::Key cacheKey() const; [[nodiscard]] bool valid() const; [[nodiscard]] QByteArray fileReference() const; bool refreshFileReference(const QByteArray &data); diff --git a/Telegram/SourceFiles/ui/image/image_source.cpp b/Telegram/SourceFiles/ui/image/image_source.cpp index c55c962a1..1200f8170 100644 --- a/Telegram/SourceFiles/ui/image/image_source.cpp +++ b/Telegram/SourceFiles/ui/image/image_source.cpp @@ -91,8 +91,8 @@ const StorageImageLocation &ImageSource::location() { void ImageSource::refreshFileReference(const QByteArray &data) { } -std::optional ImageSource::cacheKey() { - return std::nullopt; +Storage::Cache::Key ImageSource::cacheKey() { + return Storage::Cache::Key(); } void ImageSource::setDelayedStorageLocation( @@ -220,8 +220,8 @@ const StorageImageLocation &LocalFileSource::location() { void LocalFileSource::refreshFileReference(const QByteArray &data) { } -std::optional LocalFileSource::cacheKey() { - return std::nullopt; +Storage::Cache::Key LocalFileSource::cacheKey() { + return Storage::Cache::Key(); } void LocalFileSource::setDelayedStorageLocation( @@ -455,10 +455,10 @@ const StorageImageLocation &StorageSource::location() { return _location; } -std::optional StorageSource::cacheKey() { +Storage::Cache::Key StorageSource::cacheKey() { return _location.valid() - ? base::make_optional(_location.file().cacheKey()) - : std::nullopt; + ? _location.file().cacheKey() + : Storage::Cache::Key(); } int StorageSource::width() { @@ -524,10 +524,10 @@ WebCachedSource::WebCachedSource( , _size(size) { } -std::optional WebCachedSource::cacheKey() { +Storage::Cache::Key WebCachedSource::cacheKey() { return _location.isNull() - ? std::nullopt - : base::make_optional(Data::WebDocumentCacheKey(_location)); + ? Storage::Cache::Key() + : Data::WebDocumentCacheKey(_location); } int WebCachedSource::width() { @@ -574,7 +574,7 @@ GeoPointSource::GeoPointSource(const GeoPointLocation &location) : _location(location) { } -std::optional GeoPointSource::cacheKey() { +Storage::Cache::Key GeoPointSource::cacheKey() { return Data::GeoPointCacheKey(_location); } @@ -712,7 +712,7 @@ WebUrlSource::WebUrlSource(const QString &url, int width, int height) , _height(height) { } -std::optional WebUrlSource::cacheKey() { +Storage::Cache::Key WebUrlSource::cacheKey() { return Data::UrlCacheKey(_url); } diff --git a/Telegram/SourceFiles/ui/image/image_source.h b/Telegram/SourceFiles/ui/image/image_source.h index 0da22b1e5..44845c7d9 100644 --- a/Telegram/SourceFiles/ui/image/image_source.h +++ b/Telegram/SourceFiles/ui/image/image_source.h @@ -33,7 +33,7 @@ public: const StorageImageLocation &location() override; void refreshFileReference(const QByteArray &data) override; - std::optional cacheKey() override; + Storage::Cache::Key cacheKey() override; void setDelayedStorageLocation( const StorageImageLocation &location) override; void performDelayedLoad(Data::FileOrigin origin) override; @@ -82,7 +82,7 @@ public: const StorageImageLocation &location() override; void refreshFileReference(const QByteArray &data) override; - std::optional cacheKey() override; + Storage::Cache::Key cacheKey() override; void setDelayedStorageLocation( const StorageImageLocation &location) override; void performDelayedLoad(Data::FileOrigin origin) override; @@ -168,7 +168,7 @@ public: int size); const StorageImageLocation &location() override; - std::optional cacheKey() override; + Storage::Cache::Key cacheKey() override; void refreshFileReference(const QByteArray &data) override; @@ -198,7 +198,7 @@ public: int height, int size = 0); - std::optional cacheKey() override; + Storage::Cache::Key cacheKey() override; int width() override; int height() override; @@ -224,7 +224,7 @@ class GeoPointSource : public RemoteSource { public: GeoPointSource(const GeoPointLocation &location); - std::optional cacheKey() override; + Storage::Cache::Key cacheKey() override; int width() override; int height() override; @@ -283,7 +283,7 @@ public: explicit WebUrlSource(const QString &url, QSize box = QSize()); WebUrlSource(const QString &url, int width, int height); - std::optional cacheKey() override; + Storage::Cache::Key cacheKey() override; int width() override; int height() override; diff --git a/Telegram/lib_storage b/Telegram/lib_storage index 57027c7d6..df60834ea 160000 --- a/Telegram/lib_storage +++ b/Telegram/lib_storage @@ -1 +1 @@ -Subproject commit 57027c7d6c071f0d958576a530c7c0411d8d4274 +Subproject commit df60834eac1545201682b2b55444c5ef61a170d2