Skip to content

Commit

Permalink
implement MBTiles offline fallback mode
Browse files Browse the repository at this point in the history
  • Loading branch information
hjanetzek committed Oct 14, 2016
1 parent 18109cb commit 07e95b7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
44 changes: 34 additions & 10 deletions core/src/data/mbtilesDataSource.cpp
Expand Up @@ -126,6 +126,13 @@ MBTilesDataSource::~MBTilesDataSource() {

bool MBTilesDataSource::loadTileData(std::shared_ptr<TileTask> _task, TileTaskCb _cb) {

if (m_offlineMode) {
// Try next source
_task->rawSource = next->level;

return loadNextSource(_task, _cb);
}

if (_task->rawSource == this->level) {

m_worker->enqueue([this, _task, _cb](){
Expand All @@ -137,7 +144,7 @@ bool MBTilesDataSource::loadTileData(std::shared_ptr<TileTask> _task, TileTaskCb
getTileData(tileId, *task.rawTileData);

if (task.hasData()) {
// LOGW("loaded tile: %s, %d", tileId.toString().c_str(), task.rawTileData->size());
LOGW("loaded tile: %s, %d", tileId.toString().c_str(), task.rawTileData->size());

_cb.func(_task);

Expand Down Expand Up @@ -166,20 +173,37 @@ bool MBTilesDataSource::loadNextSource(std::shared_ptr<TileTask> _task, TileTask
// Intercept TileTaskCb to store result from next source.
TileTaskCb cb{[this, _cb](std::shared_ptr<TileTask> _task) {

m_worker->enqueue([this, _task](){
if (_task->hasData()) {

auto& task = static_cast<DownloadTileTask&>(*_task);
TileID tileId = _task->tileId();
m_worker->enqueue([this, _task](){

//LOGW("store tile: %s, %d", tileId.toString().c_str(), task.hasData());
auto& task = static_cast<DownloadTileTask&>(*_task);

if (task.hasData()) {
storeTileData(tileId, *task.rawTileData);
}
});
LOGW("store tile: %s, %d", _task->tileId().toString().c_str(), task.hasData());

_cb.func(_task);
storeTileData(_task->tileId(), *task.rawTileData);
});
_cb.func(_task);

} else if (m_offlineMode) {
LOGW("try fallback tile: %s, %d", _task->tileId().toString().c_str());

m_worker->enqueue([this, _task, _cb](){

auto& task = static_cast<DownloadTileTask&>(*_task);
task.rawTileData = std::make_shared<std::vector<char>>();

getTileData(_task->tileId(), *task.rawTileData);

LOGW("loaded tile: %s, %d", _task->tileId().toString().c_str(), task.rawTileData->size());

_cb.func(_task);

});
} else {
LOGW("missing tile: %s, %d", _task->tileId().toString().c_str());
_cb.func(_task);
}
}};

return next->loadTileData(_task, cb);
Expand Down
3 changes: 3 additions & 0 deletions core/src/data/mbtilesDataSource.h
Expand Up @@ -39,6 +39,9 @@ class MBTilesDataSource : public RawDataSource {
std::unique_ptr<SQLite::Database> m_db;
std::unique_ptr<MBTilesQueries> m_queries;
std::unique_ptr<AsyncWorker> m_worker;

// Offline fallback: Try download (next source) first, then fall back to mbtiles
bool m_offlineMode = true;
};

}

0 comments on commit 07e95b7

Please sign in to comment.