diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2018-02-20 15:06:26 +0100 |
---|---|---|
committer | Konstantin Käfer <mail@kkaefer.com> | 2018-02-21 14:50:13 +0100 |
commit | 021e1ae596440cfdee5ffe75907b76069ae44307 (patch) | |
tree | ebf15ff8a72e5f14291ba37b6f297ca9a738eea4 /include | |
parent | 06213d9145d3b20b63e235cc25678fd76dc296d0 (diff) | |
download | qtlocation-mapboxgl-upstream/blob.tar.gz |
[core] introduce Blob for compressed and uncompressed dataupstream/blob
- Blob is a wrapper type for a shared_ptr<const string> that has accessor functions for getting compressed and uncompressed data
- Moved util::writeFile, util::readFile, util::compress, util::uncompress, decodeImage, and encodePNG to the Blob interface
- Added Blob support to Request and file sources
- Added Blob support to VectorTile objects
- Added support for gzip decoding to util::uncompress
- We're no longer compressing WebP, PNG, and JPEG data when storing in the OfflineDatabase
- Android's HTTPRequest returns compressed Blobs by default
One caveat is that our previous decompress function didn't support gzip, so once users upgrade to this version, their offline cache may contain both zlib-compressed data and gzip-compressed data, but older versions won't be able to decompress gzip data. On the other hand, we don't support downgrading SDKs anyway, so this shouldn't be a problem. To be on the safe side, we could bump the user_version of the SQLite DB.
Diffstat (limited to 'include')
-rw-r--r-- | include/mbgl/storage/resource.hpp | 16 | ||||
-rw-r--r-- | include/mbgl/storage/response.hpp | 3 | ||||
-rw-r--r-- | include/mbgl/style/style.hpp | 5 | ||||
-rw-r--r-- | include/mbgl/util/blob.hpp | 34 | ||||
-rw-r--r-- | include/mbgl/util/compression.hpp | 7 | ||||
-rw-r--r-- | include/mbgl/util/image.hpp | 6 |
6 files changed, 63 insertions, 8 deletions
diff --git a/include/mbgl/storage/resource.hpp b/include/mbgl/storage/resource.hpp index 318fa389f4..d9da494e4d 100644 --- a/include/mbgl/storage/resource.hpp +++ b/include/mbgl/storage/resource.hpp @@ -42,12 +42,23 @@ public: All = Cache | Network, }; + enum class Compression : bool { + // The data will be an uncompressed blob, even if it was obtained in compressed form. + Uncompressed = false, + + // The data will be returned compressed if it was already obtained in compressed form, + // and uncompressed otherwise. + PreferCompressed = true, + }; + Resource(Kind kind_, std::string url_, optional<TileData> tileData_ = {}, - LoadingMethod loadingMethod_ = LoadingMethod::All) + LoadingMethod loadingMethod_ = LoadingMethod::All, + Compression compression_ = Compression::PreferCompressed) : kind(kind_), loadingMethod(loadingMethod_), + compression(compression_), url(std::move(url_)), tileData(std::move(tileData_)) { } @@ -72,6 +83,7 @@ public: Kind kind; LoadingMethod loadingMethod; + Compression compression; std::string url; // Includes auxiliary data if this is a tile request. @@ -80,7 +92,7 @@ public: optional<Timestamp> priorModified = {}; optional<Timestamp> priorExpires = {}; optional<std::string> priorEtag = {}; - std::shared_ptr<const std::string> priorData; + Blob priorData; }; diff --git a/include/mbgl/storage/response.hpp b/include/mbgl/storage/response.hpp index 508400141b..81a84effb7 100644 --- a/include/mbgl/storage/response.hpp +++ b/include/mbgl/storage/response.hpp @@ -2,6 +2,7 @@ #include <mbgl/util/chrono.hpp> #include <mbgl/util/optional.hpp> +#include <mbgl/util/blob.hpp> #include <string> #include <memory> @@ -31,7 +32,7 @@ public: bool mustRevalidate = false; // The actual data of the response. Present only for non-error, non-notModified responses. - std::shared_ptr<const std::string> data; + Blob data; optional<Timestamp> modified; optional<Timestamp> expires; diff --git a/include/mbgl/style/style.hpp b/include/mbgl/style/style.hpp index d6fdbd8f2c..dffdbf99ec 100644 --- a/include/mbgl/style/style.hpp +++ b/include/mbgl/style/style.hpp @@ -3,6 +3,7 @@ #include <mbgl/style/transition_options.hpp> #include <mbgl/map/camera.hpp> #include <mbgl/util/geo.hpp> +#include <mbgl/util/blob.hpp> #include <string> #include <vector> @@ -25,10 +26,10 @@ public: Style(Scheduler&, FileSource&, float pixelRatio); ~Style(); - void loadJSON(const std::string&); + void loadJSON(Blob); void loadURL(const std::string&); - std::string getJSON() const; + Blob getJSON() const; std::string getURL() const; // Defaults diff --git a/include/mbgl/util/blob.hpp b/include/mbgl/util/blob.hpp new file mode 100644 index 0000000000..df8fddc773 --- /dev/null +++ b/include/mbgl/util/blob.hpp @@ -0,0 +1,34 @@ +#pragma once + +#include <memory> +#include <string> + +namespace mbgl { + +class Blob { +public: + Blob(); + Blob(std::shared_ptr<const std::string> bytes, bool gzip); + Blob(std::string&& bytes, bool compressed); + + // Return uncompressed/compressed data. + std::shared_ptr<const std::string> uncompressedData() const; + std::shared_ptr<const std::string> compressedData() const; + + // Transform the blob to being uncompressed. + void uncompress(); + + bool isCompressed() const { + return compressed; + } + + explicit operator bool() const { + return (bool)bytes; + } + +private: + std::shared_ptr<const std::string> bytes; + bool compressed; +}; + +} // namespace mbgl diff --git a/include/mbgl/util/compression.hpp b/include/mbgl/util/compression.hpp index 93a3ddb8bc..cbce764683 100644 --- a/include/mbgl/util/compression.hpp +++ b/include/mbgl/util/compression.hpp @@ -5,8 +5,15 @@ namespace mbgl { namespace util { +// Compresses data with the deflate algorithm. std::string compress(const std::string& raw); + +// Decompresses data that is in deflate format, optionally wrapped in a gzip container. std::string decompress(const std::string& raw); +// Returns true when there's a good chance that the string can be compressed. +// In particular, it returns false when the data is an already compressed image format. +bool isCompressible(const std::string& raw); + } // namespace util } // namespace mbgl diff --git a/include/mbgl/util/image.hpp b/include/mbgl/util/image.hpp index 4887058f79..1cb3cca8c9 100644 --- a/include/mbgl/util/image.hpp +++ b/include/mbgl/util/image.hpp @@ -2,6 +2,7 @@ #include <mbgl/util/noncopyable.hpp> #include <mbgl/util/geometry.hpp> +#include <mbgl/util/blob.hpp> #include <mbgl/util/size.hpp> #include <string> @@ -170,8 +171,7 @@ using UnassociatedImage = Image<ImageAlphaMode::Unassociated>; using PremultipliedImage = Image<ImageAlphaMode::Premultiplied>; using AlphaImage = Image<ImageAlphaMode::Exclusive>; -// TODO: don't use std::string for binary data. -PremultipliedImage decodeImage(const std::string&); -std::string encodePNG(const PremultipliedImage&); +PremultipliedImage decodeImage(Blob); +Blob encodePNG(const PremultipliedImage&); } // namespace mbgl |