1
1
use std:: collections:: HashMap ;
2
2
use std:: sync:: { Arc , Mutex } ;
3
3
4
+ use async_trait:: async_trait;
5
+
4
6
use crate :: directory:: { DirEntry , Directory } ;
5
7
6
8
pub enum DirCacheResult {
@@ -19,25 +21,27 @@ impl From<Option<&DirEntry>> for DirCacheResult {
19
21
}
20
22
21
23
/// A cache for PMTiles directories.
24
+ #[ async_trait]
22
25
pub trait DirectoryCache {
23
26
/// Get a directory from the cache, using the offset as a key.
24
- fn get_dir_entry ( & self , offset : usize , tile_id : u64 ) -> DirCacheResult ;
27
+ async fn get_dir_entry ( & self , offset : usize , tile_id : u64 ) -> DirCacheResult ;
25
28
26
29
/// Insert a directory into the cache, using the offset as a key.
27
30
/// Note that cache must be internally mutable.
28
- fn insert_dir ( & self , offset : usize , directory : Directory ) ;
31
+ async fn insert_dir ( & self , offset : usize , directory : Directory ) ;
29
32
}
30
33
31
34
pub struct NoCache ;
32
35
36
+ #[ async_trait]
33
37
impl DirectoryCache for NoCache {
34
38
#[ inline]
35
- fn get_dir_entry ( & self , _offset : usize , _tile_id : u64 ) -> DirCacheResult {
39
+ async fn get_dir_entry ( & self , _offset : usize , _tile_id : u64 ) -> DirCacheResult {
36
40
DirCacheResult :: NotCached
37
41
}
38
42
39
43
#[ inline]
40
- fn insert_dir ( & self , _offset : usize , _directory : Directory ) { }
44
+ async fn insert_dir ( & self , _offset : usize , _directory : Directory ) { }
41
45
}
42
46
43
47
/// A simple HashMap-based implementation of a PMTiles directory cache.
@@ -46,15 +50,16 @@ pub struct HashMapCache {
46
50
pub cache : Arc < Mutex < HashMap < usize , Directory > > > ,
47
51
}
48
52
53
+ #[ async_trait]
49
54
impl DirectoryCache for HashMapCache {
50
- fn get_dir_entry ( & self , offset : usize , tile_id : u64 ) -> DirCacheResult {
55
+ async fn get_dir_entry ( & self , offset : usize , tile_id : u64 ) -> DirCacheResult {
51
56
if let Some ( dir) = self . cache . lock ( ) . unwrap ( ) . get ( & offset) {
52
57
return dir. find_tile_id ( tile_id) . into ( ) ;
53
58
}
54
59
DirCacheResult :: NotCached
55
60
}
56
61
57
- fn insert_dir ( & self , offset : usize , directory : Directory ) {
62
+ async fn insert_dir ( & self , offset : usize , directory : Directory ) {
58
63
self . cache . lock ( ) . unwrap ( ) . insert ( offset, directory) ;
59
64
}
60
65
}
0 commit comments