make storage lib separate so we can build without storage libs
This commit is contained in:
@@ -1,81 +0,0 @@
|
||||
#include <mbgl/util/compression.hpp>
|
||||
|
||||
#include <zlib.h>
|
||||
|
||||
#include <cstring>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace mbgl {
|
||||
namespace util {
|
||||
|
||||
std::string compress(const std::string &raw) {
|
||||
z_stream deflate_stream;
|
||||
memset(&deflate_stream, 0, sizeof(deflate_stream));
|
||||
|
||||
// TODO: reuse z_streams
|
||||
if (deflateInit(&deflate_stream, Z_DEFAULT_COMPRESSION) != Z_OK) {
|
||||
throw std::runtime_error("failed to initialize deflate");
|
||||
}
|
||||
|
||||
deflate_stream.next_in = (Bytef *)raw.data();
|
||||
deflate_stream.avail_in = uInt(raw.size());
|
||||
|
||||
std::string result;
|
||||
char out[16384];
|
||||
|
||||
int code;
|
||||
do {
|
||||
deflate_stream.next_out = reinterpret_cast<Bytef *>(out);
|
||||
deflate_stream.avail_out = sizeof(out);
|
||||
code = deflate(&deflate_stream, Z_FINISH);
|
||||
if (result.size() < deflate_stream.total_out) {
|
||||
// append the block to the output string
|
||||
result.append(out, deflate_stream.total_out - result.size());
|
||||
}
|
||||
} while (code == Z_OK);
|
||||
|
||||
deflateEnd(&deflate_stream);
|
||||
|
||||
if (code != Z_STREAM_END) {
|
||||
throw std::runtime_error(deflate_stream.msg);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string decompress(const std::string &raw) {
|
||||
z_stream inflate_stream;
|
||||
memset(&inflate_stream, 0, sizeof(inflate_stream));
|
||||
|
||||
// TODO: reuse z_streams
|
||||
if (inflateInit(&inflate_stream) != Z_OK) {
|
||||
throw std::runtime_error("failed to initialize inflate");
|
||||
}
|
||||
|
||||
inflate_stream.next_in = (Bytef *)raw.data();
|
||||
inflate_stream.avail_in = uInt(raw.size());
|
||||
|
||||
std::string result;
|
||||
char out[15384];
|
||||
|
||||
int code;
|
||||
do {
|
||||
inflate_stream.next_out = reinterpret_cast<Bytef *>(out);
|
||||
inflate_stream.avail_out = sizeof(out);
|
||||
code = inflate(&inflate_stream, 0);
|
||||
// result.append(out, sizeof(out) - inflate_stream.avail_out);
|
||||
if (result.size() < inflate_stream.total_out) {
|
||||
result.append(out, inflate_stream.total_out - result.size());
|
||||
}
|
||||
} while (code == Z_OK);
|
||||
|
||||
inflateEnd(&inflate_stream);
|
||||
|
||||
if (code != Z_STREAM_END) {
|
||||
throw std::runtime_error(inflate_stream.msg);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
#ifndef MBGL_UTIL_COMPRESSION
|
||||
#define MBGL_UTIL_COMPRESSION
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace mbgl {
|
||||
namespace util {
|
||||
|
||||
std::string compress(const std::string &raw);
|
||||
std::string decompress(const std::string &raw);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,165 +0,0 @@
|
||||
#include <mbgl/util/sqlite3.hpp>
|
||||
#include <sqlite3.h>
|
||||
|
||||
#include <cassert>
|
||||
|
||||
namespace mapbox {
|
||||
namespace sqlite {
|
||||
|
||||
Database::Database(const std::string &filename, int flags) {
|
||||
const int err = sqlite3_open_v2(filename.c_str(), &db, flags, nullptr);
|
||||
if (err != SQLITE_OK) {
|
||||
Exception ex { err, sqlite3_errmsg(db) };
|
||||
db = nullptr;
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
Database::Database(Database &&other)
|
||||
: db(std::move(other.db)) {}
|
||||
|
||||
Database &Database::operator=(Database &&other) {
|
||||
std::swap(db, other.db);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Database::~Database() {
|
||||
if (db) {
|
||||
const int err = sqlite3_close(db);
|
||||
if (err != SQLITE_OK) {
|
||||
throw Exception { err, sqlite3_errmsg(db) };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Database::operator bool() const {
|
||||
return db != nullptr;
|
||||
}
|
||||
|
||||
void Database::exec(const std::string &sql) {
|
||||
assert(db);
|
||||
char *msg = nullptr;
|
||||
const int err = sqlite3_exec(db, sql.c_str(), nullptr, nullptr, &msg);
|
||||
if (msg) {
|
||||
Exception ex { err, msg };
|
||||
sqlite3_free(msg);
|
||||
throw ex;
|
||||
} else if (err != SQLITE_OK) {
|
||||
throw Exception { err, sqlite3_errmsg(db) };
|
||||
}
|
||||
}
|
||||
|
||||
Statement Database::prepare(const char *query) {
|
||||
assert(db);
|
||||
return std::move(Statement(db, query));
|
||||
}
|
||||
|
||||
Statement::Statement(sqlite3 *db, const char *sql) {
|
||||
const int err = sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr);
|
||||
if (err != SQLITE_OK) {
|
||||
stmt = nullptr;
|
||||
throw Exception { err, sqlite3_errmsg(db) };
|
||||
}
|
||||
}
|
||||
|
||||
#define CHECK_SQLITE_OK(err) \
|
||||
if (err != SQLITE_OK) { \
|
||||
throw Exception { err, sqlite3_errmsg(sqlite3_db_handle(stmt)) }; \
|
||||
}
|
||||
|
||||
Statement::Statement(Statement &&other) {
|
||||
*this = std::move(other);
|
||||
}
|
||||
|
||||
Statement &Statement::operator=(Statement &&other) {
|
||||
std::swap(stmt, other.stmt);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Statement::~Statement() {
|
||||
if (stmt) {
|
||||
const int err = sqlite3_finalize(stmt);
|
||||
CHECK_SQLITE_OK(err)
|
||||
}
|
||||
}
|
||||
|
||||
Statement::operator bool() const {
|
||||
return stmt != nullptr;
|
||||
}
|
||||
|
||||
#define BIND_3(type, value) \
|
||||
assert(stmt); \
|
||||
const int err = sqlite3_bind_##type(stmt, offset, value); \
|
||||
CHECK_SQLITE_OK(err)
|
||||
|
||||
#define BIND_5(type, value, length, param) \
|
||||
assert(stmt); \
|
||||
const int err = sqlite3_bind_##type(stmt, offset, value, length, param); \
|
||||
CHECK_SQLITE_OK(err)
|
||||
|
||||
template <> void Statement::bind(int offset, int value) {
|
||||
BIND_3(int, value)
|
||||
}
|
||||
|
||||
template <> void Statement::bind(int offset, int64_t value) {
|
||||
BIND_3(int64, value)
|
||||
}
|
||||
|
||||
template <> void Statement::bind(int offset, double value) {
|
||||
BIND_3(double, value)
|
||||
}
|
||||
|
||||
template <> void Statement::bind(int offset, bool value) {
|
||||
BIND_3(int, value)
|
||||
}
|
||||
|
||||
template <> void Statement::bind(int offset, const char *value) {
|
||||
BIND_5(text, value, -1, nullptr)
|
||||
}
|
||||
|
||||
void Statement::bind(int offset, const std::string &value, bool retain) {
|
||||
BIND_5(blob, value.data(), int(value.size()), retain ? SQLITE_TRANSIENT : SQLITE_STATIC)
|
||||
}
|
||||
|
||||
bool Statement::run() {
|
||||
assert(stmt);
|
||||
const int err = sqlite3_step(stmt);
|
||||
if (err == SQLITE_DONE) {
|
||||
return false;
|
||||
} else if (err == SQLITE_ROW) {
|
||||
return true;
|
||||
} else {
|
||||
throw std::runtime_error("failed to run statement");
|
||||
}
|
||||
}
|
||||
|
||||
template <> int Statement::get(int offset) {
|
||||
assert(stmt);
|
||||
return sqlite3_column_int(stmt, offset);
|
||||
}
|
||||
|
||||
template <> int64_t Statement::get(int offset) {
|
||||
assert(stmt);
|
||||
return sqlite3_column_int64(stmt, offset);
|
||||
}
|
||||
|
||||
template <> double Statement::get(int offset) {
|
||||
assert(stmt);
|
||||
return sqlite3_column_double(stmt, offset);
|
||||
}
|
||||
|
||||
template <> std::string Statement::get(int offset) {
|
||||
assert(stmt);
|
||||
return {
|
||||
reinterpret_cast<const char *>(sqlite3_column_blob(stmt, offset)),
|
||||
size_t(sqlite3_column_bytes(stmt, offset))
|
||||
};
|
||||
}
|
||||
|
||||
void Statement::reset() {
|
||||
assert(stmt);
|
||||
sqlite3_reset(stmt);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
|
||||
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) {}
|
||||
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 &&);
|
||||
|
||||
operator bool() const;
|
||||
|
||||
void exec(const std::string &sql);
|
||||
Statement prepare(const char *query);
|
||||
|
||||
private:
|
||||
sqlite3 *db = nullptr;
|
||||
};
|
||||
|
||||
class Statement {
|
||||
private:
|
||||
Statement(const Statement &) = delete;
|
||||
Statement &operator=(const Statement &) = delete;
|
||||
|
||||
public:
|
||||
Statement(sqlite3 *db, const char *sql);
|
||||
Statement(Statement &&);
|
||||
~Statement();
|
||||
Statement &operator=(Statement &&);
|
||||
|
||||
operator bool() const;
|
||||
|
||||
template <typename T> void bind(int offset, T value);
|
||||
void bind(int offset, const std::string &value, bool retain = true);
|
||||
template <typename T> T get(int offset);
|
||||
|
||||
bool run();
|
||||
void reset();
|
||||
|
||||
private:
|
||||
sqlite3_stmt *stmt = nullptr;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user