Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Sep 30, 2024
1 parent b58200e commit ea3ab13
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 80 deletions.
4 changes: 3 additions & 1 deletion platform/default/include/mbgl/storage/local_file_request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ template <typename>
class ActorRef;
class FileSourceRequest;

void requestLocalFile(const std::string&, const ActorRef<FileSourceRequest>&, const std::optional<std::pair<uint64_t, uint64_t>>& = std::nullopt);
void requestLocalFile(const std::string&,
const ActorRef<FileSourceRequest>&,
const std::optional<std::pair<uint64_t, uint64_t>>& = std::nullopt);

} // namespace mbgl
3 changes: 2 additions & 1 deletion platform/default/src/mbgl/storage/http_file_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ HTTPRequest::HTTPRequest(HTTPFileSource::Impl *context_, Resource resource_, Fil
callback(std::move(callback_)),
handle(context->getHandle()) {
if (resource.dataRange) {
const std::string header = std::string("Range: bytes=") + std::to_string(resource.dataRange->first) + std::string("-") + std::to_string(resource.dataRange->second);
const std::string header = std::string("Range: bytes=") + std::to_string(resource.dataRange->first) +
std::string("-") + std::to_string(resource.dataRange->second);
headers = curl_slist_append(headers, header.c_str());
}

Expand Down
4 changes: 3 additions & 1 deletion platform/default/src/mbgl/storage/local_file_request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

namespace mbgl {

void requestLocalFile(const std::string& path, const ActorRef<FileSourceRequest>& req, const std::optional<std::pair<uint64_t, uint64_t>>& dataRange) {
void requestLocalFile(const std::string& path,
const ActorRef<FileSourceRequest>& req,
const std::optional<std::pair<uint64_t, uint64_t>>& dataRange) {
Response response;
struct stat buf;
int result = stat(path.c_str(), &buf);
Expand Down
3 changes: 2 additions & 1 deletion platform/default/src/mbgl/storage/local_file_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class LocalFileSource::Impl {
}

// Cut off the protocol and prefix with path.
const auto path = mbgl::util::percentDecode(resource.url.substr(std::char_traits<char>::length(util::FILE_PROTOCOL)));
const auto path = mbgl::util::percentDecode(
resource.url.substr(std::char_traits<char>::length(util::FILE_PROTOCOL)));
requestLocalFile(path, req, resource.dataRange);
}

Expand Down
179 changes: 105 additions & 74 deletions platform/default/src/mbgl/storage/pmtiles_file_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ using AsyncTileCallback = std::function<void(std::pair<uint64_t, uint32_t>, std:

class PMTilesFileSource::Impl {
public:
explicit Impl(const ActorRef<Impl> &, const ResourceOptions &resourceOptions_, const ClientOptions &clientOptions_)
explicit Impl(const ActorRef<Impl>&, const ResourceOptions& resourceOptions_, const ClientOptions& clientOptions_)
: resourceOptions(resourceOptions_.clone()),
clientOptions(clientOptions_.clone()) {}

// Generate a tilejson resource from .pmtiles file
void request_tilejson(AsyncRequest* req, const Resource& resource, const ActorRef<FileSourceRequest>& ref) {
auto url = extract_url(resource.url);

getMetadata(url, req, [=](std::unique_ptr<Response::Error> error) {
Response response;

Expand Down Expand Up @@ -83,7 +83,7 @@ class PMTilesFileSource::Impl {
ref.invoke(&FileSourceRequest::setResponse, response);
return;
}

pmtiles::headerv3 header = header_cache.at(url);

if (resource.tileData->z < header.min_zoom || resource.tileData->z > header.max_zoom) {
Expand All @@ -92,64 +92,71 @@ class PMTilesFileSource::Impl {
}

uint64_t tileID;

try {
tileID = pmtiles::zxy_to_tileid(static_cast<uint8_t>(resource.tileData->z), static_cast<uint32_t>(resource.tileData->x), static_cast<uint32_t>(resource.tileData->y));
}
catch(const std::exception& e)
{
response.error = std::make_unique<Response::Error>(
Response::Error::Reason::Other,
std::string("Invalid tile: ") + e.what());
tileID = pmtiles::zxy_to_tileid(static_cast<uint8_t>(resource.tileData->z),
static_cast<uint32_t>(resource.tileData->x),
static_cast<uint32_t>(resource.tileData->y));
} catch (const std::exception& e) {
response.error = std::make_unique<Response::Error>(Response::Error::Reason::Other,
std::string("Invalid tile: ") + e.what());
ref.invoke(&FileSourceRequest::setResponse, response);
return;
}

getTileAddress(url, req, tileID, header.root_dir_offset, header.root_dir_bytes, 0, [=](std::pair<uint64_t, uint32_t> tileAddress, std::unique_ptr<Response::Error> tileError) {
Response response;
response.noContent = true;

if (tileError) {
response.error = std::move(tileError);
ref.invoke(&FileSourceRequest::setResponse, response);
return;
}

if (tileAddress.first == 0 && tileAddress.second == 0) {
ref.invoke(&FileSourceRequest::setResponse, response);
return;
}

Resource tileResource(Resource::Kind::Source, url);
tileResource.loadingMethod = Resource::LoadingMethod::Network;
tileResource.dataRange = std::make_pair(tileAddress.first, tileAddress.first + tileAddress.second - 1);

tasks[req] = getFileSource()->request(tileResource, [=](const Response& tileResponse) {
getTileAddress(
url,
req,
tileID,
header.root_dir_offset,
header.root_dir_bytes,
0,
[=](std::pair<uint64_t, uint32_t> tileAddress, std::unique_ptr<Response::Error> tileError) {
Response response;
response.noContent = true;

if (tileResponse.error) {
response.error = std::make_unique<Response::Error>(
tileResponse.error->reason,
std::string("Error fetching PMTiles tile: ") + tileResponse.error->message);
if (tileError) {
response.error = std::move(tileError);
ref.invoke(&FileSourceRequest::setResponse, response);
return;
}

response.data = tileResponse.data;
response.noContent = false;
response.modified = tileResponse.modified;
response.expires = tileResponse.expires;
response.etag = tileResponse.etag;

if (header.tile_compression == pmtiles::COMPRESSION_GZIP) {
response.data = std::make_shared<std::string>(util::decompress(*tileResponse.data));
if (tileAddress.first == 0 && tileAddress.second == 0) {
ref.invoke(&FileSourceRequest::setResponse, response);
return;
}

ref.invoke(&FileSourceRequest::setResponse, response);
return;
Resource tileResource(Resource::Kind::Source, url);
tileResource.loadingMethod = Resource::LoadingMethod::Network;
tileResource.dataRange = std::make_pair(tileAddress.first,
tileAddress.first + tileAddress.second - 1);

tasks[req] = getFileSource()->request(tileResource, [=](const Response& tileResponse) {
Response response;
response.noContent = true;

if (tileResponse.error) {
response.error = std::make_unique<Response::Error>(
tileResponse.error->reason,
std::string("Error fetching PMTiles tile: ") + tileResponse.error->message);
ref.invoke(&FileSourceRequest::setResponse, response);
return;
}

response.data = tileResponse.data;
response.noContent = false;
response.modified = tileResponse.modified;
response.expires = tileResponse.expires;
response.etag = tileResponse.etag;

if (header.tile_compression == pmtiles::COMPRESSION_GZIP) {
response.data = std::make_shared<std::string>(util::decompress(*tileResponse.data));
}

ref.invoke(&FileSourceRequest::setResponse, response);
return;
});
});
});
});
}

Expand Down Expand Up @@ -188,9 +195,10 @@ class PMTilesFileSource::Impl {

std::shared_ptr<FileSource> getFileSource() {
if (!fileSource) {
fileSource = FileSourceManager::get()->getFileSource(FileSourceType::ResourceLoader, resourceOptions, clientOptions);
fileSource = FileSourceManager::get()->getFileSource(
FileSourceType::ResourceLoader, resourceOptions, clientOptions);
}

return fileSource;
}

Expand All @@ -208,29 +216,30 @@ class PMTilesFileSource::Impl {
callback(std::make_unique<Response::Error>(
response.error->reason,
std::string("Error fetching PMTiles header and root directory: ") + response.error->message));

return;
}

try {
pmtiles::headerv3 header = pmtiles::deserialize_header(response.data->substr(0, 127));

if (header.tile_compression != pmtiles::COMPRESSION_NONE && header.tile_compression != pmtiles::COMPRESSION_GZIP) {

if (header.tile_compression != pmtiles::COMPRESSION_NONE &&
header.tile_compression != pmtiles::COMPRESSION_GZIP) {
throw std::runtime_error("Compression method not supported");
}

header_cache.emplace(url, header);

std::string directoryData = response.data->substr(header.root_dir_offset, header.root_dir_bytes);

if (header.tile_compression == pmtiles::COMPRESSION_GZIP) {
directoryData = util::decompress(directoryData);
}

storeDirectory(url, header.root_dir_offset, header.root_dir_bytes, directoryData);

callback(std::unique_ptr<Response::Error>());
} catch(const std::exception& e) {
} catch (const std::exception& e) {
callback(std::make_unique<Response::Error>(
Response::Error::Reason::Other,
std::string("Error parsing PMTiles header and root directory: ") + e.what()));
Expand Down Expand Up @@ -319,7 +328,8 @@ class PMTilesFileSource::Impl {
if (header.json_metadata_bytes > 0) {
Resource resource(Resource::Kind::Source, url);
resource.loadingMethod = Resource::LoadingMethod::Network;
resource.dataRange = std::make_pair(header.json_metadata_offset, header.json_metadata_offset + header.json_metadata_bytes - 1);
resource.dataRange = std::make_pair(header.json_metadata_offset,
header.json_metadata_offset + header.json_metadata_bytes - 1);

tasks[req] = getFileSource()->request(resource, [=](const Response& responseMetadata) {
if (responseMetadata.error) {
Expand All @@ -335,7 +345,7 @@ class PMTilesFileSource::Impl {
if (header.internal_compression == pmtiles::COMPRESSION_GZIP) {
data = util::decompress(data);
}

parse_callback(data);
});

Expand All @@ -346,13 +356,17 @@ class PMTilesFileSource::Impl {
});
}

void storeDirectory(const std::string& url, uint64_t directoryOffset, uint64_t directoryLength, const std::string& directoryData) {
void storeDirectory(const std::string& url,
uint64_t directoryOffset,
uint64_t directoryLength,
const std::string& directoryData) {
if (directory_cache.find(url) == directory_cache.end()) {
directory_cache.emplace(url, std::map<std::string, std::vector<pmtiles::entryv3>>());
directory_cache_control.emplace(url, std::vector<std::string>());
}

std::string directory_cache_key = url + "|" + std::to_string(directoryOffset) + "|" + std::to_string(directoryLength);
std::string directory_cache_key = url + "|" + std::to_string(directoryOffset) + "|" +
std::to_string(directoryLength);
directory_cache.at(url).emplace(directory_cache_key, pmtiles::deserialize_directory(rootDirectoryData));
directory_cache_control.at(url).emplace_back(directory_cache_key);

Expand All @@ -362,12 +376,16 @@ class PMTilesFileSource::Impl {
}
}

void getDirectory(const std::string& url, AsyncRequest* req, uint64_t directoryOffset, uint32_t directoryLength, AsyncCallback callback) {
std::string directory_cache_key = url + "|" + std::to_string(directoryOffset) + "|" + std::to_string(directoryLength);
void getDirectory(const std::string& url,
AsyncRequest* req,
uint64_t directoryOffset,
uint32_t directoryLength,
AsyncCallback callback) {
std::string directory_cache_key = url + "|" + std::to_string(directoryOffset) + "|" +
std::to_string(directoryLength);

if (directory_cache.find(url) != directory_cache.end() &&
directory_cache.at(url).find(directory_cache_key) != directory_cache.at(url).end()) {

if (directory_cache_control.at(url).back() != directory_cache_key) {
directory_cache_control.at(url).emplace_back(directory_cache_key);

Expand All @@ -378,11 +396,11 @@ class PMTilesFileSource::Impl {
}
}
}

callback(std::unique_ptr<Response::Error>());
return;
}

getHeaderAndRootDirectory(url, req, [=](std::unique_ptr<Response::Error> error) {
if (error) {
callback(std::move(error));
Expand Down Expand Up @@ -410,12 +428,11 @@ class PMTilesFileSource::Impl {
if (header.tile_compression == pmtiles::COMPRESSION_GZIP) {
directoryData = util::decompress(directoryData);
}

storeDirectory(url, directoryOffset, directoryLength, directoryData);

callback(std::unique_ptr<Response::Error>());
} catch(const std::exception& e)
{
} catch (const std::exception& e) {
callback(std::make_unique<Response::Error>(
Response::Error::Reason::Other,
std::string(std::string("Error parsing PMTiles directory: ") + e.what())));
Expand All @@ -424,11 +441,18 @@ class PMTilesFileSource::Impl {
});
}

void getTileAddress(const std::string& url, AsyncRequest* req, uint64_t tileID, uint64_t directoryOffset, uint32_t directoryLength, uint32_t directoryDepth, AsyncTileCallback callback) {
void getTileAddress(const std::string& url,
AsyncRequest* req,
uint64_t tileID,
uint64_t directoryOffset,
uint32_t directoryLength,
uint32_t directoryDepth,
AsyncTileCallback callback) {
if (directoryDepth > 3) {
callback(std::make_pair<uint64_t, uint32_t>(0, 0),
std::make_unique<Response::Error>(Response::Error::Reason::Other,
std::string("Error fetching PMTiles tile address: Maximum directory depth exceeded")));
std::make_unique<Response::Error>(
Response::Error::Reason::Other,
std::string("Error fetching PMTiles tile address: Maximum directory depth exceeded")));
}

fetchDirectory(url, req, directoryOffset, directoryLength, [=](std::unique_ptr<Response::Error> error) {
Expand All @@ -438,7 +462,8 @@ class PMTilesFileSource::Impl {
}

pmtiles::headerv3 header = header_cache.at(url);
std::vector<pmtiles::entryv3> directory = directory_cache.at(url).at(url + "|" + std::to_string(directoryOffset) + "|" + std::to_string(directoryLength));
std::vector<pmtiles::entryv3> directory = directory_cache.at(url).at(
url + "|" + std::to_string(directoryOffset) + "|" + std::to_string(directoryLength));

pmtiles::entryv3 entry = pmtiles::find_tile(directory, tileID);

Expand All @@ -447,8 +472,14 @@ class PMTilesFileSource::Impl {
callback(std::make_pair(header.tile_data_offset + entry.offset, entry.length), {});
return;
}

getTileAddress(url, req, tileID, header.leaf_dirs_offset + entry.offset, entry.length, directoryDepth + 1, std::move(callback));

getTileAddress(url,
req,
tileID,
header.leaf_dirs_offset + entry.offset,
entry.length,
directoryDepth + 1,
std::move(callback));
return;
}

Expand Down
3 changes: 2 additions & 1 deletion src/mbgl/util/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ std::string read_file(const std::string &filename) {
}
}

std::optional<std::string> readFile(const std::string &filename, const std::optional<std::pair<uint64_t, uint64_t>>& dataRange) {
std::optional<std::string> readFile(const std::string &filename,
const std::optional<std::pair<uint64_t, uint64_t>> &dataRange) {
MLN_TRACE_FUNC();

std::ifstream file(filename, std::ios::binary);
Expand Down
3 changes: 2 additions & 1 deletion src/mbgl/util/io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ struct IOException : std::runtime_error {
void write_file(const std::string& filename, const std::string& data);
std::string read_file(const std::string& filename);

std::optional<std::string> readFile(const std::string& filename, const std::optional<std::pair<uint64_t, uint64_t>>& dataRange = std::nullopt);
std::optional<std::string> readFile(const std::string& filename,
const std::optional<std::pair<uint64_t, uint64_t>>& dataRange = std::nullopt);
void deleteFile(const std::string& filename);
void copyFile(const std::string& destination, const std::string& source);

Expand Down

0 comments on commit ea3ab13

Please sign in to comment.