Skip to content

Commit

Permalink
Passthrough any errors from fetch_tile_entry.
Browse files Browse the repository at this point in the history
  • Loading branch information
lseelenbinder committed Apr 10, 2024
1 parent 16fd272 commit 6d359ba
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/async_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl<B: AsyncBackend + Sync + Send, C: DirectoryCache + Sync + Send> AsyncPmTile
/// Fetches tile bytes from the archive.
pub async fn get_tile(&self, z: u8, x: u64, y: u64) -> PmtResult<Option<Bytes>> {
let tile_id = tile_id(z, x, y);
let Some(entry) = self.find_tile_entry(tile_id).await else {
let Some(entry) = self.find_tile_entry(tile_id).await? else {
return Ok(None);
};

Expand Down Expand Up @@ -137,18 +137,24 @@ impl<B: AsyncBackend + Sync + Send, C: DirectoryCache + Sync + Send> AsyncPmTile
}

/// Recursively locates a tile in the archive.
async fn find_tile_entry(&self, tile_id: u64) -> Option<DirEntry> {
async fn find_tile_entry(&self, tile_id: u64) -> PmtResult<Option<DirEntry>> {
let entry = self.root_directory.find_tile_id(tile_id);
if let Some(entry) = entry {
if entry.is_leaf() {
return self.find_entry_rec(tile_id, entry, 0).await;
}
}
entry.cloned()

Ok(entry.cloned())
}

#[async_recursion]
async fn find_entry_rec(&self, tile_id: u64, entry: &DirEntry, depth: u8) -> Option<DirEntry> {
async fn find_entry_rec(
&self,
tile_id: u64,
entry: &DirEntry,
depth: u8,
) -> PmtResult<Option<DirEntry>> {
// the recursion is done as two functions because it is a bit cleaner,
// and it allows directory to be cached later without cloning it first.
let offset = (self.header.leaf_offset + entry.offset) as _;
Expand All @@ -157,7 +163,7 @@ impl<B: AsyncBackend + Sync + Send, C: DirectoryCache + Sync + Send> AsyncPmTile
DirCacheResult::NotCached => {
// Cache miss - read from backend
let length = entry.length as _;
let dir = self.read_directory(offset, length).await.ok()?;
let dir = self.read_directory(offset, length).await?;
let entry = dir.find_tile_id(tile_id).cloned();
self.cache.insert_dir(offset, dir).await;
entry
Expand All @@ -171,12 +177,12 @@ impl<B: AsyncBackend + Sync + Send, C: DirectoryCache + Sync + Send> AsyncPmTile
return if depth <= 4 {
self.find_entry_rec(tile_id, entry, depth + 1).await
} else {
None
Ok(None)
};
}
}

entry
Ok(entry)
}

async fn read_directory(&self, offset: usize, length: usize) -> PmtResult<Directory> {
Expand Down

0 comments on commit 6d359ba

Please sign in to comment.