Skip to content

Commit

Permalink
squash^1: try download directly when not in mbtiles cache
Browse files Browse the repository at this point in the history
  • Loading branch information
hjanetzek committed Oct 14, 2016
1 parent 335a0b9 commit 18109cb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 55 deletions.
85 changes: 36 additions & 49 deletions core/src/data/mbtilesDataSource.cpp
Expand Up @@ -124,78 +124,65 @@ MBTilesDataSource::~MBTilesDataSource() {
// TODO teardown db?
}

void MBTilesDataSource::removePending(const TileID& _tileId) {
std::unique_lock<std::mutex> lock(m_queueMutex);
auto it = std::find(m_pending.begin(), m_pending.end(), _tileId);
if (it != m_pending.end()) { m_pending.erase(it); }
}

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

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

TileID tileId = _task->tileId();

{
std::unique_lock<std::mutex> lock(m_queueMutex);
if (std::find(m_pending.begin(), m_pending.end(), tileId) != m_pending.end()) {
return false;
}
m_pending.push_back(tileId);
}

m_worker->enqueue([this, _task, _cb](){
TileID tileId = _task->tileId();
TileID tileId = _task->tileId();

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

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

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

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

_cb.func(_task);
} else if (next) {

} else if (next) {
// TODO _task->needsLoading(true);
// Don't try this source again
_task->rawSource = next->level;

// Don't try this source again
_task->rawSource = next->level;
// Trigger TileManager update so that tile will be downloaded
// next time.
if (!loadNextSource(_task, _cb)) {
// Trigger TileManager update so that tile will be
// downloaded next time.
_task->setNeedsLoading(true);
requestRender();
}

removePending(tileId);
});

return false;
}
});
return true;
}

if (next) {
TileTaskCb cb{[this, _cb](std::shared_ptr<TileTask> _task) {
return loadNextSource(_task, _cb);
}

m_worker->enqueue([this, _task](){
bool MBTilesDataSource::loadNextSource(std::shared_ptr<TileTask> _task, TileTaskCb _cb) {
if (!next) { return false; }

auto& task = static_cast<DownloadTileTask&>(*_task);
TileID tileId = _task->tileId();
// Intercept TileTaskCb to store result from next source.
TileTaskCb cb{[this, _cb](std::shared_ptr<TileTask> _task) {

LOGW("store tile: %s, %d", tileId.toString().c_str(), task.hasData());
m_worker->enqueue([this, _task](){

//if (task.hasData()) {
storeTileData(tileId, *task.rawTileData);
//}
});
auto& task = static_cast<DownloadTileTask&>(*_task);
TileID tileId = _task->tileId();

_cb.func(_task);
//LOGW("store tile: %s, %d", tileId.toString().c_str(), task.hasData());

}};
if (task.hasData()) {
storeTileData(tileId, *task.rawTileData);
}
});

return next->loadTileData(_task, cb);
}
_cb.func(_task);

return false;
}};

return next->loadTileData(_task, cb);
}

void MBTilesDataSource::setupMBTiles() {
Expand Down
7 changes: 1 addition & 6 deletions core/src/data/mbtilesDataSource.h
Expand Up @@ -24,7 +24,7 @@ class MBTilesDataSource : public RawDataSource {
private:
bool getTileData(const TileID& _tileId, std::vector<char>& _data);
void storeTileData(const TileID& _tileId, const std::vector<char>& _data);
void removePending(const TileID& _tileId);
bool loadNextSource(std::shared_ptr<TileTask> _task, TileTaskCb _cb);

void setupMBTiles();
void initMBTilesSchema(SQLite::Database& db, std::string _name, std::string _mimeType);
Expand All @@ -39,11 +39,6 @@ class MBTilesDataSource : public RawDataSource {
std::unique_ptr<SQLite::Database> m_db;
std::unique_ptr<MBTilesQueries> m_queries;
std::unique_ptr<AsyncWorker> m_worker;

std::mutex m_queueMutex;

std::vector<TileID> m_pending;

};

}
5 changes: 5 additions & 0 deletions core/src/tile/tileTask.h
Expand Up @@ -86,6 +86,11 @@ class TileTask {
// return false;
}

// Set whether DataSource should (re)try loading data
void setNeedsLoading(bool _needsLoading) {
m_needsLoading = _needsLoading;
}

void startedLoading() { m_needsLoading = false; }

protected:
Expand Down

0 comments on commit 18109cb

Please sign in to comment.