use async_trait::async_trait;
use bytes::Bytes;
use futures::stream::BoxStream;
use object_store::local::LocalFileSystem;
use object_store::path::Path;
use object_store::{
GetOptions, GetResult, ListResult, MultipartId, ObjectMeta, ObjectStore,
};
use std::fmt::Formatter;
use tempfile::tempdir;
use tokio::io::AsyncWrite;
#[derive(Debug)]
struct MyStore(LocalFileSystem);
impl std::fmt::Display for MyStore {
fn fmt(&self, _: &mut Formatter<'_>) -> std::fmt::Result {
todo!()
}
}
#[async_trait]
impl ObjectStore for MyStore {
async fn put(&self, path: &Path, data: Bytes) -> object_store::Result<()> {
self.0.put(path, data).await
}
async fn put_multipart(
&self,
_: &Path,
) -> object_store::Result<(MultipartId, Box<dyn AsyncWrite + Unpin + Send>)> {
todo!()
}
async fn abort_multipart(
&self,
_: &Path,
_: &MultipartId,
) -> object_store::Result<()> {
todo!()
}
async fn get_opts(
&self,
location: &Path,
options: GetOptions,
) -> object_store::Result<GetResult> {
self.0.get_opts(location, options).await
}
async fn head(&self, _: &Path) -> object_store::Result<ObjectMeta> {
todo!()
}
async fn delete(&self, _: &Path) -> object_store::Result<()> {
todo!()
}
async fn list(
&self,
_: Option<&Path>,
) -> object_store::Result<BoxStream<'_, object_store::Result<ObjectMeta>>> {
todo!()
}
async fn list_with_delimiter(
&self,
_: Option<&Path>,
) -> object_store::Result<ListResult> {
todo!()
}
async fn copy(&self, _: &Path, _: &Path) -> object_store::Result<()> {
todo!()
}
async fn copy_if_not_exists(&self, _: &Path, _: &Path) -> object_store::Result<()> {
todo!()
}
}
#[tokio::test]
async fn test_get_range() {
let tmp = tempdir().unwrap();
let store = MyStore(LocalFileSystem::new_with_prefix(tmp.path()).unwrap());
let path = Path::from("foo");
let expected = Bytes::from_static(b"hello world");
store.put(&path, expected.clone()).await.unwrap();
let fetched = store.get(&path).await.unwrap().bytes().await.unwrap();
assert_eq!(expected, fetched);
for range in [0..10, 3..5, 0..expected.len()] {
let data = store.get_range(&path, range.clone()).await.unwrap();
assert_eq!(&data[..], &expected[range])
}
}