Files
MapLibreParticleLab/platform/default/include/mbgl/storage/offline_database.hpp
Petr Pokorny d62ff400c6 Replacing hardcoded configuration with configurable API and removing Mapbox assets (#90)
* Refactoring online file source so that tile server configuration is not hardcoded - part 1

* Added default templates

* Update from master

* Renaming MGLAccountManager to MGLSettings. Passing ResourceOptions and TileServerOptions to relevant classes and method in order to remove mapbox hardocded configuration

* Fixing build

* Addedm missing theme

* Unit tests fixes

* Fixing unit tests, adding factory methods for creating predefined configuration.

* Fixing mapbox unit tests

* Additional unit test fixes

* Making MapboxConfiguration default

* Making get/set resource options available in all classes that inherits from FileSource

* Making get/set resource options available in all classes that inherits from FileSource - part 2

* Fixes for Android

* Disable test coverage reports

* Disable test coverage reports

* Disable test coverage reports

* Disable test coverage reports

* Removing hardcoded configuration in iOS project - part 1

* Fixing wrong file extension, making use of tile server options.

* Use apiKey and MGL_API_KEY everywhere

* Just a rename

* Re-enabling tests and improving MGLSettings init

* Removing mapbox hardcoded configuration, using api key everywhere, implementing JNI conversion for TileServerOptions

* Lint issues.

* Integrating configurable tile server properties into Android SDK - part 2

* Removing mabox accounts

* Exposing multiple configurations to Android SDK - bug fixes

* Removing TileServerOptions from public interface - native methods dont work before mapbox-gl library is loaded

* Adding support for configurable predefined styles

* Migrating predefined styles in test app + cleanup

* Parameter fix

* Bug fixes

* Fixing render test build

* Fixing offline tester

* Fixing benchmark tool

* Fix: typo in method name

* Adding code for public access to TileServerOptions

* Bug fixes and debug code removal

* Fixes for macOS, adding unit tests from MapLibre, removing redundant members from OnlineFileSource

* Unit test fixes

* MacOS test app - migrating to configurable tile server options, part 1

* Fixing offline maps

* Migrating macOS test app to MapTiler

* Minor improvements

* Putting back apiKey test

* Added unit tests for MapTiler configuration

* Migrating test resources to MapTiler

* minor fix

* A few fixes

* Fixing macOS tests, removing feedback functionality

* Migrating ios test app to MapTiler

* Migrating ui tests

* UI tests fix

* Benchmarking app fix

* Cleanup and strings updates

* Migrating benchmark app to maptiler

* API key injection in benchmark app

* Putting back release configuration

* Additional tests and fixing issues identified by thread sanitizer.

* Removing test filters

* Fixes and addressing lint issues

* More Maptiler tests + minro fix

* Android instrumentation test migration to MapTiler. Exposing missing TileServerOptions in Android SDK. Minor fixes.

* No requirements on API key format.

* Fixing offline cache test tool

* Adding configurable domain name for sources, adding source domain int MapTiler configuration, cleanup

* Migrating tests to MapTiler, adding code for canonicalizing URLs, removing mapbox stuff from comments, cleanup

* More tests

* Updated database size

* Lint issues

* Fixed test

* Updating developer config API key

* Fixes for Android, propagaing updated tileServerOptions to native file sources, more tests

* Just a few more fixes

* Propagating updated settings to file sources + additional unit test

* Lint issues

* Removing test filter and fixing incorrect uri

* Release preparation

* macos release notes
2021-06-29 12:44:51 +02:00

192 lines
6.7 KiB
C++

#pragma once
#include <mbgl/storage/resource.hpp>
#include <mbgl/storage/offline.hpp>
#include <mbgl/util/tile_server_options.hpp>
#include <mbgl/util/exception.hpp>
#include <mbgl/util/optional.hpp>
#include <mbgl/util/constants.hpp>
#include <mbgl/util/mapbox.hpp>
#include <mbgl/util/expected.hpp>
#include <list>
#include <map>
#include <memory>
#include <string>
namespace mapbox {
namespace sqlite {
class Database;
class Statement;
class Query;
class Exception;
} // namespace sqlite
} // namespace mapbox
namespace mbgl {
class Response;
class TileID;
namespace util {
struct IOException;
} // namespace util
struct MapboxTileLimitExceededException : util::Exception {
MapboxTileLimitExceededException() : util::Exception("Mapbox tile limit exceeded") {}
};
class OfflineDatabase {
public:
OfflineDatabase(std::string path, const TileServerOptions& options);
~OfflineDatabase();
void changePath(const std::string&);
std::exception_ptr resetDatabase();
optional<Response> get(const Resource&);
// Return value is (inserted, stored size)
std::pair<bool, uint64_t> put(const Resource&, const Response&);
// Force Mapbox GL Native to revalidate tiles stored in the ambient
// cache with the tile server before using them, making sure they
// are the latest version. This is more efficient than cleaning the
// cache because if the tile is considered valid after the server
// lookup, it will not get downloaded again.
std::exception_ptr invalidateAmbientCache();
// Clear the tile cache, freeing resources. This operation can be
// potentially slow because it will trigger a VACUUM on SQLite,
// forcing the database to move pages on the filesystem.
std::exception_ptr clearAmbientCache();
expected<OfflineRegions, std::exception_ptr> listRegions();
expected<OfflineRegion, std::exception_ptr> createRegion(const OfflineRegionDefinition&,
const OfflineRegionMetadata&);
expected<OfflineRegions, std::exception_ptr>
mergeDatabase(const std::string& sideDatabasePath);
expected<OfflineRegionMetadata, std::exception_ptr> updateMetadata(int64_t regionID, const OfflineRegionMetadata&);
std::exception_ptr deleteRegion(OfflineRegion&&);
std::exception_ptr invalidateRegion(int64_t regionID);
// Return value is (response, stored size)
optional<std::pair<Response, uint64_t>> getRegionResource(const Resource&);
optional<int64_t> hasRegionResource(const Resource&);
uint64_t putRegionResource(int64_t regionID, const Resource&, const Response&);
void putRegionResources(int64_t regionID, const std::list<std::tuple<Resource, Response>>&, OfflineRegionStatus&);
expected<OfflineRegionDefinition, std::exception_ptr> getRegionDefinition(int64_t regionID);
expected<OfflineRegionStatus, std::exception_ptr> getRegionCompletedStatus(int64_t regionID);
std::exception_ptr setMaximumAmbientCacheSize(uint64_t);
void setOfflineMapboxTileCountLimit(uint64_t);
uint64_t getOfflineMapboxTileCountLimit();
bool offlineMapboxTileCountLimitExceeded();
uint64_t getOfflineMapboxTileCount();
bool exceedsOfflineMapboxTileCountLimit(const Resource&);
void markUsedResources(int64_t regionID, const std::list<Resource>&);
std::exception_ptr pack();
void runPackDatabaseAutomatically(bool autopack_) { autopack = autopack_; }
void reopenDatabaseReadOnly(bool readOnly);
private:
class DatabaseSizeChangeStats;
void initialize();
void handleError(const mapbox::sqlite::Exception&, const char* action);
void handleError(const util::IOException&, const char* action);
void handleError(const std::runtime_error& ex, const char* action);
void handleError(const char* action);
void removeExisting();
void removeOldCacheTable();
void createSchema();
void migrateToVersion5();
void migrateToVersion3();
void migrateToVersion6();
void cleanup();
bool disabled();
void vacuum();
void checkFlags();
mapbox::sqlite::Statement& getStatement(const char *);
optional<std::pair<Response, uint64_t>> getTile(const Resource::TileData&);
optional<int64_t> hasTile(const Resource::TileData&);
bool putTile(const Resource::TileData&, const Response&,
const std::string&, bool compressed);
optional<std::pair<Response, uint64_t>> getResource(const Resource&);
optional<int64_t> hasResource(const Resource&);
bool putResource(const Resource&, const Response&,
const std::string&, bool compressed);
uint64_t putRegionResourceInternal(int64_t regionID, const Resource&, const Response&);
optional<std::pair<Response, uint64_t>> getInternal(const Resource&);
optional<int64_t> hasInternal(const Resource&);
std::pair<bool, uint64_t> putInternal(const Resource&, const Response&, bool evict);
// Return value is true iff the resource was previously unused by any other regions.
bool markUsed(int64_t regionID, const Resource&);
std::pair<int64_t, int64_t> getCompletedResourceCountAndSize(int64_t regionID);
std::pair<int64_t, int64_t> getCompletedTileCountAndSize(int64_t regionID);
std::string path;
std::unique_ptr<mapbox::sqlite::Database> db;
std::map<const char*, const std::unique_ptr<mapbox::sqlite::Statement>> statements;
template <class T>
T getPragma(const char *);
uint64_t maximumAmbientCacheSize = util::DEFAULT_MAX_CACHE_SIZE;
uint64_t offlineMapboxTileCountLimit = util::mapbox::DEFAULT_OFFLINE_TILE_COUNT_LIMIT;
optional<uint64_t> offlineMapboxTileCount;
bool evict(uint64_t neededFreeSize, DatabaseSizeChangeStats& stats);
TileServerOptions tileServerOptions;
class DatabaseSizeChangeStats {
public:
explicit DatabaseSizeChangeStats(OfflineDatabase*);
// Returns difference between current database size and
// database size at the time of creation of this object.
int64_t diff() const;
// Returns how many bytes were released comparing to a database
// size at the time of creation of this object.
uint64_t bytesReleased() const;
// Returns page size for the database.
uint64_t pageSize() const;
private:
uint64_t pageSize_ = 0u;
uint64_t pageCount_ = 0u;
uint64_t initialSize_ = 0u;
OfflineDatabase* db = nullptr;
};
friend class DatabaseSizeChangeStats;
// Lazily initializes currentAmbientCacheSize.
std::exception_ptr initAmbientCacheSize();
optional<uint64_t> currentAmbientCacheSize;
void updateAmbientCacheSize(DatabaseSizeChangeStats&);
bool autopack = true;
bool readOnly = false;
};
} // namespace mbgl