Skip to content

Commit

Permalink
Code refactoring for hasTile and hasResource methods to make the code…
Browse files Browse the repository at this point in the history
… cleaner
  • Loading branch information
Patrick Schevenels committed Dec 6, 2023
1 parent 5c75a7b commit 2085fcb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 50 deletions.
2 changes: 2 additions & 0 deletions platform/default/include/mbgl/storage/offline_database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,12 @@ class OfflineDatabase {

std::optional<std::pair<Response, uint64_t>> getTile(const Resource::TileData&);
std::optional<int64_t> hasTile(const Resource::TileData&);
std::optional<int64_t> extractTileDataSize(const Resource::TileData&, const char *);
bool putTile(const Resource::TileData&, const Response&, const std::string&, bool compressed, bool ambient);

std::optional<std::pair<Response, uint64_t>> getResource(const Resource&);
std::optional<int64_t> hasResource(const Resource&);
std::optional<int64_t> extractResourceDataSize(const Resource&, const char *);
bool putResource(const Resource&, const Response&, const std::string&, bool compressed, bool ambient);

uint64_t putRegionResourceInternal(int64_t regionID, const Resource&, const Response&);
Expand Down
89 changes: 39 additions & 50 deletions platform/default/src/mbgl/storage/offline_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -469,26 +469,31 @@ std::optional<std::pair<Response, uint64_t>> OfflineDatabase::getResource(const
return queryResource("resources");
}

std::optional<int64_t> OfflineDatabase::hasResource(const Resource& resource) {

//std::cout<<"-------- HASRESOURCE FOR URL = ";
//std::cout<<resource.url;
//std::cout<<"\n";

// First, try to find the resource in the 'resources' table

mapbox::sqlite::Query selectResourcesQuery(getStatement("SELECT length(data) FROM resources WHERE url = ?1"));
std::optional<int64_t> OfflineDatabase::extractResourceDataSize(const Resource& resource, const char *sql) {
std::optional<int64_t> selectResourcesResult;
mapbox::sqlite::Query selectResourcesQuery(getStatement(sql));

selectResourcesQuery.bind(1, resource.url);

std::optional<int64_t> selectResourcesResult;

if (selectResourcesQuery.run()) {
selectResourcesResult = selectResourcesQuery.get<std::optional<int64_t>>(0);
} else {
selectResourcesResult = std::nullopt;
}
return selectResourcesResult;
}


std::optional<int64_t> OfflineDatabase::hasResource(const Resource& resource) {

//std::cout<<"-------- HASRESOURCE FOR URL = ";
//std::cout<<resource.url;
//std::cout<<"\n";

// First, try to find the resource in the 'resources' table

std::optional<int64_t> selectResourcesResult = extractResourceDataSize(resource, "SELECT length(data) FROM resources WHERE url = ?1");

if(selectResourcesResult)
{
//std::cout << "-------- HASRESOURCE - FOUND IN RESOURCES\n";
Expand All @@ -497,17 +502,8 @@ std::optional<int64_t> OfflineDatabase::hasResource(const Resource& resource) {
else
{
// If not found in 'resources', try the 'ambient_resources' table
mapbox::sqlite::Query selectAmbientResourcesQuery(getStatement("SELECT length(data) FROM ambient_resources WHERE url = ?1"));

selectAmbientResourcesQuery.bind(1, resource.url);

std::optional<int64_t> selectAmbientResourcesResult;

if (selectAmbientResourcesQuery.run()) {
selectAmbientResourcesResult = selectAmbientResourcesQuery.get<std::optional<int64_t>>(0);
} else {
selectAmbientResourcesResult = std::nullopt;
}
std::optional<int64_t> selectAmbientResourcesResult = extractResourceDataSize(resource, "SELECT length(data) FROM ambient_resources WHERE url = ?1");

if(selectAmbientResourcesResult)
{
Expand Down Expand Up @@ -733,24 +729,12 @@ std::optional<std::pair<Response, uint64_t>> OfflineDatabase::getTile(const Reso
return offlineResult;
}

std::optional<int64_t> OfflineDatabase::hasTile(const Resource::TileData& tile) {

// First, try to find the tile in the 'tiles' table
std::optional<int64_t> selectTilesResult;
mapbox::sqlite::Query selectTilesQuery(getStatement("SELECT length(data) FROM tiles WHERE url_template = ?1 AND pixel_ratio = ?2 AND x = ?3 AND y = ?4 AND z = ?5"));

selectTilesQuery.bind(1, tile.urlTemplate);
selectTilesQuery.bind(2, tile.pixelRatio);
selectTilesQuery.bind(3, tile.x);
selectTilesQuery.bind(4, tile.y);
selectTilesQuery.bind(5, tile.z);
std::optional<int64_t> OfflineDatabase::hasTile(const Resource::TileData& tile) {

if (selectTilesQuery.run()) {
selectTilesResult = selectTilesQuery.get<std::optional<int64_t>>(0);
} else {
selectTilesResult = std::nullopt;
}

// First, try to find the tile in the 'tiles' table
std::optional<int64_t> selectTilesResult = extractTileDataSize(tile, "SELECT length(data) FROM tiles WHERE url_template = ?1 AND pixel_ratio = ?2 AND x = ?3 AND y = ?4 AND z = ?5");
if(selectTilesResult)
{
//std::cout << "-------- HASTILE - FOUND IN TILES\n";
Expand All @@ -759,20 +743,7 @@ std::optional<int64_t> OfflineDatabase::hasTile(const Resource::TileData& tile)
else
{
// If not found in 'tiles', try the 'ambient_tiles' table
std::optional<int64_t> selectAmbientTilesResult;
mapbox::sqlite::Query selectAmbientTilesQuery(getStatement("SELECT length(data) FROM ambient_tiles WHERE url_template = ?1 AND pixel_ratio = ?2 AND x = ?3 AND y = ?4 AND z = ?5"));

selectAmbientTilesQuery.bind(1, tile.urlTemplate);
selectAmbientTilesQuery.bind(2, tile.pixelRatio);
selectAmbientTilesQuery.bind(3, tile.x);
selectAmbientTilesQuery.bind(4, tile.y);
selectAmbientTilesQuery.bind(5, tile.z);

if (selectAmbientTilesQuery.run()) {
selectAmbientTilesResult = selectAmbientTilesQuery.get<std::optional<int64_t>>(0);
} else {
selectAmbientTilesResult = std::nullopt;
}
std::optional<int64_t> selectAmbientTilesResult = extractTileDataSize(tile, "SELECT length(data) FROM ambient_tiles WHERE url_template = ?1 AND pixel_ratio = ?2 AND x = ?3 AND y = ?4 AND z = ?5");
if(selectAmbientTilesResult)
{
//std::cout << "-------- HASTILE - FOUND IN AMBIENT_TILES\n";
Expand Down Expand Up @@ -807,6 +778,24 @@ std::optional<int64_t> OfflineDatabase::hasTile(const Resource::TileData& tile)
}
}

std::optional<int64_t> OfflineDatabase::extractTileDataSize(const Resource::TileData& tile, const char *sql) {
std::optional<int64_t> selectTilesResult;
mapbox::sqlite::Query selectTilesQuery(getStatement(sql));

selectTilesQuery.bind(1, tile.urlTemplate);
selectTilesQuery.bind(2, tile.pixelRatio);
selectTilesQuery.bind(3, tile.x);
selectTilesQuery.bind(4, tile.y);
selectTilesQuery.bind(5, tile.z);

if (selectTilesQuery.run()) {
selectTilesResult = selectTilesQuery.get<std::optional<int64_t>>(0);
} else {
selectTilesResult = std::nullopt;
}
return selectTilesResult;
}


bool OfflineDatabase::putTile(
const Resource::TileData& tile, const Response& response, const std::string& data, bool compressed, bool ambient) {
Expand Down

0 comments on commit 2085fcb

Please sign in to comment.