Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buffer sqlite writes if needed #608

Merged
merged 1 commit into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions include/mbtiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
#include <vector>
#include "sqlite_modern_cpp.h"

struct PendingStatement {
int zoom;
int x;
int y;
std::string data;
bool isMerge;
};

/** \brief Write to MBTiles (sqlite) database
*
* (note that sqlite_modern_cpp.h is very slightly changed from the original, for blob support and an .init method)
Expand All @@ -17,6 +25,12 @@ class MBTiles {
std::mutex m;
bool inTransaction;

std::shared_ptr<std::vector<PendingStatement>> pendingStatements1, pendingStatements2;
std::mutex pendingStatementsMutex;

void insertOrReplace(int zoom, int x, int y, const std::string& data, bool isMerge);
void flushPendingStatements();

public:
MBTiles();
virtual ~MBTiles();
Expand Down
45 changes: 38 additions & 7 deletions src/mbtiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ using namespace sqlite;
using namespace std;
namespace bio = boost::iostreams;

MBTiles::MBTiles() {}
MBTiles::MBTiles():
pendingStatements1(std::make_shared<std::vector<PendingStatement>>()),
pendingStatements2(std::make_shared<std::vector<PendingStatement>>())
{}

MBTiles::~MBTiles() {
if (db && inTransaction) db << "COMMIT;"; // commit all the changes if open
Expand Down Expand Up @@ -52,18 +55,46 @@ void MBTiles::writeMetadata(string key, string value) {
db << "REPLACE INTO metadata (name,value) VALUES (?,?);" << key << value;
m.unlock();
}

void MBTiles::saveTile(int zoom, int x, int y, string *data, bool isMerge) {
int tmsY = pow(2,zoom) - 1 - y;

void MBTiles::insertOrReplace(int zoom, int x, int y, const std::string& data, bool isMerge) {
// NB: assumes we have the `m` mutex
int tmsY = pow(2, zoom) - 1 - y;
int s = isMerge ? 1 : 0;
m.lock();
preparedStatements[s].reset();
preparedStatements[s] << zoom << x << tmsY && *data;
preparedStatements[s] << zoom << x << tmsY && data;
preparedStatements[s].execute();
m.unlock();
}

void MBTiles::flushPendingStatements() {
// NB: assumes we have the `m` mutex

for (int i = 0; i < 2; i++) {
while(!pendingStatements2->empty()) {
const PendingStatement& stmt = pendingStatements2->back();
insertOrReplace(stmt.zoom, stmt.x, stmt.y, stmt.data, stmt.isMerge);
pendingStatements2->pop_back();
}

std::lock_guard<std::mutex> lock(pendingStatementsMutex);
pendingStatements1.swap(pendingStatements2);
}
}

void MBTiles::saveTile(int zoom, int x, int y, string *data, bool isMerge) {
// If the lock is available, write directly to SQLite.
if (m.try_lock()) {
insertOrReplace(zoom, x, y, *data, isMerge);
flushPendingStatements();
m.unlock();
} else {
// Else buffer the write for later, copying its binary blob.
const std::lock_guard<std::mutex> lock(pendingStatementsMutex);
pendingStatements1->push_back({zoom, x, y, *data, isMerge});
}
}

void MBTiles::closeForWriting() {
flushPendingStatements();
db << "CREATE UNIQUE INDEX IF NOT EXISTS tile_index on tiles (zoom_level, tile_column, tile_row);";
preparedStatements[0].used(true);
preparedStatements[1].used(true);
Expand Down
Loading