[core] Calculate size of an ambient cache without offline resources

This commit is contained in:
Alexander Shalamov
2019-09-12 21:38:07 +03:00
parent 35fc402d34
commit 202b5a8ecc
2 changed files with 147 additions and 25 deletions

View File

@@ -3,7 +3,6 @@
#include <mbgl/storage/resource.hpp>
#include <mbgl/storage/offline.hpp>
#include <mbgl/util/exception.hpp>
#include <mbgl/util/noncopyable.hpp>
#include <mbgl/util/optional.hpp>
#include <mbgl/util/constants.hpp>
#include <mbgl/util/mapbox.hpp>
@@ -36,7 +35,7 @@ struct MapboxTileLimitExceededException : util::Exception {
MapboxTileLimitExceededException() : util::Exception("Mapbox tile limit exceeded") {}
};
class OfflineDatabase : private util::noncopyable {
class OfflineDatabase {
public:
OfflineDatabase(std::string path);
~OfflineDatabase();
@@ -97,6 +96,8 @@ public:
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);
@@ -150,7 +151,37 @@ private:
optional<uint64_t> offlineMapboxTileCount;
bool evict(uint64_t neededFreeSize);
bool evict(uint64_t neededFreeSize, DatabaseSizeChangeStats& stats);
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;
};