-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[wip] add hashmap-based directory cache
this is a first pass of the caching
- Loading branch information
Showing
5 changed files
with
136 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use crate::directory::{Directory, Entry}; | ||
|
||
pub enum SearchResult { | ||
NotCached, | ||
NotFound, | ||
Found(Entry), | ||
} | ||
|
||
impl From<Option<&Entry>> for SearchResult { | ||
fn from(entry: Option<&Entry>) -> Self { | ||
match entry { | ||
Some(entry) => SearchResult::Found(entry.clone()), | ||
None => SearchResult::NotFound, | ||
} | ||
} | ||
} | ||
|
||
/// A cache for PMTiles directories. | ||
pub trait Cache { | ||
/// Clear the cache. | ||
fn clear(&mut self); | ||
|
||
/// Get a directory from the cache, using the offset as a key. | ||
fn get_dir_entry(&self, offset: usize, tile_id: u64) -> SearchResult; | ||
|
||
/// Insert a directory into the cache, using the offset as a key. | ||
/// Note that cache must be internally mutable. | ||
fn insert_directory(&self, offset: usize, directory: Directory); | ||
} | ||
|
||
pub struct NoCache; | ||
|
||
impl Cache for NoCache { | ||
fn clear(&mut self) {} | ||
|
||
#[inline] | ||
fn get_dir_entry(&self, _offset: usize, _tile_id: u64) -> SearchResult { | ||
SearchResult::NotCached | ||
} | ||
|
||
#[inline] | ||
fn insert_directory(&self, _offset: usize, _directory: Directory) {} | ||
} | ||
|
||
#[cfg(feature = "cache")] | ||
#[derive(Default)] | ||
pub struct HashMapCache { | ||
cache: std::sync::Arc<std::sync::Mutex<std::collections::HashMap<usize, Directory>>>, | ||
} | ||
|
||
#[cfg(feature = "cache")] | ||
impl Cache for HashMapCache { | ||
fn clear(&mut self) { | ||
self.cache.lock().unwrap().clear(); | ||
} | ||
|
||
fn get_dir_entry(&self, offset: usize, tile_id: u64) -> SearchResult { | ||
if let Some(dir) = self.cache.lock().unwrap().get(&offset) { | ||
return dir.find_tile_id(tile_id).into(); | ||
} | ||
SearchResult::NotCached | ||
} | ||
|
||
fn insert_directory(&self, offset: usize, directory: Directory) { | ||
self.cache.lock().unwrap().insert(offset, directory); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters