-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
687a53c
commit 6de41ce
Showing
4 changed files
with
125 additions
and
3 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,120 @@ | ||
use std::{ | ||
collections::{hash_map::Entry, HashMap}, | ||
fmt::{Display, Formatter}, | ||
ops::Range, | ||
}; | ||
|
||
use async_trait::async_trait; | ||
use bytes::Bytes; | ||
use futures::{lock::Mutex, stream::BoxStream}; | ||
use leptos::logging::log; | ||
use object_store::{ | ||
path::Path, GetOptions, GetResult, ListResult, MultipartUpload, ObjectMeta, ObjectStore, | ||
PutMultipartOpts, PutOptions, PutPayload, PutResult, | ||
}; | ||
use object_store_opendal::OpendalStore; | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct ObjectStoreCache { | ||
inner: OpendalStore, | ||
cache: Mutex<HashMap<(Path, Range<usize>), Bytes>>, | ||
} | ||
|
||
impl ObjectStoreCache { | ||
pub(crate) fn new(inner: OpendalStore) -> Self { | ||
Self { | ||
inner, | ||
cache: Mutex::new(HashMap::new()), | ||
} | ||
} | ||
} | ||
|
||
impl Display for ObjectStoreCache { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "ObjectStoreCache") | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl ObjectStore for ObjectStoreCache { | ||
async fn put_opts( | ||
&self, | ||
location: &Path, | ||
payload: PutPayload, | ||
opts: PutOptions, | ||
) -> Result<PutResult, object_store::Error> { | ||
self.inner.put_opts(location, payload, opts).await | ||
} | ||
|
||
async fn put_multipart_opts( | ||
&self, | ||
location: &Path, | ||
opts: PutMultipartOpts, | ||
) -> Result<Box<dyn MultipartUpload>, object_store::Error> { | ||
self.inner.put_multipart_opts(location, opts).await | ||
} | ||
|
||
async fn get(&self, location: &Path) -> Result<GetResult, object_store::Error> { | ||
self.inner.get(location).await | ||
} | ||
|
||
async fn head(&self, location: &Path) -> Result<ObjectMeta, object_store::Error> { | ||
self.inner.head(location).await | ||
} | ||
|
||
async fn get_opts( | ||
&self, | ||
location: &Path, | ||
options: GetOptions, | ||
) -> Result<GetResult, object_store::Error> { | ||
return self.inner.get_opts(location, options).await; | ||
} | ||
|
||
async fn get_range( | ||
&self, | ||
location: &Path, | ||
range: Range<usize>, | ||
) -> Result<Bytes, object_store::Error> { | ||
let key = (location.clone(), range); | ||
let mut cache = self.cache.lock().await; | ||
let bytes = match cache.entry(key) { | ||
Entry::Occupied(o) => { | ||
log!("hit cache"); | ||
o.get().clone() | ||
} | ||
Entry::Vacant(v) => { | ||
let k = v.key(); | ||
let bs = self.inner.get_range(location, k.1.clone()).await?; | ||
v.insert(bs.clone()); | ||
bs | ||
} | ||
}; | ||
Ok(bytes) | ||
} | ||
|
||
async fn delete(&self, location: &Path) -> Result<(), object_store::Error> { | ||
self.inner.delete(location).await | ||
} | ||
|
||
fn list( | ||
&self, | ||
prefix: Option<&Path>, | ||
) -> BoxStream<'_, Result<ObjectMeta, object_store::Error>> { | ||
self.inner.list(prefix) | ||
} | ||
|
||
async fn list_with_delimiter( | ||
&self, | ||
prefix: Option<&Path>, | ||
) -> Result<ListResult, object_store::Error> { | ||
self.inner.list_with_delimiter(prefix).await | ||
} | ||
|
||
async fn copy(&self, from: &Path, to: &Path) -> Result<(), object_store::Error> { | ||
self.inner.copy(from, to).await | ||
} | ||
|
||
async fn copy_if_not_exists(&self, from: &Path, to: &Path) -> Result<(), object_store::Error> { | ||
self.inner.copy_if_not_exists(from, to).await | ||
} | ||
} |
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