Skip to content

Commit

Permalink
rename SearchResult
Browse files Browse the repository at this point in the history
  • Loading branch information
nyurik committed Nov 10, 2023
1 parent 1221c9a commit 8ddc78e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/async_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use reqwest::{Client, IntoUrl};
#[cfg(any(feature = "http-async", feature = "mmap-async-tokio"))]
use tokio::io::AsyncReadExt;

use crate::cache::SearchResult;
use crate::cache::DirCacheResult;
#[cfg(any(feature = "http-async", feature = "mmap-async-tokio"))]
use crate::cache::{DirectoryCache, NoCache};
use crate::directory::{DirEntry, Directory};
Expand Down Expand Up @@ -158,16 +158,16 @@ impl<B: AsyncBackend + Sync + Send, C: DirectoryCache + Sync + Send> AsyncPmTile
let offset = (self.header.leaf_offset + entry.offset) as _;

let entry = match self.cache.get_dir_entry(offset, tile_id) {
SearchResult::NotCached => {
DirCacheResult::NotCached => {
// Cache miss - read from backend
let length = entry.length as _;
let dir = self.read_directory(offset, length).await.ok()?;
let entry = dir.find_tile_id(tile_id).cloned();
self.cache.insert_dir(offset, dir);
entry
}
SearchResult::NotFound => None,
SearchResult::Found(entry) => Some(entry),
DirCacheResult::NotFound => None,
DirCacheResult::Found(entry) => Some(entry),
};

if let Some(ref entry) = entry {
Expand Down
18 changes: 9 additions & 9 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ use std::sync::{Arc, Mutex};

use crate::directory::{DirEntry, Directory};

pub enum SearchResult {
pub enum DirCacheResult {
NotCached,
NotFound,
Found(DirEntry),
}

impl From<Option<&DirEntry>> for SearchResult {
impl From<Option<&DirEntry>> for DirCacheResult {
fn from(entry: Option<&DirEntry>) -> Self {
match entry {
Some(entry) => SearchResult::Found(entry.clone()),
None => SearchResult::NotFound,
Some(entry) => DirCacheResult::Found(entry.clone()),
None => DirCacheResult::NotFound,
}
}
}

/// A cache for PMTiles directories.
pub trait DirectoryCache {
/// Get a directory from the cache, using the offset as a key.
fn get_dir_entry(&self, offset: usize, tile_id: u64) -> SearchResult;
fn get_dir_entry(&self, offset: usize, tile_id: u64) -> DirCacheResult;

/// Insert a directory into the cache, using the offset as a key.
/// Note that cache must be internally mutable.
Expand All @@ -32,8 +32,8 @@ pub struct NoCache;

impl DirectoryCache for NoCache {
#[inline]
fn get_dir_entry(&self, _offset: usize, _tile_id: u64) -> SearchResult {
SearchResult::NotCached
fn get_dir_entry(&self, _offset: usize, _tile_id: u64) -> DirCacheResult {
DirCacheResult::NotCached
}

#[inline]
Expand All @@ -47,11 +47,11 @@ pub struct HashMapCache {
}

impl DirectoryCache for HashMapCache {
fn get_dir_entry(&self, offset: usize, tile_id: u64) -> SearchResult {
fn get_dir_entry(&self, offset: usize, tile_id: u64) -> DirCacheResult {
if let Some(dir) = self.cache.lock().unwrap().get(&offset) {
return dir.find_tile_id(tile_id).into();
}
SearchResult::NotCached
DirCacheResult::NotCached
}

fn insert_dir(&self, offset: usize, directory: Directory) {
Expand Down

0 comments on commit 8ddc78e

Please sign in to comment.