Skip to main content

supertable_core/
storage.rs

1use bytes::Bytes;
2use object_store::path::Path;
3use object_store::{ObjectStore, ObjectStoreExt};
4use std::sync::Arc;
5
6#[derive(Clone)]
7pub struct Storage {
8    inner: Arc<dyn ObjectStore>,
9}
10
11impl std::fmt::Debug for Storage {
12    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
13        f.debug_struct("Storage").finish()
14    }
15}
16
17impl Storage {
18    pub fn new(inner: Arc<dyn ObjectStore>) -> Self {
19        Self { inner }
20    }
21
22    pub async fn read(&self, path: &str) -> anyhow::Result<Bytes> {
23        let path = Path::from(path);
24        let result = self.inner.get(&path).await?;
25        let bytes = result.bytes().await?;
26        Ok(bytes)
27    }
28
29    pub async fn write(&self, path: &str, content: Bytes) -> anyhow::Result<()> {
30        let path = Path::from(path);
31        self.inner.put(&path, content.into()).await?;
32        Ok(())
33    }
34
35    pub async fn delete(&self, path: &str) -> anyhow::Result<()> {
36        let path = Path::from(path);
37        self.inner.delete(&path).await?;
38        Ok(())
39    }
40
41    pub async fn list_files(&self, prefix: &str) -> anyhow::Result<Vec<String>> {
42        let prefix = Path::from(prefix);
43        let mut list = self.inner.list(Some(&prefix));
44        let mut files = Vec::new();
45
46        use futures::StreamExt;
47        while let Some(meta) = list.next().await {
48            let meta = meta?;
49            files.push(meta.location.to_string());
50        }
51
52        Ok(files)
53    }
54}