Skip to content

Commit

Permalink
buffer sqlite writes if needed (#608)
Browse files Browse the repository at this point in the history
Fixes #596, I think.

Time to process Antarctica drops from 9m5s to 3m51s
  • Loading branch information
cldellow authored Dec 9, 2023
1 parent 8300b0c commit 0d951ac
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
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 "external/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

0 comments on commit 0d951ac

Please sign in to comment.