Files
MapLibreParticleLab/platform/default/sqlite3.hpp
John Firebaugh db3620c58f [core] Implement an eviction policy for OfflineDatabase
When inserting an cached resource, or removing a region, remove least-recently used resources and tiles, not used by offline regions, until the used database size, as calculated by multiplying the number of in-use pages by the page size, is less than the maximum cache size minus 5 times the page size.

In addition, OfflineDatabase may be configured to ignore cache puts of individual resources larger than a certain size.

This policy is similar but not identical to the former SQLiteCache policy:

* It accounts for offline, by exempting resources required by offline regions from eviction.
* It must delete from two tables (resources and tiles), rather than one. Currently the strategy is naive: evict 50 rows at a time from each table.
* It makes maximumCacheSize and maximumCacheEntrySize completely independent. The SQLiteCache implementation evicted when `usedSize > maximumCacheSize - 2 * maximumCacheEntrySize`. This evicts when `usedSize > maximumCacheSize - 5 * pageSize`.
* It uses a non-unlimited default value for maximumCacheSize: 50 MB. We should have always had a limit in place; "a cache without an eviction policy is a resource leak".
2016-02-10 15:40:20 -08:00

93 lines
2.1 KiB
C++

#pragma once
#include <string>
#include <vector>
#include <stdexcept>
#include <chrono>
typedef struct sqlite3 sqlite3;
typedef struct sqlite3_stmt sqlite3_stmt;
namespace mapbox {
namespace sqlite {
enum OpenFlag : int {
ReadOnly = 0x00000001,
ReadWrite = 0x00000002,
Create = 0x00000004,
NoMutex = 0x00008000,
FullMutex = 0x00010000,
SharedCache = 0x00020000,
PrivateCache = 0x00040000,
};
struct Exception : std::runtime_error {
inline Exception(int err, const char *msg) : std::runtime_error(msg), code(err) {}
inline Exception(int err, const std::string& msg) : std::runtime_error(msg), code(err) {}
const int code = 0;
};
class Statement;
class Database {
private:
Database(const Database &) = delete;
Database &operator=(const Database &) = delete;
public:
Database(const std::string &filename, int flags = 0);
Database(Database &&);
~Database();
Database &operator=(Database &&);
explicit operator bool() const;
void setBusyTimeout(std::chrono::milliseconds);
void exec(const std::string &sql);
Statement prepare(const char *query);
int64_t lastInsertRowid() const;
uint64_t changes() const;
private:
sqlite3 *db = nullptr;
};
class Statement {
private:
Statement(const Statement &) = delete;
Statement &operator=(const Statement &) = delete;
void check(int err);
public:
Statement(sqlite3 *db, const char *sql);
Statement(Statement &&);
~Statement();
Statement &operator=(Statement &&);
explicit operator bool() const;
template <typename T> void bind(int offset, T value);
// Text
void bind(int offset, const char *, std::size_t length, bool retain = true);
void bind(int offset, const std::string&, bool retain = true);
// Blob
void bindBlob(int offset, const void *, std::size_t length, bool retain = true);
void bindBlob(int offset, const std::vector<uint8_t>&, bool retain = true);
template <typename T> T get(int offset);
bool run();
void reset();
void clearBindings();
private:
sqlite3_stmt *stmt = nullptr;
};
}
}